blob: b6b26e2b838c667f94d0926791de2d41c092231d [file] [log] [blame]
Malcolm Chen9ef63c62017-06-20 16:53:01 -07001/*
2 * Copyright (C) 2017 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
17package com.android.phone;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.net.NetworkTemplate;
Malcolm Chen9ef63c62017-06-20 16:53:01 -070023import android.preference.Preference;
24import android.provider.Settings;
25import android.telephony.TelephonyManager;
26import android.text.format.Formatter;
27import android.util.AttributeSet;
28
29import com.android.settingslib.net.DataUsageController;
30
31/**
32 * The preference that shows mobile data usage summary and
33 * leads to mobile data usage list page.
34 */
35public class DataUsagePreference extends Preference {
36
37 private NetworkTemplate mTemplate;
38 private int mSubId;
39
40 public DataUsagePreference(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 }
43
44 /**
45 * After creating this preference, this functions needs to be called to
46 * initialize which subID it connects to.
47 */
48 public void initialize(int subId) {
49 Activity activity = (Activity) getContext();
50
51 mSubId = subId;
52 mTemplate = getNetworkTemplate(activity, subId);
53
54 DataUsageController controller = new DataUsageController(activity);
55
56 DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
57 setSummary(activity.getString(R.string.data_usage_template,
58 Formatter.formatFileSize(activity, usageInfo.usageLevel), usageInfo.period));
59 setIntent(getIntent());
60 }
61
62 @Override
63 public Intent getIntent() {
Malcolm Chen9ef63c62017-06-20 16:53:01 -070064 Intent intent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
Malcolm Chen9ef63c62017-06-20 16:53:01 -070065
66 intent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
67 intent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
68
69 return intent;
70 }
71
72 private NetworkTemplate getNetworkTemplate(Activity activity, int subId) {
73 TelephonyManager tm = (TelephonyManager) activity
74 .getSystemService(Context.TELEPHONY_SERVICE);
75 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
76 tm.getSubscriberId(subId));
77 return NetworkTemplate.normalize(mobileAll,
78 tm.getMergedSubscriberIds());
79 }
80}