Bernard Chau | 92db5bf | 2018-12-17 18:03:24 +0000 | [diff] [blame] | 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 | |
Fan Zhang | c3fd289 | 2019-01-29 16:00:19 -0800 | [diff] [blame] | 19 | import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME; |
| 20 | |
Bernard Chau | 92db5bf | 2018-12-17 18:03:24 +0000 | [diff] [blame] | 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 | |
Bernard Chau | 92db5bf | 2018-12-17 18:03:24 +0000 | [diff] [blame] | 37 | /** |
| 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 Chau | 00870bf | 2019-01-29 18:28:13 +0000 | [diff] [blame] | 71 | public static String getCallingAppPackageName(IBinder activityToken) { |
Bernard Chau | 92db5bf | 2018-12-17 18:03:24 +0000 | [diff] [blame] | 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}. */ |
| 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 Tate | 9efc5ab | 2019-08-19 16:20:52 -0700 | [diff] [blame] | 92 | message, |
| 93 | false); |
Bernard Chau | 92db5bf | 2018-12-17 18:03:24 +0000 | [diff] [blame] | 94 | } catch (RemoteException e) { |
| 95 | Log.v(TAG, "Could not talk to activity manager.", e); |
| 96 | } |
| 97 | } |
| 98 | } |