• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
28 import android.util.DisplayMetrics;
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;
45     private ComponentName mComponentName;
46     private String mCallingPackage;
47     private AppWidgetManager mAppWidgetManager;
48 
49     // Indicates whether this activity was closed because of a click
50     private boolean mClicked;
51 
onClick(DialogInterface dialog, int which)52     public void onClick(DialogInterface dialog, int which) {
53         if (which == AlertDialog.BUTTON_POSITIVE) {
54             // By default, set the result to cancelled
55             setResult(RESULT_CANCELED);
56             if (mAppWidgetId != -1 && mComponentName != null && mCallingPackage != null) {
57                 try {
58                     mAppWidgetManager.bindAppWidgetId(mAppWidgetId, mComponentName);
59                     Intent result = new Intent();
60                     result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
61                     setResult(RESULT_OK, result);
62                 } catch (Exception e) {
63                     Log.v("BIND_APPWIDGET", "Error binding widget with id "
64                             + mAppWidgetId + " and component " + mComponentName);
65                 }
66             }
67             boolean alwaysAllowBind = mAlwaysUse.isChecked();
68             if (alwaysAllowBind != mAppWidgetManager.hasBindAppWidgetPermission(mCallingPackage)) {
69                 mAppWidgetManager.setBindAppWidgetPermission(mCallingPackage, alwaysAllowBind);
70             }
71         }
72         finish();
73     }
74 
onDestroy()75     protected void onDestroy() {
76         if (!mClicked) {
77             setResult(RESULT_CANCELED);
78             finish();
79         }
80         super.onDestroy();
81     }
82 
onCreate(Bundle savedInstanceState)83     protected void onCreate(Bundle savedInstanceState) {
84         super.onCreate(savedInstanceState);
85         Intent intent = getIntent();
86         CharSequence label = "";
87         if (intent != null) {
88             try {
89                 mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
90                 mComponentName = (ComponentName)
91                         intent.getParcelableExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER);
92                 mCallingPackage = getCallingPackage();
93                 PackageManager pm = getPackageManager();
94                 ApplicationInfo ai = pm.getApplicationInfo(mCallingPackage, 0);
95                 label = pm.getApplicationLabel(ai);
96             } catch (Exception e) {
97                 mAppWidgetId = -1;
98                 mComponentName = null;
99                 mCallingPackage = null;
100                 Log.v("BIND_APPWIDGET", "Error getting parameters");
101                 setResult(RESULT_CANCELED);
102                 finish();
103                 return;
104             }
105         }
106         AlertController.AlertParams ap = mAlertParams;
107         ap.mTitle = getString(R.string.allow_bind_app_widget_activity_allow_bind_title);
108         ap.mMessage = getString(R.string.allow_bind_app_widget_activity_allow_bind, label);
109         ap.mPositiveButtonText = getString(R.string.create);
110         ap.mNegativeButtonText = getString(android.R.string.cancel);
111         ap.mPositiveButtonListener = this;
112         ap.mNegativeButtonListener = this;
113         LayoutInflater inflater =
114                 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115         ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
116         mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
117         mAlwaysUse.setText(getString(R.string.allow_bind_app_widget_activity_always_allow_bind, label));
118 
119         mAlwaysUse.setPadding(mAlwaysUse.getPaddingLeft(),
120                 mAlwaysUse.getPaddingTop(),
121                 mAlwaysUse.getPaddingRight(),
122                 (int) (mAlwaysUse.getPaddingBottom() +
123                         getResources().getDimension(R.dimen.bind_app_widget_dialog_checkbox_bottom_padding)));
124 
125         mAppWidgetManager = AppWidgetManager.getInstance(this);
126         mAlwaysUse.setChecked(mAppWidgetManager.hasBindAppWidgetPermission(mCallingPackage));
127 
128         setupAlert();
129     }
130 }
131