• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.phone;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.NetworkTemplate;
23 import android.preference.Preference;
24 import android.provider.Settings;
25 import android.telephony.TelephonyManager;
26 import android.text.format.Formatter;
27 import android.util.AttributeSet;
28 
29 import com.android.settingslib.drawer.SettingsDrawerActivity;
30 import 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  */
36 public class DataUsagePreference extends Preference {
37 
38     private NetworkTemplate mTemplate;
39     private int mSubId;
40 
DataUsagePreference(Context context, AttributeSet attrs)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      */
initialize(int subId)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
getIntent()64     public Intent getIntent() {
65         Intent intent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
66         intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true);
67 
68         intent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
69         intent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
70 
71         return intent;
72     }
73 
getNetworkTemplate(Activity activity, int subId)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 }
83