1 /*
2  * Copyright (C) 2018 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.settings.fuelgauge.batterytip;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.UserHandle;
22 import android.util.IconDrawableFactory;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import androidx.recyclerview.widget.RecyclerView;
30 
31 import com.android.settings.R;
32 import com.android.settings.Utils;
33 
34 import java.util.List;
35 
36 /** Adapter for the high usage app list */
37 public class HighUsageAdapter extends RecyclerView.Adapter<HighUsageAdapter.ViewHolder> {
38     private final Context mContext;
39     private final IconDrawableFactory mIconDrawableFactory;
40     private final PackageManager mPackageManager;
41     private final List<AppInfo> mHighUsageAppList;
42 
43     public static class ViewHolder extends RecyclerView.ViewHolder {
44         public View view;
45         public ImageView appIcon;
46         public TextView appName;
47         public TextView appTime;
48 
ViewHolder(View v)49         public ViewHolder(View v) {
50             super(v);
51             view = v;
52             appIcon = v.findViewById(R.id.app_icon);
53             appName = v.findViewById(R.id.app_name);
54             appTime = v.findViewById(R.id.app_screen_time);
55         }
56     }
57 
HighUsageAdapter(Context context, List<AppInfo> highUsageAppList)58     public HighUsageAdapter(Context context, List<AppInfo> highUsageAppList) {
59         mContext = context;
60         mHighUsageAppList = highUsageAppList;
61         mIconDrawableFactory = IconDrawableFactory.newInstance(context);
62         mPackageManager = context.getPackageManager();
63     }
64 
65     @Override
onCreateViewHolder(ViewGroup parent, int viewType)66     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
67         final View view =
68                 LayoutInflater.from(mContext).inflate(R.layout.app_high_usage_item, parent, false);
69         return new ViewHolder(view);
70     }
71 
72     @Override
onBindViewHolder(ViewHolder holder, int position)73     public void onBindViewHolder(ViewHolder holder, int position) {
74         final AppInfo app = mHighUsageAppList.get(position);
75         holder.appIcon.setImageDrawable(
76                 Utils.getBadgedIcon(
77                         mIconDrawableFactory,
78                         mPackageManager,
79                         app.packageName,
80                         UserHandle.getUserId(app.uid)));
81         CharSequence label = Utils.getApplicationLabel(mContext, app.packageName);
82         if (label == null) {
83             label = app.packageName;
84         }
85 
86         holder.appName.setText(label);
87     }
88 
89     @Override
getItemCount()90     public int getItemCount() {
91         return mHighUsageAppList.size();
92     }
93 }
94