1 /*
2  * Copyright (C) 2016 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.Resources;
22 import android.os.Bundle;
23 
24 import com.android.deskclock.R;
25 import com.android.deskclock.Utils;
26 
27 public final class WidgetUtils {
28 
WidgetUtils()29     private WidgetUtils() {}
30 
31     // Calculate the scale factor of the fonts in the widget
getScaleRatio(Context context, Bundle options, int id, int cityCount)32     public static float getScaleRatio(Context context, Bundle options, int id, int cityCount) {
33         if (options == null) {
34             AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
35             if (widgetManager == null) {
36                 // no manager , do no scaling
37                 return 1f;
38             }
39             options = widgetManager.getAppWidgetOptions(id);
40         }
41         if (options != null) {
42             int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
43             if (minWidth == 0) {
44                 // No data , do no scaling
45                 return 1f;
46             }
47             final Resources res = context.getResources();
48             float density = res.getDisplayMetrics().density;
49             float ratio = (density * minWidth) / res.getDimension(R.dimen.min_digital_widget_width);
50             ratio = Math.min(ratio, getHeightScaleRatio(context, options, id));
51             ratio *= .83f;
52 
53             if (cityCount > 0) {
54                 return (ratio > 1f) ? 1f : ratio;
55             }
56 
57             ratio = Math.min(ratio, 1.6f);
58             if (Utils.isPortrait(context)) {
59                 ratio = Math.max(ratio, .71f);
60             }
61             else {
62                 ratio = Math.max(ratio, .45f);
63             }
64             return ratio;
65         }
66         return 1f;
67     }
68 
69     // Calculate the scale factor of the fonts in the list of  the widget using the widget height
getHeightScaleRatio(Context context, Bundle options, int id)70     private static float getHeightScaleRatio(Context context, Bundle options, int id) {
71         if (options == null) {
72             AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
73             if (widgetManager == null) {
74                 // no manager , do no scaling
75                 return 1f;
76             }
77             options = widgetManager.getAppWidgetOptions(id);
78         }
79         if (options != null) {
80             int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
81             if (minHeight == 0) {
82                 // No data , do no scaling
83                 return 1f;
84             }
85             final Resources res = context.getResources();
86             float density = res.getDisplayMetrics().density;
87             float ratio = density * minHeight / res.getDimension(R.dimen.min_digital_widget_height);
88             if (Utils.isPortrait(context)) {
89                 return ratio * 1.75f;
90             }
91             return ratio;
92         }
93         return 1;
94     }
95 }