blob: 85e77001f7c8da69c69c9e15a53c7815fefdf67c [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
Malcolm Chen359d1ab2017-08-16 17:30:45 -070029import com.android.settingslib.drawer.SettingsDrawerActivity;
Malcolm Chen9ef63c62017-06-20 16:53:01 -070030import com.android.settingslib.net.DataUsageController;
31
32/**
33 * The preference that shows mobile data usage summary and
34 * leads to mobile data usage list page.
35 */
36public class DataUsagePreference extends Preference {
37
38 private NetworkTemplate mTemplate;
39 private int mSubId;
40
41 public DataUsagePreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
43 }
44
45 /**
46 * After creating this preference, this functions needs to be called to
47 * initialize which subID it connects to.
48 */
49 public void initialize(int subId) {
50 Activity activity = (Activity) getContext();
51
52 mSubId = subId;
53 mTemplate = getNetworkTemplate(activity, subId);
54
55 DataUsageController controller = new DataUsageController(activity);
56
57 DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
58 setSummary(activity.getString(R.string.data_usage_template,
59 Formatter.formatFileSize(activity, usageInfo.usageLevel), usageInfo.period));
60 setIntent(getIntent());
61 }
62
63 @Override
64 public Intent getIntent() {
Malcolm Chen9ef63c62017-06-20 16:53:01 -070065 Intent intent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
Malcolm Chen359d1ab2017-08-16 17:30:45 -070066 intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true);
Malcolm Chen9ef63c62017-06-20 16:53:01 -070067
68 intent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
69 intent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
70
71 return intent;
72 }
73
74 private NetworkTemplate getNetworkTemplate(Activity activity, int subId) {
75 TelephonyManager tm = (TelephonyManager) activity
76 .getSystemService(Context.TELEPHONY_SERVICE);
77 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
78 tm.getSubscriberId(subId));
79 return NetworkTemplate.normalize(mobileAll,
80 tm.getMergedSubscriberIds());
81 }
82}