Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/kidsmode/KidsModeSettingsObserver.java

raw symbols SCIP
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.wm.shell.kidsmode;
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 kids mode relative setting keys:
33 * - {@link Settings.Secure#NAVIGATION_MODE}
34 * - {@link Settings.Secure#NAV_BAR_KIDS_MODE}
35 *
36 * @hide
37 */
38 public class KidsModeSettingsObserver extends ContentObserver {
39 private Context mContext;
40 private Runnable mOnChangeRunnable;
41
42 public KidsModeSettingsObserver(Handler handler, Context context) {
43 super(handler);
44 mContext = context;
45 }
46
47 public void setOnChangeRunnable(Runnable r) {
48 mOnChangeRunnable = r;
49 }
50
51 /**
52 * Registers the observer.
53 */
54 public void register() {
55 final ContentResolver r = mContext.getContentResolver();
56 r.registerContentObserver(
57 Settings.Secure.getUriFor(Settings.Secure.NAVIGATION_MODE),
58 false, this, UserHandle.USER_ALL);
59 r.registerContentObserver(
60 Settings.Secure.getUriFor(Settings.Secure.NAV_BAR_KIDS_MODE),
61 false, this, UserHandle.USER_ALL);
62 }
63
64 /**
65 * Unregisters the observer.
66 */
67 public void unregister() {
68 mContext.getContentResolver().unregisterContentObserver(this);
69 }
70
71 @Override
72 public void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags, int userId) {
73 if (userId != ActivityManager.getCurrentUser()) {
74 return;
75 }
76
77 if (mOnChangeRunnable != null) {
78 mOnChangeRunnable.run();
79 }
80 }
81
82 /**
83 * Returns true only when it's in three button nav mode and the kid nav bar mode is enabled.
84 * Otherwise, return false.
85 */
86 public boolean isEnabled() {
87 return Settings.Secure.getIntForUser(mContext.getContentResolver(),
88 Settings.Secure.NAVIGATION_MODE, 0, UserHandle.USER_CURRENT) == 0
89 && Settings.Secure.getIntForUser(mContext.getContentResolver(),
90 Settings.Secure.NAV_BAR_KIDS_MODE, 0, UserHandle.USER_CURRENT) == 1;
91 }
92 }