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.os.AsyncTask;
19 import android.support.v7.preference.Preference;
20 import android.support.v7.preference.PreferenceViewHolder;
21 import android.text.format.Formatter;
22 import android.view.View;
23 import android.widget.ProgressBar;
24 import com.android.settingslib.AppItem;
25 import com.android.settingslib.net.UidDetail;
26 import com.android.settingslib.net.UidDetailProvider;
27 
28 import static com.android.internal.util.Preconditions.checkNotNull;
29 
30 public class AppDataUsagePreference extends Preference {
31 
32     private final AppItem mItem;
33     private final int mPercent;
34 
AppDataUsagePreference(Context context, AppItem item, int percent, UidDetailProvider provider)35     public AppDataUsagePreference(Context context, AppItem item, int percent,
36             UidDetailProvider provider) {
37         super(context);
38         mItem = item;
39         mPercent = percent;
40         setLayoutResource(com.android.settings.R.layout.data_usage_item);
41         setWidgetLayoutResource(com.android.settings.R.layout.widget_progress_bar);
42         if (item.restricted && item.total <= 0) {
43             setSummary(com.android.settings.R.string.data_usage_app_restricted);
44         } else {
45             setSummary(Formatter.formatFileSize(context, item.total));
46         }
47 
48         // kick off async load of app details
49         UidDetailTask.bindView(provider, item, this);
50     }
51 
52     @Override
onBindViewHolder(PreferenceViewHolder holder)53     public void onBindViewHolder(PreferenceViewHolder holder) {
54         super.onBindViewHolder(holder);
55 
56         final ProgressBar progress = (ProgressBar) holder.findViewById(
57                 android.R.id.progress);
58 
59         if (mItem.restricted && mItem.total <= 0) {
60             progress.setVisibility(View.GONE);
61         } else {
62             progress.setVisibility(View.VISIBLE);
63         }
64         progress.setProgress(mPercent);
65     }
66 
getItem()67     public AppItem getItem() {
68         return mItem;
69     }
70 
71     /**
72      * Background task that loads {@link UidDetail}, binding to
73      * {@link DataUsageAdapter} row item when finished.
74      */
75     private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
76         private final UidDetailProvider mProvider;
77         private final AppItem mItem;
78         private final AppDataUsagePreference mTarget;
79 
UidDetailTask(UidDetailProvider provider, AppItem item, AppDataUsagePreference target)80         private UidDetailTask(UidDetailProvider provider, AppItem item,
81                 AppDataUsagePreference target) {
82             mProvider = checkNotNull(provider);
83             mItem = checkNotNull(item);
84             mTarget = checkNotNull(target);
85         }
86 
bindView(UidDetailProvider provider, AppItem item, AppDataUsagePreference target)87         public static void bindView(UidDetailProvider provider, AppItem item,
88                 AppDataUsagePreference target) {
89             final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
90             if (cachedDetail != null) {
91                 bindView(cachedDetail, target);
92             } else {
93                 new UidDetailTask(provider, item, target).executeOnExecutor(
94                         AsyncTask.THREAD_POOL_EXECUTOR);
95             }
96         }
97 
bindView(UidDetail detail, Preference target)98         private static void bindView(UidDetail detail, Preference target) {
99             if (detail != null) {
100                 target.setIcon(detail.icon);
101                 target.setTitle(detail.label);
102             } else {
103                 target.setIcon(null);
104                 target.setTitle(null);
105             }
106         }
107 
108         @Override
onPreExecute()109         protected void onPreExecute() {
110             bindView(null, mTarget);
111         }
112 
113         @Override
doInBackground(Void... params)114         protected UidDetail doInBackground(Void... params) {
115             return mProvider.getUidDetail(mItem.key, true);
116         }
117 
118         @Override
onPostExecute(UidDetail result)119         protected void onPostExecute(UidDetail result) {
120             bindView(result, mTarget);
121         }
122     }
123 }
124