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
17package com.android.settings.fuelgauge.batterytip;
18
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.os.UserHandle;
22import android.support.v7.widget.RecyclerView;
23import android.util.IconDrawableFactory;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ImageView;
28import android.widget.TextView;
29
30import com.android.settings.R;
31import com.android.settings.Utils;
32
33import com.android.settingslib.utils.StringUtil;
34import java.util.List;
35
36/**
37 * Adapter for the high usage app list
38 */
39public 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
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
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
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
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        holder.appName.setText(Utils.getApplicationLabel(mContext, app.packageName));
81        if (app.screenOnTimeMs != 0) {
82            holder.appTime.setText(StringUtil.formatElapsedTime(mContext, app.screenOnTimeMs, false));
83        }
84    }
85
86    @Override
87    public int getItemCount() {
88        return mHighUsageAppList.size();
89    }
90}