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