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.content.DialogInterface;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.graphics.Typeface;
26import android.media.projection.IMediaProjection;
27import android.media.projection.IMediaProjectionManager;
28import android.media.projection.MediaProjectionManager;
29import android.os.Bundle;
30import android.os.IBinder;
31import android.os.RemoteException;
32import android.os.ServiceManager;
33import android.text.BidiFormatter;
34import android.text.SpannableString;
35import android.text.TextPaint;
36import android.text.TextUtils;
37import android.text.style.StyleSpan;
38import android.util.Log;
39import android.view.WindowManager;
40import android.widget.CheckBox;
41import android.widget.CompoundButton;
42
43import com.android.systemui.R;
44
45public class MediaProjectionPermissionActivity extends Activity
46        implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener,
47        DialogInterface.OnCancelListener {
48    private static final String TAG = "MediaProjectionPermissionActivity";
49    private static final float MAX_APP_NAME_SIZE_PX = 500f;
50    private static final String ELLIPSIS = "\u2026";
51
52    private boolean mPermanentGrant;
53    private String mPackageName;
54    private int mUid;
55    private IMediaProjectionManager mService;
56
57    private AlertDialog mDialog;
58
59    @Override
60    public void onCreate(Bundle icicle) {
61        super.onCreate(icicle);
62
63        mPackageName = getCallingPackage();
64        IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
65        mService = IMediaProjectionManager.Stub.asInterface(b);
66
67        if (mPackageName == null) {
68            finish();
69            return;
70        }
71
72        PackageManager packageManager = getPackageManager();
73        ApplicationInfo aInfo;
74        try {
75            aInfo = packageManager.getApplicationInfo(mPackageName, 0);
76            mUid = aInfo.uid;
77        } catch (PackageManager.NameNotFoundException e) {
78            Log.e(TAG, "unable to look up package name", e);
79            finish();
80            return;
81        }
82
83        try {
84            if (mService.hasProjectionPermission(mUid, mPackageName)) {
85                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName,
86                        false /*permanentGrant*/));
87                finish();
88                return;
89            }
90        } catch (RemoteException e) {
91            Log.e(TAG, "Error checking projection permissions", e);
92            finish();
93            return;
94        }
95
96        TextPaint paint = new TextPaint();
97        paint.setTextSize(42);
98
99        String label = aInfo.loadLabel(packageManager).toString();
100
101        // If the label contains new line characters it may push the security
102        // message below the fold of the dialog. Labels shouldn't have new line
103        // characters anyways, so just truncate the message the first time one
104        // is seen.
105        final int labelLength = label.length();
106        int offset = 0;
107        while (offset < labelLength) {
108            final int codePoint = label.codePointAt(offset);
109            final int type = Character.getType(codePoint);
110            if (type == Character.LINE_SEPARATOR
111                    || type == Character.CONTROL
112                    || type == Character.PARAGRAPH_SEPARATOR) {
113                label = label.substring(0, offset) + ELLIPSIS;
114                break;
115            }
116            offset += Character.charCount(codePoint);
117        }
118
119        if (label.isEmpty()) {
120            label = mPackageName;
121        }
122
123        String unsanitizedAppName = TextUtils.ellipsize(label,
124                paint, MAX_APP_NAME_SIZE_PX, TextUtils.TruncateAt.END).toString();
125        String appName = BidiFormatter.getInstance().unicodeWrap(unsanitizedAppName);
126
127        String actionText = getString(R.string.media_projection_dialog_text, appName);
128        SpannableString message = new SpannableString(actionText);
129
130        int appNameIndex = actionText.indexOf(appName);
131        if (appNameIndex >= 0) {
132            message.setSpan(new StyleSpan(Typeface.BOLD),
133                    appNameIndex, appNameIndex + appName.length(), 0);
134        }
135
136        mDialog = new AlertDialog.Builder(this)
137                .setIcon(aInfo.loadIcon(packageManager))
138                .setMessage(message)
139                .setPositiveButton(R.string.media_projection_action_text, this)
140                .setNegativeButton(android.R.string.cancel, this)
141                .setView(R.layout.remember_permission_checkbox)
142                .setOnCancelListener(this)
143                .create();
144
145        mDialog.create();
146        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
147
148        ((CheckBox) mDialog.findViewById(R.id.remember)).setOnCheckedChangeListener(this);
149        mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
150
151        mDialog.show();
152    }
153
154    @Override
155    protected void onDestroy() {
156        super.onDestroy();
157        if (mDialog != null) {
158            mDialog.dismiss();
159        }
160    }
161
162    @Override
163    public void onClick(DialogInterface dialog, int which) {
164        try {
165            if (which == AlertDialog.BUTTON_POSITIVE) {
166                setResult(RESULT_OK, getMediaProjectionIntent(
167                        mUid, mPackageName, mPermanentGrant));
168            }
169        } catch (RemoteException e) {
170            Log.e(TAG, "Error granting projection permission", e);
171            setResult(RESULT_CANCELED);
172        } finally {
173            if (mDialog != null) {
174                mDialog.dismiss();
175            }
176            finish();
177        }
178    }
179
180    @Override
181    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
182        mPermanentGrant = isChecked;
183    }
184
185    private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant)
186            throws RemoteException {
187        IMediaProjection projection = mService.createProjection(uid, packageName,
188                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant);
189        Intent intent = new Intent();
190        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
191        return intent;
192    }
193
194    @Override
195    public void onCancel(DialogInterface dialog) {
196        finish();
197    }
198}
199