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.TelephonyManager; 24 import android.telephony.data.ApnSetting; 25 import android.util.AttributeSet; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.core.SubSettingLauncher; 31 import com.android.settings.network.MobileDataEnabledListener; 32 33 /** 34 * Preference which displays billing cycle of subscription 35 */ 36 public class BillingCyclePreference extends Preference 37 implements TemplatePreference, MobileDataEnabledListener.Client { 38 39 private NetworkTemplate mTemplate; 40 private NetworkServices mServices; 41 private int mSubId; 42 private MobileDataEnabledListener mListener; 43 44 /** 45 * Preference constructor 46 * 47 * @param context Context of preference 48 * @param arrts The attributes of the XML tag that is inflating the preference 49 */ BillingCyclePreference(Context context, AttributeSet attrs)50 public BillingCyclePreference(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 mListener = new MobileDataEnabledListener(context, this); 53 } 54 55 @Override onAttached()56 public void onAttached() { 57 super.onAttached(); 58 mListener.start(mSubId); 59 } 60 61 @Override onDetached()62 public void onDetached() { 63 mListener.stop(); 64 super.onDetached(); 65 } 66 67 @Override setTemplate(NetworkTemplate template, int subId, NetworkServices services)68 public void setTemplate(NetworkTemplate template, int subId, 69 NetworkServices services) { 70 mTemplate = template; 71 mSubId = subId; 72 mServices = services; 73 setSummary(null); 74 75 setIntent(getIntent()); 76 } 77 updateEnabled()78 private void updateEnabled() { 79 try { 80 setEnabled(mServices.mNetworkService.isBandwidthControlEnabled() 81 && mServices.mTelephonyManager.createForSubscriptionId(mSubId) 82 .isDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER) 83 && mServices.mUserManager.isAdminUser()); 84 } catch (RemoteException e) { 85 setEnabled(false); 86 } 87 } 88 89 @Override getIntent()90 public Intent getIntent() { 91 final Bundle args = new Bundle(); 92 args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); 93 return new SubSettingLauncher(getContext()) 94 .setDestination(BillingCycleSettings.class.getName()) 95 .setArguments(args) 96 .setTitleRes(R.string.billing_cycle) 97 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN) 98 .toIntent(); 99 } 100 101 /** 102 * Implementation of {@code MobileDataEnabledListener.Client} 103 */ onMobileDataEnabledChange()104 public void onMobileDataEnabledChange() { 105 updateEnabled(); 106 } 107 } 108