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 28 import com.android.deskclock.R; 29 30 public class WidgetUtils { 31 static final String TAG = "WidgetUtils"; 32 33 public static void setClockSize(Context context, RemoteViews clock, float scale) { 34 float fontSize = context.getResources().getDimension(R.dimen.widget_big_font_size); 35 clock.setTextViewTextSize( 36 R.id.the_clock1, TypedValue.COMPLEX_UNIT_PX, fontSize * scale); 37 clock.setTextViewTextSize( 38 R.id.the_clock2, TypedValue.COMPLEX_UNIT_PX, fontSize * scale); 39 } 40 41 // Calculate the scale factor of the fonts in the widget 42 public static float getScaleRatio(Context context, Bundle options, int id) { 43 if (options == null) { 44 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 45 if (widgetManager == null) { 46 // no manager , do no scaling 47 return 1f; 48 } 49 options = widgetManager.getAppWidgetOptions(id); 50 } 51 if (options != null) { 52 int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); 53 if (minWidth == 0) { 54 // No data , do no scaling 55 return 1f; 56 } 57 Resources res = context.getResources(); 58 float density = res.getDisplayMetrics().density; 59 float ratio = (density * minWidth) / res.getDimension(R.dimen.min_digital_widget_width); 60 // Check if the height could introduce a font size constraint 61 int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); 62 if (minHeight > 0 && (density * minHeight) 63 < res.getDimension(R.dimen.min_digital_widget_height)) { 64 ratio = Math.min(ratio, getHeightScaleRatio(context, options, id)); 65 } 66 return (ratio > 1) ? 1 : ratio; 67 } 68 return 1; 69 } 70 71 // Calculate the scale factor of the fonts in the list of the widget using the widget height 72 private static float getHeightScaleRatio(Context context, Bundle options, int id) { 73 if (options == null) { 74 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 75 if (widgetManager == null) { 76 // no manager , do no scaling 77 return 1f; 78 } 79 options = widgetManager.getAppWidgetOptions(id); 80 } 81 if (options != null) { 82 int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); 83 if (minHeight == 0) { 84 // No data , do no scaling 85 return 1f; 86 } 87 Resources res = context.getResources(); 88 float density = res.getDisplayMetrics().density; 89 // Estimate height of date text box - 1.35 roughly approximates the text box padding 90 float lblBox = 1.35f * res.getDimension(R.dimen.label_font_size); 91 // Ensure divisor for ratio is positive number 92 if (res.getDimension(R.dimen.min_digital_widget_height) - lblBox > 0) { 93 float ratio = ((density * minHeight) - lblBox) 94 / (res.getDimension(R.dimen.min_digital_widget_height) - lblBox); 95 return (ratio > 1) ? 1 : ratio; 96 } 97 } 98 return 1; 99 } 100 101 102 // Decide if to show the list of world clock. 103 // Check to see if the widget size is big enough, if it is return true. 104 public static boolean showList(Context context, int id, float scale) { 105 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 106 if (widgetManager == null) { 107 // no manager to make the calculation, show the list anyway 108 return true; 109 } 110 Bundle options = widgetManager.getAppWidgetOptions(id); 111 if (options == null) { 112 // no data to make the calculation, show the list anyway 113 return true; 114 } 115 Resources res = context.getResources(); 116 String whichHeight = res.getConfiguration().orientation == 117 Configuration.ORIENTATION_PORTRAIT 118 ? AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT 119 : AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; 120 int height = options.getInt(whichHeight); 121 if (height == 0) { 122 // no data to make the calculation, show the list anyway 123 return true; 124 } 125 float density = res.getDisplayMetrics().density; 126 // Estimate height of date text box 127 float lblBox = 1.35f * res.getDimension(R.dimen.label_font_size); 128 float neededSize = res.getDimension(R.dimen.digital_widget_list_min_fixed_height) + 129 2 * lblBox + 130 scale * res.getDimension(R.dimen.digital_widget_list_min_scaled_height); 131 return ((density * height) > neededSize); 132 } 133 } 134 135