• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 package com.android.alarmclock
17 
18 import android.appwidget.AppWidgetManager
19 import android.content.Context
20 import android.content.res.Resources
21 import android.os.Bundle
22 
23 import com.android.deskclock.R
24 import com.android.deskclock.Utils
25 
26 object WidgetUtils {
27     // Calculate the scale factor of the fonts in the widget
getScaleRationull28     fun getScaleRatio(context: Context, options: Bundle?, id: Int, cityCount: Int): Float {
29         var options: Bundle? = options
30         if (options == null) {
31             val widgetManager: AppWidgetManager =
32                     AppWidgetManager.getInstance(context) // no manager , do no scaling
33                     ?: return 1f
34             options = widgetManager.getAppWidgetOptions(id)
35         }
36         options?.let {
37             val minWidth: Int = it.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
38             if (minWidth == 0) {
39                 // No data , do no scaling
40                 return 1f
41             }
42             val res: Resources = context.getResources()
43             val density: Float = res.getDisplayMetrics().density
44             var ratio: Float =
45                     density * minWidth / res.getDimension(R.dimen.min_digital_widget_width)
46             ratio = Math.min(ratio, getHeightScaleRatio(context, it))
47             ratio *= .83f
48 
49             if (cityCount > 0) {
50                 return if (ratio > 1f) 1f else ratio
51             }
52 
53             ratio = Math.min(ratio, 1.6f)
54             ratio = if (Utils.isPortrait(context)) {
55                 Math.max(ratio, .71f)
56             } else {
57                 Math.max(ratio, .45f)
58             }
59             return ratio
60         }
61         return 1f
62     }
63 
64     // Calculate the scale factor of the fonts in the list of the widget using the widget height
getHeightScaleRationull65     private fun getHeightScaleRatio(context: Context, options: Bundle): Float {
66         val minHeight: Int = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
67         if (minHeight == 0) {
68             // No data , do no scaling
69             return 1f
70         }
71         val res: Resources = context.getResources()
72         val density: Float = res.getDisplayMetrics().density
73         val ratio: Float = density * minHeight / res.getDimension(R.dimen.min_digital_widget_height)
74         return if (Utils.isPortrait(context)) { ratio * 1.75f } else ratio
75     }
76 }