1 /* 2 * Copyright (C) 2017 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.applications.assist; 18 19 import android.content.ContentResolver; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.provider.Settings; 23 import android.support.annotation.MainThread; 24 25 import com.android.settingslib.utils.ThreadUtils; 26 27 import java.util.List; 28 29 public abstract class AssistSettingObserver extends ContentObserver { 30 31 private final Uri ASSIST_URI = 32 Settings.Secure.getUriFor(Settings.Secure.ASSISTANT); 33 AssistSettingObserver()34 public AssistSettingObserver() { 35 super(null /* handler */); 36 } 37 register(ContentResolver cr, boolean register)38 public void register(ContentResolver cr, boolean register) { 39 if (register) { 40 cr.registerContentObserver(ASSIST_URI, false, this); 41 final List<Uri> settingUri = getSettingUris(); 42 if (settingUri != null) { 43 for (Uri uri : settingUri) { 44 cr.registerContentObserver(uri, false, this); 45 } 46 } 47 } else { 48 cr.unregisterContentObserver(this); 49 } 50 } 51 52 @Override onChange(boolean selfChange, Uri uri)53 public void onChange(boolean selfChange, Uri uri) { 54 super.onChange(selfChange, uri); 55 boolean shouldUpdatePreference = false; 56 final List<Uri> settingUri = getSettingUris(); 57 if (ASSIST_URI.equals(uri) || (settingUri != null && settingUri.contains(uri))) { 58 shouldUpdatePreference = true; 59 } 60 if (shouldUpdatePreference) { 61 ThreadUtils.postOnMainThread(() -> { 62 onSettingChange(); 63 }); 64 65 } 66 } 67 getSettingUris()68 protected abstract List<Uri> getSettingUris(); 69 70 @MainThread onSettingChange()71 public abstract void onSettingChange(); 72 } 73