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.support.v7.preference.PreferenceViewHolder; 19 import android.view.View; 20 import android.widget.ProgressBar; 21 22 import com.android.settings.widget.AppPreference; 23 import com.android.settingslib.AppItem; 24 import com.android.settingslib.net.UidDetail; 25 import com.android.settingslib.net.UidDetailProvider; 26 import com.android.settingslib.utils.ThreadUtils; 27 28 public class AppDataUsagePreference extends AppPreference { 29 30 private final AppItem mItem; 31 private final int mPercent; 32 private UidDetail mDetail; 33 34 public AppDataUsagePreference(Context context, AppItem item, int percent, 35 UidDetailProvider provider) { 36 super(context); 37 mItem = item; 38 mPercent = percent; 39 40 if (item.restricted && item.total <= 0) { 41 setSummary(com.android.settings.R.string.data_usage_app_restricted); 42 } else { 43 setSummary(DataUsageUtils.formatDataUsage(context, item.total)); 44 } 45 mDetail = provider.getUidDetail(item.key, false /* blocking */); 46 if (mDetail != null) { 47 setAppInfo(); 48 } else { 49 ThreadUtils.postOnBackgroundThread(() -> { 50 mDetail = provider.getUidDetail(mItem.key, true /* blocking */); 51 ThreadUtils.postOnMainThread(() -> setAppInfo()); 52 }); 53 } 54 } 55 56 @Override 57 public void onBindViewHolder(PreferenceViewHolder holder) { 58 super.onBindViewHolder(holder); 59 final ProgressBar progress = (ProgressBar) holder.findViewById( 60 android.R.id.progress); 61 62 if (mItem.restricted && mItem.total <= 0) { 63 progress.setVisibility(View.GONE); 64 } else { 65 progress.setVisibility(View.VISIBLE); 66 } 67 progress.setProgress(mPercent); 68 } 69 70 private void setAppInfo() { 71 if (mDetail != null) { 72 setIcon(mDetail.icon); 73 setTitle(mDetail.label); 74 } else { 75 setIcon(null); 76 setTitle(null); 77 } 78 } 79 80 public AppItem getItem() { 81 return mItem; 82 } 83 } 84