1 /* 2 * Copyright (C) 2024 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.content.res.Resources; 24 import android.database.ContentObserver; 25 import android.net.Uri; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.provider.Settings; 29 30 import androidx.annotation.Nullable; 31 import androidx.annotation.StringRes; 32 import androidx.annotation.VisibleForTesting; 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceScreen; 35 import androidx.preference.TwoStatePreference; 36 37 import com.android.settings.R; 38 import com.android.settings.accessibility.MagnificationCapabilities.MagnificationMode; 39 import com.android.settingslib.core.lifecycle.LifecycleObserver; 40 import com.android.settingslib.core.lifecycle.events.OnPause; 41 import com.android.settingslib.core.lifecycle.events.OnResume; 42 43 public class MagnificationOneFingerPanningPreferenceController extends 44 MagnificationTogglePreferenceController implements LifecycleObserver, OnResume, OnPause { 45 static final String PREF_KEY = Settings.Secure.ACCESSIBILITY_SINGLE_FINGER_PANNING_ENABLED; 46 47 private TwoStatePreference mSwitchPreference; 48 49 @VisibleForTesting 50 final boolean mDefaultValue; 51 52 @VisibleForTesting 53 final ContentObserver mContentObserver = new ContentObserver( 54 new Handler(Looper.getMainLooper())) { 55 @Override 56 public void onChange(boolean selfChange, @Nullable Uri uri) { 57 updateState(mSwitchPreference); 58 } 59 }; 60 MagnificationOneFingerPanningPreferenceController(Context context)61 public MagnificationOneFingerPanningPreferenceController(Context context) { 62 super(context, PREF_KEY); 63 boolean defaultValue; 64 try { 65 defaultValue = context.getResources().getBoolean( 66 com.android.internal.R.bool.config_enable_a11y_magnification_single_panning); 67 } catch (Resources.NotFoundException e) { 68 defaultValue = false; 69 } 70 mDefaultValue = defaultValue; 71 } 72 73 @Override onResume()74 public void onResume() { 75 MagnificationCapabilities.registerObserver(mContext, mContentObserver); 76 } 77 78 @Override onPause()79 public void onPause() { 80 MagnificationCapabilities.unregisterObserver(mContext, mContentObserver); 81 } 82 83 @Override getAvailabilityStatus()84 public int getAvailabilityStatus() { 85 return isInSetupWizard() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE; 86 } 87 88 @Override isChecked()89 public boolean isChecked() { 90 return Settings.Secure.getInt( 91 mContext.getContentResolver(), 92 PREF_KEY, 93 (mDefaultValue) ? ON : OFF) == ON; 94 } 95 96 @Override setChecked(boolean isChecked)97 public boolean setChecked(boolean isChecked) { 98 var toReturn = Settings.Secure.putInt(mContext.getContentResolver(), 99 PREF_KEY, 100 (isChecked ? ON : OFF)); 101 refreshSummary(mSwitchPreference); 102 return toReturn; 103 } 104 105 @Override getSummary()106 public CharSequence getSummary() { 107 @StringRes int resId = mSwitchPreference.isEnabled() 108 ? R.string.accessibility_magnification_one_finger_panning_summary 109 : R.string.accessibility_magnification_one_finger_panning_summary_unavailable; 110 return mContext.getString(resId); 111 } 112 113 @Override getSliceHighlightMenuRes()114 public int getSliceHighlightMenuRes() { 115 return R.string.menu_key_accessibility; 116 } 117 118 @Override displayPreference(PreferenceScreen screen)119 public void displayPreference(PreferenceScreen screen) { 120 super.displayPreference(screen); 121 mSwitchPreference = screen.findPreference(getPreferenceKey()); 122 updateState(mSwitchPreference); 123 } 124 125 @Override updateState(Preference preference)126 public void updateState(Preference preference) { 127 super.updateState(preference); 128 129 if (preference == null) { 130 return; 131 } 132 @MagnificationMode int mode = 133 MagnificationCapabilities.getCapabilities(mContext); 134 preference.setEnabled( 135 mode == MagnificationMode.FULLSCREEN || mode == MagnificationMode.ALL); 136 refreshSummary(preference); 137 } 138 } 139