blob: 2f98ab30556259cfe5fbf5c1b57f55ff92816510 [file] [log] [blame]
Michael Jurkac9029d92012-04-20 01:47:44 -07001/*
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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.appwidget.AppWidgetManager;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.os.Bundle;
Svet Ganovc15c0c02014-08-01 15:06:43 -070028import android.os.UserHandle;
Michael Jurkac9029d92012-04-20 01:47:44 -070029import android.util.Log;
30import android.view.LayoutInflater;
31import android.widget.CheckBox;
32
33import com.android.internal.app.AlertActivity;
34import 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 */
40public class AllowBindAppWidgetActivity extends AlertActivity implements
41 DialogInterface.OnClickListener {
42
43 private CheckBox mAlwaysUse;
44 private int mAppWidgetId;
Sunny Goyale78499d2017-01-23 10:57:58 -080045 private Bundle mBindOptions;
Svet Ganovc15c0c02014-08-01 15:06:43 -070046 private UserHandle mProfile;
Michael Jurkac9029d92012-04-20 01:47:44 -070047 private ComponentName mComponentName;
48 private String mCallingPackage;
49 private AppWidgetManager mAppWidgetManager;
50
51 // Indicates whether this activity was closed because of a click
52 private boolean mClicked;
53
54 public void onClick(DialogInterface dialog, int which) {
55 if (which == AlertDialog.BUTTON_POSITIVE) {
56 // By default, set the result to cancelled
57 setResult(RESULT_CANCELED);
58 if (mAppWidgetId != -1 && mComponentName != null && mCallingPackage != null) {
59 try {
Svet Ganovc15c0c02014-08-01 15:06:43 -070060 final boolean bound = mAppWidgetManager.bindAppWidgetIdIfAllowed(mAppWidgetId,
Sunny Goyale78499d2017-01-23 10:57:58 -080061 mProfile, mComponentName, mBindOptions);
Svet Ganovc15c0c02014-08-01 15:06:43 -070062 if (bound) {
63 Intent result = new Intent();
64 result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
65 setResult(RESULT_OK, result);
66 }
Michael Jurkac9029d92012-04-20 01:47:44 -070067 } catch (Exception e) {
68 Log.v("BIND_APPWIDGET", "Error binding widget with id "
69 + mAppWidgetId + " and component " + mComponentName);
70 }
Svet Ganovc15c0c02014-08-01 15:06:43 -070071
72 final boolean alwaysAllowBind = mAlwaysUse.isChecked();
73 if (alwaysAllowBind != mAppWidgetManager.hasBindAppWidgetPermission(
74 mCallingPackage)) {
75 mAppWidgetManager.setBindAppWidgetPermission(mCallingPackage,
76 alwaysAllowBind);
77 }
Michael Jurkac9029d92012-04-20 01:47:44 -070078 }
79 }
80 finish();
81 }
82
Svet Ganovc15c0c02014-08-01 15:06:43 -070083 protected void onPause() {
84 if (isDestroyed() && !mClicked) {
Michael Jurkac9029d92012-04-20 01:47:44 -070085 setResult(RESULT_CANCELED);
Michael Jurkac9029d92012-04-20 01:47:44 -070086 }
87 super.onDestroy();
88 }
89
90 protected void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 Intent intent = getIntent();
93 CharSequence label = "";
94 if (intent != null) {
95 try {
96 mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Svet Ganovc15c0c02014-08-01 15:06:43 -070097 mProfile = intent.getParcelableExtra(
98 AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
99 if (mProfile == null) {
100 mProfile = android.os.Process.myUserHandle();
101 }
102 mComponentName =
Michael Jurkac9029d92012-04-20 01:47:44 -0700103 intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER);
Sunny Goyale78499d2017-01-23 10:57:58 -0800104 mBindOptions =
105 intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
Michael Jurkac9029d92012-04-20 01:47:44 -0700106 mCallingPackage = getCallingPackage();
107 PackageManager pm = getPackageManager();
108 ApplicationInfo ai = pm.getApplicationInfo(mCallingPackage, 0);
109 label = pm.getApplicationLabel(ai);
110 } catch (Exception e) {
111 mAppWidgetId = -1;
112 mComponentName = null;
113 mCallingPackage = null;
114 Log.v("BIND_APPWIDGET", "Error getting parameters");
115 setResult(RESULT_CANCELED);
116 finish();
117 return;
118 }
119 }
120 AlertController.AlertParams ap = mAlertParams;
121 ap.mTitle = getString(R.string.allow_bind_app_widget_activity_allow_bind_title);
122 ap.mMessage = getString(R.string.allow_bind_app_widget_activity_allow_bind, label);
123 ap.mPositiveButtonText = getString(R.string.create);
124 ap.mNegativeButtonText = getString(android.R.string.cancel);
125 ap.mPositiveButtonListener = this;
126 ap.mNegativeButtonListener = this;
127 LayoutInflater inflater =
128 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
129 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
130 mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
131 mAlwaysUse.setText(getString(R.string.allow_bind_app_widget_activity_always_allow_bind, label));
132
133 mAlwaysUse.setPadding(mAlwaysUse.getPaddingLeft(),
134 mAlwaysUse.getPaddingTop(),
135 mAlwaysUse.getPaddingRight(),
136 (int) (mAlwaysUse.getPaddingBottom() +
137 getResources().getDimension(R.dimen.bind_app_widget_dialog_checkbox_bottom_padding)));
138
139 mAppWidgetManager = AppWidgetManager.getInstance(this);
Svet Ganovc15c0c02014-08-01 15:06:43 -0700140 mAlwaysUse.setChecked(mAppWidgetManager.hasBindAppWidgetPermission(mCallingPackage,
141 mProfile.getIdentifier()));
Michael Jurkac9029d92012-04-20 01:47:44 -0700142
143 setupAlert();
144 }
145}