1 /* 2 * Copyright (C) 2011 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.inputmethod.latin.spellcheck; 18 19 import android.Manifest; 20 import android.content.SharedPreferences; 21 import android.os.Bundle; 22 import android.preference.PreferenceScreen; 23 import android.preference.SwitchPreference; 24 import android.text.TextUtils; 25 26 import com.android.inputmethod.latin.R; 27 import com.android.inputmethod.latin.permissions.PermissionsManager; 28 import com.android.inputmethod.latin.permissions.PermissionsUtil; 29 import com.android.inputmethod.latin.settings.SubScreenFragment; 30 import com.android.inputmethod.latin.settings.TwoStatePreferenceHelper; 31 import com.android.inputmethod.latin.utils.ApplicationUtils; 32 33 import static com.android.inputmethod.latin.permissions.PermissionsManager.get; 34 35 /** 36 * Preference screen. 37 */ 38 public final class SpellCheckerSettingsFragment extends SubScreenFragment 39 implements SharedPreferences.OnSharedPreferenceChangeListener, 40 PermissionsManager.PermissionsResultCallback { 41 42 private SwitchPreference mLookupContactsPreference; 43 44 @Override onActivityCreated(final Bundle savedInstanceState)45 public void onActivityCreated(final Bundle savedInstanceState) { 46 super.onActivityCreated(savedInstanceState); 47 addPreferencesFromResource(R.xml.spell_checker_settings); 48 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 49 preferenceScreen.setTitle(ApplicationUtils.getActivityTitleResId( 50 getActivity(), SpellCheckerSettingsActivity.class)); 51 TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences(preferenceScreen); 52 53 mLookupContactsPreference = (SwitchPreference) findPreference( 54 AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY); 55 turnOffLookupContactsIfNoPermission(); 56 } 57 58 @Override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)59 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 60 if (!TextUtils.equals(key, AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY)) { 61 return; 62 } 63 64 if (!sharedPreferences.getBoolean(key, false)) { 65 // don't care if the preference is turned off. 66 return; 67 } 68 69 // Check for permissions. 70 if (PermissionsUtil.checkAllPermissionsGranted( 71 getActivity() /* context */, Manifest.permission.READ_CONTACTS)) { 72 return; // all permissions granted, no need to request permissions. 73 } 74 75 get(getActivity() /* context */).requestPermissions(this /* PermissionsResultCallback */, 76 getActivity() /* activity */, Manifest.permission.READ_CONTACTS); 77 } 78 79 @Override onRequestPermissionsResult(boolean allGranted)80 public void onRequestPermissionsResult(boolean allGranted) { 81 turnOffLookupContactsIfNoPermission(); 82 } 83 turnOffLookupContactsIfNoPermission()84 private void turnOffLookupContactsIfNoPermission() { 85 if (!PermissionsUtil.checkAllPermissionsGranted( 86 getActivity(), Manifest.permission.READ_CONTACTS)) { 87 mLookupContactsPreference.setChecked(false); 88 } 89 } 90 } 91