• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.companion.virtual.audio;
18 
19 import android.annotation.NonNull;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.Bundle;
25 import android.os.UserManager;
26 
27 import com.android.internal.annotations.GuardedBy;
28 
29 /**
30  * Class to detect the user restrictions change for microphone usage.
31  */
32 final class UserRestrictionsDetector extends BroadcastReceiver {
33     private static final String TAG = "UserRestrictionsDetector";
34 
35     /** Interface for listening user restrictions change. */
36     interface UserRestrictionsCallback {
37 
38         /** Notifies when value of {@link UserManager#DISALLOW_UNMUTE_MICROPHONE} is changed. */
onMicrophoneRestrictionChanged(boolean isUnmuteMicDisallowed)39         void onMicrophoneRestrictionChanged(boolean isUnmuteMicDisallowed);
40     }
41 
42     private final Context mContext;
43     private final UserManager mUserManager;
44     private final Object mLock = new Object();
45     @GuardedBy("mLock")
46     private boolean mIsUnmuteMicDisallowed;
47     private UserRestrictionsCallback mUserRestrictionsCallback;
48 
UserRestrictionsDetector(Context context)49     UserRestrictionsDetector(Context context) {
50         mContext = context;
51         mUserManager = context.getSystemService(UserManager.class);
52     }
53 
54     /** Returns value of {@link UserManager#DISALLOW_UNMUTE_MICROPHONE}. */
isUnmuteMicrophoneDisallowed()55     boolean isUnmuteMicrophoneDisallowed() {
56         Bundle bundle = mUserManager.getUserRestrictions();
57         return bundle.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE);
58     }
59 
60     /** Registers user restrictions change. */
register(@onNull UserRestrictionsCallback callback)61     void register(@NonNull UserRestrictionsCallback callback) {
62         mUserRestrictionsCallback = callback;
63         IntentFilter filter = new IntentFilter();
64         filter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED);
65         mContext.registerReceiver(/* receiver= */ this, filter);
66 
67         synchronized (mLock) {
68             // Gets initial value.
69             mIsUnmuteMicDisallowed = isUnmuteMicrophoneDisallowed();
70         }
71     }
72 
73     /** Unregisters user restrictions change. */
unregister()74     void unregister() {
75         if (mUserRestrictionsCallback != null) {
76             mUserRestrictionsCallback = null;
77             mContext.unregisterReceiver(/* receiver= */ this);
78         }
79     }
80 
81     @Override
onReceive(Context context, Intent intent)82     public void onReceive(Context context, Intent intent) {
83         final String action = intent.getAction();
84         if (UserManager.ACTION_USER_RESTRICTIONS_CHANGED.equals(action)) {
85             boolean isUnmuteMicDisallowed = isUnmuteMicrophoneDisallowed();
86             synchronized (mLock) {
87                 if (isUnmuteMicDisallowed == mIsUnmuteMicDisallowed) {
88                     return;
89                 }
90                 mIsUnmuteMicDisallowed = isUnmuteMicDisallowed;
91             }
92             if (mUserRestrictionsCallback != null) {
93                 mUserRestrictionsCallback.onMicrophoneRestrictionChanged(isUnmuteMicDisallowed);
94             }
95         }
96     }
97 }
98