1 /* 2 * Copyright 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.inputmethod; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE; 20 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; 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.UserHandle; 28 29 import androidx.annotation.NonNull; 30 import androidx.appcompat.app.AlertDialog; 31 import androidx.fragment.app.Fragment; 32 import androidx.fragment.app.FragmentManager; 33 import androidx.lifecycle.LifecycleObserver; 34 import androidx.lifecycle.OnLifecycleEvent; 35 import androidx.preference.Preference; 36 37 import com.android.settings.core.TogglePreferenceController; 38 import com.android.settings.keyboard.Flags; 39 import com.android.settings.overlay.FeatureFactory; 40 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 41 42 /** 43 * Abstract class for toggle controllers of Keyboard input setting related function. 44 */ 45 public abstract class InputSettingPreferenceController extends TogglePreferenceController implements 46 LifecycleObserver { 47 private final ContentResolver mContentResolver; 48 protected final MetricsFeatureProvider mMetricsFeatureProvider; 49 protected FragmentManager mFragmentManager; 50 51 private final ContentObserver mContentObserver = new ContentObserver(new Handler(true)) { 52 @Override 53 public void onChange(boolean selfChange, Uri uri) { 54 if (getSettingUri().equals(uri)) { 55 onInputSettingUpdated(); 56 } 57 } 58 }; 59 protected AlertDialog mAlertDialog; 60 onInputSettingUpdated()61 protected abstract void onInputSettingUpdated(); 62 getSettingUri()63 protected abstract Uri getSettingUri(); 64 updateInputSettingKeysValue(int thresholdTimeMillis)65 protected void updateInputSettingKeysValue(int thresholdTimeMillis) { 66 } 67 InputSettingPreferenceController(@onNull Context context, @NonNull String preferenceKey)68 public InputSettingPreferenceController(@NonNull Context context, 69 @NonNull String preferenceKey) { 70 super(context, preferenceKey); 71 mContentResolver = context.getContentResolver(); 72 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 73 } 74 setFragment(Fragment fragment)75 public void setFragment(Fragment fragment) { 76 mFragmentManager = fragment.getParentFragmentManager(); 77 } 78 79 @Override updateState(@onNull Preference preference)80 public void updateState(@NonNull Preference preference) { 81 super.updateState(preference); 82 refreshSummary(preference); 83 } 84 85 @Override getAvailabilityStatus()86 public int getAvailabilityStatus() { 87 return Flags.keyboardAndTouchpadA11yNewPageEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 88 } 89 90 @Override getSliceHighlightMenuRes()91 public int getSliceHighlightMenuRes() { 92 return 0; 93 } 94 95 /** Invoked when the panel is resumed. */ 96 @OnLifecycleEvent(ON_RESUME) onResume()97 public void onResume() { 98 registerSettingsObserver(); 99 } 100 101 /** Invoked when the panel is paused. */ 102 @OnLifecycleEvent(ON_PAUSE) onPause()103 public void onPause() { 104 unregisterSettingsObserver(); 105 } 106 registerSettingsObserver()107 private void registerSettingsObserver() { 108 unregisterSettingsObserver(); 109 mContentResolver.registerContentObserver( 110 getSettingUri(), 111 false, 112 mContentObserver, 113 UserHandle.myUserId()); 114 onInputSettingUpdated(); 115 } 116 unregisterSettingsObserver()117 private void unregisterSettingsObserver() { 118 mContentResolver.unregisterContentObserver(mContentObserver); 119 } 120 } 121