1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.telephony.dataconnection; 18 19 20import android.os.Handler; 21import android.os.RegistrantList; 22import android.util.Pair; 23 24/** 25 * The class to hold different data enabled/disabled settings. Also it allows clients to register 26 * for overall data enabled setting changed event. 27 * @hide 28 */ 29public class DataEnabledSettings { 30 31 public static final int REASON_REGISTERED = 0; 32 33 public static final int REASON_INTERNAL_DATA_ENABLED = 1; 34 35 public static final int REASON_USER_DATA_ENABLED = 2; 36 37 public static final int REASON_POLICY_DATA_ENABLED = 3; 38 39 public static final int REASON_DATA_ENABLED_BY_CARRIER = 4; 40 41 /** 42 * responds to the setInternalDataEnabled call - used internally to turn off data. 43 * For example during emergency calls 44 */ 45 private boolean mInternalDataEnabled = true; 46 47 /** 48 * responds to public (user) API to enable/disable data use independent of 49 * mInternalDataEnabled and requests for APN access persisted 50 */ 51 private boolean mUserDataEnabled = true; 52 53 /** 54 * Flag indicating data allowed by network policy manager or not. 55 */ 56 private boolean mPolicyDataEnabled = true; 57 58 /** 59 * Indicate if metered APNs are enabled by the carrier. set false to block all the metered APNs 60 * from continuously sending requests, which causes undesired network load. 61 */ 62 private boolean mCarrierDataEnabled = true; 63 64 private final RegistrantList mDataEnabledChangedRegistrants = new RegistrantList(); 65 66 @Override 67 public String toString() { 68 return "[mInternalDataEnabled=" + mInternalDataEnabled + ", mUserDataEnabled=" 69 + mUserDataEnabled + ", mPolicyDataEnabled=" + mPolicyDataEnabled 70 + ", mCarrierDataEnabled=" + mCarrierDataEnabled + "]"; 71 } 72 73 public synchronized void setInternalDataEnabled(boolean enabled) { 74 boolean prevDataEnabled = isDataEnabled(); 75 mInternalDataEnabled = enabled; 76 if (prevDataEnabled != isDataEnabled()) { 77 notifyDataEnabledChanged(!prevDataEnabled, REASON_INTERNAL_DATA_ENABLED); 78 } 79 } 80 public synchronized boolean isInternalDataEnabled() { 81 return mInternalDataEnabled; 82 } 83 84 public synchronized void setUserDataEnabled(boolean enabled) { 85 boolean prevDataEnabled = isDataEnabled(); 86 mUserDataEnabled = enabled; 87 if (prevDataEnabled != isDataEnabled()) { 88 notifyDataEnabledChanged(!prevDataEnabled, REASON_USER_DATA_ENABLED); 89 } 90 } 91 public synchronized boolean isUserDataEnabled() { 92 return mUserDataEnabled; 93 } 94 95 public synchronized void setPolicyDataEnabled(boolean enabled) { 96 boolean prevDataEnabled = isDataEnabled(); 97 mPolicyDataEnabled = enabled; 98 if (prevDataEnabled != isDataEnabled()) { 99 notifyDataEnabledChanged(!prevDataEnabled, REASON_POLICY_DATA_ENABLED); 100 } 101 } 102 public synchronized boolean isPolicyDataEnabled() { 103 return mPolicyDataEnabled; 104 } 105 106 public synchronized void setCarrierDataEnabled(boolean enabled) { 107 boolean prevDataEnabled = isDataEnabled(); 108 mCarrierDataEnabled = enabled; 109 if (prevDataEnabled != isDataEnabled()) { 110 notifyDataEnabledChanged(!prevDataEnabled, REASON_DATA_ENABLED_BY_CARRIER); 111 } 112 } 113 public synchronized boolean isCarrierDataEnabled() { 114 return mCarrierDataEnabled; 115 } 116 117 public synchronized boolean isDataEnabled() { 118 return (mInternalDataEnabled && mUserDataEnabled && mPolicyDataEnabled 119 && mCarrierDataEnabled); 120 } 121 122 private void notifyDataEnabledChanged(boolean enabled, int reason) { 123 mDataEnabledChangedRegistrants.notifyResult(new Pair<>(enabled, reason)); 124 } 125 126 public void registerForDataEnabledChanged(Handler h, int what, Object obj) { 127 mDataEnabledChangedRegistrants.addUnique(h, what, obj); 128 notifyDataEnabledChanged(isDataEnabled(), REASON_REGISTERED); 129 } 130 131 public void unregisterForDataEnabledChanged(Handler h) { 132 mDataEnabledChangedRegistrants.remove(h); 133 } 134} 135