• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.developeroptions.network.telephony;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.NetworkTemplate;
22 import android.provider.Settings;
23 import android.telephony.SubscriptionManager;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 import android.text.format.Formatter;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.car.developeroptions.R;
31 import com.android.settingslib.net.DataUsageController;
32 
33 /**
34  * Preference controller for "Data usage"
35  */
36 public class DataUsagePreferenceController extends TelephonyBasePreferenceController {
37 
38     private NetworkTemplate mTemplate;
39     private DataUsageController.DataUsageInfo mDataUsageInfo;
40     private Intent mIntent;
41 
DataUsagePreferenceController(Context context, String key)42     public DataUsagePreferenceController(Context context, String key) {
43         super(context, key);
44     }
45 
46     @Override
getAvailabilityStatus(int subId)47     public int getAvailabilityStatus(int subId) {
48         return subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
49                 ? AVAILABLE
50                 : AVAILABLE_UNSEARCHABLE;
51     }
52 
53     @Override
handlePreferenceTreeClick(Preference preference)54     public boolean handlePreferenceTreeClick(Preference preference) {
55         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
56             mContext.startActivity(mIntent);
57             return true;
58         }
59 
60         return false;
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         super.updateState(preference);
66         if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
67             preference.setEnabled(false);
68             return;
69         }
70         long usageLevel = mDataUsageInfo.usageLevel;
71         if (usageLevel <= 0L) {
72             final DataUsageController controller = new DataUsageController(mContext);
73             usageLevel = controller.getHistoricalUsageLevel(mTemplate);
74         }
75         final boolean enabled = usageLevel > 0L;
76         preference.setEnabled(enabled);
77 
78         if (enabled) {
79             preference.setSummary(mContext.getString(R.string.data_usage_template,
80                     Formatter.formatFileSize(mContext, mDataUsageInfo.usageLevel),
81                     mDataUsageInfo.period));
82         }
83     }
84 
init(int subId)85     public void init(int subId) {
86         mSubId = subId;
87 
88         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
89             mTemplate = getNetworkTemplate(mContext, subId);
90 
91             final DataUsageController controller = new DataUsageController(mContext);
92             mDataUsageInfo = controller.getDataUsageInfo(mTemplate);
93 
94             mIntent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
95             mIntent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
96             mIntent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
97         }
98     }
99 
getNetworkTemplate(Context context, int subId)100     private NetworkTemplate getNetworkTemplate(Context context, int subId) {
101         final TelephonyManager tm = TelephonyManager.from(context).createForSubscriptionId(subId);
102         NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(tm.getSubscriberId());
103 
104         return NetworkTemplate.normalize(mobileAll, tm.getMergedSubscriberIds());
105     }
106 
107 }
108