1 /*
2  * Copyright (C) 2022 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.policy;
18 
19 import android.annotation.NonNull;
20 import android.app.ActivityManager;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.net.Uri;
25 import android.os.Handler;
26 import android.os.UserHandle;
27 import android.provider.Settings;
28 
29 import java.util.Collection;
30 
31 /**
32  * A ContentObserver for listening {@link Settings.Secure#NAV_BAR_FORCE_VISIBLE} setting key.
33  *
34  * @hide
35  */
36 public class ForceShowNavBarSettingsObserver extends ContentObserver {
37     private Context mContext;
38     private Runnable mOnChangeRunnable;
39 
ForceShowNavBarSettingsObserver(Handler handler, Context context)40     public ForceShowNavBarSettingsObserver(Handler handler, Context context) {
41         super(handler);
42         mContext = context;
43     }
44 
setOnChangeRunnable(Runnable r)45     public void setOnChangeRunnable(Runnable r) {
46         mOnChangeRunnable = r;
47     }
48 
49     /**
50      * Registers the observer.
51      */
register()52     public void register() {
53         final ContentResolver r = mContext.getContentResolver();
54         r.registerContentObserver(
55                 Settings.Secure.getUriFor(Settings.Secure.NAV_BAR_FORCE_VISIBLE),
56                 false, this, UserHandle.USER_ALL);
57     }
58 
59     /**
60      * Unregisters the observer.
61      */
unregister()62     public void unregister() {
63         mContext.getContentResolver().unregisterContentObserver(this);
64     }
65 
66     @Override
onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags, int userId)67     public void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags, int userId) {
68         if (userId != ActivityManager.getCurrentUser()) {
69             return;
70         }
71 
72         if (mOnChangeRunnable != null) {
73             mOnChangeRunnable.run();
74         }
75     }
76 
77     /**
78      * Returns true only when it's in orce show navigation bar mode. Otherwise, return false.
79      */
isEnabled()80     public boolean isEnabled() {
81         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
82                 Settings.Secure.NAV_BAR_FORCE_VISIBLE, 0, UserHandle.USER_CURRENT) == 1;
83     }
84 }
85