1 /*
2  * Copyright (C) 2019 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.specialaccess.zenaccess;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.provider.Settings;
26 
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnStart;
29 import com.android.settingslib.core.lifecycle.events.OnStop;
30 
31 public class ZenAccessSettingObserverMixin extends ContentObserver implements LifecycleObserver,
32         OnStart, OnStop {
33 
34     public interface Listener {
onZenAccessPolicyChanged()35         void onZenAccessPolicyChanged();
36     }
37 
38     private final Context mContext;
39     private final Listener mListener;
40 
ZenAccessSettingObserverMixin(Context context, Listener listener)41     public ZenAccessSettingObserverMixin(Context context, Listener listener) {
42         super(new Handler(Looper.getMainLooper()));
43         mContext = context;
44         mListener = listener;
45     }
46 
47     @Override
onChange(boolean selfChange, Uri uri)48     public void onChange(boolean selfChange, Uri uri) {
49         if (mListener != null) {
50             mListener.onZenAccessPolicyChanged();
51         }
52     }
53 
54     @Override
onStart()55     public void onStart() {
56         if (!ZenAccessController.isSupported(mContext.getSystemService(ActivityManager.class))) {
57             return;
58         }
59         mContext.getContentResolver().registerContentObserver(
60                 Settings.Secure.getUriFor(
61                         Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES),
62                 false /* notifyForDescendants */,
63                 this /* observer */);
64         mContext.getContentResolver().registerContentObserver(
65                 Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS),
66                 false /* notifyForDescendants */,
67                 this /* observer */);
68     }
69 
70     @Override
onStop()71     public void onStop() {
72         if (!ZenAccessController.isSupported(mContext.getSystemService(ActivityManager.class))) {
73             return;
74         }
75         mContext.getContentResolver().unregisterContentObserver(this /* observer */);
76     }
77 }
78