blob: 21f3bd0675d4441bc14d965fa9539163f17f5ccf [file] [log] [blame]
James Lemieux8bf1c3b2016-05-06 14:04:29 -07001/*
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
17package com.android.alarmclock;
18
19import android.appwidget.AppWidgetManager;
20import android.content.Context;
James Lemieux8bf1c3b2016-05-06 14:04:29 -070021import android.content.res.Resources;
22import android.os.Bundle;
23
24import com.android.deskclock.R;
James Lemieuxce6b8802016-05-19 13:26:40 -070025import com.android.deskclock.Utils;
James Lemieux8bf1c3b2016-05-06 14:04:29 -070026
27public final class WidgetUtils {
28
Michael Wc3763b42023-05-19 18:31:11 +020029 private static final String PREFS_NAME = "com.android.alarmclock.widgets";
30 private static final String PREF_PREFIX_KEY = "appwidget_";
31 private static final String PREF_MODE_PREFIX = PREF_PREFIX_KEY + "solid_";
Michael Wd65b3b62023-04-10 17:49:09 +020032
33
James Lemieux8bf1c3b2016-05-06 14:04:29 -070034 private WidgetUtils() {}
35
36 // Calculate the scale factor of the fonts in the widget
37 public static float getScaleRatio(Context context, Bundle options, int id, int cityCount) {
38 if (options == null) {
39 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
40 if (widgetManager == null) {
41 // no manager , do no scaling
42 return 1f;
43 }
44 options = widgetManager.getAppWidgetOptions(id);
45 }
46 if (options != null) {
47 int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
48 if (minWidth == 0) {
49 // No data , do no scaling
50 return 1f;
51 }
James Lemieuxce6b8802016-05-19 13:26:40 -070052 final Resources res = context.getResources();
James Lemieux8bf1c3b2016-05-06 14:04:29 -070053 float density = res.getDisplayMetrics().density;
54 float ratio = (density * minWidth) / res.getDimension(R.dimen.min_digital_widget_width);
55 ratio = Math.min(ratio, getHeightScaleRatio(context, options, id));
56 ratio *= .83f;
57
58 if (cityCount > 0) {
Michael W483f12a2022-12-29 13:41:06 +010059 return Math.min(ratio, 1f);
James Lemieux8bf1c3b2016-05-06 14:04:29 -070060 }
61
62 ratio = Math.min(ratio, 1.6f);
James Lemieuxce6b8802016-05-19 13:26:40 -070063 if (Utils.isPortrait(context)) {
James Lemieux8bf1c3b2016-05-06 14:04:29 -070064 ratio = Math.max(ratio, .71f);
65 }
66 else {
67 ratio = Math.max(ratio, .45f);
68 }
69 return ratio;
70 }
71 return 1f;
72 }
73
74 // Calculate the scale factor of the fonts in the list of the widget using the widget height
75 private static float getHeightScaleRatio(Context context, Bundle options, int id) {
76 if (options == null) {
77 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
78 if (widgetManager == null) {
79 // no manager , do no scaling
80 return 1f;
81 }
82 options = widgetManager.getAppWidgetOptions(id);
83 }
84 if (options != null) {
85 int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
86 if (minHeight == 0) {
87 // No data , do no scaling
88 return 1f;
89 }
James Lemieuxce6b8802016-05-19 13:26:40 -070090 final Resources res = context.getResources();
James Lemieux8bf1c3b2016-05-06 14:04:29 -070091 float density = res.getDisplayMetrics().density;
92 float ratio = density * minHeight / res.getDimension(R.dimen.min_digital_widget_height);
James Lemieuxce6b8802016-05-19 13:26:40 -070093 if (Utils.isPortrait(context)) {
James Lemieux8bf1c3b2016-05-06 14:04:29 -070094 return ratio * 1.75f;
95 }
96 return ratio;
97 }
98 return 1;
99 }
Michael Wd65b3b62023-04-10 17:49:09 +0200100
101 public static void saveWidgetMode(Context context, int appWidgetId, boolean isSolid) {
102 context.getSharedPreferences(PREFS_NAME, 0).edit()
103 .putBoolean(PREF_MODE_PREFIX + appWidgetId, isSolid)
104 .commit();
105 }
106
107 public static boolean getWidgetMode(Context context, int appWidgetId) {
108 return context.getSharedPreferences(PREFS_NAME, 0)
109 .getBoolean(PREF_MODE_PREFIX + appWidgetId, false);
110 }
111
112 public static int[] getWidgetLayouts(Context context, int appWidgetId) {
113 int[] layoutIds = new int[2];
114 if (getWidgetMode(context, appWidgetId)) {
115 layoutIds[0] = R.layout.digital_widget_darkbg_solid_theme_root;
116 layoutIds[1] = R.layout.digital_widget_lightbg_solid_theme_root;
117 } else {
118 layoutIds[0] = R.layout.digital_widget_darkbg_theme_root;
119 layoutIds[1] = R.layout.digital_widget_lightbg_theme_root;
120 }
121 return layoutIds;
122 }
Michael W7b2140c2023-01-21 14:45:45 +0100123}