1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.datausage;
16 
17 import android.content.Context;
18 import android.view.View;
19 import android.widget.ProgressBar;
20 
21 import androidx.preference.PreferenceViewHolder;
22 
23 import com.android.settings.R;
24 import com.android.settingslib.AppItem;
25 import com.android.settingslib.net.UidDetail;
26 import com.android.settingslib.net.UidDetailProvider;
27 import com.android.settingslib.utils.ThreadUtils;
28 import com.android.settingslib.widget.AppPreference;
29 
30 import java.text.NumberFormat;
31 
32 public class AppDataUsagePreference extends AppPreference {
33 
34     private final AppItem mItem;
35     private final int mPercent;
36     private UidDetail mDetail;
37 
AppDataUsagePreference(Context context, AppItem item, int percent, UidDetailProvider provider)38     public AppDataUsagePreference(Context context, AppItem item, int percent,
39             UidDetailProvider provider) {
40         super(context);
41         setKey("app_data_usage_" + item.key);
42         mItem = item;
43         mPercent = percent;
44 
45         if (item.restricted && item.total <= 0) {
46             setSummary(com.android.settings.R.string.data_usage_app_restricted);
47         } else {
48             setSummary(DataUsageUtils.formatDataUsage(context, item.total));
49         }
50         mDetail = provider.getUidDetail(item.key, false /* blocking */);
51         if (mDetail != null) {
52             setAppInfo();
53         } else {
54             // Set a placeholder title before starting to fetch real title, this is necessary
55             // to avoid preference height change.
56             setTitle(R.string.summary_placeholder);
57             ThreadUtils.postOnBackgroundThread(() -> {
58                 mDetail = provider.getUidDetail(mItem.key, true /* blocking */);
59                 ThreadUtils.postOnMainThread(() -> setAppInfo());
60             });
61         }
62     }
63 
64     @Override
onBindViewHolder(PreferenceViewHolder holder)65     public void onBindViewHolder(PreferenceViewHolder holder) {
66         super.onBindViewHolder(holder);
67         final ProgressBar progress = (ProgressBar) holder.findViewById(
68                 android.R.id.progress);
69 
70         if (mItem.restricted && mItem.total <= 0) {
71             progress.setVisibility(View.GONE);
72         } else {
73             progress.setVisibility(View.VISIBLE);
74         }
75         progress.setProgress(mPercent);
76         progress.setContentDescription(
77                 NumberFormat.getPercentInstance().format((double) mPercent / 100));
78     }
79 
setAppInfo()80     private void setAppInfo() {
81         if (mDetail != null) {
82             setIcon(mDetail.icon);
83             setTitle(mDetail.label);
84         } else {
85             setIcon(null);
86             setTitle(null);
87         }
88     }
89 
getItem()90     public AppItem getItem() {
91         return mItem;
92     }
93 }
94