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