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