Home | History | Annotate | Download | only in alarmclock
      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.appwidget.AppWidgetProviderInfo;
     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;
     30 import android.text.TextUtils;
     31 import android.text.format.DateFormat;
     32 import android.util.Log;
     33 import android.util.TypedValue;
     34 import android.view.View;
     35 import android.widget.RemoteViews;
     36 
     37 import com.android.deskclock.Alarms;
     38 import com.android.deskclock.DeskClock;
     39 import com.android.deskclock.R;
     40 import com.android.deskclock.Utils;
     41 import com.android.deskclock.worldclock.CitiesActivity;
     42 
     43 import java.util.Calendar;
     44 
     45 public class DigitalAppWidgetProvider extends AppWidgetProvider {
     46     private static final String TAG = "DigitalAppWidgetProvider";
     47 
     48     public DigitalAppWidgetProvider() {
     49     }
     50 
     51     @Override
     52     public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     53         for (int appWidgetId : appWidgetIds) {
     54             float ratio = WidgetUtils.getScaleRatio(ctxt, null, appWidgetId);
     55             updateClock(ctxt, appWidgetManager, appWidgetId, ratio);
     56 
     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
     65         float ratio = WidgetUtils.getScaleRatio(context, newOptions, appWidgetId);
     66         AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
     67         updateClock(context, widgetManager, appWidgetId, ratio);
     68     }
     69 
     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) {
     76         RemoteViews widget = new RemoteViews(c.getPackageName(), R.layout.digital_appwidget);
     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         }
     85         refreshAlarm(c, widget);
     86         WidgetUtils.setClockSize(c, widget, ratio);
     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,
     92                 PendingIntent.getActivity(c, 0, new Intent(c, CitiesActivity.class), 0));
     93         appWidgetManager.notifyAppWidgetViewDataChanged(
     94                 appWidgetId, R.id.digital_appwidget_listview);
     95         appWidgetManager.updateAppWidget(appWidgetId, widget);
     96     }
     97 
     98     private void refreshAlarm(Context c, RemoteViews clock) {
     99         String nextAlarm = Settings.System.getString(
    100                 c.getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED);
    101         if (!TextUtils.isEmpty(nextAlarm)) {
    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     }
    109 }
    110