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.AlertDialog;
20import android.app.PendingIntent;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.media.projection.MediaProjectionManager;
27import android.media.projection.IMediaProjectionManager;
28import android.media.projection.IMediaProjection;
29import android.os.Bundle;
30import android.os.IBinder;
31import android.os.RemoteException;
32import android.os.ServiceManager;
33import android.util.Log;
34import android.view.LayoutInflater;
35import android.widget.CheckBox;
36import android.widget.CompoundButton;
37import android.widget.TextView;
38
39import com.android.internal.app.AlertActivity;
40import com.android.internal.app.AlertController;
41import com.android.systemui.R;
42
43public class MediaProjectionPermissionActivity extends AlertActivity
44        implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
45    private static final String TAG = "MediaProjectionPermissionActivity";
46
47    private boolean mPermanentGrant;
48    private String mPackageName;
49    private int mUid;
50    private IMediaProjectionManager mService;
51
52    @Override
53    public void onCreate(Bundle icicle) {
54        super.onCreate(icicle);
55
56        Intent intent = getIntent();
57        mPackageName = getCallingPackage();
58        IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
59        mService = IMediaProjectionManager.Stub.asInterface(b);
60
61        if (mPackageName == null) {
62            finish();
63            return;
64        }
65
66        PackageManager packageManager = getPackageManager();
67        ApplicationInfo aInfo;
68        try {
69            aInfo = packageManager.getApplicationInfo(mPackageName, 0);
70            mUid = aInfo.uid;
71        } catch (PackageManager.NameNotFoundException e) {
72            Log.e(TAG, "unable to look up package name", e);
73            finish();
74            return;
75        }
76
77        try {
78            if (mService.hasProjectionPermission(mUid, mPackageName)) {
79                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName,
80                        false /*permanentGrant*/));
81                finish();
82                return;
83            }
84        } catch (RemoteException e) {
85            Log.e(TAG, "Error checking projection permissions", e);
86            finish();
87            return;
88        }
89
90        String appName = aInfo.loadLabel(packageManager).toString();
91
92        final AlertController.AlertParams ap = mAlertParams;
93        ap.mIcon = aInfo.loadIcon(packageManager);
94        ap.mMessage = getString(R.string.media_projection_dialog_text, appName);
95        ap.mPositiveButtonText = getString(R.string.media_projection_action_text);
96        ap.mNegativeButtonText = getString(android.R.string.cancel);
97        ap.mPositiveButtonListener = this;
98        ap.mNegativeButtonListener = this;
99
100        // add "always use" checkbox
101        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
102        ap.mView = inflater.inflate(R.layout.remember_permission_checkbox, null);
103        CheckBox rememberPermissionCheckbox =
104                (CheckBox)ap.mView.findViewById(R.id.remember);
105        rememberPermissionCheckbox.setOnCheckedChangeListener(this);
106
107        setupAlert();
108    }
109
110    @Override
111    public void onClick(DialogInterface dialog, int which) {
112        try {
113            if (which == AlertDialog.BUTTON_POSITIVE) {
114                setResult(RESULT_OK, getMediaProjectionIntent(
115                        mUid, mPackageName, mPermanentGrant));
116            }
117        } catch (RemoteException e) {
118            Log.e(TAG, "Error granting projection permission", e);
119            setResult(RESULT_CANCELED);
120        } finally {
121            finish();
122        }
123    }
124
125    @Override
126    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
127        mPermanentGrant = isChecked;
128    }
129
130    private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant)
131            throws RemoteException {
132        IMediaProjection projection = mService.createProjection(uid, packageName,
133                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant);
134        Intent intent = new Intent();
135        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
136        return intent;
137    }
138}
139