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 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; 28import android.os.UserHandle; 29import 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; 45 private Bundle mBindOptions; 46 private UserHandle mProfile; 47 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 mClicked = true; 56 if (which == AlertDialog.BUTTON_POSITIVE) { 57 if (mAppWidgetId != -1 && mComponentName != null && mCallingPackage != null) { 58 try { 59 final boolean bound = mAppWidgetManager.bindAppWidgetIdIfAllowed(mAppWidgetId, 60 mProfile, mComponentName, mBindOptions); 61 if (bound) { 62 Intent result = new Intent(); 63 result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 64 setResult(RESULT_OK, result); 65 } 66 } catch (Exception e) { 67 Log.v("BIND_APPWIDGET", "Error binding widget with id " 68 + mAppWidgetId + " and component " + mComponentName); 69 } 70 71 final boolean alwaysAllowBind = mAlwaysUse.isChecked(); 72 if (alwaysAllowBind != mAppWidgetManager.hasBindAppWidgetPermission( 73 mCallingPackage)) { 74 mAppWidgetManager.setBindAppWidgetPermission(mCallingPackage, 75 alwaysAllowBind); 76 } 77 } 78 } 79 finish(); 80 } 81 82 @Override 83 protected void onPause() { 84 if (!mClicked) { // RESULT_CANCELED 85 finish(); 86 } 87 super.onPause(); 88 } 89 90 @Override 91 protected void onCreate(Bundle savedInstanceState) { 92 super.onCreate(savedInstanceState); 93 setResult(RESULT_CANCELED); // By default, set the result to cancelled 94 Intent intent = getIntent(); 95 CharSequence label = ""; 96 if (intent != null) { 97 try { 98 mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 99 mProfile = intent.getParcelableExtra( 100 AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE); 101 if (mProfile == null) { 102 mProfile = android.os.Process.myUserHandle(); 103 } 104 mComponentName = 105 intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER); 106 mBindOptions = 107 intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS); 108 mCallingPackage = getCallingPackage(); 109 PackageManager pm = getPackageManager(); 110 ApplicationInfo ai = pm.getApplicationInfo(mCallingPackage, 0); 111 label = pm.getApplicationLabel(ai); 112 } catch (Exception e) { 113 mAppWidgetId = -1; 114 mComponentName = null; 115 mCallingPackage = null; 116 Log.v("BIND_APPWIDGET", "Error getting parameters"); 117 finish(); 118 return; 119 } 120 } 121 AlertController.AlertParams ap = mAlertParams; 122 ap.mTitle = getString(R.string.allow_bind_app_widget_activity_allow_bind_title); 123 ap.mMessage = getString(R.string.allow_bind_app_widget_activity_allow_bind, label); 124 ap.mPositiveButtonText = getString(R.string.create); 125 ap.mNegativeButtonText = getString(android.R.string.cancel); 126 ap.mPositiveButtonListener = this; 127 ap.mNegativeButtonListener = this; 128 LayoutInflater inflater = 129 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 130 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); 131 mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse); 132 mAlwaysUse.setText(getString(R.string.allow_bind_app_widget_activity_always_allow_bind, label)); 133 134 mAlwaysUse.setPadding(mAlwaysUse.getPaddingLeft(), 135 mAlwaysUse.getPaddingTop(), 136 mAlwaysUse.getPaddingRight(), 137 (int) (mAlwaysUse.getPaddingBottom() + 138 getResources().getDimension(R.dimen.bind_app_widget_dialog_checkbox_bottom_padding))); 139 140 mAppWidgetManager = AppWidgetManager.getInstance(this); 141 mAlwaysUse.setChecked(mAppWidgetManager.hasBindAppWidgetPermission(mCallingPackage, 142 mProfile.getIdentifier())); 143 144 setupAlert(); 145 } 146} 147