1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.alarmclock;
18
19import android.app.PendingIntent;
20import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProvider;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.content.res.Resources;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.Settings;
29import android.text.TextUtils;
30import android.text.format.DateFormat;
31import android.util.Log;
32import android.util.TypedValue;
33import android.view.View;
34import android.widget.RemoteViews;
35
36import com.android.deskclock.Alarms;
37import com.android.deskclock.DeskClock;
38import com.android.deskclock.R;
39import com.android.deskclock.Utils;
40
41import java.util.Calendar;
42
43public class DigitalAppWidgetProvider extends AppWidgetProvider {
44    private static final String TAG = "DigitalAppWidgetProvider";
45
46    public DigitalAppWidgetProvider() {
47    }
48
49    @Override
50    public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
51        for (int appWidgetId : appWidgetIds) {
52            float ratio = WidgetUtils.getScaleRatio(ctxt, null, appWidgetId);
53            updateClock(ctxt, appWidgetManager, appWidgetId, ratio);
54
55        }
56        super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
57    }
58
59    @Override
60    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
61            int appWidgetId, Bundle newOptions) {
62        // scale the fonts of the clock to fit inside the new size
63        float ratio = WidgetUtils.getScaleRatio(context, newOptions, appWidgetId);
64        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
65        updateClock(context, widgetManager, appWidgetId, ratio);
66    }
67
68    static ComponentName getComponentName(Context context) {
69        return new ComponentName(context, DigitalAppWidgetProvider.class);
70    }
71
72    private void updateClock(
73            Context c, AppWidgetManager appWidgetManager, int appWidgetId, float ratio) {
74        RemoteViews widget = new RemoteViews(c.getPackageName(), R.layout.digital_appwidget);
75        widget.setOnClickPendingIntent(R.id.digital_appwidget,
76                PendingIntent.getActivity(c, 0, new Intent(c, DeskClock.class), 0));
77        refreshAlarm(c, widget);
78        WidgetUtils.setClockSize(c, widget, ratio);
79        final Intent intent = new Intent(c, DigitalAppWidgetService.class);
80        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
81        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
82        widget.setRemoteAdapter(appWidgetId, R.id.digital_appwidget_listview, intent);
83        widget.setPendingIntentTemplate(R.id.digital_appwidget_listview,
84                PendingIntent.getActivity(c, 0, new Intent(c, DeskClock.class), 0));
85        appWidgetManager.notifyAppWidgetViewDataChanged(
86                appWidgetId, R.id.digital_appwidget_listview);
87        appWidgetManager.updateAppWidget(appWidgetId, widget);
88    }
89
90    private void refreshAlarm(Context c, RemoteViews clock) {
91        String nextAlarm = Settings.System.getString(
92                c.getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED);
93        if (!TextUtils.isEmpty(nextAlarm)) {
94            clock.setTextViewText(R.id.nextAlarm,
95                    c.getString(R.string.control_set_alarm_with_existing, nextAlarm));
96            clock.setViewVisibility(R.id.nextAlarm, View.VISIBLE);
97        } else {
98            clock.setViewVisibility(R.id.nextAlarm, View.GONE);
99        }
100    }
101}
102