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 android.app.Activity;
20import android.app.AlertDialog;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.media.projection.MediaProjectionManager;
28import android.media.projection.IMediaProjectionManager;
29import android.media.projection.IMediaProjection;
30import android.os.Bundle;
31import android.os.IBinder;
32import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.util.Log;
35import android.view.LayoutInflater;
36import android.view.WindowManager;
37import android.widget.CheckBox;
38import android.widget.CompoundButton;
39import android.widget.TextView;
40
41import com.android.internal.app.AlertActivity;
42import com.android.internal.app.AlertController;
43import com.android.systemui.R;
44import com.android.systemui.statusbar.phone.SystemUIDialog;
45
46public 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        mDialog.dismiss();
118    }
119
120    @Override
121    public void onClick(DialogInterface dialog, int which) {
122        try {
123            if (which == AlertDialog.BUTTON_POSITIVE) {
124                setResult(RESULT_OK, getMediaProjectionIntent(
125                        mUid, mPackageName, mPermanentGrant));
126            }
127        } catch (RemoteException e) {
128            Log.e(TAG, "Error granting projection permission", e);
129            setResult(RESULT_CANCELED);
130        } finally {
131            mDialog.dismiss();
132            finish();
133        }
134    }
135
136    @Override
137    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
138        mPermanentGrant = isChecked;
139    }
140
141    private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant)
142            throws RemoteException {
143        IMediaProjection projection = mService.createProjection(uid, packageName,
144                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant);
145        Intent intent = new Intent();
146        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
147        return intent;
148    }
149
150    @Override
151    public void onCancel(DialogInterface dialog) {
152        finish();
153    }
154}
155