Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.alarmclock; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.appwidget.AppWidgetManager; |
| 21 | import android.appwidget.AppWidgetProvider; |
Isaac Katzenelson | 69ba4ed | 2012-12-12 15:49:11 -0800 | [diff] [blame^] | 22 | import android.appwidget.AppWidgetProviderInfo; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.res.Resources; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.Settings; |
Isaac Katzenelson | 2c25763 | 2012-11-02 16:25:37 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 31 | import android.text.format.DateFormat; |
| 32 | import android.util.Log; |
Isaac Katzenelson | bdfaec3 | 2012-11-05 18:18:06 -0800 | [diff] [blame] | 33 | import android.util.TypedValue; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.widget.RemoteViews; |
| 36 | |
Isaac Katzenelson | bdfaec3 | 2012-11-05 18:18:06 -0800 | [diff] [blame] | 37 | import com.android.deskclock.Alarms; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 38 | import com.android.deskclock.DeskClock; |
| 39 | import com.android.deskclock.R; |
Isaac Katzenelson | bdfaec3 | 2012-11-05 18:18:06 -0800 | [diff] [blame] | 40 | import com.android.deskclock.Utils; |
Isaac Katzenelson | 69ba4ed | 2012-12-12 15:49:11 -0800 | [diff] [blame^] | 41 | import com.android.deskclock.worldclock.CitiesActivity; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.Calendar; |
| 44 | |
| 45 | public class DigitalAppWidgetProvider extends AppWidgetProvider { |
| 46 | private static final String TAG = "DigitalAppWidgetProvider"; |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 47 | |
| 48 | public DigitalAppWidgetProvider() { |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
| 53 | for (int appWidgetId : appWidgetIds) { |
Itzhak Katzenelson | 03b1364 | 2012-11-05 10:42:01 -0800 | [diff] [blame] | 54 | float ratio = WidgetUtils.getScaleRatio(ctxt, null, appWidgetId); |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 55 | updateClock(ctxt, appWidgetManager, appWidgetId, ratio); |
Isaac Katzenelson | 980ff50 | 2012-11-09 15:16:16 -0800 | [diff] [blame] | 56 | |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 57 | } |
| 58 | super.onUpdate(ctxt, appWidgetManager, appWidgetIds); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, |
| 63 | int appWidgetId, Bundle newOptions) { |
| 64 | // scale the fonts of the clock to fit inside the new size |
Itzhak Katzenelson | 03b1364 | 2012-11-05 10:42:01 -0800 | [diff] [blame] | 65 | float ratio = WidgetUtils.getScaleRatio(context, newOptions, appWidgetId); |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 66 | AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); |
| 67 | updateClock(context, widgetManager, appWidgetId, ratio); |
| 68 | } |
| 69 | |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 70 | static ComponentName getComponentName(Context context) { |
| 71 | return new ComponentName(context, DigitalAppWidgetProvider.class); |
| 72 | } |
| 73 | |
| 74 | private void updateClock( |
| 75 | Context c, AppWidgetManager appWidgetManager, int appWidgetId, float ratio) { |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 76 | RemoteViews widget = new RemoteViews(c.getPackageName(), R.layout.digital_appwidget); |
Isaac Katzenelson | 69ba4ed | 2012-12-12 15:49:11 -0800 | [diff] [blame^] | 77 | // launch clock when clicking on the time in the widget only if not a lock screen widget |
| 78 | Bundle newOptions = appWidgetManager.getAppWidgetOptions(appWidgetId); |
| 79 | if (newOptions != null && |
| 80 | newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1) |
| 81 | != AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) { |
| 82 | widget.setOnClickPendingIntent(R.id.digital_appwidget, |
| 83 | PendingIntent.getActivity(c, 0, new Intent(c, DeskClock.class), 0)); |
| 84 | } |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 85 | refreshAlarm(c, widget); |
Itzhak Katzenelson | 03b1364 | 2012-11-05 10:42:01 -0800 | [diff] [blame] | 86 | WidgetUtils.setClockSize(c, widget, ratio); |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 87 | final Intent intent = new Intent(c, DigitalAppWidgetService.class); |
| 88 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 89 | intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); |
| 90 | widget.setRemoteAdapter(appWidgetId, R.id.digital_appwidget_listview, intent); |
| 91 | widget.setPendingIntentTemplate(R.id.digital_appwidget_listview, |
Isaac Katzenelson | 69ba4ed | 2012-12-12 15:49:11 -0800 | [diff] [blame^] | 92 | PendingIntent.getActivity(c, 0, new Intent(c, CitiesActivity.class), 0)); |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 93 | appWidgetManager.notifyAppWidgetViewDataChanged( |
| 94 | appWidgetId, R.id.digital_appwidget_listview); |
| 95 | appWidgetManager.updateAppWidget(appWidgetId, widget); |
| 96 | } |
| 97 | |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 98 | private void refreshAlarm(Context c, RemoteViews clock) { |
| 99 | String nextAlarm = Settings.System.getString( |
| 100 | c.getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED); |
Isaac Katzenelson | 2c25763 | 2012-11-02 16:25:37 -0700 | [diff] [blame] | 101 | if (!TextUtils.isEmpty(nextAlarm)) { |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 102 | clock.setTextViewText(R.id.nextAlarm, |
| 103 | c.getString(R.string.control_set_alarm_with_existing, nextAlarm)); |
| 104 | clock.setViewVisibility(R.id.nextAlarm, View.VISIBLE); |
| 105 | } else { |
| 106 | clock.setViewVisibility(R.id.nextAlarm, View.GONE); |
| 107 | } |
| 108 | } |
Isaac Katzenelson | 5c464d3 | 2012-10-24 14:23:59 -0700 | [diff] [blame] | 109 | } |