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 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.Button; 33 import android.widget.LinearLayout; 34 import android.widget.TextView; 35 36 import com.android.settings.R; 37 import com.android.settings.Utils; 38 39 public final class PasswordUtils extends com.android.settingslib.Utils { 40 41 private static final String TAG = "Settings"; 42 43 /** 44 * Returns whether the uid which the activity with {@code activityToken} is launched from has 45 * been granted the {@code permission}. 46 */ isCallingAppPermitted(Context context, IBinder activityToken, String permission)47 public static boolean isCallingAppPermitted(Context context, IBinder activityToken, 48 String permission) { 49 try { 50 return context.checkPermission(permission, /* pid= */ -1, 51 ActivityManager.getService().getLaunchedFromUid(activityToken)) 52 == PackageManager.PERMISSION_GRANTED; 53 } catch (RemoteException e) { 54 Log.v(TAG, "Could not talk to activity manager.", e); 55 return false; 56 } 57 } 58 59 /** 60 * Returns the label of the package which the activity with {@code activityToken} is launched 61 * from or {@code null} if it is launched from the settings app itself. 62 */ 63 @Nullable getCallingAppLabel(Context context, IBinder activityToken)64 public static CharSequence getCallingAppLabel(Context context, IBinder activityToken) { 65 String pkg = getCallingAppPackageName(activityToken); 66 if (pkg == null || pkg.equals(SETTINGS_PACKAGE_NAME)) { 67 return null; 68 } 69 70 return Utils.getApplicationLabel(context, pkg); 71 } 72 73 /** 74 * Returns the package name which the activity with {@code activityToken} is launched from. 75 */ 76 @Nullable getCallingAppPackageName(IBinder activityToken)77 public static String getCallingAppPackageName(IBinder activityToken) { 78 String pkg = null; 79 try { 80 pkg = ActivityManager.getService().getLaunchedFromPackage(activityToken); 81 } catch (RemoteException e) { 82 Log.v(TAG, "Could not talk to activity manager.", e); 83 } 84 return pkg; 85 } 86 87 /** Crashes the calling application and provides it with {@code message}. */ crashCallingApplication(IBinder activityToken, String message, int exceptionTypeId)88 public static void crashCallingApplication(IBinder activityToken, String message, 89 int exceptionTypeId) { 90 IActivityManager am = ActivityManager.getService(); 91 try { 92 int uid = am.getLaunchedFromUid(activityToken); 93 int userId = UserHandle.getUserId(uid); 94 am.crashApplicationWithType( 95 uid, 96 /* initialPid= */ -1, 97 getCallingAppPackageName(activityToken), 98 userId, 99 message, 100 false, 101 exceptionTypeId); 102 } catch (RemoteException e) { 103 Log.v(TAG, "Could not talk to activity manager.", e); 104 } 105 } 106 107 /** Setup screen lock options button under the Glif Header. */ setupScreenLockOptionsButton(Context context, View view, Button optButton)108 public static void setupScreenLockOptionsButton(Context context, View view, Button optButton) { 109 final LinearLayout headerLayout = view.findViewById( 110 R.id.sud_layout_header); 111 final TextView sucTitleView = headerLayout.findViewById(R.id.suc_layout_title); 112 if (headerLayout != null && sucTitleView != null) { 113 final ViewGroup.MarginLayoutParams layoutTitleParams = 114 (ViewGroup.MarginLayoutParams) sucTitleView.getLayoutParams(); 115 final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( 116 ViewGroup.LayoutParams.WRAP_CONTENT, 117 ViewGroup.LayoutParams.WRAP_CONTENT); 118 lp.leftMargin = layoutTitleParams.leftMargin; 119 lp.topMargin = (int) context.getResources().getDimensionPixelSize( 120 R.dimen.screen_lock_options_button_margin_top); 121 optButton.setPadding(0, 0, 0, 0); 122 optButton.setLayoutParams(lp); 123 optButton.setText(context.getString(R.string.setup_lock_settings_options_button_label)); 124 headerLayout.addView(optButton); 125 } 126 } 127 } 128