blob: 291b6d22d354ddd8bd0e57e2e7c532cbd01be32f [file] [log] [blame]
Robin Lee2bd92d52015-04-09 17:13:08 +01001/*
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
17package com.android.settings.vpn2;
18
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080019import static android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
20
Robin Lee2bd92d52015-04-09 17:13:08 +010021import android.content.Context;
Victor Chang14c2ac42016-03-15 18:48:08 +000022import android.content.res.Resources;
23import android.os.UserHandle;
24import android.os.UserManager;
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080025import android.text.SpannableString;
Victor Chang14c2ac42016-03-15 18:48:08 +000026import android.text.TextUtils;
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080027import android.text.style.ForegroundColorSpan;
Robin Lee2bd92d52015-04-09 17:13:08 +010028import android.util.AttributeSet;
Robin Lee2bd92d52015-04-09 17:13:08 +010029
30import com.android.settings.R;
Fan Zhangc7162cd2018-06-18 15:21:41 -070031import com.android.settings.widget.GearPreference;
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080032import com.android.settingslib.Utils;
Robin Lee2bd92d52015-04-09 17:13:08 +010033
34/**
Victor Chang14c2ac42016-03-15 18:48:08 +000035 * This class sets appropriate enabled state and user admin message when userId is set
Robin Lee2bd92d52015-04-09 17:13:08 +010036 */
Victor Chang14c2ac42016-03-15 18:48:08 +000037public abstract class ManageablePreference extends GearPreference {
Robin Lee2bd92d52015-04-09 17:13:08 +010038
Victor Chang14c2ac42016-03-15 18:48:08 +000039 public static int STATE_NONE = -1;
40
41 boolean mIsAlwaysOn = false;
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080042 boolean mIsInsecureVpn = false;
Robin Leee06d7572016-04-19 12:29:02 +010043 int mState = STATE_NONE;
Victor Chang14c2ac42016-03-15 18:48:08 +000044 int mUserId;
45
46 public ManageablePreference(Context context, AttributeSet attrs) {
Bonian Chen61c45062023-01-09 08:10:11 +000047 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 Lee2bd92d52015-04-09 17:13:08 +010053 setPersistent(false);
54 setOrder(0);
Victor Chang14c2ac42016-03-15 18:48:08 +000055 setUserId(UserHandle.myUserId());
Robin Lee2bd92d52015-04-09 17:13:08 +010056 }
57
Victor Chang14c2ac42016-03-15 18:48:08 +000058 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 Goldmanab8eeca2021-03-11 13:29:46 +080071 public boolean isInsecureVpn() {
72 return mIsInsecureVpn;
73 }
74
Robin Leee06d7572016-04-19 12:29:02 +010075 public int getState() {
76 return mState;
77 }
78
79 public void setState(int state) {
Robin Leeb166ea22016-04-21 12:56:21 +010080 if (mState != state) {
81 mState = state;
82 updateSummary();
83 notifyHierarchyChanged();
84 }
Robin Leee06d7572016-04-19 12:29:02 +010085 }
86
Victor Chang14c2ac42016-03-15 18:48:08 +000087 public void setAlwaysOn(boolean isEnabled) {
Robin Leeb166ea22016-04-21 12:56:21 +010088 if (mIsAlwaysOn != isEnabled) {
89 mIsAlwaysOn = isEnabled;
90 updateSummary();
91 }
Victor Chang14c2ac42016-03-15 18:48:08 +000092 }
93
94 /**
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +080095 * 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 Leee06d7572016-04-19 12:29:02 +0100106 * Update the preference summary string (see {@see Preference#setSummary}) with a string
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +0800107 * reflecting connection status, always-on setting and whether the vpn is insecure.
Victor Chang14c2ac42016-03-15 18:48:08 +0000108 *
Robin Leee06d7572016-04-19 12:29:02 +0100109 * State is not shown for {@code STATE_NONE}.
Victor Chang14c2ac42016-03-15 18:48:08 +0000110 */
Robin Leee06d7572016-04-19 12:29:02 +0100111 protected void updateSummary() {
Victor Chang14c2ac42016-03-15 18:48:08 +0000112 final Resources res = getContext().getResources();
113 final String[] states = res.getStringArray(R.array.vpn_states);
Robin Leee06d7572016-04-19 12:29:02 +0100114 String summary = (mState == STATE_NONE ? "" : states[mState]);
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +0800115 if (mIsInsecureVpn) {
116 final String insecureString = res.getString(R.string.vpn_insecure_summary);
Jeremy Goldmand62b2c22021-04-07 16:53:02 +0800117 summary = TextUtils.isEmpty(summary) ? insecureString : summary + " / "
118 + insecureString;
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +0800119
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 Goldmand62b2c22021-04-07 16:53:02 +0800125 } 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 Goldmancf8ac142021-05-04 16:32:54 +0800129 setSummary(summary);
Jeremy Goldmanab8eeca2021-03-11 13:29:46 +0800130 } else {
131 setSummary(summary);
132 }
Robin Lee2bd92d52015-04-09 17:13:08 +0100133 }
134}