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.internal.telephony; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.os.Handler; 24 import android.telephony.Rlog; 25 26 import java.util.HashMap; 27 import java.util.Map; 28 29 /** 30 * The class to describe settings observer 31 */ 32 public class SettingsObserver extends ContentObserver { 33 private final Map<Uri, Integer> mUriEventMap; 34 private final Context mContext; 35 private final Handler mHandler; 36 private static final String TAG = "SettingsObserver"; 37 SettingsObserver(Context context, Handler handler)38 public SettingsObserver(Context context, Handler handler) { 39 super(null); 40 mUriEventMap = new HashMap<>(); 41 mContext = context; 42 mHandler = handler; 43 } 44 45 /** 46 * Start observing a content. 47 * @param uri Content URI 48 * @param what The event to fire if the content changes 49 */ observe(Uri uri, int what)50 public void observe(Uri uri, int what) { 51 mUriEventMap.put(uri, what); 52 final ContentResolver resolver = mContext.getContentResolver(); 53 resolver.registerContentObserver(uri, false, this); 54 } 55 56 /** 57 * Stop observing a content. 58 */ unobserve()59 public void unobserve() { 60 final ContentResolver resolver = mContext.getContentResolver(); 61 resolver.unregisterContentObserver(this); 62 } 63 64 @Override onChange(boolean selfChange)65 public void onChange(boolean selfChange) { 66 Rlog.e(TAG, "Should never be reached."); 67 } 68 69 @Override onChange(boolean selfChange, Uri uri)70 public void onChange(boolean selfChange, Uri uri) { 71 final Integer what = mUriEventMap.get(uri); 72 if (what != null) { 73 mHandler.obtainMessage(what.intValue()).sendToTarget(); 74 } else { 75 Rlog.e(TAG, "No matching event to send for URI=" + uri); 76 } 77 } 78 } 79