1 /* 2 * Copyright (C) 2008 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.tests.appwidgethost; 18 19 import android.app.Activity; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.appwidget.AppWidgetHost; 25 import android.appwidget.AppWidgetHostView; 26 import android.appwidget.AppWidgetProviderInfo; 27 import android.appwidget.AppWidgetManager; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.view.ContextMenu; 31 import android.view.MenuItem; 32 import android.view.View; 33 import android.widget.LinearLayout; 34 35 public class AppWidgetHostActivity extends Activity 36 { 37 static final String TAG = "AppWidgetHostActivity"; 38 39 static final int DISCOVER_APPWIDGET_REQUEST = 1; 40 static final int CONFIGURE_APPWIDGET_REQUEST = 2; 41 static final int HOST_ID = 1234; 42 43 static final String PENDING_APPWIDGET_ID = "pending_appwidget"; 44 45 AppWidgetManager mAppWidgetManager; 46 AppWidgetContainerView mAppWidgetContainer; 47 48 public AppWidgetHostActivity() { 49 } 50 51 @Override 52 public void onCreate(Bundle icicle) { 53 super.onCreate(icicle); 54 mAppWidgetManager = AppWidgetManager.getInstance(this); 55 56 setContentView(R.layout.appwidget_host); 57 58 mHost = new AppWidgetHost(this, HOST_ID) { 59 protected AppWidgetHostView onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { 60 return new MyAppWidgetView(appWidgetId); 61 } 62 }; 63 64 65 findViewById(R.id.add_appwidget).setOnClickListener(mOnClickListener); 66 mAppWidgetContainer = (AppWidgetContainerView)findViewById(R.id.appwidget_container); 67 68 if (false) { 69 if (false) { 70 mHost.deleteHost(); 71 } else { 72 AppWidgetHost.deleteAllHosts(); 73 } 74 } 75 } 76 77 View.OnClickListener mOnClickListener = new View.OnClickListener() { 78 public void onClick(View v) { 79 discoverAppWidget(DISCOVER_APPWIDGET_REQUEST); 80 } 81 }; 82 83 void discoverAppWidget(int requestCode) { 84 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); 85 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mHost.allocateAppWidgetId()); 86 startActivityForResult(intent, requestCode); 87 } 88 89 void configureAppWidget(int requestCode, int appWidgetId, ComponentName configure) { 90 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 91 intent.setComponent(configure); 92 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 93 SharedPreferences.Editor prefs = getPreferences(0).edit(); 94 prefs.putInt(PENDING_APPWIDGET_ID, appWidgetId); 95 prefs.commit(); 96 startActivityForResult(intent, requestCode); 97 } 98 99 void handleAppWidgetPickResult(int resultCode, Intent intent) { 100 // BEGIN_INCLUDE(getExtra_EXTRA_APPWIDGET_ID) 101 Bundle extras = intent.getExtras(); 102 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); 103 // END_INCLUDE(getExtra_EXTRA_APPWIDGET_ID) 104 if (resultCode == RESULT_OK) { 105 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 106 107 if (appWidget.configure != null) { 108 // configure the AppWidget if we should 109 configureAppWidget(CONFIGURE_APPWIDGET_REQUEST, appWidgetId, appWidget.configure); 110 } else { 111 // just add it as is 112 addAppWidgetView(appWidgetId, appWidget); 113 } 114 } else { 115 mHost.deleteAppWidgetId(appWidgetId); 116 } 117 } 118 119 void handleAppWidgetConfigureResult(int resultCode, Intent data) { 120 int appWidgetId = getPreferences(0).getInt(PENDING_APPWIDGET_ID, -1); 121 Log.d(TAG, "resultCode=" + resultCode + " appWidgetId=" + appWidgetId); 122 if (appWidgetId < 0) { 123 Log.w(TAG, "was no preference for PENDING_APPWIDGET_ID"); 124 return; 125 } 126 if (resultCode == RESULT_OK) { 127 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 128 addAppWidgetView(appWidgetId, appWidget); 129 } else { 130 mHost.deleteAppWidgetId(appWidgetId); 131 } 132 } 133 134 void addAppWidgetView(int appWidgetId, AppWidgetProviderInfo appWidget) { 135 // Inflate the AppWidget's RemoteViews 136 AppWidgetHostView view = mHost.createView(this, appWidgetId, appWidget); 137 138 // Add it to the list 139 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 140 LinearLayout.LayoutParams.MATCH_PARENT, 141 LinearLayout.LayoutParams.WRAP_CONTENT); 142 mAppWidgetContainer.addView(view, layoutParams); 143 144 registerForContextMenu(view); 145 } 146 147 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 148 switch (requestCode) { 149 case DISCOVER_APPWIDGET_REQUEST: 150 handleAppWidgetPickResult(resultCode, data); 151 break; 152 case CONFIGURE_APPWIDGET_REQUEST: 153 handleAppWidgetConfigureResult(resultCode, data); 154 } 155 } 156 157 protected void onStart() { 158 super.onStart(); 159 mHost.startListening(); 160 } 161 162 protected void onStop() { 163 super.onStop(); 164 mHost.stopListening(); 165 } 166 167 public void onCreateContextMenu(ContextMenu menu, View v, 168 ContextMenu.ContextMenuInfo menuInfo) { 169 menu.add(ContextMenu.NONE, R.string.delete_appwidget, ContextMenu.NONE, 170 R.string.delete_appwidget); 171 } 172 173 public boolean onContextItemSelected(MenuItem item) { 174 MyAppWidgetView view = (MyAppWidgetView)item.getMenuInfo(); 175 switch (item.getItemId()) { 176 case R.string.delete_appwidget: 177 Log.d(TAG, "delete! " + view.appWidgetId); 178 mAppWidgetContainer.removeView(view); 179 mHost.deleteAppWidgetId(view.appWidgetId); 180 break; 181 } 182 183 return true; 184 } 185 186 class MyAppWidgetView extends AppWidgetHostView implements ContextMenu.ContextMenuInfo { 187 int appWidgetId; 188 189 MyAppWidgetView(int appWidgetId) { 190 super(AppWidgetHostActivity.this); 191 this.appWidgetId = appWidgetId; 192 } 193 194 public ContextMenu.ContextMenuInfo getContextMenuInfo() { 195 return this; 196 } 197 } 198 199 AppWidgetHost mHost; 200 } 201 202 203