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 mContext.getContentResolver().registerContentObserver( 57 Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS), 58 false /* notifyForDescendants */, 59 this /* observer */); 60 } 61 62 @Override onStop()63 public void onStop() { 64 mContext.getContentResolver().unregisterContentObserver(this /* observer */); 65 } 66 } 67