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