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.launcher3.util; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.provider.Settings; 24 25 /** 26 * Utility class to listen for secure settings changes 27 */ 28 public class SecureSettingsObserver extends ContentObserver { 29 30 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ 31 public static final String NOTIFICATION_BADGING = "notification_badging"; 32 33 private final ContentResolver mResolver; 34 private final String mKeySetting; 35 private final int mDefaultValue; 36 private final OnChangeListener mOnChangeListener; 37 SecureSettingsObserver(ContentResolver resolver, OnChangeListener listener, String keySetting, int defaultValue)38 public SecureSettingsObserver(ContentResolver resolver, OnChangeListener listener, 39 String keySetting, int defaultValue) { 40 super(new Handler()); 41 42 mResolver = resolver; 43 mOnChangeListener = listener; 44 mKeySetting = keySetting; 45 mDefaultValue = defaultValue; 46 } 47 48 @Override onChange(boolean selfChange)49 public void onChange(boolean selfChange) { 50 mOnChangeListener.onSettingsChanged(getValue()); 51 } 52 getValue()53 public boolean getValue() { 54 return Settings.Secure.getInt(mResolver, mKeySetting, mDefaultValue) == 1; 55 } 56 register()57 public void register() { 58 mResolver.registerContentObserver(Settings.Secure.getUriFor(mKeySetting), false, this); 59 } 60 getResolver()61 public ContentResolver getResolver() { 62 return mResolver; 63 } 64 dispatchOnChange()65 public void dispatchOnChange() { 66 onChange(true); 67 } 68 unregister()69 public void unregister() { 70 mResolver.unregisterContentObserver(this); 71 } 72 73 public interface OnChangeListener { onSettingsChanged(boolean isEnabled)74 void onSettingsChanged(boolean isEnabled); 75 } 76 newNotificationSettingsObserver(Context context, OnChangeListener listener)77 public static SecureSettingsObserver newNotificationSettingsObserver(Context context, 78 OnChangeListener listener) { 79 return new SecureSettingsObserver( 80 context.getContentResolver(), listener, NOTIFICATION_BADGING, 1); 81 } 82 } 83