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