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