1 /*
2  * Copyright (C) 2014 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.server.telecom;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.UserInfo;
23 import android.net.Uri;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.telecom.Log;
27 import android.telecom.PhoneAccount;
28 import android.telecom.PhoneAccountHandle;
29 
30 import com.android.server.telecom.components.ErrorDialogActivity;
31 import com.android.server.telecom.flags.FeatureFlags;
32 
33 public final class UserUtil {
34 
UserUtil()35     private UserUtil() {
36     }
37 
getUserInfoFromUserHandle(Context context, UserHandle userHandle)38     private static UserInfo getUserInfoFromUserHandle(Context context, UserHandle userHandle) {
39         UserManager userManager = context.getSystemService(UserManager.class);
40         return userManager.getUserInfo(userHandle.getIdentifier());
41     }
42 
isManagedProfile(Context context, UserHandle userHandle, FeatureFlags featureFlags)43     public static boolean isManagedProfile(Context context, UserHandle userHandle,
44             FeatureFlags featureFlags) {
45         UserManager userManager = context.createContextAsUser(userHandle, 0)
46                 .getSystemService(UserManager.class);
47         UserInfo userInfo = getUserInfoFromUserHandle(context, userHandle);
48         return featureFlags.telecomResolveHiddenDependencies()
49                 ? userManager != null && userManager.isManagedProfile()
50                 : userInfo != null && userInfo.isManagedProfile();
51     }
52 
isPrivateProfile(UserHandle userHandle, Context context)53     public static boolean isPrivateProfile(UserHandle userHandle, Context context) {
54         UserManager um = context.createContextAsUser(userHandle, 0).getSystemService(
55                 UserManager.class);
56         return um != null && um.isPrivateProfile();
57     }
58 
isProfile(Context context, UserHandle userHandle, FeatureFlags featureFlags)59     public static boolean isProfile(Context context, UserHandle userHandle,
60             FeatureFlags featureFlags) {
61         UserManager userManager = context.createContextAsUser(userHandle, 0)
62                 .getSystemService(UserManager.class);
63         UserInfo userInfo = getUserInfoFromUserHandle(context, userHandle);
64         return featureFlags.telecomResolveHiddenDependencies()
65                 ? userManager != null && userManager.isProfile()
66                 : userInfo != null && userInfo.profileGroupId != userInfo.id
67                         && userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID;
68     }
69 
showErrorDialogForRestrictedOutgoingCall(Context context, int stringId, String tag, String reason)70     public static void showErrorDialogForRestrictedOutgoingCall(Context context,
71             int stringId, String tag, String reason) {
72         final Intent intent = new Intent(context, ErrorDialogActivity.class);
73         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74         intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, stringId);
75         context.startActivityAsUser(intent, UserHandle.CURRENT);
76         Log.w(tag, "Rejecting non-emergency phone call because "
77                 + reason);
78     }
79 
hasOutgoingCallsUserRestriction(Context context, UserHandle userHandle, Uri handle, boolean isSelfManaged, String tag, FeatureFlags featureFlags)80     public static boolean hasOutgoingCallsUserRestriction(Context context,
81             UserHandle userHandle, Uri handle, boolean isSelfManaged, String tag,
82             FeatureFlags featureFlags) {
83         // Set handle for conference calls. Refer to {@link Connection#ADHOC_CONFERENCE_ADDRESS}.
84         if (handle == null) {
85             handle = Uri.parse("tel:conf-factory");
86         }
87 
88         if(!isSelfManaged) {
89             // Check DISALLOW_OUTGOING_CALLS restriction. Note: We are skipping this
90             // check in a managed profile user because this check can always be bypassed
91             // by copying and pasting the phone number into the personal dialer.
92             if (!UserUtil.isManagedProfile(context, userHandle, featureFlags)) {
93                 final UserManager userManager = context.getSystemService(UserManager.class);
94                 boolean hasUserRestriction = featureFlags.telecomResolveHiddenDependencies()
95                         ? userManager.hasUserRestrictionForUser(
96                                 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
97                         : userManager.hasUserRestriction(
98                                 UserManager.DISALLOW_OUTGOING_CALLS, userHandle);
99                 // Only emergency calls are allowed for users with the DISALLOW_OUTGOING_CALLS
100                 // restriction.
101                 if (!TelephonyUtil.shouldProcessAsEmergency(context, handle)) {
102                     if (userManager.hasBaseUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
103                             userHandle)) {
104                         String reason = "of DISALLOW_OUTGOING_CALLS restriction";
105                         showErrorDialogForRestrictedOutgoingCall(context,
106                                 R.string.outgoing_call_not_allowed_user_restriction, tag, reason);
107                         return true;
108                     } else if (hasUserRestriction) {
109                         final DevicePolicyManager dpm =
110                                 context.getSystemService(DevicePolicyManager.class);
111                         if (dpm == null) {
112                             return true;
113                         }
114                         final Intent adminSupportIntent = dpm.createAdminSupportIntent(
115                                 UserManager.DISALLOW_OUTGOING_CALLS);
116                         if (adminSupportIntent != null) {
117                             context.startActivityAsUser(adminSupportIntent, userHandle);
118                         }
119                         return true;
120                     }
121                 }
122             }
123         }
124         return false;
125     }
126 
127     /**
128      * Gets the associated user for the given call. Note: this is applicable to all calls except
129      * outgoing calls as the associated user is already based off of the user placing the
130      * call.
131      *
132      * @param phoneAccountRegistrar
133      * @param currentUser Current user profile (this can either be the admin or a secondary/guest
134      *                    user). Note that work profile users fall under the admin user.
135      * @param targetPhoneAccount The phone account to retrieve the {@link UserHandle} from.
136      * @return current user if it isn't the admin or if the work profile is paused for the target
137      * phone account handle user, otherwise return the target phone account handle user. If the
138      * flag is disabled, return the legacy {@link UserHandle}.
139      */
getAssociatedUserForCall(boolean isAssociatedUserFlagEnabled, PhoneAccountRegistrar phoneAccountRegistrar, UserHandle currentUser, PhoneAccountHandle targetPhoneAccount)140     public static UserHandle getAssociatedUserForCall(boolean isAssociatedUserFlagEnabled,
141             PhoneAccountRegistrar phoneAccountRegistrar, UserHandle currentUser,
142             PhoneAccountHandle targetPhoneAccount) {
143         if (!isAssociatedUserFlagEnabled) {
144             return targetPhoneAccount.getUserHandle();
145         }
146         // For multi-user phone accounts, associate the call with the profile receiving/placing
147         // the call. For SIM accounts (that are assigned to specific users), the user association
148         // will be placed on the target phone account handle user.
149         PhoneAccount account = phoneAccountRegistrar.getPhoneAccountUnchecked(targetPhoneAccount);
150         if (account != null) {
151             return account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)
152                     ? currentUser
153                     : targetPhoneAccount.getUserHandle();
154         }
155         // If target phone account handle is null or account cannot be found,
156         // return the current user.
157         return currentUser;
158     }
159 }
160