1 /* 2 * Copyright (C) 2025 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.ContentResolver; 23 import android.content.Context; 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.NonNull; 31 import androidx.annotation.Nullable; 32 import androidx.annotation.VisibleForTesting; 33 import androidx.lifecycle.DefaultLifecycleObserver; 34 import androidx.lifecycle.LifecycleOwner; 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceScreen; 37 38 import com.android.server.accessibility.Flags; 39 import com.android.settings.R; 40 import com.android.settings.core.TogglePreferenceController; 41 42 /** The controller to handle main switch to turn on or turn off accessibility autoclick. */ 43 public class ToggleAutoclickMainSwitchPreferenceController 44 extends TogglePreferenceController implements DefaultLifecycleObserver { 45 46 static final Uri ACCESSIBILITY_AUTOCLICK_ENABLED_URI = 47 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED); 48 private final ContentResolver mContentResolver; 49 private @Nullable Preference mPreference; 50 51 @VisibleForTesting 52 final ContentObserver mSettingsObserver = 53 new ContentObserver(new Handler(Looper.getMainLooper())) { 54 @Override 55 public void onChange(boolean selfChange, @Nullable Uri uri) { 56 if (mPreference == null || uri == null) { 57 return; 58 } 59 updateState(mPreference); 60 } 61 }; 62 ToggleAutoclickMainSwitchPreferenceController( @onNull Context context, @NonNull String preferenceKey)63 public ToggleAutoclickMainSwitchPreferenceController( 64 @NonNull Context context, @NonNull String preferenceKey) { 65 super(context, preferenceKey); 66 mContentResolver = context.getContentResolver(); 67 } 68 69 @Override getAvailabilityStatus()70 public int getAvailabilityStatus() { 71 return Flags.enableAutoclickIndicator() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 72 } 73 74 @Override displayPreference(@onNull PreferenceScreen screen)75 public void displayPreference(@NonNull PreferenceScreen screen) { 76 super.displayPreference(screen); 77 mPreference = screen.findPreference(getPreferenceKey()); 78 } 79 80 @Override isChecked()81 public boolean isChecked() { 82 return Settings.Secure.getInt(mContentResolver, 83 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF) == ON; 84 } 85 86 @Override setChecked(boolean isChecked)87 public boolean setChecked(boolean isChecked) { 88 Settings.Secure.putInt(mContentResolver, 89 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 90 isChecked ? ON : OFF); 91 92 return true; 93 } 94 95 @Override onStart(@onNull LifecycleOwner owner)96 public void onStart(@NonNull LifecycleOwner owner) { 97 mContentResolver.registerContentObserver( 98 ACCESSIBILITY_AUTOCLICK_ENABLED_URI, 99 /* notifyForDescendants= */ false, 100 mSettingsObserver); 101 } 102 103 @Override onStop(@onNull LifecycleOwner owner)104 public void onStop(@NonNull LifecycleOwner owner) { 105 mContentResolver.unregisterContentObserver(mSettingsObserver); 106 } 107 108 @Override getSliceHighlightMenuRes()109 public int getSliceHighlightMenuRes() { 110 return R.string.menu_key_system; 111 } 112 } 113