Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 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 android.app.Activity; |
| 18 | import android.app.AlertDialog; |
| 19 | import android.app.slice.SliceManager; |
| 20 | import android.app.slice.SliceProvider; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.content.DialogInterface.OnClickListener; |
| 23 | import android.content.DialogInterface.OnDismissListener; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.PackageManager.NameNotFoundException; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.util.Log; |
| 29 | import android.widget.CheckBox; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | public class SlicePermissionActivity extends Activity implements OnClickListener, |
| 33 | OnDismissListener { |
| 34 | |
| 35 | private static final String TAG = "SlicePermissionActivity"; |
| 36 | |
| 37 | private CheckBox mAllCheckbox; |
| 38 | |
| 39 | private Uri mUri; |
| 40 | private String mCallingPkg; |
| 41 | private String mProviderPkg; |
| 42 | |
| 43 | @Override |
| 44 | protected void onCreate(Bundle savedInstanceState) { |
| 45 | super.onCreate(savedInstanceState); |
| 46 | |
| 47 | mUri = getIntent().getParcelableExtra(SliceProvider.EXTRA_BIND_URI); |
| 48 | mCallingPkg = getIntent().getStringExtra(SliceProvider.EXTRA_PKG); |
| 49 | mProviderPkg = getIntent().getStringExtra(SliceProvider.EXTRA_PROVIDER_PKG); |
| 50 | |
| 51 | try { |
| 52 | PackageManager pm = getPackageManager(); |
| 53 | CharSequence app1 = pm.getApplicationInfo(mCallingPkg, 0).loadLabel(pm); |
| 54 | CharSequence app2 = pm.getApplicationInfo(mProviderPkg, 0).loadLabel(pm); |
| 55 | AlertDialog dialog = new AlertDialog.Builder(this) |
| 56 | .setTitle(getString(R.string.slice_permission_title, app1, app2)) |
| 57 | .setView(R.layout.slice_permission_request) |
| 58 | .setNegativeButton(R.string.slice_permission_deny, this) |
| 59 | .setPositiveButton(R.string.slice_permission_allow, this) |
| 60 | .setOnDismissListener(this) |
| 61 | .show(); |
| 62 | TextView t1 = dialog.getWindow().getDecorView().findViewById(R.id.text1); |
| 63 | t1.setText(getString(R.string.slice_permission_text_1, app2)); |
| 64 | TextView t2 = dialog.getWindow().getDecorView().findViewById(R.id.text2); |
| 65 | t2.setText(getString(R.string.slice_permission_text_2, app2)); |
| 66 | mAllCheckbox = dialog.getWindow().getDecorView().findViewById( |
| 67 | R.id.slice_permission_checkbox); |
| 68 | mAllCheckbox.setText(getString(R.string.slice_permission_checkbox, app1)); |
| 69 | } catch (NameNotFoundException e) { |
| 70 | Log.e(TAG, "Couldn't find package", e); |
| 71 | finish(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onClick(DialogInterface dialog, int which) { |
| 77 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 78 | getSystemService(SliceManager.class).grantPermissionFromUser(mUri, mCallingPkg, |
| 79 | mAllCheckbox.isChecked()); |
| 80 | } |
| 81 | finish(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onDismiss(DialogInterface dialog) { |
| 86 | finish(); |
| 87 | } |
| 88 | } |