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