1 /* 2 * Copyright (C) 2021 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.fakesystemapp.systemui; 18 19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; 20 21 import android.app.Activity; 22 import android.app.AlertDialog; 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.IMediaProjection; 28 import android.media.projection.IMediaProjectionManager; 29 import android.media.projection.MediaProjectionManager; 30 import android.os.Bundle; 31 import android.os.IBinder; 32 import android.os.RemoteException; 33 import android.os.ServiceManager; 34 import android.text.TextPaint; 35 import android.util.Log; 36 import android.view.Display; 37 import android.view.Window; 38 import android.view.WindowManager; 39 40 import com.android.fakesystemapp.R; 41 42 /** 43 * A fork of systemui's MediaProjectionPermissionActivity 44 * 45 * Modifications are marked with SLIM-CHANGED 46 */ 47 public class SlimMediaProjectionPermissionActivity extends Activity implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { 48 private static final String TAG = "MediaProjectionPermissionActivity"; 49 50 private String mPackageName; 51 private int mUid; 52 private IMediaProjectionManager mService; 53 54 private AlertDialog mDialog; 55 56 @Override onCreate(Bundle icicle)57 public void onCreate(Bundle icicle) { 58 super.onCreate(icicle); 59 60 mPackageName = getCallingPackage(); 61 IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE); 62 mService = IMediaProjectionManager.Stub.asInterface(b); 63 64 if (mPackageName == null) { 65 finish(); 66 return; 67 } 68 69 PackageManager packageManager = getPackageManager(); 70 ApplicationInfo aInfo; 71 try { 72 aInfo = packageManager.getApplicationInfo(mPackageName, 0); 73 mUid = aInfo.uid; 74 } catch (PackageManager.NameNotFoundException e) { 75 Log.e(TAG, "unable to look up package name", e); 76 finish(); 77 return; 78 } 79 80 try { 81 if (mService.hasProjectionPermission(mUid, mPackageName)) { 82 setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName)); 83 finish(); 84 return; 85 } 86 } catch (RemoteException e) { 87 Log.e(TAG, "Error checking projection permissions", e); 88 finish(); 89 return; 90 } 91 92 TextPaint paint = new TextPaint(); 93 paint.setTextSize(42); 94 95 96 CharSequence dialogText = null; 97 CharSequence dialogTitle = null; 98 // SLIM-CHANGED - simplify text and title 99 100 dialogText = getString(R.string.media_projection_dialog_service_text); 101 dialogTitle = getString(R.string.media_projection_dialog_service_title); 102 103 mDialog = new AlertDialog.Builder(this) 104 .setTitle(dialogTitle) 105 .setIcon(R.drawable.ic_media_projection_permission) 106 .setMessage(dialogText) 107 .setPositiveButton(R.string.media_projection_action_text, this) 108 .setNegativeButton(android.R.string.cancel, this) 109 .setOnCancelListener(this) 110 .create(); 111 112 mDialog.create(); 113 mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true); 114 115 final Window w = mDialog.getWindow(); 116 w.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 117 w.addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 118 119 mDialog.show(); 120 } 121 122 @Override onDestroy()123 protected void onDestroy() { 124 super.onDestroy(); 125 if (mDialog != null) { 126 mDialog.dismiss(); 127 } 128 } 129 130 @Override onClick(DialogInterface dialog, int which)131 public void onClick(DialogInterface dialog, int which) { 132 try { 133 if (which == AlertDialog.BUTTON_POSITIVE) { 134 setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName)); 135 } 136 } catch (RemoteException e) { 137 Log.e(TAG, "Error granting projection permission", e); 138 setResult(RESULT_CANCELED); 139 } finally { 140 if (mDialog != null) { 141 mDialog.dismiss(); 142 } 143 finish(); 144 } 145 } 146 getMediaProjectionIntent(int uid, String packageName)147 private Intent getMediaProjectionIntent(int uid, String packageName) 148 throws RemoteException { 149 IMediaProjection projection = mService.createProjection(uid, packageName, 150 MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */, Display.DEFAULT_DISPLAY); 151 Intent intent = new Intent(); 152 intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder()); 153 return intent; 154 } 155 156 @Override onCancel(DialogInterface dialog)157 public void onCancel(DialogInterface dialog) { 158 finish(); 159 } 160 161 } 162 163