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.util.AttributeSet; 24 25 import androidx.preference.Preference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.SubSettingLauncher; 29 import com.android.settings.datausage.CellDataPreference.DataStateListener; 30 31 public class BillingCyclePreference extends Preference implements TemplatePreference { 32 33 private NetworkTemplate mTemplate; 34 private NetworkServices mServices; 35 private int mSubId; 36 BillingCyclePreference(Context context, AttributeSet attrs)37 public BillingCyclePreference(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 @Override onAttached()42 public void onAttached() { 43 super.onAttached(); 44 mListener.setListener(true, mSubId, getContext()); 45 } 46 47 @Override onDetached()48 public void onDetached() { 49 mListener.setListener(false, mSubId, getContext()); 50 super.onDetached(); 51 } 52 53 @Override setTemplate(NetworkTemplate template, int subId, NetworkServices services)54 public void setTemplate(NetworkTemplate template, int subId, 55 NetworkServices services) { 56 mTemplate = template; 57 mSubId = subId; 58 mServices = services; 59 setSummary(null); 60 61 setIntent(getIntent()); 62 } 63 updateEnabled()64 private void updateEnabled() { 65 try { 66 setEnabled(mServices.mNetworkService.isBandwidthControlEnabled() 67 && mServices.mTelephonyManager.getDataEnabled(mSubId) 68 && mServices.mUserManager.isAdminUser()); 69 } catch (RemoteException e) { 70 setEnabled(false); 71 } 72 } 73 74 @Override getIntent()75 public Intent getIntent() { 76 Bundle args = new Bundle(); 77 args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); 78 return new SubSettingLauncher(getContext()) 79 .setDestination(BillingCycleSettings.class.getName()) 80 .setArguments(args) 81 .setTitleRes(R.string.billing_cycle) 82 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN) 83 .toIntent(); 84 } 85 86 private final DataStateListener mListener = new DataStateListener() { 87 @Override 88 public void onChange(boolean selfChange) { 89 updateEnabled(); 90 } 91 }; 92 } 93