1 /*
2  * Copyright (C) 2016 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 android.car.settings;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Handler;
23 import android.provider.Settings;
24 
25 import java.lang.ref.WeakReference;
26 
27 /**
28  * A content observer for garage mode settings.
29  * @hide
30  */
31 public abstract class GarageModeSettingsObserver extends ContentObserver {
32 
33     public static final Uri GARAGE_MODE_ENABLED_URI =
34             Settings.Global.getUriFor(CarSettings.Global.KEY_GARAGE_MODE_ENABLED);
35     public static final Uri GARAGE_MODE_WAKE_UP_TIME_URI =
36             Settings.Global.getUriFor(CarSettings.Global.KEY_GARAGE_MODE_WAKE_UP_TIME);
37     public static final Uri GARAGE_MODE_MAINTENANCE_WINDOW_URI =
38             Settings.Global.getUriFor(CarSettings.Global.KEY_GARAGE_MODE_MAINTENANCE_WINDOW);
39 
40     public static final Uri[] GARAGE_SETTING_URIS = {GARAGE_MODE_ENABLED_URI,
41             GARAGE_MODE_WAKE_UP_TIME_URI, GARAGE_MODE_MAINTENANCE_WINDOW_URI};
42 
43     private final WeakReference<Context> mContext;
44 
GarageModeSettingsObserver(Context context, Handler handler)45     public GarageModeSettingsObserver(Context context, Handler handler) {
46         super(handler);
47         mContext = new WeakReference<Context>(context);
48     }
49 
register()50     public void register() {
51         if (mContext.get() == null) {
52             return;
53         }
54         for (Uri uri : GARAGE_SETTING_URIS) {
55             mContext.get().getContentResolver().registerContentObserver(
56                     uri, false, this);
57         }
58     }
59 
unregister()60     public void unregister() {
61         if (mContext.get() == null) {
62             return;
63         }
64         mContext.get().getContentResolver().unregisterContentObserver(this);
65     }
66 
67 }
68