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