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.os.Handler; 23 import android.provider.Settings; 24 25 import java.util.List; 26 27 public abstract class AssistSettingObserver extends ContentObserver { 28 29 private final Uri ASSIST_URI = 30 Settings.Secure.getUriFor(Settings.Secure.ASSISTANT); 31 AssistSettingObserver()32 public AssistSettingObserver() { 33 super(null /* handler */); 34 } 35 register(ContentResolver cr, boolean register)36 public void register(ContentResolver cr, boolean register) { 37 if (register) { 38 cr.registerContentObserver(ASSIST_URI, false, this); 39 final List<Uri> settingUri = getSettingUris(); 40 if (settingUri != null) { 41 for (Uri uri : settingUri) 42 cr.registerContentObserver(uri, false, this); 43 } 44 } else { 45 cr.unregisterContentObserver(this); 46 } 47 } 48 49 @Override onChange(boolean selfChange, Uri uri)50 public void onChange(boolean selfChange, Uri uri) { 51 super.onChange(selfChange, uri); 52 boolean shouldUpdatePreference = false; 53 final List<Uri> settingUri = getSettingUris(); 54 if (ASSIST_URI.equals(uri) || (settingUri != null && settingUri.contains(uri))) { 55 shouldUpdatePreference = true; 56 } 57 if (shouldUpdatePreference) { 58 onSettingChange(); 59 } 60 } 61 getSettingUris()62 protected abstract List<Uri> getSettingUris(); 63 onSettingChange()64 public abstract void onSettingChange(); 65 } 66