• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.settings.vpn2;
18 
19 import static android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.text.SpannableString;
26 import android.text.TextUtils;
27 import android.text.style.ForegroundColorSpan;
28 import android.util.AttributeSet;
29 
30 import com.android.settings.R;
31 import com.android.settings.widget.GearPreference;
32 import com.android.settingslib.Utils;
33 
34 /**
35  * This class sets appropriate enabled state and user admin message when userId is set
36  */
37 public abstract class ManageablePreference extends GearPreference {
38 
39     public static int STATE_NONE = -1;
40 
41     boolean mIsAlwaysOn = false;
42     boolean mIsInsecureVpn = false;
43     int mState = STATE_NONE;
44     int mUserId;
45 
ManageablePreference(Context context, AttributeSet attrs)46     public ManageablePreference(Context context, AttributeSet attrs) {
47         super(context, attrs);
48         setPersistent(false);
49         setOrder(0);
50         setUserId(UserHandle.myUserId());
51     }
52 
getUserId()53     public int getUserId() {
54         return mUserId;
55     }
56 
setUserId(int userId)57     public void setUserId(int userId) {
58         mUserId = userId;
59         checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN, userId);
60     }
61 
isAlwaysOn()62     public boolean isAlwaysOn() {
63         return mIsAlwaysOn;
64     }
65 
isInsecureVpn()66     public boolean isInsecureVpn() {
67         return mIsInsecureVpn;
68     }
69 
getState()70     public int getState() {
71         return mState;
72     }
73 
setState(int state)74     public void setState(int state) {
75         if (mState != state) {
76             mState = state;
77             updateSummary();
78             notifyHierarchyChanged();
79         }
80     }
81 
setAlwaysOn(boolean isEnabled)82     public void setAlwaysOn(boolean isEnabled) {
83         if (mIsAlwaysOn != isEnabled) {
84             mIsAlwaysOn = isEnabled;
85             updateSummary();
86         }
87     }
88 
89     /**
90      * Set whether the VPN associated with this preference has an insecure type.
91      * By default the value will be False.
92      */
setInsecureVpn(boolean isInsecureVpn)93     public void setInsecureVpn(boolean isInsecureVpn) {
94         if (mIsInsecureVpn != isInsecureVpn) {
95             mIsInsecureVpn = isInsecureVpn;
96             updateSummary();
97         }
98     }
99 
100     /**
101      * Update the preference summary string (see {@see Preference#setSummary}) with a string
102      * reflecting connection status, always-on setting and whether the vpn is insecure.
103      *
104      * State is not shown for {@code STATE_NONE}.
105      */
updateSummary()106     protected void updateSummary() {
107         final Resources res = getContext().getResources();
108         final String[] states = res.getStringArray(R.array.vpn_states);
109         String summary = (mState == STATE_NONE ? "" : states[mState]);
110         if (mIsInsecureVpn) {
111             final String insecureString = res.getString(R.string.vpn_insecure_summary);
112             summary = TextUtils.isEmpty(summary) ? insecureString : summary + " / "
113                     + insecureString;
114 
115             SpannableString summarySpan = new SpannableString(summary);
116             final int colorError = Utils.getColorErrorDefaultColor(getContext());
117             summarySpan.setSpan(new ForegroundColorSpan(colorError), 0, summary.length(),
118                     SPAN_EXCLUSIVE_INCLUSIVE);
119             setSummary(summarySpan);
120         } else if (mIsAlwaysOn) {
121             final String alwaysOnString = res.getString(R.string.vpn_always_on_summary_active);
122             summary = TextUtils.isEmpty(summary) ? alwaysOnString : summary + " / "
123                     + alwaysOnString;
124             setSummary(summary);
125         } else {
126             setSummary(summary);
127         }
128     }
129 }
130