• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.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.text.TextUtils;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.datausage.DataUsageUtils;
30 import com.android.settingslib.net.DataUsageController;
31 
32 /**
33  * Preference controller for "Data usage"
34  */
35 public class DataUsagePreferenceController extends TelephonyBasePreferenceController {
36 
37     private NetworkTemplate mTemplate;
38     private DataUsageController.DataUsageInfo mDataUsageInfo;
39     private Intent mIntent;
40 
DataUsagePreferenceController(Context context, String key)41     public DataUsagePreferenceController(Context context, String key) {
42         super(context, key);
43     }
44 
45     @Override
getAvailabilityStatus(int subId)46     public int getAvailabilityStatus(int subId) {
47         return subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
48                 ? AVAILABLE
49                 : AVAILABLE_UNSEARCHABLE;
50     }
51 
52     @Override
handlePreferenceTreeClick(Preference preference)53     public boolean handlePreferenceTreeClick(Preference preference) {
54         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
55             mContext.startActivity(mIntent);
56             return true;
57         }
58 
59         return false;
60     }
61 
62     @Override
updateState(Preference preference)63     public void updateState(Preference preference) {
64         super.updateState(preference);
65         if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
66             preference.setEnabled(false);
67             return;
68         }
69         long usageLevel = mDataUsageInfo.usageLevel;
70         if (usageLevel <= 0L) {
71             final DataUsageController controller = new DataUsageController(mContext);
72             usageLevel = controller.getHistoricalUsageLevel(mTemplate);
73         }
74         final boolean enabled = usageLevel > 0L;
75         preference.setEnabled(enabled);
76 
77         if (enabled) {
78             preference.setSummary(mContext.getString(R.string.data_usage_template,
79                     DataUsageUtils.formatDataUsage(mContext, mDataUsageInfo.usageLevel),
80                     mDataUsageInfo.period));
81         }
82     }
83 
init(int subId)84     public void init(int subId) {
85         mSubId = subId;
86 
87         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
88             mTemplate = DataUsageUtils.getDefaultTemplate(mContext, mSubId);
89 
90             final DataUsageController controller = new DataUsageController(mContext);
91             controller.setSubscriptionId(mSubId);
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 }
100