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");
      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.appwidget.AppWidgetManager;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.res.Resources;
     23 import android.text.format.DateFormat;
     24 import android.util.Log;
     25 import android.util.TypedValue;
     26 import android.view.View;
     27 import android.widget.RemoteViews;
     28 import android.widget.RemoteViewsService.RemoteViewsFactory;
     29 
     30 import com.android.deskclock.R;
     31 import com.android.deskclock.Utils;
     32 import com.android.deskclock.worldclock.CityObj;
     33 import com.android.deskclock.worldclock.WorldClockAdapter;
     34 
     35 import java.util.Calendar;
     36 import java.util.Locale;
     37 import java.util.TimeZone;
     38 
     39 public class DigitalWidgetViewsFactory implements RemoteViewsFactory {
     40     private static final String TAG = "DigitalWidgetViewsFactory";
     41 
     42     private Context mContext;
     43     private Resources mResources;
     44     private int mId = AppWidgetManager.INVALID_APPWIDGET_ID;
     45     private RemoteWorldClockAdapter mAdapter;
     46     private float mFontScale = 1;
     47 
     48     // An adapter to provide the view for the list of cities in the world clock.
     49     private class RemoteWorldClockAdapter extends WorldClockAdapter {
     50         private final float mFontSize;
     51         private final float mFont24Size;
     52 
     53         public RemoteWorldClockAdapter(Context context) {
     54             super(context);
     55             mFontSize = context.getResources().getDimension(R.dimen.widget_medium_font_size);
     56             mFont24Size = context.getResources().getDimension(R.dimen.widget_24_medium_font_size);
     57         }
     58 
     59         public RemoteViews getViewAt(int position) {
     60             // There are 2 cities per item
     61             int index = position * 2;
     62             if (index < 0 || index >= mCitiesList.length) {
     63                 return null;
     64             }
     65 
     66             RemoteViews views = new RemoteViews(
     67                     mContext.getPackageName(), R.layout.world_clock_remote_list_item);
     68 
     69             // Always how the left clock
     70             updateView(views, (CityObj) mCitiesList[index], R.id.left_clock,
     71                     R.id.city_name_left, R.id.city_day_left);
     72             // Show the right clock if any, make it invisible if there is no
     73             // clock on the right
     74             // to keep the left view on the left.
     75             if (index + 1 < mCitiesList.length) {
     76                 updateView(views, (CityObj) mCitiesList[index + 1], R.id.right_clock,
     77                         R.id.city_name_right, R.id.city_day_right);
     78             } else {
     79                 hideView(views, R.id.right_clock, R.id.city_name_right,
     80                         R.id.city_day_right);
     81             }
     82 
     83             // Hide last spacer if last row
     84             int lastRow = ((mCitiesList.length + 1) / 2) - 1;
     85             if (position == lastRow) {
     86                 views.setViewVisibility(R.id.city_spacer, View.GONE);
     87             } else {
     88                 views.setViewVisibility(R.id.city_spacer, View.VISIBLE);
     89             }
     90 
     91             return views;
     92         }
     93 
     94         private void updateView(RemoteViews clock, CityObj cityObj, int clockId,
     95                 int labelId, int dayId) {
     96             final Calendar now = Calendar.getInstance();
     97             now.setTimeInMillis(System.currentTimeMillis());
     98             int myDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
     99             CityObj cityInDb = mCitiesDb.get(cityObj.mCityId);
    100             String cityTZ = (cityInDb != null) ? cityInDb.mTimeZone : cityObj.mTimeZone;
    101             now.setTimeZone(TimeZone.getTimeZone(cityTZ));
    102             int cityDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    103 
    104             WidgetUtils.setTimeFormat(clock,
    105                     (int)mResources.getDimension(R.dimen.widget_label_font_size), clockId);
    106             float fontSize = mFontScale * (DateFormat.is24HourFormat(mContext)
    107                     ? mFont24Size : mFontSize);
    108             clock.setTextViewTextSize(clockId, TypedValue.COMPLEX_UNIT_PX, fontSize * mFontScale);
    109             clock.setString(clockId, "setTimeZone", cityObj.mTimeZone);
    110 
    111             // Home city or city not in DB , use data from the save selected cities list
    112             clock.setTextViewText(labelId, Utils.getCityName(cityObj, cityInDb));
    113 
    114             if (myDayOfWeek != cityDayOfWeek) {
    115                 clock.setTextViewText(dayId, mContext.getString(
    116                         R.string.world_day_of_week_label, now.getDisplayName(
    117                                 Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault())));
    118                 clock.setViewVisibility(dayId, View.VISIBLE);
    119             } else {
    120                 clock.setViewVisibility(dayId, View.GONE);
    121             }
    122 
    123             clock.setViewVisibility(clockId, View.VISIBLE);
    124             clock.setViewVisibility(labelId, View.VISIBLE);
    125         }
    126 
    127         private void hideView(
    128                 RemoteViews clock, int clockId, int labelId, int dayId) {
    129             clock.setViewVisibility(clockId, View.INVISIBLE);
    130             clock.setViewVisibility(labelId, View.INVISIBLE);
    131             clock.setViewVisibility(dayId, View.INVISIBLE);
    132         }
    133     }
    134 
    135     public DigitalWidgetViewsFactory(Context context, Intent intent) {
    136         mContext = context;
    137         mResources = mContext.getResources();
    138         mId = intent.getIntExtra(
    139                 AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    140         mAdapter = new RemoteWorldClockAdapter(context);
    141     }
    142 
    143     @SuppressWarnings("unused")
    144     public DigitalWidgetViewsFactory() {
    145     }
    146 
    147     @Override
    148     public int getCount() {
    149         if (WidgetUtils.showList(mContext, mId, mFontScale)) {
    150             return mAdapter.getCount();
    151         }
    152         return 0;
    153     }
    154 
    155     @Override
    156     public long getItemId(int position) {
    157         return position;
    158     }
    159 
    160     @Override
    161     public RemoteViews getLoadingView() {
    162         return null;
    163     }
    164 
    165     @Override
    166     public RemoteViews getViewAt(int position) {
    167         RemoteViews v = mAdapter.getViewAt(position);
    168         if (v != null) {
    169             Intent fillInIntent = new Intent();
    170             v.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
    171         }
    172         return v;
    173     }
    174 
    175     @Override
    176     public int getViewTypeCount() {
    177         return 1;
    178     }
    179 
    180     @Override
    181     public boolean hasStableIds() {
    182         return true;
    183     }
    184 
    185     @Override
    186     public void onCreate() {
    187         if (DigitalAppWidgetService.LOGGING) {
    188             Log.i(TAG, "DigitalWidget onCreate " + mId);
    189         }
    190     }
    191 
    192     @Override
    193     public void onDataSetChanged() {
    194         mAdapter.loadData(mContext);
    195         mAdapter.loadCitiesDb(mContext);
    196         mAdapter.updateHomeLabel(mContext);
    197 
    198         mFontScale = WidgetUtils.getScaleRatio(mContext, null, mId);
    199     }
    200 
    201     @Override
    202     public void onDestroy() {
    203         if (DigitalAppWidgetService.LOGGING) {
    204             Log.i(TAG, "DigitalWidget onDestroy " + mId);
    205         }
    206     }
    207 }
    208 
    209