Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 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 | |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 19 | import static android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE; |
| 20 | |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 21 | import android.content.Context; |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.os.UserHandle; |
| 24 | import android.os.UserManager; |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 25 | import android.text.SpannableString; |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 26 | import android.text.TextUtils; |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 27 | import android.text.style.ForegroundColorSpan; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 29 | |
| 30 | import com.android.settings.R; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 31 | import com.android.settings.widget.GearPreference; |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 32 | import com.android.settingslib.Utils; |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 35 | * This class sets appropriate enabled state and user admin message when userId is set |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 36 | */ |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 37 | public abstract class ManageablePreference extends GearPreference { |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 38 | |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 39 | public static int STATE_NONE = -1; |
| 40 | |
| 41 | boolean mIsAlwaysOn = false; |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 42 | boolean mIsInsecureVpn = false; |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 43 | int mState = STATE_NONE; |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 44 | int mUserId; |
| 45 | |
| 46 | public ManageablePreference(Context context, AttributeSet attrs) { |
Bonian Chen | 61c4506 | 2023-01-09 08:10:11 +0000 | [diff] [blame] | 47 | this(context, attrs, 0, 0); |
| 48 | } |
| 49 | |
| 50 | public ManageablePreference(Context context, AttributeSet attrs, |
| 51 | int defStyleAttr, int defStyleRes) { |
| 52 | super(context, attrs, defStyleAttr, defStyleRes); |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 53 | setPersistent(false); |
| 54 | setOrder(0); |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 55 | setUserId(UserHandle.myUserId()); |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 58 | public int getUserId() { |
| 59 | return mUserId; |
| 60 | } |
| 61 | |
| 62 | public void setUserId(int userId) { |
| 63 | mUserId = userId; |
| 64 | checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN, userId); |
| 65 | } |
| 66 | |
| 67 | public boolean isAlwaysOn() { |
| 68 | return mIsAlwaysOn; |
| 69 | } |
| 70 | |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 71 | public boolean isInsecureVpn() { |
| 72 | return mIsInsecureVpn; |
| 73 | } |
| 74 | |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 75 | public int getState() { |
| 76 | return mState; |
| 77 | } |
| 78 | |
| 79 | public void setState(int state) { |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 80 | if (mState != state) { |
| 81 | mState = state; |
| 82 | updateSummary(); |
| 83 | notifyHierarchyChanged(); |
| 84 | } |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 87 | public void setAlwaysOn(boolean isEnabled) { |
Robin Lee | b166ea2 | 2016-04-21 12:56:21 +0100 | [diff] [blame] | 88 | if (mIsAlwaysOn != isEnabled) { |
| 89 | mIsAlwaysOn = isEnabled; |
| 90 | updateSummary(); |
| 91 | } |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 95 | * Set whether the VPN associated with this preference has an insecure type. |
| 96 | * By default the value will be False. |
| 97 | */ |
| 98 | public void setInsecureVpn(boolean isInsecureVpn) { |
| 99 | if (mIsInsecureVpn != isInsecureVpn) { |
| 100 | mIsInsecureVpn = isInsecureVpn; |
| 101 | updateSummary(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 106 | * Update the preference summary string (see {@see Preference#setSummary}) with a string |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 107 | * reflecting connection status, always-on setting and whether the vpn is insecure. |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 108 | * |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 109 | * State is not shown for {@code STATE_NONE}. |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 110 | */ |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 111 | protected void updateSummary() { |
Victor Chang | 14c2ac4 | 2016-03-15 18:48:08 +0000 | [diff] [blame] | 112 | final Resources res = getContext().getResources(); |
| 113 | final String[] states = res.getStringArray(R.array.vpn_states); |
Robin Lee | e06d757 | 2016-04-19 12:29:02 +0100 | [diff] [blame] | 114 | String summary = (mState == STATE_NONE ? "" : states[mState]); |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 115 | if (mIsInsecureVpn) { |
| 116 | final String insecureString = res.getString(R.string.vpn_insecure_summary); |
Jeremy Goldman | d62b2c2 | 2021-04-07 16:53:02 +0800 | [diff] [blame] | 117 | summary = TextUtils.isEmpty(summary) ? insecureString : summary + " / " |
| 118 | + insecureString; |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 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); |
Jeremy Goldman | d62b2c2 | 2021-04-07 16:53:02 +0800 | [diff] [blame] | 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; |
Jeremy Goldman | cf8ac14 | 2021-05-04 16:32:54 +0800 | [diff] [blame] | 129 | setSummary(summary); |
Jeremy Goldman | ab8eeca | 2021-03-11 13:29:46 +0800 | [diff] [blame] | 130 | } else { |
| 131 | setSummary(summary); |
| 132 | } |
Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 133 | } |
| 134 | } |