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.app.settings.SettingsEnums; 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.res.TypedArray; 21 import android.net.NetworkTemplate; 22 import android.os.Bundle; 23 import android.util.AttributeSet; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.core.content.res.TypedArrayUtils; 27 import androidx.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.core.SubSettingLauncher; 31 import com.android.settingslib.net.DataUsageController; 32 33 public class DataUsagePreference extends Preference implements TemplatePreference { 34 35 private NetworkTemplate mTemplate; 36 private int mSubId; 37 private int mTitleRes; 38 DataUsagePreference(Context context, AttributeSet attrs)39 public DataUsagePreference(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 final TypedArray a = context.obtainStyledAttributes( 42 attrs, new int[] {com.android.internal.R.attr.title}, 43 TypedArrayUtils.getAttr( 44 context, androidx.preference.R.attr.preferenceStyle, 45 android.R.attr.preferenceStyle), 0); 46 mTitleRes = a.getResourceId(0, 0); 47 a.recycle(); 48 } 49 50 @Override setTemplate(NetworkTemplate template, int subId)51 public void setTemplate(NetworkTemplate template, int subId) { 52 mTemplate = template; 53 mSubId = subId; 54 final DataUsageController controller = getDataUsageController(); 55 if (mTemplate.getMatchRule() == NetworkTemplate.MATCH_MOBILE) { 56 setTitle(R.string.app_cellular_data_usage); 57 } else { 58 final DataUsageController.DataUsageInfo usageInfo = 59 controller.getDataUsageInfo(mTemplate); 60 setTitle(mTitleRes); 61 setSummary(getContext().getString(R.string.data_usage_template, 62 DataUsageUtils.formatDataUsage(getContext(), usageInfo.usageLevel), 63 usageInfo.period)); 64 } 65 final long usageLevel = controller.getHistoricalUsageLevel(template); 66 if (usageLevel > 0L) { 67 setIntent(getIntent()); 68 } else { 69 setIntent(null); 70 setEnabled(false); 71 } 72 } 73 74 @Override getIntent()75 public Intent getIntent() { 76 final Bundle args = new Bundle(); 77 final SubSettingLauncher launcher; 78 args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); 79 args.putInt(DataUsageList.EXTRA_SUB_ID, mSubId); 80 launcher = new SubSettingLauncher(getContext()) 81 .setArguments(args) 82 .setDestination(DataUsageList.class.getName()) 83 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN); 84 if (mTemplate.getMatchRule() == NetworkTemplate.MATCH_MOBILE) { 85 launcher.setTitleRes(R.string.app_cellular_data_usage); 86 } else { 87 launcher.setTitleRes(mTitleRes); 88 } 89 return launcher.toIntent(); 90 } 91 92 @VisibleForTesting getDataUsageController()93 DataUsageController getDataUsageController() { 94 return new DataUsageController(getContext()); 95 } 96 } 97