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