TestAppWidgetProvider.java revision c39a6e0c51e182338deb8b63d07933b585134929
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.appwidgetprovider;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.appwidget.AppWidgetManager;
24import android.os.Bundle;
25import android.os.SystemClock;
26import android.util.Log;
27import android.widget.RemoteViews;
28
29public class TestAppWidgetProvider extends BroadcastReceiver {
30
31    static final String TAG = "TestAppWidgetProvider";
32
33    public void onReceive(Context context, Intent intent) {
34        String action = intent.getAction();
35        Log.d(TAG, "intent=" + intent);
36
37        if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
38            Log.d(TAG, "ENABLED");
39        }
40        else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
41            Log.d(TAG, "DISABLED");
42        }
43        else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
44            Log.d(TAG, "UPDATE");
45            Bundle extras = intent.getExtras();
46            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
47
48            AppWidgetManager gm = AppWidgetManager.getInstance(context);
49            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_appwidget);
50            views.setTextViewText(R.id.oh_hai_text, "hai: " + SystemClock.elapsedRealtime());
51            if (false) {
52                gm.updateAppWidget(appWidgetIds, views);
53            } else {
54                gm.updateAppWidget(new ComponentName("com.android.tests.appwidgetprovider",
55                            "com.android.tests.appwidgetprovider.TestAppWidgetProvider"), views);
56            }
57        }
58    }
59}
60
61