1 /* 2 * Copyright (C) 2022 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.accessibility; 18 19 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.os.Looper; 27 import android.provider.Settings; 28 29 import androidx.annotation.Nullable; 30 import androidx.annotation.StringRes; 31 import androidx.annotation.VisibleForTesting; 32 import androidx.preference.Preference; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settings.R; 36 import com.android.settings.accessibility.MagnificationCapabilities.MagnificationMode; 37 import com.android.settingslib.core.lifecycle.LifecycleObserver; 38 import com.android.settingslib.core.lifecycle.events.OnPause; 39 import com.android.settingslib.core.lifecycle.events.OnResume; 40 41 /** 42 * Controller that accesses and switches the preference status of the magnification always on 43 * feature, where the magnifier will not deactivate on Activity transitions; it will only zoom out 44 * to 100%. 45 */ 46 public class MagnificationAlwaysOnPreferenceController extends 47 MagnificationTogglePreferenceController implements LifecycleObserver, OnResume, OnPause { 48 49 private static final String TAG = 50 MagnificationAlwaysOnPreferenceController.class.getSimpleName(); 51 static final String PREF_KEY = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED; 52 53 private Preference mPreference; 54 55 @VisibleForTesting 56 final ContentObserver mContentObserver = new ContentObserver( 57 new Handler(Looper.getMainLooper())) { 58 @Override 59 public void onChange(boolean selfChange, @Nullable Uri uri) { 60 updateState(mPreference); 61 } 62 }; 63 MagnificationAlwaysOnPreferenceController(Context context, String preferenceKey)64 public MagnificationAlwaysOnPreferenceController(Context context, String preferenceKey) { 65 super(context, preferenceKey); 66 } 67 68 @Override onResume()69 public void onResume() { 70 MagnificationCapabilities.registerObserver(mContext, mContentObserver); 71 } 72 73 @Override onPause()74 public void onPause() { 75 MagnificationCapabilities.unregisterObserver(mContext, mContentObserver); 76 } 77 78 @Override displayPreference(PreferenceScreen screen)79 public void displayPreference(PreferenceScreen screen) { 80 super.displayPreference(screen); 81 mPreference = screen.findPreference(getPreferenceKey()); 82 updateState(mPreference); 83 } 84 85 @Override getAvailabilityStatus()86 public int getAvailabilityStatus() { 87 return isInSetupWizard() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE; 88 } 89 90 @Override isChecked()91 public boolean isChecked() { 92 return Settings.Secure.getInt(mContext.getContentResolver(), 93 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED, ON) == ON; 94 } 95 96 @Override setChecked(boolean isChecked)97 public boolean setChecked(boolean isChecked) { 98 return Settings.Secure.putInt(mContext.getContentResolver(), 99 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_ALWAYS_ON_ENABLED, 100 (isChecked ? ON : OFF)); 101 } 102 103 @Override getSliceHighlightMenuRes()104 public int getSliceHighlightMenuRes() { 105 return R.string.menu_key_accessibility; 106 } 107 108 @Override getSummary()109 public CharSequence getSummary() { 110 @StringRes int resId = mPreference.isEnabled() 111 ? R.string.accessibility_screen_magnification_always_on_summary 112 : R.string.accessibility_screen_magnification_always_on_unavailable_summary; 113 return mContext.getString(resId); 114 } 115 116 @Override updateState(Preference preference)117 public void updateState(Preference preference) { 118 super.updateState(preference); 119 120 if (preference == null) { 121 return; 122 } 123 @MagnificationMode int mode = 124 MagnificationCapabilities.getCapabilities(mContext); 125 preference.setEnabled( 126 mode == MagnificationMode.FULLSCREEN || mode == MagnificationMode.ALL); 127 refreshSummary(preference); 128 } 129 } 130