• 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 android.content.Context;
20 import android.content.res.Resources;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 
26 import com.android.settings.R;
27 import com.android.settings.widget.GearPreference;
28 
29 /**
30  * This class sets appropriate enabled state and user admin message when userId is set
31  */
32 public abstract class ManageablePreference extends GearPreference {
33 
34     public static int STATE_NONE = -1;
35 
36     boolean mIsAlwaysOn = false;
37     int mState = STATE_NONE;
38     int mUserId;
39 
ManageablePreference(Context context, AttributeSet attrs)40     public ManageablePreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         setPersistent(false);
43         setOrder(0);
44         setUserId(UserHandle.myUserId());
45     }
46 
getUserId()47     public int getUserId() {
48         return mUserId;
49     }
50 
setUserId(int userId)51     public void setUserId(int userId) {
52         mUserId = userId;
53         checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN, userId);
54     }
55 
isAlwaysOn()56     public boolean isAlwaysOn() {
57         return mIsAlwaysOn;
58     }
59 
getState()60     public int getState() {
61         return mState;
62     }
63 
setState(int state)64     public void setState(int state) {
65         if (mState != state) {
66             mState = state;
67             updateSummary();
68             notifyHierarchyChanged();
69         }
70     }
71 
setAlwaysOn(boolean isEnabled)72     public void setAlwaysOn(boolean isEnabled) {
73         if (mIsAlwaysOn != isEnabled) {
74             mIsAlwaysOn = isEnabled;
75             updateSummary();
76         }
77     }
78 
79     /**
80      * Update the preference summary string (see {@see Preference#setSummary}) with a string
81      * reflecting connection status and always-on setting.
82      *
83      * State is not shown for {@code STATE_NONE}.
84      */
updateSummary()85     protected void updateSummary() {
86         final Resources res = getContext().getResources();
87         final String[] states = res.getStringArray(R.array.vpn_states);
88         String summary = (mState == STATE_NONE ? "" : states[mState]);
89         if (mIsAlwaysOn) {
90             final String alwaysOnString = res.getString(R.string.vpn_always_on_summary_active);
91             summary = TextUtils.isEmpty(summary) ? alwaysOnString : res.getString(
92                     R.string.join_two_unrelated_items, summary, alwaysOnString);
93         }
94         setSummary(summary);
95     }
96 }
97