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 this(context, attrs, 0, 0); 48 } 49 ManageablePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)50 public ManageablePreference(Context context, AttributeSet attrs, 51 int defStyleAttr, int defStyleRes) { 52 super(context, attrs, defStyleAttr, defStyleRes); 53 setPersistent(false); 54 setOrder(0); 55 setUserId(UserHandle.myUserId()); 56 } 57 getUserId()58 public int getUserId() { 59 return mUserId; 60 } 61 setUserId(int userId)62 public void setUserId(int userId) { 63 mUserId = userId; 64 checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN, userId); 65 } 66 isAlwaysOn()67 public boolean isAlwaysOn() { 68 return mIsAlwaysOn; 69 } 70 isInsecureVpn()71 public boolean isInsecureVpn() { 72 return mIsInsecureVpn; 73 } 74 getState()75 public int getState() { 76 return mState; 77 } 78 setState(int state)79 public void setState(int state) { 80 if (mState != state) { 81 mState = state; 82 updateSummary(); 83 notifyHierarchyChanged(); 84 } 85 } 86 setAlwaysOn(boolean isEnabled)87 public void setAlwaysOn(boolean isEnabled) { 88 if (mIsAlwaysOn != isEnabled) { 89 mIsAlwaysOn = isEnabled; 90 updateSummary(); 91 } 92 } 93 94 /** 95 * Set whether the VPN associated with this preference has an insecure type. 96 * By default the value will be False. 97 */ setInsecureVpn(boolean isInsecureVpn)98 public void setInsecureVpn(boolean isInsecureVpn) { 99 if (mIsInsecureVpn != isInsecureVpn) { 100 mIsInsecureVpn = isInsecureVpn; 101 updateSummary(); 102 } 103 } 104 105 /** 106 * Update the preference summary string (see {@see Preference#setSummary}) with a string 107 * reflecting connection status, always-on setting and whether the vpn is insecure. 108 * 109 * State is not shown for {@code STATE_NONE}. 110 */ updateSummary()111 protected void updateSummary() { 112 final Resources res = getContext().getResources(); 113 final String[] states = res.getStringArray(R.array.vpn_states); 114 String summary = (mState == STATE_NONE ? "" : states[mState]); 115 if (mIsInsecureVpn) { 116 final String insecureString = res.getString(R.string.vpn_insecure_summary); 117 summary = TextUtils.isEmpty(summary) ? insecureString : summary + " / " 118 + insecureString; 119 120 SpannableString summarySpan = new SpannableString(summary); 121 final int colorError = Utils.getColorErrorDefaultColor(getContext()); 122 summarySpan.setSpan(new ForegroundColorSpan(colorError), 0, summary.length(), 123 SPAN_EXCLUSIVE_INCLUSIVE); 124 setSummary(summarySpan); 125 } else if (mIsAlwaysOn) { 126 final String alwaysOnString = res.getString(R.string.vpn_always_on_summary_active); 127 summary = TextUtils.isEmpty(summary) ? alwaysOnString : summary + " / " 128 + alwaysOnString; 129 setSummary(summary); 130 } else { 131 setSummary(summary); 132 } 133 } 134 } 135