1 /*
2  * Copyright (C) 2023 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.settings.accounts;
18 
19 import android.app.admin.flags.Flags;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.util.Log;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.VisibleForTesting;
30 import androidx.lifecycle.DefaultLifecycleObserver;
31 import androidx.lifecycle.LifecycleOwner;
32 
33 import com.android.settings.Utils;
34 
35 import javax.annotation.Nullable;
36 
37 /**
38  * A class that controls the managed profile's quiet mode, listens to quiet mode changes and
39  * modifies managed profile settings. The user facing term for quiet mode is "work apps".
40  */
41 final class ManagedProfileQuietModeEnabler implements DefaultLifecycleObserver {
42 
43     private static final String TAG = "QuietModeEnabler";
44     private final Context mContext;
45     private final QuietModeChangeListener mListener;
46     @Nullable private final UserHandle mManagedProfile;
47     private final UserManager mUserManager;
48 
49     public interface QuietModeChangeListener {
50         /** Called when quiet mode has changed. */
onQuietModeChanged()51         void onQuietModeChanged();
52     }
53 
ManagedProfileQuietModeEnabler(Context context, QuietModeChangeListener listener)54     ManagedProfileQuietModeEnabler(Context context, QuietModeChangeListener listener) {
55         mContext = context;
56         mListener = listener;
57         mUserManager = context.getSystemService(UserManager.class);
58         mManagedProfile = Utils.getManagedProfile(mUserManager);
59     }
60 
setQuietModeEnabled(boolean enabled)61     public void setQuietModeEnabled(boolean enabled) {
62         if (mManagedProfile == null) {
63             return;
64         }
65         if (Flags.quietModeCredentialBugFix()) {
66             if (isQuietModeEnabled() != enabled) {
67                 mUserManager.requestQuietModeEnabled(enabled, mManagedProfile);
68             }
69         } else {
70             mUserManager.requestQuietModeEnabled(enabled, mManagedProfile);
71         }
72     }
73 
isQuietModeEnabled()74     public boolean isQuietModeEnabled() {
75         return mManagedProfile != null && mUserManager.isQuietModeEnabled(mManagedProfile);
76     }
77 
78     @Override
onStart(@onNull LifecycleOwner owner)79     public void onStart(@NonNull LifecycleOwner owner) {
80         IntentFilter intentFilter = new IntentFilter();
81         intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
82         intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
83         mContext.registerReceiver(mReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED);
84     }
85 
86     @Override
onStop(@onNull LifecycleOwner owner)87     public void onStop(@NonNull LifecycleOwner owner) {
88         mContext.unregisterReceiver(mReceiver);
89     }
90 
isAvailable()91     public boolean isAvailable() {
92         return (mManagedProfile != null);
93     }
94 
refreshQuietMode()95     private void refreshQuietMode() {
96         if (mListener != null) {
97             mListener.onQuietModeChanged();
98         }
99     }
100 
101     /**
102      * Receiver that listens to {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
103      * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE}, and updates the work mode
104      */
105     @VisibleForTesting
106     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
107         @Override
108         public void onReceive(Context context, Intent intent) {
109             if (intent == null) {
110                 return;
111             }
112             String action = intent.getAction();
113             Log.v(TAG, "Received broadcast: " + action);
114 
115             if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)
116                     || Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
117                 int intentUserIdentifier = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
118                         UserHandle.USER_NULL);
119                 if (intentUserIdentifier == mManagedProfile.getIdentifier()) {
120                     refreshQuietMode();
121                 } else {
122                     Log.w(TAG, "Managed profile broadcast ID: " + intentUserIdentifier
123                             + " does not match managed user: " + mManagedProfile);
124                 }
125             } else {
126                 Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
127             }
128         }
129     };
130 }
131