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