Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 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.systemui.media; |
| 18 | |
Philip P. Moltmann | 66ce238 | 2018-10-09 13:46:11 -0700 | [diff] [blame] | 19 | import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; |
Wale Ogunwale | d6bb8e9 | 2018-05-16 16:42:29 -0700 | [diff] [blame] | 20 | |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 21 | import android.app.Activity; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 22 | import android.app.AlertDialog; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 23 | import android.content.DialogInterface; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 27 | import android.graphics.Typeface; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 28 | import android.media.projection.IMediaProjection; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 29 | import android.media.projection.IMediaProjectionManager; |
| 30 | import android.media.projection.MediaProjectionManager; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 35 | import android.text.BidiFormatter; |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 36 | import android.text.SpannableString; |
| 37 | import android.text.TextPaint; |
| 38 | import android.text.TextUtils; |
| 39 | import android.text.style.StyleSpan; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 40 | import android.util.Log; |
Wale Ogunwale | d6bb8e9 | 2018-05-16 16:42:29 -0700 | [diff] [blame] | 41 | import android.view.Window; |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 42 | import android.view.WindowManager; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 43 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 44 | import com.android.systemui.R; |
| 45 | |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 46 | public class MediaProjectionPermissionActivity extends Activity |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 47 | implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 48 | private static final String TAG = "MediaProjectionPermissionActivity"; |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 49 | private static final float MAX_APP_NAME_SIZE_PX = 500f; |
| 50 | private static final String ELLIPSIS = "\u2026"; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 51 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 52 | private String mPackageName; |
| 53 | private int mUid; |
| 54 | private IMediaProjectionManager mService; |
| 55 | |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 56 | private AlertDialog mDialog; |
| 57 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 58 | @Override |
| 59 | public void onCreate(Bundle icicle) { |
| 60 | super.onCreate(icicle); |
| 61 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 62 | mPackageName = getCallingPackage(); |
| 63 | IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE); |
| 64 | mService = IMediaProjectionManager.Stub.asInterface(b); |
| 65 | |
| 66 | if (mPackageName == null) { |
| 67 | finish(); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | PackageManager packageManager = getPackageManager(); |
| 72 | ApplicationInfo aInfo; |
| 73 | try { |
| 74 | aInfo = packageManager.getApplicationInfo(mPackageName, 0); |
| 75 | mUid = aInfo.uid; |
| 76 | } catch (PackageManager.NameNotFoundException e) { |
| 77 | Log.e(TAG, "unable to look up package name", e); |
| 78 | finish(); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | if (mService.hasProjectionPermission(mUid, mPackageName)) { |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 84 | setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName)); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 85 | finish(); |
| 86 | return; |
| 87 | } |
| 88 | } catch (RemoteException e) { |
| 89 | Log.e(TAG, "Error checking projection permissions", e); |
| 90 | finish(); |
| 91 | return; |
| 92 | } |
| 93 | |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 94 | TextPaint paint = new TextPaint(); |
| 95 | paint.setTextSize(42); |
| 96 | |
| 97 | String label = aInfo.loadLabel(packageManager).toString(); |
| 98 | |
| 99 | // If the label contains new line characters it may push the security |
| 100 | // message below the fold of the dialog. Labels shouldn't have new line |
| 101 | // characters anyways, so just truncate the message the first time one |
| 102 | // is seen. |
| 103 | final int labelLength = label.length(); |
| 104 | int offset = 0; |
| 105 | while (offset < labelLength) { |
| 106 | final int codePoint = label.codePointAt(offset); |
| 107 | final int type = Character.getType(codePoint); |
| 108 | if (type == Character.LINE_SEPARATOR |
| 109 | || type == Character.CONTROL |
| 110 | || type == Character.PARAGRAPH_SEPARATOR) { |
| 111 | label = label.substring(0, offset) + ELLIPSIS; |
| 112 | break; |
| 113 | } |
| 114 | offset += Character.charCount(codePoint); |
| 115 | } |
| 116 | |
| 117 | if (label.isEmpty()) { |
| 118 | label = mPackageName; |
| 119 | } |
| 120 | |
| 121 | String unsanitizedAppName = TextUtils.ellipsize(label, |
| 122 | paint, MAX_APP_NAME_SIZE_PX, TextUtils.TruncateAt.END).toString(); |
| 123 | String appName = BidiFormatter.getInstance().unicodeWrap(unsanitizedAppName); |
| 124 | |
| 125 | String actionText = getString(R.string.media_projection_dialog_text, appName); |
| 126 | SpannableString message = new SpannableString(actionText); |
| 127 | |
| 128 | int appNameIndex = actionText.indexOf(appName); |
| 129 | if (appNameIndex >= 0) { |
| 130 | message.setSpan(new StyleSpan(Typeface.BOLD), |
| 131 | appNameIndex, appNameIndex + appName.length(), 0); |
| 132 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 133 | |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 134 | String dialogTitle = getString(R.string.media_projection_dialog_title, appName); |
| 135 | |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 136 | mDialog = new AlertDialog.Builder(this) |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 137 | .setTitle(dialogTitle) |
Narayan Kamath | ac3af3d | 2019-03-04 14:28:27 +0000 | [diff] [blame] | 138 | .setIcon(R.drawable.ic_media_projection_permission) |
Michael Wright | 58950a9 | 2015-08-25 00:00:05 +0100 | [diff] [blame] | 139 | .setMessage(message) |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 140 | .setPositiveButton(R.string.media_projection_action_text, this) |
| 141 | .setNegativeButton(android.R.string.cancel, this) |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 142 | .setOnCancelListener(this) |
| 143 | .create(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 144 | |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 145 | mDialog.create(); |
Dongwon Kang | b0cf7ea | 2015-06-08 13:43:07 -0700 | [diff] [blame] | 146 | mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 147 | |
Wale Ogunwale | d6bb8e9 | 2018-05-16 16:42:29 -0700 | [diff] [blame] | 148 | final Window w = mDialog.getWindow(); |
| 149 | w.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); |
Philip P. Moltmann | 66ce238 | 2018-10-09 13:46:11 -0700 | [diff] [blame] | 150 | w.addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 151 | |
| 152 | mDialog.show(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onDestroy() { |
| 157 | super.onDestroy(); |
John Spurlock | 3d332f4 | 2015-02-24 12:14:06 -0500 | [diff] [blame] | 158 | if (mDialog != null) { |
| 159 | mDialog.dismiss(); |
| 160 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void onClick(DialogInterface dialog, int which) { |
| 165 | try { |
| 166 | if (which == AlertDialog.BUTTON_POSITIVE) { |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 167 | setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName)); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } catch (RemoteException e) { |
| 170 | Log.e(TAG, "Error granting projection permission", e); |
| 171 | setResult(RESULT_CANCELED); |
| 172 | } finally { |
John Spurlock | 3d332f4 | 2015-02-24 12:14:06 -0500 | [diff] [blame] | 173 | if (mDialog != null) { |
| 174 | mDialog.dismiss(); |
| 175 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 176 | finish(); |
| 177 | } |
| 178 | } |
| 179 | |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 180 | private Intent getMediaProjectionIntent(int uid, String packageName) |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 181 | throws RemoteException { |
| 182 | IMediaProjection projection = mService.createProjection(uid, packageName, |
Narayan Kamath | cc52e82 | 2019-02-19 18:42:56 +0000 | [diff] [blame] | 183 | MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 184 | Intent intent = new Intent(); |
| 185 | intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder()); |
| 186 | return intent; |
| 187 | } |
Adrian Roos | 752aee8 | 2014-11-25 21:11:19 +0100 | [diff] [blame] | 188 | |
| 189 | @Override |
| 190 | public void onCancel(DialogInterface dialog) { |
| 191 | finish(); |
| 192 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 193 | } |