Jason Monk | 315070d | 2014-06-19 10:33:41 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.admin.DevicePolicyManager; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.res.Resources; |
| 23 | import android.graphics.BitmapFactory; |
| 24 | import android.os.Bundle; |
| 25 | import android.preference.Preference; |
| 26 | import android.preference.Preference.OnPreferenceChangeListener; |
| 27 | import android.preference.PreferenceScreen; |
| 28 | import android.preference.SwitchPreference; |
| 29 | import android.provider.Settings; |
| 30 | import android.provider.Settings.SettingNotFoundException; |
| 31 | import android.text.Spannable; |
| 32 | import android.text.SpannableString; |
| 33 | import android.text.style.DynamicDrawableSpan; |
| 34 | import android.text.style.ImageSpan; |
| 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
| 38 | import android.widget.Switch; |
| 39 | import android.widget.TextView; |
| 40 | |
| 41 | import com.android.internal.widget.LockPatternUtils; |
| 42 | import com.android.settings.search.BaseSearchIndexProvider; |
| 43 | import com.android.settings.search.Indexable; |
| 44 | import com.android.settings.search.SearchIndexableRaw; |
| 45 | import com.android.settings.widget.SwitchBar; |
| 46 | |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.List; |
| 49 | |
| 50 | /** |
| 51 | * Location access settings. |
| 52 | */ |
| 53 | public class LockToAppSettings extends SettingsPreferenceFragment |
| 54 | implements SwitchBar.OnSwitchChangeListener, Indexable { |
| 55 | private static final String TAG = "LockToAppSettings"; |
| 56 | |
| 57 | private static final CharSequence KEY_USE_SCREEN_LOCK = "use_screen_lock"; |
| 58 | private static final int CHANGE_LOCK_METHOD_REQUEST = 123; |
| 59 | |
| 60 | private SwitchBar mSwitchBar; |
| 61 | private SwitchPreference mUseScreenLock; |
| 62 | |
| 63 | @Override |
| 64 | public void onActivityCreated(Bundle savedInstanceState) { |
| 65 | super.onActivityCreated(savedInstanceState); |
| 66 | |
| 67 | final SettingsActivity activity = (SettingsActivity) getActivity(); |
| 68 | |
| 69 | mSwitchBar = activity.getSwitchBar(); |
| 70 | mSwitchBar.addOnSwitchChangeListener(this); |
| 71 | mSwitchBar.show(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void onDestroyView() { |
| 76 | super.onDestroyView(); |
| 77 | |
| 78 | mSwitchBar.removeOnSwitchChangeListener(this); |
| 79 | mSwitchBar.hide(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onStop() { |
| 84 | super.onStop(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onResume() { |
| 89 | super.onResume(); |
| 90 | if (isLockToAppEnabled()) { |
| 91 | mSwitchBar.setChecked(true); |
| 92 | createPreferenceHierarchy(); |
| 93 | } else { |
| 94 | mSwitchBar.setChecked(false); |
| 95 | createSetupInstructions(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 101 | Bundle savedInstanceState) { |
| 102 | return inflater.inflate(R.layout.lock_to_app_instructions, null); |
| 103 | } |
| 104 | |
| 105 | private boolean isLockToAppEnabled() { |
| 106 | try { |
| 107 | return Settings.System.getInt(getContentResolver(), Settings.System.LOCK_TO_APP_ENABLED) |
| 108 | != 0; |
| 109 | } catch (SettingNotFoundException e) { |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private boolean isLockScreenEnabled() { |
| 115 | try { |
| 116 | return Settings.System.getInt(getContentResolver(), |
| 117 | Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0; |
| 118 | } catch (SettingNotFoundException e) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private void setLockToAppEnabled(boolean isEnabled) { |
| 124 | Settings.System.putInt(getContentResolver(), Settings.System.LOCK_TO_APP_ENABLED, |
| 125 | isEnabled ? 1 : 0); |
| 126 | } |
| 127 | |
| 128 | private void setUseLockScreen(boolean useLockScreen) { |
| 129 | if (useLockScreen) { |
| 130 | LockPatternUtils lockPatternUtils = new LockPatternUtils(getActivity()); |
| 131 | if (lockPatternUtils.getKeyguardStoredPasswordQuality() |
| 132 | == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 133 | Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); |
| 134 | chooseLockIntent.putExtra( |
| 135 | ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY, |
| 136 | DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
| 137 | startActivityForResult(chooseLockIntent, CHANGE_LOCK_METHOD_REQUEST); |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | setUseLockScreenSetting(useLockScreen); |
| 142 | } |
| 143 | |
| 144 | private void setUseLockScreenSetting(boolean useLockScreen) { |
| 145 | mUseScreenLock.setChecked(useLockScreen); |
| 146 | Settings.System.putInt(getContentResolver(), Settings.System.LOCK_TO_APP_EXIT_LOCKED, |
| 147 | useLockScreen ? 1 : 0); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 152 | super.onActivityResult(requestCode, resultCode, data); |
| 153 | if (requestCode == CHANGE_LOCK_METHOD_REQUEST) { |
| 154 | LockPatternUtils lockPatternUtils = new LockPatternUtils(getActivity()); |
| 155 | setUseLockScreenSetting(lockPatternUtils.getKeyguardStoredPasswordQuality() |
| 156 | != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private PreferenceScreen createPreferenceHierarchy() { |
| 161 | PreferenceScreen root = getPreferenceScreen(); |
| 162 | if (root != null) { |
| 163 | root.removeAll(); |
| 164 | } |
| 165 | getView().findViewById(R.id.instructions_area).setVisibility(View.INVISIBLE); |
| 166 | addPreferencesFromResource(R.xml.lock_to_app_settings); |
| 167 | root = getPreferenceScreen(); |
| 168 | |
| 169 | mUseScreenLock = (SwitchPreference) root.findPreference(KEY_USE_SCREEN_LOCK); |
| 170 | mUseScreenLock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { |
| 171 | @Override |
| 172 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 173 | setUseLockScreen((boolean) newValue); |
| 174 | return true; |
| 175 | } |
| 176 | }); |
| 177 | mUseScreenLock.setChecked(isLockScreenEnabled()); |
| 178 | |
| 179 | return root; |
| 180 | } |
| 181 | |
| 182 | private void createSetupInstructions() { |
| 183 | PreferenceScreen root = getPreferenceScreen(); |
| 184 | if (root != null) { |
| 185 | root.removeAll(); |
| 186 | } |
| 187 | final View view = getView(); |
| 188 | view.findViewById(R.id.instructions_area).setVisibility(View.VISIBLE); |
| 189 | |
| 190 | final Resources r = Resources.getSystem(); |
| 191 | TextView tv = (TextView) view.findViewById(R.id.lock_to_app_start_step3); |
| 192 | ImageSpan imageSpan = new ImageSpan(getActivity(), |
| 193 | BitmapFactory.decodeResource(r, com.android.internal.R.drawable.ic_recent), |
| 194 | DynamicDrawableSpan.ALIGN_BOTTOM); |
| 195 | |
| 196 | String descriptionString = getResources().getString(R.string.lock_to_app_start_step3); |
| 197 | SpannableString description = |
| 198 | new SpannableString(descriptionString.replace('$', ' ')); |
| 199 | int index = descriptionString.indexOf('$'); |
| 200 | if (index >= 0) { |
| 201 | description.setSpan(imageSpan, index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 202 | } |
| 203 | |
| 204 | // Make icon fit. |
| 205 | float width = imageSpan.getDrawable().getIntrinsicWidth(); |
| 206 | float height = imageSpan.getDrawable().getIntrinsicHeight(); |
| 207 | int lineHeight = tv.getLineHeight(); |
| 208 | imageSpan.getDrawable().setBounds(0, 0, (int) (lineHeight * width / height), lineHeight); |
| 209 | |
| 210 | tv.setText(description); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Listens to the state change of the lock-to-app master switch. |
| 215 | */ |
| 216 | @Override |
| 217 | public void onSwitchChanged(Switch switchView, boolean isChecked) { |
| 218 | setLockToAppEnabled(isChecked); |
| 219 | if (isChecked) { |
| 220 | createPreferenceHierarchy(); |
| 221 | } else { |
| 222 | createSetupInstructions(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * For search |
| 228 | */ |
| 229 | public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
| 230 | new BaseSearchIndexProvider() { |
| 231 | @Override |
| 232 | public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { |
| 233 | final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); |
| 234 | |
| 235 | final Resources res = context.getResources(); |
| 236 | |
| 237 | // Add fragment title |
| 238 | SearchIndexableRaw data = new SearchIndexableRaw(context); |
| 239 | data.title = res.getString(R.string.lock_to_app_title); |
| 240 | data.screenTitle = res.getString(R.string.lock_to_app_title); |
| 241 | result.add(data); |
| 242 | |
| 243 | // Lock-to-app description. |
| 244 | data = new SearchIndexableRaw(context); |
| 245 | data.title = res.getString(R.string.lock_to_app_description); |
| 246 | data.screenTitle = res.getString(R.string.lock_to_app_title); |
| 247 | result.add(data); |
| 248 | |
| 249 | // Lock-to-app use screen lock. |
| 250 | data = new SearchIndexableRaw(context); |
| 251 | data.title = res.getString(R.string.lock_to_app_screen_lock); |
| 252 | data.screenTitle = res.getString(R.string.lock_to_app_title); |
| 253 | result.add(data); |
| 254 | |
| 255 | return result; |
| 256 | } |
| 257 | }; |
| 258 | } |