1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.datausage;
16 
17 import android.app.settings.SettingsEnums;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.net.NetworkTemplate;
21 import android.os.Bundle;
22 import android.os.RemoteException;
23 import android.telephony.data.ApnSetting;
24 import android.util.AttributeSet;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.SubSettingLauncher;
30 import com.android.settings.network.MobileDataEnabledListener;
31 
32 /**
33  * Preference which displays billing cycle of subscription
34  */
35 public class BillingCyclePreference extends Preference
36         implements TemplatePreference, MobileDataEnabledListener.Client {
37 
38     private NetworkTemplate mTemplate;
39     private NetworkServices mServices;
40     private int mSubId;
41     private MobileDataEnabledListener mListener;
42 
43     /**
44      * Preference constructor
45      *
46      * @param context Context of preference
47      * @param arrts The attributes of the XML tag that is inflating the preference
48      */
BillingCyclePreference(Context context, AttributeSet attrs)49     public BillingCyclePreference(Context context, AttributeSet attrs) {
50         super(context, attrs);
51         mListener = new MobileDataEnabledListener(context, this);
52     }
53 
54     @Override
onAttached()55     public void onAttached() {
56         super.onAttached();
57         mListener.start(mSubId);
58     }
59 
60     @Override
onDetached()61     public void onDetached() {
62         mListener.stop();
63         super.onDetached();
64     }
65 
66     @Override
setTemplate(NetworkTemplate template, int subId, NetworkServices services)67     public void setTemplate(NetworkTemplate template, int subId,
68             NetworkServices services) {
69         mTemplate = template;
70         mSubId = subId;
71         mServices = services;
72         setSummary(null);
73 
74         setIntent(getIntent());
75     }
76 
updateEnabled()77     private void updateEnabled() {
78         try {
79             setEnabled(mServices.mNetworkService.isBandwidthControlEnabled()
80                     && mServices.mTelephonyManager.createForSubscriptionId(mSubId)
81                             .isDataEnabledForApn(ApnSetting.TYPE_DEFAULT)
82                     && mServices.mUserManager.isAdminUser());
83         } catch (RemoteException e) {
84             setEnabled(false);
85         }
86     }
87 
88     @Override
getIntent()89     public Intent getIntent() {
90         final Bundle args = new Bundle();
91         args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate);
92         return new SubSettingLauncher(getContext())
93                 .setDestination(BillingCycleSettings.class.getName())
94                 .setArguments(args)
95                 .setTitleRes(R.string.billing_cycle)
96                 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
97                 .toIntent();
98     }
99 
100     /**
101      * Implementation of {@code MobileDataEnabledListener.Client}
102      */
onMobileDataEnabledChange()103     public void onMobileDataEnabledChange() {
104         updateEnabled();
105     }
106 }
107