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.example.android.apis.appwidget;
18
19import android.appwidget.AppWidgetManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.util.Log;
24
25import java.util.ArrayList;
26
27/**
28 * A BroadcastReceiver that listens for updates for the ExampleAppWidgetProvider.  This
29 * BroadcastReceiver starts off disabled, and we only enable it when there is a widget
30 * instance created, in order to only receive notifications when we need them.
31 */
32public class ExampleBroadcastReceiver extends BroadcastReceiver {
33
34    @Override
35    public void onReceive(Context context, Intent intent) {
36        Log.d("ExmampleBroadcastReceiver", "intent=" + intent);
37
38        // For our example, we'll also update all of the widgets when the timezone
39        // changes, or the user or network sets the time.
40        String action = intent.getAction();
41        if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
42                || action.equals(Intent.ACTION_TIME_CHANGED)) {
43            AppWidgetManager gm = AppWidgetManager.getInstance(context);
44            ArrayList<Integer> appWidgetIds = new ArrayList<Integer>();
45            ArrayList<String> texts = new ArrayList<String>();
46
47            ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts);
48
49            final int N = appWidgetIds.size();
50            for (int i=0; i<N; i++) {
51                ExampleAppWidgetProvider.updateAppWidget(context, gm, appWidgetIds.get(i), texts.get(i));
52            }
53        }
54    }
55
56}
57