• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.settings;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 import android.preference.Preference.OnPreferenceChangeListener;
25 import android.preference.PreferenceScreen;
26 import android.preference.SwitchPreference;
27 import android.provider.Settings;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.Switch;
32 
33 import com.android.internal.widget.LockPatternUtils;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settings.search.Indexable;
36 import com.android.settings.search.SearchIndexableRaw;
37 import com.android.settings.widget.SwitchBar;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 /**
43  * Screen pinning settings.
44  */
45 public class ScreenPinningSettings extends SettingsPreferenceFragment
46         implements SwitchBar.OnSwitchChangeListener, Indexable {
47 
48     private static final CharSequence KEY_USE_SCREEN_LOCK = "use_screen_lock";
49     private static final int CHANGE_LOCK_METHOD_REQUEST = 43;
50 
51     private SwitchBar mSwitchBar;
52     private SwitchPreference mUseScreenLock;
53     private LockPatternUtils mLockPatternUtils;
54 
55     @Override
onActivityCreated(Bundle savedInstanceState)56     public void onActivityCreated(Bundle savedInstanceState) {
57         super.onActivityCreated(savedInstanceState);
58 
59         final SettingsActivity activity = (SettingsActivity) getActivity();
60         mLockPatternUtils = new LockPatternUtils(activity);
61 
62         View emptyView = LayoutInflater.from(activity)
63                 .inflate(R.layout.screen_pinning_instructions, null);
64         ((ViewGroup) getListView().getParent()).addView(emptyView);
65         getListView().setEmptyView(emptyView);
66 
67         mSwitchBar = activity.getSwitchBar();
68         mSwitchBar.addOnSwitchChangeListener(this);
69         mSwitchBar.show();
70         mSwitchBar.setChecked(isLockToAppEnabled(getActivity()));
71     }
72 
73     @Override
onDestroyView()74     public void onDestroyView() {
75         super.onDestroyView();
76 
77         mSwitchBar.removeOnSwitchChangeListener(this);
78         mSwitchBar.hide();
79     }
80 
isLockToAppEnabled(Context context)81     private static boolean isLockToAppEnabled(Context context) {
82         return Settings.System.getInt(context.getContentResolver(),
83                 Settings.System.LOCK_TO_APP_ENABLED, 0) != 0;
84     }
85 
setLockToAppEnabled(boolean isEnabled)86     private void setLockToAppEnabled(boolean isEnabled) {
87         Settings.System.putInt(getContentResolver(), Settings.System.LOCK_TO_APP_ENABLED,
88                 isEnabled ? 1 : 0);
89     }
90 
isScreenLockUsed()91     private boolean isScreenLockUsed() {
92         int def = getCurrentSecurityTitle() != R.string.screen_pinning_unlock_none ? 1 : 0;
93         return Settings.Secure.getInt(getContentResolver(),
94                 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED, def) != 0;
95     }
96 
setScreenLockUsed(boolean isEnabled)97     private boolean setScreenLockUsed(boolean isEnabled) {
98         if (isEnabled) {
99             LockPatternUtils lockPatternUtils = new LockPatternUtils(getActivity());
100             if (lockPatternUtils.getKeyguardStoredPasswordQuality()
101                     == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
102                 Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
103                 chooseLockIntent.putExtra(
104                         ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
105                         DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
106                 startActivityForResult(chooseLockIntent, CHANGE_LOCK_METHOD_REQUEST);
107                 return false;
108             }
109         }
110         Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCK_TO_APP_EXIT_LOCKED,
111                 isEnabled ? 1 : 0);
112         return true;
113     }
114 
115     @Override
onActivityResult(int requestCode, int resultCode, Intent data)116     public void onActivityResult(int requestCode, int resultCode, Intent data) {
117         super.onActivityResult(requestCode, resultCode, data);
118         if (requestCode == CHANGE_LOCK_METHOD_REQUEST) {
119             LockPatternUtils lockPatternUtils = new LockPatternUtils(getActivity());
120             boolean validPassQuality = lockPatternUtils.getKeyguardStoredPasswordQuality()
121                     != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
122             setScreenLockUsed(validPassQuality);
123             // Make sure the screen updates.
124             mUseScreenLock.setChecked(validPassQuality);
125         }
126     }
127 
getCurrentSecurityTitle()128     private int getCurrentSecurityTitle() {
129         int quality = mLockPatternUtils.getKeyguardStoredPasswordQuality();
130         switch (quality) {
131             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
132             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
133                 return R.string.screen_pinning_unlock_pin;
134             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
135             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
136             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
137                 return R.string.screen_pinning_unlock_password;
138             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
139                 if (mLockPatternUtils.isLockPatternEnabled()) {
140                     return R.string.screen_pinning_unlock_pattern;
141                 }
142         }
143         return R.string.screen_pinning_unlock_none;
144     }
145 
146     /**
147      * Listens to the state change of the lock-to-app master switch.
148      */
149     @Override
onSwitchChanged(Switch switchView, boolean isChecked)150     public void onSwitchChanged(Switch switchView, boolean isChecked) {
151         setLockToAppEnabled(isChecked);
152         updateDisplay();
153     }
154 
updateDisplay()155     public void updateDisplay() {
156         PreferenceScreen root = getPreferenceScreen();
157         if (root != null) {
158             root.removeAll();
159         }
160         if (isLockToAppEnabled(getActivity())) {
161             addPreferencesFromResource(R.xml.screen_pinning_settings);
162             root = getPreferenceScreen();
163 
164             mUseScreenLock = (SwitchPreference) root.findPreference(KEY_USE_SCREEN_LOCK);
165             mUseScreenLock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
166                 @Override
167                 public boolean onPreferenceChange(Preference preference, Object newValue) {
168                     return setScreenLockUsed((boolean) newValue);
169                 }
170             });
171             mUseScreenLock.setChecked(isScreenLockUsed());
172             mUseScreenLock.setTitle(getCurrentSecurityTitle());
173         }
174     }
175 
176     /**
177      * For search
178      */
179     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
180         new BaseSearchIndexProvider() {
181             @Override
182             public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
183                 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
184 
185                 final Resources res = context.getResources();
186 
187                 // Add fragment title
188                 SearchIndexableRaw data = new SearchIndexableRaw(context);
189                 data.title = res.getString(R.string.screen_pinning_title);
190                 data.screenTitle = res.getString(R.string.screen_pinning_title);
191                 result.add(data);
192 
193                 if (isLockToAppEnabled(context)) {
194                     // Screen lock option
195                     data = new SearchIndexableRaw(context);
196                     data.title = res.getString(R.string.screen_pinning_unlock_none);
197                     data.screenTitle = res.getString(R.string.screen_pinning_title);
198                     result.add(data);
199                 } else {
200                     // Screen pinning description.
201                     data = new SearchIndexableRaw(context);
202                     data.title = res.getString(R.string.screen_pinning_description);
203                     data.screenTitle = res.getString(R.string.screen_pinning_title);
204                     result.add(data);
205                 }
206 
207                 return result;
208             }
209         };
210 }
211