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.app.PendingIntent; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.PackageManager; 27 import android.media.projection.MediaProjectionManager; 28 import android.media.projection.IMediaProjectionManager; 29 import android.media.projection.IMediaProjection; 30 import android.os.Bundle; 31 import android.os.IBinder; 32 import android.os.RemoteException; 33 import android.os.ServiceManager; 34 import android.util.Log; 35 import android.view.LayoutInflater; 36 import android.view.WindowManager; 37 import android.widget.CheckBox; 38 import android.widget.CompoundButton; 39 import android.widget.TextView; 40 41 import com.android.internal.app.AlertActivity; 42 import com.android.internal.app.AlertController; 43 import com.android.systemui.R; 44 import com.android.systemui.statusbar.phone.SystemUIDialog; 45 46 public class MediaProjectionPermissionActivity extends Activity 47 implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener, 48 DialogInterface.OnCancelListener { 49 private static final String TAG = "MediaProjectionPermissionActivity"; 50 51 private boolean mPermanentGrant; 52 private String mPackageName; 53 private int mUid; 54 private IMediaProjectionManager mService; 55 56 private AlertDialog mDialog; 57 58 @Override 59 public void onCreate(Bundle icicle) { 60 super.onCreate(icicle); 61 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)) { 84 setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName, 85 false /*permanentGrant*/)); 86 finish(); 87 return; 88 } 89 } catch (RemoteException e) { 90 Log.e(TAG, "Error checking projection permissions", e); 91 finish(); 92 return; 93 } 94 95 String appName = aInfo.loadLabel(packageManager).toString(); 96 97 mDialog = new AlertDialog.Builder(this) 98 .setIcon(aInfo.loadIcon(packageManager)) 99 .setMessage(getString(R.string.media_projection_dialog_text, appName)) 100 .setPositiveButton(R.string.media_projection_action_text, this) 101 .setNegativeButton(android.R.string.cancel, this) 102 .setView(R.layout.remember_permission_checkbox) 103 .setOnCancelListener(this) 104 .create(); 105 106 mDialog.create(); 107 108 ((CheckBox) mDialog.findViewById(R.id.remember)).setOnCheckedChangeListener(this); 109 mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 110 111 mDialog.show(); 112 } 113 114 @Override 115 protected void onDestroy() { 116 super.onDestroy(); 117 if (mDialog != null) { 118 mDialog.dismiss(); 119 } 120 } 121 122 @Override 123 public void onClick(DialogInterface dialog, int which) { 124 try { 125 if (which == AlertDialog.BUTTON_POSITIVE) { 126 setResult(RESULT_OK, getMediaProjectionIntent( 127 mUid, mPackageName, mPermanentGrant)); 128 } 129 } catch (RemoteException e) { 130 Log.e(TAG, "Error granting projection permission", e); 131 setResult(RESULT_CANCELED); 132 } finally { 133 if (mDialog != null) { 134 mDialog.dismiss(); 135 } 136 finish(); 137 } 138 } 139 140 @Override 141 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 142 mPermanentGrant = isChecked; 143 } 144 145 private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant) 146 throws RemoteException { 147 IMediaProjection projection = mService.createProjection(uid, packageName, 148 MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant); 149 Intent intent = new Intent(); 150 intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder()); 151 return intent; 152 } 153 154 @Override 155 public void onCancel(DialogInterface dialog) { 156 finish(); 157 } 158 } 159