James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 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; |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.os.Bundle; |
| 23 | |
| 24 | import com.android.deskclock.R; |
James Lemieux | ce6b880 | 2016-05-19 13:26:40 -0700 | [diff] [blame] | 25 | import com.android.deskclock.Utils; |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 26 | |
| 27 | public final class WidgetUtils { |
| 28 | |
Michael W | c3763b4 | 2023-05-19 18:31:11 +0200 | [diff] [blame] | 29 | 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 W | d65b3b6 | 2023-04-10 17:49:09 +0200 | [diff] [blame] | 32 | |
| 33 | |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 34 | 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 Lemieux | ce6b880 | 2016-05-19 13:26:40 -0700 | [diff] [blame] | 52 | final Resources res = context.getResources(); |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 53 | 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 W | 483f12a | 2022-12-29 13:41:06 +0100 | [diff] [blame] | 59 | return Math.min(ratio, 1f); |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ratio = Math.min(ratio, 1.6f); |
James Lemieux | ce6b880 | 2016-05-19 13:26:40 -0700 | [diff] [blame] | 63 | if (Utils.isPortrait(context)) { |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 64 | 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 Lemieux | ce6b880 | 2016-05-19 13:26:40 -0700 | [diff] [blame] | 90 | final Resources res = context.getResources(); |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 91 | float density = res.getDisplayMetrics().density; |
| 92 | float ratio = density * minHeight / res.getDimension(R.dimen.min_digital_widget_height); |
James Lemieux | ce6b880 | 2016-05-19 13:26:40 -0700 | [diff] [blame] | 93 | if (Utils.isPortrait(context)) { |
James Lemieux | 8bf1c3b | 2016-05-06 14:04:29 -0700 | [diff] [blame] | 94 | return ratio * 1.75f; |
| 95 | } |
| 96 | return ratio; |
| 97 | } |
| 98 | return 1; |
| 99 | } |
Michael W | d65b3b6 | 2023-04-10 17:49:09 +0200 | [diff] [blame] | 100 | |
| 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 W | 7b2140c | 2023-01-21 14:45:45 +0100 | [diff] [blame] | 123 | } |