Home | History | Annotate | Download | only in discovery
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.launcher3.discovery;
     18 
     19 import android.content.Context;
     20 import android.support.annotation.NonNull;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.widget.ImageView;
     24 import android.widget.RelativeLayout;
     25 import android.widget.TextView;
     26 
     27 import com.android.launcher3.R;
     28 
     29 import java.text.DecimalFormat;
     30 import java.text.NumberFormat;
     31 
     32 public class AppDiscoveryItemView extends RelativeLayout {
     33 
     34     private static boolean SHOW_REVIEW_COUNT = false;
     35 
     36     private ImageView mImage;
     37     private TextView mTitle;
     38     private TextView mRatingText;
     39     private RatingView mRatingView;
     40     private TextView mReviewCount;
     41     private TextView mPrice;
     42     private OnLongClickListener mOnLongClickListener;
     43 
     44     public AppDiscoveryItemView(Context context) {
     45         this(context, null);
     46     }
     47 
     48     public AppDiscoveryItemView(Context context, AttributeSet attrs) {
     49         this(context, attrs, 0);
     50     }
     51 
     52     public AppDiscoveryItemView(Context context, AttributeSet attrs, int defStyle) {
     53         super(context, attrs, defStyle);
     54     }
     55 
     56     @Override
     57     protected void onFinishInflate() {
     58         super.onFinishInflate();
     59         this.mImage = (ImageView) findViewById(R.id.image);
     60         this.mTitle = (TextView) findViewById(R.id.title);
     61         this.mRatingText = (TextView) findViewById(R.id.rating);
     62         this.mRatingView = (RatingView) findViewById(R.id.rating_view);
     63         this.mPrice = (TextView) findViewById(R.id.price);
     64         this.mReviewCount = (TextView) findViewById(R.id.review_count);
     65     }
     66 
     67     public void init(OnClickListener clickListener,
     68                      AccessibilityDelegate accessibilityDelegate,
     69                      OnLongClickListener onLongClickListener) {
     70         setOnClickListener(clickListener);
     71         mImage.setOnClickListener(clickListener);
     72         setAccessibilityDelegate(accessibilityDelegate);
     73         mOnLongClickListener = onLongClickListener;
     74     }
     75 
     76     public void apply(@NonNull AppDiscoveryAppInfo info) {
     77         setTag(info);
     78         mImage.setTag(info);
     79         mImage.setImageBitmap(info.iconBitmap);
     80         mImage.setOnLongClickListener(info.isDragAndDropSupported() ? mOnLongClickListener : null);
     81         mTitle.setText(info.title);
     82         mPrice.setText(info.priceFormatted != null ? info.priceFormatted : "");
     83         mReviewCount.setVisibility(SHOW_REVIEW_COUNT ? View.VISIBLE : View.GONE);
     84         if (info.rating >= 0) {
     85             mRatingText.setText(new DecimalFormat("#.0").format(info.rating));
     86             mRatingView.setRating(info.rating);
     87             mRatingView.setVisibility(View.VISIBLE);
     88             String reviewCountFormatted = NumberFormat.getInstance().format(info.reviewCount);
     89             mReviewCount.setText("(" + reviewCountFormatted + ")");
     90         } else {
     91             // if we don't have a rating
     92             mRatingView.setVisibility(View.GONE);
     93             mRatingText.setText("");
     94             mReviewCount.setText("");
     95         }
     96     }
     97 }
     98