• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 import com.android.server.telecom.components.ErrorDialogActivity;
29 
30 public final class UserUtil {
31 
UserUtil()32     private UserUtil() {
33     }
34 
getUserInfoFromUserHandle(Context context, UserHandle userHandle)35     private static UserInfo getUserInfoFromUserHandle(Context context, UserHandle userHandle) {
36         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
37         return userManager.getUserInfo(userHandle.getIdentifier());
38     }
39 
isManagedProfile(Context context, UserHandle userHandle)40     public static boolean isManagedProfile(Context context, UserHandle userHandle) {
41         UserInfo userInfo = getUserInfoFromUserHandle(context, userHandle);
42         return userInfo != null && userInfo.isManagedProfile();
43     }
44 
isProfile(Context context, UserHandle userHandle)45     public static boolean isProfile(Context context, UserHandle userHandle) {
46         UserInfo userInfo = getUserInfoFromUserHandle(context, userHandle);
47         return userInfo != null && userInfo.profileGroupId != userInfo.id;
48     }
49 
showErrorDialogForRestrictedOutgoingCall(Context context, int stringId, String tag, String reason)50     public static void showErrorDialogForRestrictedOutgoingCall(Context context,
51             int stringId, String tag, String reason) {
52         final Intent intent = new Intent(context, ErrorDialogActivity.class);
53         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
54         intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, stringId);
55         context.startActivityAsUser(intent, UserHandle.CURRENT);
56         Log.w(tag, "Rejecting non-emergency phone call because "
57                 + reason);
58     }
59 
hasOutgoingCallsUserRestriction(Context context, UserHandle userHandle, Uri handle, boolean isSelfManaged, String tag)60     public static boolean hasOutgoingCallsUserRestriction(Context context,
61             UserHandle userHandle, Uri handle, boolean isSelfManaged, String tag) {
62         // Set handle for conference calls. Refer to {@link Connection#ADHOC_CONFERENCE_ADDRESS}.
63         if (handle == null) {
64             handle = Uri.parse("tel:conf-factory");
65         }
66 
67         if(!isSelfManaged) {
68             // Check DISALLOW_OUTGOING_CALLS restriction. Note: We are skipping this
69             // check in a managed profile user because this check can always be bypassed
70             // by copying and pasting the phone number into the personal dialer.
71             if (!UserUtil.isManagedProfile(context, userHandle)) {
72                 // Only emergency calls are allowed for users with the DISALLOW_OUTGOING_CALLS
73                 // restriction.
74                 if (!TelephonyUtil.shouldProcessAsEmergency(context, handle)) {
75                     final UserManager userManager =
76                             (UserManager) context.getSystemService(Context.USER_SERVICE);
77                     if (userManager.hasBaseUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
78                             userHandle)) {
79                         String reason = "of DISALLOW_OUTGOING_CALLS restriction";
80                         showErrorDialogForRestrictedOutgoingCall(context,
81                                 R.string.outgoing_call_not_allowed_user_restriction, tag, reason);
82                         return true;
83                     } else if (userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
84                             userHandle)) {
85                         final DevicePolicyManager dpm =
86                                 context.getSystemService(DevicePolicyManager.class);
87                         if (dpm == null) {
88                             return true;
89                         }
90                         final Intent adminSupportIntent = dpm.createAdminSupportIntent(
91                                 UserManager.DISALLOW_OUTGOING_CALLS);
92                         if (adminSupportIntent != null) {
93                             context.startActivityAsUser(adminSupportIntent, userHandle);
94                         }
95                         return true;
96                     }
97                 }
98             }
99         }
100         return false;
101     }
102 }
103