Update widget size
This commit is contained in:
parent
dd450443c8
commit
bea0803c3a
@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="com.tommasoberlose.anotherwidget">
|
package="com.tommasoberlose.anotherwidget">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.READ_CALENDAR" />
|
<uses-permission android:name="android.permission.READ_CALENDAR" />
|
||||||
@ -16,19 +17,20 @@
|
|||||||
android:name=".AWApplication"
|
android:name=".AWApplication"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme"
|
||||||
<activity android:name=".ui.activities.MainActivity" android:launchMode="singleInstance" android:theme="@style/AppTheme.Main">
|
tools:ignore="LockedOrientationActivity">
|
||||||
|
<activity android:name=".ui.activities.MainActivity" android:launchMode="singleInstance" android:theme="@style/AppTheme.Main" android:screenOrientation="portrait">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
<activity android:name=".ui.activities.ChooseApplicationActivity" android:launchMode="singleInstance" />
|
<activity android:name=".ui.activities.ChooseApplicationActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" />
|
||||||
<activity android:name=".ui.activities.CustomLocationActivity" android:launchMode="singleInstance" />
|
<activity android:name=".ui.activities.CustomLocationActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" />
|
||||||
<activity android:name=".ui.activities.WeatherProviderActivity" android:launchMode="singleInstance" />
|
<activity android:name=".ui.activities.WeatherProviderActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" />
|
||||||
<activity android:name=".ui.activities.SupportDevActivity" android:launchMode="singleInstance" />
|
<activity android:name=".ui.activities.SupportDevActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" />
|
||||||
<activity android:name=".ui.activities.CustomDateActivity" android:launchMode="singleInstance" />
|
<activity android:name=".ui.activities.CustomDateActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" />
|
||||||
|
|
||||||
|
|
||||||
<receiver android:name=".ui.widgets.MainWidget">
|
<receiver android:name=".ui.widgets.MainWidget">
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.tommasoberlose.anotherwidget.helpers
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.res.Configuration.ORIENTATION_PORTRAIT
|
||||||
|
|
||||||
|
object WidgetHelper {
|
||||||
|
class WidgetSizeProvider(
|
||||||
|
private val context: Context,
|
||||||
|
private val appWidgetManager: AppWidgetManager
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun getWidgetsSize(widgetId: Int): Pair<Int, Int> {
|
||||||
|
val isPortrait = context.resources.configuration.orientation == ORIENTATION_PORTRAIT
|
||||||
|
val width = getWidgetWidth(isPortrait, widgetId)
|
||||||
|
val height = getWidgetHeight(isPortrait, widgetId)
|
||||||
|
val widthInPx = context.dip(width)
|
||||||
|
val heightInPx = context.dip(height)
|
||||||
|
return widthInPx to heightInPx
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWidgetWidth(isPortrait: Boolean, widgetId: Int): Int =
|
||||||
|
if (isPortrait) {
|
||||||
|
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
|
||||||
|
} else {
|
||||||
|
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWidgetHeight(isPortrait: Boolean, widgetId: Int): Int =
|
||||||
|
if (isPortrait) {
|
||||||
|
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
|
||||||
|
} else {
|
||||||
|
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWidgetSizeInDp(widgetId: Int, key: String): Int =
|
||||||
|
appWidgetManager.getAppWidgetOptions(widgetId).getInt(key, 0)
|
||||||
|
|
||||||
|
private fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -65,9 +65,11 @@ class MainWidget : AppWidgetProvider() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDisabled(context: Context) {
|
override fun onDisabled(context: Context) {
|
||||||
|
if (getWidgetCount(context) == 0) {
|
||||||
UpdatesReceiver.removeUpdates(context)
|
UpdatesReceiver.removeUpdates(context)
|
||||||
WeatherReceiver.removeUpdates(context)
|
WeatherReceiver.removeUpdates(context)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
@ -81,26 +83,26 @@ class MainWidget : AppWidgetProvider() {
|
|||||||
context.sendBroadcast(update)
|
context.sendBroadcast(update)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getWidgetCount(context: Context): Int {
|
||||||
|
val widgetManager = AppWidgetManager.getInstance(context)
|
||||||
|
val widgetComponent = ComponentName(context, MainWidget::class.java)
|
||||||
|
return widgetManager.getAppWidgetIds(widgetComponent).size
|
||||||
|
}
|
||||||
|
|
||||||
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager,
|
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager,
|
||||||
appWidgetId: Int) {
|
appWidgetId: Int) {
|
||||||
val displayMetrics = Resources.getSystem().displayMetrics
|
val displayMetrics = Resources.getSystem().displayMetrics
|
||||||
var height = 110.toPixel(context)
|
|
||||||
val width = displayMetrics.widthPixels
|
val width = displayMetrics.widthPixels
|
||||||
if (Preferences.showClock) {
|
|
||||||
height += Preferences.clockTextSize.convertSpToPixels(context).toInt() + 16.toPixel(context)
|
|
||||||
}
|
|
||||||
if (Preferences.textMainSize > 30 && Preferences.textSecondSize > 22) {
|
|
||||||
height += 24.toPixel(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
generateWidgetView(context, appWidgetId, appWidgetManager, width - 16.toPixel(context))
|
val dimensions = WidgetHelper.WidgetSizeProvider(context, appWidgetManager).getWidgetsSize(appWidgetId)
|
||||||
|
generateWidgetView(context, appWidgetId, appWidgetManager, dimensions.first - 8.toPixel(context) /*width - 16.toPixel(context)*/)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateWidgetView(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, w: Int) {
|
private fun generateWidgetView(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, w: Int) {
|
||||||
var views = RemoteViews(context.packageName, R.layout.the_widget_sans)
|
var views = RemoteViews(context.packageName, R.layout.the_widget_sans)
|
||||||
|
|
||||||
val generatedView = generateWidgetView(context)
|
val generatedView = generateWidgetView(context)
|
||||||
views.setImageViewBitmap(R.id.bitmap_container, BitmapHelper.getBitmapFromView(generatedView, width = w - 32.toPixel(context)))
|
views.setImageViewBitmap(R.id.bitmap_container, BitmapHelper.getBitmapFromView(generatedView, width = w))
|
||||||
|
|
||||||
// Clock
|
// Clock
|
||||||
views = updateClockView(context, views, appWidgetId)
|
views = updateClockView(context, views, appWidgetId)
|
||||||
|
@ -1 +1 @@
|
|||||||
#Mon May 04 17:41:28 CEST 2020
|
#Mon May 04 20:02:45 CEST 2020
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#Mon May 04 18:47:09 CEST 2020
|
#Mon May 04 20:03:44 CEST 2020
|
||||||
base.0=/Users/tommaso/Documents/MyCode/another-widget/tasksintegration/build/intermediates/dex/debug/mergeProjectDexDebug/out/classes.dex
|
base.0=/Users/tommaso/Documents/MyCode/another-widget/tasksintegration/build/intermediates/dex/debug/mergeProjectDexDebug/out/classes.dex
|
||||||
path.0=classes.dex
|
path.0=classes.dex
|
||||||
renamed.0=classes.dex
|
renamed.0=classes.dex
|
||||||
|
Loading…
x
Reference in New Issue
Block a user