• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
37  * Adapter for the high usage app list
38  */
39 public class HighUsageAdapter extends RecyclerView.Adapter<HighUsageAdapter.ViewHolder> {
40     private final Context mContext;
41     private final IconDrawableFactory mIconDrawableFactory;
42     private final PackageManager mPackageManager;
43     private final List<AppInfo> mHighUsageAppList;
44 
45     public static class ViewHolder extends RecyclerView.ViewHolder {
46         public View view;
47         public ImageView appIcon;
48         public TextView appName;
49         public TextView appTime;
50 
ViewHolder(View v)51         public ViewHolder(View v) {
52             super(v);
53             view = v;
54             appIcon = v.findViewById(R.id.app_icon);
55             appName = v.findViewById(R.id.app_name);
56             appTime = v.findViewById(R.id.app_screen_time);
57         }
58     }
59 
HighUsageAdapter(Context context, List<AppInfo> highUsageAppList)60     public HighUsageAdapter(Context context, List<AppInfo> highUsageAppList) {
61         mContext = context;
62         mHighUsageAppList = highUsageAppList;
63         mIconDrawableFactory = IconDrawableFactory.newInstance(context);
64         mPackageManager = context.getPackageManager();
65     }
66 
67     @Override
onCreateViewHolder(ViewGroup parent, int viewType)68     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
69         final View view = LayoutInflater.from(mContext).inflate(R.layout.app_high_usage_item,
70                 parent, false);
71         return new ViewHolder(view);
72     }
73 
74     @Override
onBindViewHolder(ViewHolder holder, int position)75     public void onBindViewHolder(ViewHolder holder, int position) {
76         final AppInfo app = mHighUsageAppList.get(position);
77         holder.appIcon.setImageDrawable(
78                 Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, app.packageName,
79                         UserHandle.getUserId(app.uid)));
80         CharSequence label = Utils.getApplicationLabel(mContext, app.packageName);
81         if (label == null) {
82             label = app.packageName;
83         }
84 
85         holder.appName.setText(label);
86     }
87 
88     @Override
getItemCount()89     public int getItemCount() {
90         return mHighUsageAppList.size();
91     }
92 }