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.res.Configuration; 22 import android.content.res.Resources; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.util.TypedValue; 26 import android.widget.RemoteViews; 27 import android.widget.RemoteViews.RemoteView; 28 import android.widget.TextClock; 29 30 import com.android.deskclock.R; 31 import com.android.deskclock.Utils; 32 33 public class WidgetUtils { 34 static final String TAG = "WidgetUtils"; 35 setClockSize(Context context, RemoteViews clock, float scale)36 public static void setClockSize(Context context, RemoteViews clock, float scale) { 37 float fontSize = context.getResources().getDimension(R.dimen.widget_big_font_size); 38 clock.setTextViewTextSize( 39 R.id.the_clock, TypedValue.COMPLEX_UNIT_PX, fontSize * scale); 40 } 41 42 // Calculate the scale factor of the fonts in the widget getScaleRatio(Context context, Bundle options, int id)43 public static float getScaleRatio(Context context, Bundle options, int id) { 44 if (options == null) { 45 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 46 if (widgetManager == null) { 47 // no manager , do no scaling 48 return 1f; 49 } 50 options = widgetManager.getAppWidgetOptions(id); 51 } 52 if (options != null) { 53 int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); 54 if (minWidth == 0) { 55 // No data , do no scaling 56 return 1f; 57 } 58 Resources res = context.getResources(); 59 float density = res.getDisplayMetrics().density; 60 float ratio = (density * minWidth) / res.getDimension(R.dimen.min_digital_widget_width); 61 // Check if the height could introduce a font size constraint 62 int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); 63 if (minHeight > 0 && (density * minHeight) 64 < res.getDimension(R.dimen.min_digital_widget_height)) { 65 ratio = Math.min(ratio, getHeightScaleRatio(context, options, id)); 66 } 67 return (ratio > 1) ? 1 : ratio; 68 } 69 return 1; 70 } 71 72 // Calculate the scale factor of the fonts in the list of the widget using the widget height getHeightScaleRatio(Context context, Bundle options, int id)73 private static float getHeightScaleRatio(Context context, Bundle options, int id) { 74 if (options == null) { 75 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 76 if (widgetManager == null) { 77 // no manager , do no scaling 78 return 1f; 79 } 80 options = widgetManager.getAppWidgetOptions(id); 81 } 82 if (options != null) { 83 int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); 84 if (minHeight == 0) { 85 // No data , do no scaling 86 return 1f; 87 } 88 Resources res = context.getResources(); 89 float density = res.getDisplayMetrics().density; 90 // Estimate height of date text box - 1.35 roughly approximates the text box padding 91 float lblBox = 1.35f * res.getDimension(R.dimen.label_font_size); 92 // Ensure divisor for ratio is positive number 93 if (res.getDimension(R.dimen.min_digital_widget_height) - lblBox > 0) { 94 float ratio = ((density * minHeight) - lblBox) 95 / (res.getDimension(R.dimen.min_digital_widget_height) - lblBox); 96 return (ratio > 1) ? 1 : ratio; 97 } 98 } 99 return 1; 100 } 101 102 103 // Decide if to show the list of world clock. 104 // Check to see if the widget size is big enough, if it is return true. showList(Context context, int id, float scale)105 public static boolean showList(Context context, int id, float scale) { 106 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 107 if (widgetManager == null) { 108 // no manager to make the calculation, show the list anyway 109 return true; 110 } 111 Bundle options = widgetManager.getAppWidgetOptions(id); 112 if (options == null) { 113 // no data to make the calculation, show the list anyway 114 return true; 115 } 116 Resources res = context.getResources(); 117 String whichHeight = res.getConfiguration().orientation == 118 Configuration.ORIENTATION_PORTRAIT 119 ? AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT 120 : AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; 121 int height = options.getInt(whichHeight); 122 if (height == 0) { 123 // no data to make the calculation, show the list anyway 124 return true; 125 } 126 float density = res.getDisplayMetrics().density; 127 // Estimate height of date text box 128 float lblBox = 1.35f * res.getDimension(R.dimen.label_font_size); 129 float neededSize = res.getDimension(R.dimen.digital_widget_list_min_fixed_height) + 130 2 * lblBox + 131 scale * res.getDimension(R.dimen.digital_widget_list_min_scaled_height); 132 return ((density * height) > neededSize); 133 } 134 135 /*** 136 * Set the format of the time on the clock accrding to the locale 137 * @param clock - view to format 138 * @param amPmFontSize - size of am/pm label, zero size means no am/om label 139 * @param clockId - id of TextClock view as defined in the clock's layout. 140 */ setTimeFormat(RemoteViews clock, int amPmFontSize, int clockId)141 public static void setTimeFormat(RemoteViews clock, int amPmFontSize, int clockId) { 142 if (clock != null) { 143 // Set the best format for 12 hours mode according to the locale 144 clock.setCharSequence(clockId, "setFormat12Hour", Utils.get12ModeFormat(amPmFontSize)); 145 // Set the best format for 24 hours mode according to the locale 146 clock.setCharSequence(clockId, "setFormat24Hour", Utils.get24ModeFormat()); 147 } 148 } 149 } 150 151