AssistSettingObserver.java revision c108b97f1757b60d1dbd72e2c6afa29bb229f7cd
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
17package com.android.settings.applications.assist;
18
19import android.content.ContentResolver;
20import android.database.ContentObserver;
21import android.net.Uri;
22import android.os.Handler;
23import android.provider.Settings;
24
25import java.util.List;
26
27public abstract class AssistSettingObserver extends ContentObserver {
28
29    private final Uri ASSIST_URI =
30            Settings.Secure.getUriFor(Settings.Secure.ASSISTANT);
31
32    public AssistSettingObserver() {
33        super(null /* handler */);
34    }
35
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
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
62    protected abstract List<Uri> getSettingUris();
63
64    public abstract void onSettingChange();
65}
66