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