• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.internal.telephony.dataconnection;
18 
19 
20 import android.os.Handler;
21 import android.os.RegistrantList;
22 import 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  */
29 public 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 
setInternalDataEnabled(boolean enabled)66     public synchronized void setInternalDataEnabled(boolean enabled) {
67         boolean prevDataEnabled = isDataEnabled(true);
68         mInternalDataEnabled = enabled;
69         if (prevDataEnabled != isDataEnabled(true)) {
70             notifyDataEnabledChanged(!prevDataEnabled, REASON_INTERNAL_DATA_ENABLED);
71         }
72     }
isInternalDataEnabled()73     public synchronized boolean isInternalDataEnabled() {
74         return mInternalDataEnabled;
75     }
76 
setUserDataEnabled(boolean enabled)77     public synchronized void setUserDataEnabled(boolean enabled) {
78         boolean prevDataEnabled = isDataEnabled(true);
79         mUserDataEnabled = enabled;
80         if (prevDataEnabled != isDataEnabled(true)) {
81             notifyDataEnabledChanged(!prevDataEnabled, REASON_USER_DATA_ENABLED);
82         }
83     }
isUserDataEnabled()84     public synchronized boolean isUserDataEnabled() {
85         return mUserDataEnabled;
86     }
87 
setPolicyDataEnabled(boolean enabled)88     public synchronized void setPolicyDataEnabled(boolean enabled) {
89         boolean prevDataEnabled = isDataEnabled(true);
90         mPolicyDataEnabled = enabled;
91         if (prevDataEnabled != isDataEnabled(true)) {
92             notifyDataEnabledChanged(!prevDataEnabled, REASON_POLICY_DATA_ENABLED);
93         }
94     }
isPolicyDataEnabled()95     public synchronized boolean isPolicyDataEnabled() {
96         return mPolicyDataEnabled;
97     }
98 
setCarrierDataEnabled(boolean enabled)99     public synchronized void setCarrierDataEnabled(boolean enabled) {
100         boolean prevDataEnabled = isDataEnabled(true);
101         mCarrierDataEnabled = enabled;
102         if (prevDataEnabled != isDataEnabled(true)) {
103             notifyDataEnabledChanged(!prevDataEnabled, REASON_DATA_ENABLED_BY_CARRIER);
104         }
105     }
isCarrierDataEnabled()106     public synchronized boolean isCarrierDataEnabled() {
107         return mCarrierDataEnabled;
108     }
109 
isDataEnabled(boolean checkUserDataEnabled)110     public synchronized boolean isDataEnabled(boolean checkUserDataEnabled) {
111         return (mInternalDataEnabled
112                 && (!checkUserDataEnabled || mUserDataEnabled)
113                 && (!checkUserDataEnabled || mPolicyDataEnabled)
114                 && (!checkUserDataEnabled || mCarrierDataEnabled));
115     }
116 
notifyDataEnabledChanged(boolean enabled, int reason)117     private void notifyDataEnabledChanged(boolean enabled, int reason) {
118         mDataEnabledChangedRegistrants.notifyResult(new Pair<>(enabled, reason));
119     }
120 
registerForDataEnabledChanged(Handler h, int what, Object obj)121     public void registerForDataEnabledChanged(Handler h, int what, Object obj) {
122         mDataEnabledChangedRegistrants.addUnique(h, what, obj);
123         notifyDataEnabledChanged(isDataEnabled(true), REASON_REGISTERED);
124     }
125 
unregisterForDataEnabledChanged(Handler h)126     public void unregisterForDataEnabledChanged(Handler h) {
127         mDataEnabledChangedRegistrants.remove(h);
128     }
129 }
130