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.content.Context; 18 import android.content.Intent; 19 import android.net.NetworkPolicy; 20 import android.net.NetworkTemplate; 21 import android.os.Bundle; 22 import android.os.RemoteException; 23 import android.support.v7.preference.Preference; 24 import android.util.AttributeSet; 25 26 import com.android.internal.logging.nano.MetricsProto; 27 import com.android.settings.R; 28 import com.android.settings.Utils; 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 NetworkPolicy mPolicy; 36 private int mSubId; 37 BillingCyclePreference(Context context, AttributeSet attrs)38 public BillingCyclePreference(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 42 @Override onAttached()43 public void onAttached() { 44 super.onAttached(); 45 mListener.setListener(true, mSubId, getContext()); 46 } 47 48 @Override onDetached()49 public void onDetached() { 50 mListener.setListener(false, mSubId, getContext()); 51 super.onDetached(); 52 } 53 54 @Override setTemplate(NetworkTemplate template, int subId, NetworkServices services)55 public void setTemplate(NetworkTemplate template, int subId, 56 NetworkServices services) { 57 mTemplate = template; 58 mSubId = subId; 59 mServices = services; 60 mPolicy = services.mPolicyEditor.getPolicy(mTemplate); 61 setSummary(getContext().getString(R.string.billing_cycle_fragment_summary, mPolicy != null 62 ? mPolicy.cycleDay 63 : "1")); 64 setIntent(getIntent()); 65 } 66 updateEnabled()67 private void updateEnabled() { 68 try { 69 setEnabled(mPolicy != null && mServices.mNetworkService.isBandwidthControlEnabled() 70 && mServices.mTelephonyManager.getDataEnabled(mSubId) 71 && mServices.mUserManager.isAdminUser()); 72 } catch (RemoteException e) { 73 setEnabled(false); 74 } 75 } 76 77 @Override getIntent()78 public Intent getIntent() { 79 Bundle args = new Bundle(); 80 args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); 81 return Utils.onBuildStartFragmentIntent(getContext(), BillingCycleSettings.class.getName(), 82 args, null, 0, getTitle(), false, MetricsProto.MetricsEvent.VIEW_UNKNOWN); 83 } 84 85 private final DataStateListener mListener = new DataStateListener() { 86 @Override 87 public void onChange(boolean selfChange) { 88 updateEnabled(); 89 } 90 }; 91 } 92