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