1 /*
2  * Copyright (C) 2009 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.alarmclock;
18 
19 import android.app.PendingIntent;
20 import android.appwidget.AppWidgetManager;
21 import android.appwidget.AppWidgetProvider;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.widget.RemoteViews;
26 
27 import com.android.deskclock.DeskClock;
28 import com.android.deskclock.R;
29 import com.android.deskclock.Utils;
30 import com.android.deskclock.data.DataModel;
31 
32 /**
33  * Simple widget to show an analog clock.
34  */
35 public class AnalogAppWidgetProvider extends AppWidgetProvider {
36 
37     @Override
onReceive(Context context, Intent intent)38     public void onReceive(Context context, Intent intent) {
39         super.onReceive(context, intent);
40 
41         final AppWidgetManager wm = AppWidgetManager.getInstance(context);
42         if (wm == null) {
43             return;
44         }
45 
46         // Send events for newly created/deleted widgets.
47         final ComponentName provider = new ComponentName(context, getClass());
48         final int widgetCount = wm.getAppWidgetIds(provider).length;
49 
50         final DataModel dm = DataModel.getDataModel();
51         dm.updateWidgetCount(getClass(), widgetCount, R.string.category_analog_widget);
52     }
53 
54     /**
55      * Called when widgets must provide remote views.
56      */
57     @Override
onUpdate(Context context, AppWidgetManager wm, int[] widgetIds)58     public void onUpdate(Context context, AppWidgetManager wm, int[] widgetIds) {
59         super.onUpdate(context, wm, widgetIds);
60 
61         for (int widgetId : widgetIds) {
62             final String packageName = context.getPackageName();
63             final RemoteViews widget = new RemoteViews(packageName, R.layout.analog_appwidget);
64 
65             // Tapping on the widget opens the app (if not on the lock screen).
66             if (Utils.isWidgetClickable(wm, widgetId)) {
67                 final Intent openApp = new Intent(context, DeskClock.class);
68                 final PendingIntent pi = PendingIntent.getActivity(context, 0, openApp, 0);
69                 widget.setOnClickPendingIntent(R.id.analog_appwidget, pi);
70             }
71 
72             wm.updateAppWidget(widgetId, widget);
73         }
74     }
75 }