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 mItem = item; 42 mPercent = percent; 43 44 if (item.restricted && item.total <= 0) { 45 setSummary(com.android.settings.R.string.data_usage_app_restricted); 46 } else { 47 setSummary(DataUsageUtils.formatDataUsage(context, item.total)); 48 } 49 mDetail = provider.getUidDetail(item.key, false /* blocking */); 50 if (mDetail != null) { 51 setAppInfo(); 52 } else { 53 // Set a placeholder title before starting to fetch real title, this is necessary 54 // to avoid preference height change. 55 setTitle(R.string.summary_placeholder); 56 ThreadUtils.postOnBackgroundThread(() -> { 57 mDetail = provider.getUidDetail(mItem.key, true /* blocking */); 58 ThreadUtils.postOnMainThread(() -> setAppInfo()); 59 }); 60 } 61 } 62 63 @Override onBindViewHolder(PreferenceViewHolder holder)64 public void onBindViewHolder(PreferenceViewHolder holder) { 65 super.onBindViewHolder(holder); 66 final ProgressBar progress = (ProgressBar) holder.findViewById( 67 android.R.id.progress); 68 69 if (mItem.restricted && mItem.total <= 0) { 70 progress.setVisibility(View.GONE); 71 } else { 72 progress.setVisibility(View.VISIBLE); 73 } 74 progress.setProgress(mPercent); 75 progress.setContentDescription( 76 NumberFormat.getPercentInstance().format((double) mPercent / 100)); 77 } 78 setAppInfo()79 private void setAppInfo() { 80 if (mDetail != null) { 81 setIcon(mDetail.icon); 82 setTitle(mDetail.label); 83 } else { 84 setIcon(null); 85 setTitle(null); 86 } 87 } 88 getItem()89 public AppItem getItem() { 90 return mItem; 91 } 92 } 93