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; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.res.Resources; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.provider.Settings; 29 import android.text.TextUtils; 30 import android.text.format.DateFormat; 31 import android.util.Log; 32 import android.util.TypedValue; 33 import android.view.View; 34 import android.widget.RemoteViews; 35 36 import com.android.deskclock.Alarms; 37 import com.android.deskclock.DeskClock; 38 import com.android.deskclock.R; 39 import com.android.deskclock.Utils; 40 41 import java.util.Calendar; 42 43 public class DigitalAppWidgetProvider extends AppWidgetProvider { 44 private static final String TAG = "DigitalAppWidgetProvider"; 45 DigitalAppWidgetProvider()46 public DigitalAppWidgetProvider() { 47 } 48 49 @Override onUpdate(Context ctxt, AppWidgetManager appWidgetManager, int[] appWidgetIds)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 onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)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 getComponentName(Context context)68 static ComponentName getComponentName(Context context) { 69 return new ComponentName(context, DigitalAppWidgetProvider.class); 70 } 71 updateClock( Context c, AppWidgetManager appWidgetManager, int appWidgetId, float ratio)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 refreshAlarm(Context c, RemoteViews clock)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