1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui;
16 
17 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.slice.SliceManager;
22 import android.app.slice.SliceProvider;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.DialogInterface.OnDismissListener;
26 import android.content.pm.PackageItemInfo;
27 import android.content.pm.PackageManager;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.text.BidiFormatter;
32 import android.util.Log;
33 import android.widget.CheckBox;
34 import android.widget.TextView;
35 
36 public class SlicePermissionActivity extends Activity implements OnClickListener,
37         OnDismissListener {
38 
39     private static final String TAG = "SlicePermissionActivity";
40 
41     private CheckBox mAllCheckbox;
42 
43     private Uri mUri;
44     private String mCallingPkg;
45     private String mProviderPkg;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         mUri = getIntent().getParcelableExtra(SliceProvider.EXTRA_BIND_URI);
52         mCallingPkg = getIntent().getStringExtra(SliceProvider.EXTRA_PKG);
53         mProviderPkg = getIntent().getStringExtra(SliceProvider.EXTRA_PROVIDER_PKG);
54 
55         try {
56             PackageManager pm = getPackageManager();
57             CharSequence app1 = BidiFormatter.getInstance().unicodeWrap(pm.getApplicationInfo(
58                     mCallingPkg, 0).loadSafeLabel(pm, PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX,
59                     PackageItemInfo.SAFE_LABEL_FLAG_TRIM
60                             | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE).toString());
61             CharSequence app2 = BidiFormatter.getInstance().unicodeWrap(pm.getApplicationInfo(
62                     mProviderPkg, 0).loadSafeLabel(pm, PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX,
63                     PackageItemInfo.SAFE_LABEL_FLAG_TRIM
64                             | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE).toString());
65             AlertDialog dialog = new AlertDialog.Builder(this)
66                     .setTitle(getString(R.string.slice_permission_title, app1, app2))
67                     .setView(R.layout.slice_permission_request)
68                     .setNegativeButton(R.string.slice_permission_deny, this)
69                     .setPositiveButton(R.string.slice_permission_allow, this)
70                     .setOnDismissListener(this)
71                     .create();
72             dialog.getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
73             dialog.show();
74             TextView t1 = dialog.getWindow().getDecorView().findViewById(R.id.text1);
75             t1.setText(getString(R.string.slice_permission_text_1, app2));
76             TextView t2 = dialog.getWindow().getDecorView().findViewById(R.id.text2);
77             t2.setText(getString(R.string.slice_permission_text_2, app2));
78             mAllCheckbox = dialog.getWindow().getDecorView().findViewById(
79                     R.id.slice_permission_checkbox);
80             mAllCheckbox.setText(getString(R.string.slice_permission_checkbox, app1));
81         } catch (NameNotFoundException e) {
82             Log.e(TAG, "Couldn't find package", e);
83             finish();
84         }
85     }
86 
87     @Override
onClick(DialogInterface dialog, int which)88     public void onClick(DialogInterface dialog, int which) {
89         if (which == DialogInterface.BUTTON_POSITIVE) {
90             getSystemService(SliceManager.class).grantPermissionFromUser(mUri, mCallingPkg,
91                     mAllCheckbox.isChecked());
92         }
93         finish();
94     }
95 
96     @Override
onDismiss(DialogInterface dialog)97     public void onDismiss(DialogInterface dialog) {
98         finish();
99     }
100 }
101