2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.media;
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;
43 import com.android.systemui.R;
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";
52 private boolean mPermanentGrant;
53 private String mPackageName;
55 private IMediaProjectionManager mService;
57 private AlertDialog mDialog;
60 public void onCreate(Bundle icicle) {
61 super.onCreate(icicle);
63 mPackageName = getCallingPackage();
64 IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
65 mService = IMediaProjectionManager.Stub.asInterface(b);
67 if (mPackageName == null) {
72 PackageManager packageManager = getPackageManager();
73 ApplicationInfo aInfo;
75 aInfo = packageManager.getApplicationInfo(mPackageName, 0);
77 } catch (PackageManager.NameNotFoundException e) {
78 Log.e(TAG, "unable to look up package name", e);
84 if (mService.hasProjectionPermission(mUid, mPackageName)) {
85 setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName,
86 false /*permanentGrant*/));
90 } catch (RemoteException e) {
91 Log.e(TAG, "Error checking projection permissions", e);
96 TextPaint paint = new TextPaint();
97 paint.setTextSize(42);
99 String label = aInfo.loadLabel(packageManager).toString();
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
105 final int labelLength = label.length();
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;
116 offset += Character.charCount(codePoint);
119 if (label.isEmpty()) {
120 label = mPackageName;
123 String unsanitizedAppName = TextUtils.ellipsize(label,
124 paint, MAX_APP_NAME_SIZE_PX, TextUtils.TruncateAt.END).toString();
125 String appName = BidiFormatter.getInstance().unicodeWrap(unsanitizedAppName);
127 String actionText = getString(R.string.media_projection_dialog_text, appName);
128 SpannableString message = new SpannableString(actionText);
130 int appNameIndex = actionText.indexOf(appName);
131 if (appNameIndex >= 0) {
132 message.setSpan(new StyleSpan(Typeface.BOLD),
133 appNameIndex, appNameIndex + appName.length(), 0);
136 mDialog = new AlertDialog.Builder(this)
137 .setIcon(aInfo.loadIcon(packageManager))
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)
146 mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
148 ((CheckBox) mDialog.findViewById(R.id.remember)).setOnCheckedChangeListener(this);
149 mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
155 protected void onDestroy() {
157 if (mDialog != null) {
163 public void onClick(DialogInterface dialog, int which) {
165 if (which == AlertDialog.BUTTON_POSITIVE) {
166 setResult(RESULT_OK, getMediaProjectionIntent(
167 mUid, mPackageName, mPermanentGrant));
169 } catch (RemoteException e) {
170 Log.e(TAG, "Error granting projection permission", e);
171 setResult(RESULT_CANCELED);
173 if (mDialog != null) {
181 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
182 mPermanentGrant = isChecked;
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());
195 public void onCancel(DialogInterface dialog) {