Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | package com.android.settings; |
| 18 | |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 19 | import android.appwidget.AppWidgetManager; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.os.Bundle; |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.widget.CheckBox; |
| 31 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 32 | import androidx.appcompat.app.AlertDialog; |
| 33 | |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 34 | import com.android.internal.app.AlertActivity; |
| 35 | import com.android.internal.app.AlertController; |
| 36 | |
| 37 | /** |
| 38 | * This activity is displayed when an app launches the BIND_APPWIDGET intent. This allows apps |
| 39 | * that don't have the BIND_APPWIDGET permission to bind specific widgets. |
| 40 | */ |
| 41 | public class AllowBindAppWidgetActivity extends AlertActivity implements |
| 42 | DialogInterface.OnClickListener { |
| 43 | |
| 44 | private CheckBox mAlwaysUse; |
| 45 | private int mAppWidgetId; |
Sunny Goyal | e78499d | 2017-01-23 10:57:58 -0800 | [diff] [blame] | 46 | private Bundle mBindOptions; |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 47 | private UserHandle mProfile; |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 48 | private ComponentName mComponentName; |
| 49 | private String mCallingPackage; |
| 50 | private AppWidgetManager mAppWidgetManager; |
| 51 | |
| 52 | // Indicates whether this activity was closed because of a click |
| 53 | private boolean mClicked; |
| 54 | |
| 55 | public void onClick(DialogInterface dialog, int which) { |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 56 | mClicked = true; |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 57 | if (which == AlertDialog.BUTTON_POSITIVE) { |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 58 | if (mAppWidgetId != -1 && mComponentName != null && mCallingPackage != null) { |
| 59 | try { |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 60 | final boolean bound = mAppWidgetManager.bindAppWidgetIdIfAllowed(mAppWidgetId, |
Sunny Goyal | e78499d | 2017-01-23 10:57:58 -0800 | [diff] [blame] | 61 | mProfile, mComponentName, mBindOptions); |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 62 | if (bound) { |
| 63 | Intent result = new Intent(); |
| 64 | result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); |
| 65 | setResult(RESULT_OK, result); |
| 66 | } |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 67 | } catch (Exception e) { |
| 68 | Log.v("BIND_APPWIDGET", "Error binding widget with id " |
| 69 | + mAppWidgetId + " and component " + mComponentName); |
| 70 | } |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 71 | |
| 72 | final boolean alwaysAllowBind = mAlwaysUse.isChecked(); |
| 73 | if (alwaysAllowBind != mAppWidgetManager.hasBindAppWidgetPermission( |
| 74 | mCallingPackage)) { |
| 75 | mAppWidgetManager.setBindAppWidgetPermission(mCallingPackage, |
| 76 | alwaysAllowBind); |
| 77 | } |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | finish(); |
| 81 | } |
| 82 | |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 83 | @Override |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 84 | protected void onPause() { |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 85 | if (!mClicked) { // RESULT_CANCELED |
| 86 | finish(); |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 87 | } |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 88 | super.onPause(); |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 91 | @Override |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 92 | protected void onCreate(Bundle savedInstanceState) { |
| 93 | super.onCreate(savedInstanceState); |
Mikhail Malakhov | bd5729b | 2016-10-20 22:45:18 +0300 | [diff] [blame] | 94 | setResult(RESULT_CANCELED); // By default, set the result to cancelled |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 95 | Intent intent = getIntent(); |
| 96 | CharSequence label = ""; |
| 97 | if (intent != null) { |
| 98 | try { |
| 99 | mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 100 | mProfile = intent.getParcelableExtra( |
| 101 | AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE); |
| 102 | if (mProfile == null) { |
| 103 | mProfile = android.os.Process.myUserHandle(); |
| 104 | } |
| 105 | mComponentName = |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 106 | intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER); |
Sunny Goyal | e78499d | 2017-01-23 10:57:58 -0800 | [diff] [blame] | 107 | mBindOptions = |
| 108 | intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS); |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 109 | mCallingPackage = getCallingPackage(); |
| 110 | PackageManager pm = getPackageManager(); |
| 111 | ApplicationInfo ai = pm.getApplicationInfo(mCallingPackage, 0); |
| 112 | label = pm.getApplicationLabel(ai); |
| 113 | } catch (Exception e) { |
| 114 | mAppWidgetId = -1; |
| 115 | mComponentName = null; |
| 116 | mCallingPackage = null; |
| 117 | Log.v("BIND_APPWIDGET", "Error getting parameters"); |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 118 | finish(); |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | AlertController.AlertParams ap = mAlertParams; |
| 123 | ap.mTitle = getString(R.string.allow_bind_app_widget_activity_allow_bind_title); |
| 124 | ap.mMessage = getString(R.string.allow_bind_app_widget_activity_allow_bind, label); |
| 125 | ap.mPositiveButtonText = getString(R.string.create); |
| 126 | ap.mNegativeButtonText = getString(android.R.string.cancel); |
| 127 | ap.mPositiveButtonListener = this; |
| 128 | ap.mNegativeButtonListener = this; |
| 129 | LayoutInflater inflater = |
| 130 | (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 131 | ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); |
| 132 | mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse); |
| 133 | mAlwaysUse.setText(getString(R.string.allow_bind_app_widget_activity_always_allow_bind, label)); |
| 134 | |
| 135 | mAlwaysUse.setPadding(mAlwaysUse.getPaddingLeft(), |
| 136 | mAlwaysUse.getPaddingTop(), |
| 137 | mAlwaysUse.getPaddingRight(), |
| 138 | (int) (mAlwaysUse.getPaddingBottom() + |
| 139 | getResources().getDimension(R.dimen.bind_app_widget_dialog_checkbox_bottom_padding))); |
| 140 | |
| 141 | mAppWidgetManager = AppWidgetManager.getInstance(this); |
Svet Ganov | c15c0c0 | 2014-08-01 15:06:43 -0700 | [diff] [blame] | 142 | mAlwaysUse.setChecked(mAppWidgetManager.hasBindAppWidgetPermission(mCallingPackage, |
| 143 | mProfile.getIdentifier())); |
Michael Jurka | c9029d9 | 2012-04-20 01:47:44 -0700 | [diff] [blame] | 144 | |
| 145 | setupAlert(); |
| 146 | } |
| 147 | } |