blob: 5b75033f0f2c2091d78d1988d0f81b6a20db4872 [file] [log] [blame]
Bernard Chau92db5bf2018-12-17 18:03:24 +00001/*
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
17package com.android.settings.password;
18
Fan Zhangc3fd2892019-01-29 16:00:19 -080019import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
20
Bernard Chau92db5bf2018-12-17 18:03:24 +000021import android.annotation.Nullable;
22import android.app.ActivityManager;
23import android.app.IActivityManager;
24import android.content.Context;
25import android.content.pm.PackageManager;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.UserHandle;
29import android.util.Log;
30
31import com.android.settings.Utils;
32
33public final class PasswordUtils extends com.android.settingslib.Utils {
34
35 private static final String TAG = "Settings";
36
Bernard Chau92db5bf2018-12-17 18:03:24 +000037 /**
38 * Returns whether the uid which the activity with {@code activityToken} is launched from has
39 * been granted the {@code permission}.
40 */
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
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
Bernard Chau00870bf2019-01-29 18:28:13 +000071 public static String getCallingAppPackageName(IBinder activityToken) {
Bernard Chau92db5bf2018-12-17 18:03:24 +000072 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}. */
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,
Christopher Tate9efc5ab2019-08-19 16:20:52 -070092 message,
93 false);
Bernard Chau92db5bf2018-12-17 18:03:24 +000094 } catch (RemoteException e) {
95 Log.v(TAG, "Could not talk to activity manager.", e);
96 }
97 }
98}