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