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