• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.password;
18 
19 import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
20 
21 import android.annotation.Nullable;
22 import android.app.ActivityManager;
23 import android.app.IActivityManager;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.os.IBinder;
27 import android.os.RemoteException;
28 import android.os.UserHandle;
29 import android.util.Log;
30 
31 import com.android.settings.Utils;
32 
33 public final class PasswordUtils extends com.android.settingslib.Utils {
34 
35     private static final String TAG = "Settings";
36 
37     /**
38      * Returns whether the uid which the activity with {@code activityToken} is launched from has
39      * been granted the {@code permission}.
40      */
isCallingAppPermitted(Context context, IBinder activityToken, String permission)41     public static boolean isCallingAppPermitted(Context context, IBinder activityToken,
42             String permission) {
43         try {
44             return context.checkPermission(permission, /* pid= */ -1,
45                     ActivityManager.getService().getLaunchedFromUid(activityToken))
46                     == PackageManager.PERMISSION_GRANTED;
47         } catch (RemoteException e) {
48             Log.v(TAG, "Could not talk to activity manager.", e);
49             return false;
50         }
51     }
52 
53     /**
54      * Returns the label of the package which the activity with {@code activityToken} is launched
55      * from or {@code null} if it is launched from the settings app itself.
56      */
57     @Nullable
getCallingAppLabel(Context context, IBinder activityToken)58     public static CharSequence getCallingAppLabel(Context context, IBinder activityToken) {
59         String pkg = getCallingAppPackageName(activityToken);
60         if (pkg == null || pkg.equals(SETTINGS_PACKAGE_NAME)) {
61             return null;
62         }
63 
64         return Utils.getApplicationLabel(context, pkg);
65     }
66 
67     /**
68      * Returns the package name which the activity with {@code activityToken} is launched from.
69      */
70     @Nullable
getCallingAppPackageName(IBinder activityToken)71     public static String getCallingAppPackageName(IBinder activityToken) {
72         String pkg = null;
73         try {
74             pkg = ActivityManager.getService().getLaunchedFromPackage(activityToken);
75         } catch (RemoteException e) {
76             Log.v(TAG, "Could not talk to activity manager.", e);
77         }
78         return pkg;
79     }
80 
81     /** Crashes the calling application and provides it with {@code message}. */
crashCallingApplication(IBinder activityToken, String message)82     public static void crashCallingApplication(IBinder activityToken, String message) {
83         IActivityManager am = ActivityManager.getService();
84         try {
85             int uid = am.getLaunchedFromUid(activityToken);
86             int userId = UserHandle.getUserId(uid);
87             am.crashApplication(
88                     uid,
89                     /* initialPid= */ -1,
90                     getCallingAppPackageName(activityToken),
91                     userId,
92                     message,
93                     false);
94         } catch (RemoteException e) {
95             Log.v(TAG, "Could not talk to activity manager.", e);
96         }
97     }
98 }
99