FileDocCategorySizeDatePackage
AppWidgetHostActivity.javaAPI DocAndroid 1.5 API7001Wed May 06 22:42:02 BST 2009com.android.tests.appwidgethost

AppWidgetHostActivity

public class AppWidgetHostActivity extends android.app.Activity

Fields Summary
static final String
TAG
static final int
DISCOVER_APPWIDGET_REQUEST
static final int
CONFIGURE_APPWIDGET_REQUEST
static final int
HOST_ID
static final String
PENDING_APPWIDGET_ID
android.appwidget.AppWidgetManager
mAppWidgetManager
AppWidgetContainerView
mAppWidgetContainer
View.OnClickListener
mOnClickListener
android.appwidget.AppWidgetHost
mHost
Constructors Summary
public AppWidgetHostActivity()


      
        mAppWidgetManager = AppWidgetManager.getInstance(this);
    
Methods Summary
voidaddAppWidgetView(int appWidgetId, android.appwidget.AppWidgetProviderInfo appWidget)

        // Inflate the AppWidget's RemoteViews
        AppWidgetHostView view = mHost.createView(this, appWidgetId, appWidget);

        // Add it to the list
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mAppWidgetContainer.addView(view, layoutParams);

        registerForContextMenu(view);
    
voidconfigureAppWidget(int requestCode, int appWidgetId, android.content.ComponentName configure)

        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        SharedPreferences.Editor prefs = getPreferences(0).edit();
        prefs.putInt(PENDING_APPWIDGET_ID, appWidgetId);
        prefs.commit();
        startActivityForResult(intent, requestCode);
    
voiddiscoverAppWidget(int requestCode)


       
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mHost.allocateAppWidgetId());
        startActivityForResult(intent, requestCode);
    
voidhandleAppWidgetConfigureResult(int resultCode, android.content.Intent data)

        int appWidgetId = getPreferences(0).getInt(PENDING_APPWIDGET_ID, -1);
        Log.d(TAG, "resultCode=" + resultCode + " appWidgetId=" + appWidgetId);
        if (appWidgetId < 0) {
            Log.w(TAG, "was no preference for PENDING_APPWIDGET_ID");
            return;
        }
        if (resultCode == RESULT_OK) {
            AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
            addAppWidgetView(appWidgetId, appWidget);
        } else {
            mHost.deleteAppWidgetId(appWidgetId);
        }
    
voidhandleAppWidgetPickResult(int resultCode, android.content.Intent intent)

        // BEGIN_INCLUDE(getExtra_EXTRA_APPWIDGET_ID)
        Bundle extras = intent.getExtras();
        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
        // END_INCLUDE(getExtra_EXTRA_APPWIDGET_ID)
        if (resultCode == RESULT_OK) {
            AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

            if (appWidget.configure != null) {
                // configure the AppWidget if we should
                configureAppWidget(CONFIGURE_APPWIDGET_REQUEST, appWidgetId, appWidget.configure);
            } else {
                // just add it as is
                addAppWidgetView(appWidgetId, appWidget);
            }
        } else {
            mHost.deleteAppWidgetId(appWidgetId);
        }
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        switch (requestCode) {
        case DISCOVER_APPWIDGET_REQUEST:
            handleAppWidgetPickResult(resultCode, data);
            break;
        case CONFIGURE_APPWIDGET_REQUEST:
            handleAppWidgetConfigureResult(resultCode, data);
        }
    
public booleanonContextItemSelected(android.view.MenuItem item)

        MyAppWidgetView view = (MyAppWidgetView)item.getMenuInfo();
        switch (item.getItemId()) {
        case R.string.delete_appwidget:
            Log.d(TAG, "delete! " + view.appWidgetId);
            mAppWidgetContainer.removeView(view);
            mHost.deleteAppWidgetId(view.appWidgetId);
            break;
        }

        return true;
    
public voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);
        setContentView(R.layout.appwidget_host);

        findViewById(R.id.add_appwidget).setOnClickListener(mOnClickListener);
        mAppWidgetContainer = (AppWidgetContainerView)findViewById(R.id.appwidget_container);

        if (false) {
            if (false) {
                mHost.deleteHost();
            } else {
                AppWidgetHost.deleteAllHosts();
            }
        }
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, ContextMenu.ContextMenuInfo menuInfo)

        menu.add(ContextMenu.NONE, R.string.delete_appwidget, ContextMenu.NONE,
                R.string.delete_appwidget);
    
protected voidonStart()

        super.onStart();
        mHost.startListening();
    
protected voidonStop()

        super.onStop();
        mHost.stopListening();