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