1 /* 2 * Copyright (C) 2021 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.systemui.accessibility; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.UserHandle; 25 import android.provider.Settings; 26 27 import androidx.annotation.NonNull; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.systemui.settings.UserTracker; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Objects; 35 36 /** 37 * Provides basic methods for adding, removing arbitrary listeners and inquiry given {@code 38 * secureSettingsKey} value; it must comes from {@link Settings.Secure}. 39 * 40 * This abstract class is intended to be subclassed and specialized to maintain 41 * a registry of listeners of specific types and dispatch changes to them. 42 * 43 * @param <T> The listener type 44 */ 45 public abstract class SecureSettingsContentObserver<T> { 46 47 private final ContentResolver mContentResolver; 48 private final UserTracker mUserTracker; 49 @VisibleForTesting 50 final ContentObserver mContentObserver; 51 52 private final String mKey; 53 54 @VisibleForTesting 55 final List<T> mListeners = new ArrayList<>(); 56 SecureSettingsContentObserver(Context context, UserTracker userTracker, String secureSettingsKey)57 protected SecureSettingsContentObserver(Context context, UserTracker userTracker, 58 String secureSettingsKey) { 59 mKey = secureSettingsKey; 60 mContentResolver = context.getContentResolver(); 61 mUserTracker = userTracker; 62 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 63 @Override 64 public void onChange(boolean selfChange) { 65 updateValueChanged(); 66 } 67 }; 68 } 69 70 /** 71 * Registers a listener to receive updates from given settings key {@code secureSettingsKey}. 72 * 73 * @param listener A listener to be added to receive the changes 74 */ addListener(@onNull T listener)75 public void addListener(@NonNull T listener) { 76 Objects.requireNonNull(listener, "listener must be non-null"); 77 78 if (!mListeners.contains(listener)) { 79 mListeners.add(listener); 80 } 81 82 if (mListeners.size() == 1) { 83 mContentResolver.registerContentObserver( 84 Settings.Secure.getUriFor(mKey), /* notifyForDescendants= */ 85 false, mContentObserver, UserHandle.USER_ALL); 86 } 87 } 88 89 /** 90 * Unregisters a listener previously registered with {@link #addListener(T listener)}. 91 * 92 * @param listener A listener to be removed from receiving the changes 93 */ removeListener(@onNull T listener)94 public void removeListener(@NonNull T listener) { 95 Objects.requireNonNull(listener, "listener must be non-null"); 96 97 mListeners.remove(listener); 98 99 if (mListeners.isEmpty()) { 100 mContentResolver.unregisterContentObserver(mContentObserver); 101 } 102 } 103 104 /** 105 * Gets the value from the current user's secure settings. 106 * 107 * See {@link Settings.Secure}. 108 */ getSettingsValue()109 public final String getSettingsValue() { 110 return Settings.Secure.getStringForUser(mContentResolver, mKey, mUserTracker.getUserId()); 111 } 112 updateValueChanged()113 private void updateValueChanged() { 114 final String value = getSettingsValue(); 115 final int listenerSize = mListeners.size(); 116 for (int i = 0; i < listenerSize; i++) { 117 onValueChanged(mListeners.get(i), value); 118 } 119 } 120 121 /** 122 * Called when the registered value from {@code secureSettingsKey} changes. 123 * 124 * @param listener A listener could be used to receive the updates 125 * @param value Content changed value from {@code secureSettingsKey} 126 */ onValueChanged(T listener, String value)127 abstract void onValueChanged(T listener, String value); 128 } 129