add support for app widgets (see #44)
Some checks are pending
Android CI / build (push) Waiting to run

This commit is contained in:
Josia Pietsch 2025-04-24 14:37:05 +02:00
parent 077bd1ce44
commit e7a06c443d
Signed by: jrpie
GPG key ID: E70B571D66986A2D
35 changed files with 1691 additions and 100 deletions

View file

@ -106,6 +106,7 @@ dependencies {
implementation 'com.google.android.material:material:1.12.0'
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation "eu.jonahbauer:android-preference-annotations:1.1.2"
implementation 'androidx.activity:activity:1.10.1'
annotationProcessor "eu.jonahbauer:android-preference-annotations:1.1.2"
annotationProcessor "com.android.databinding:compiler:$android_plugin_version"
testImplementation 'junit:junit:4.13.2'

View file

@ -8,6 +8,7 @@
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.ACCESS_HIDDEN_PROFILES" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.BIND_APPWIDGET" />
<application
android:name=".Application"
@ -19,6 +20,13 @@
android:supportsRtl="true"
android:theme="@style/launcherBaseTheme"
tools:ignore="UnusedAttribute">
<activity
android:name=".ui.widgets.manage.ManageWidgetsActivity"
android:theme="@style/launcherHomeTheme"
android:exported="false" />
<activity
android:name=".ui.widgets.manage.SelectWidgetActivity"
android:exported="false" />
<activity
android:name=".ui.PinShortcutActivity"
android:autoRemoveFromRecents="true"

View file

@ -12,6 +12,8 @@ import android.os.Build.VERSION_CODES
import android.os.UserHandle
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import android.appwidget.AppWidgetHost
import android.appwidget.AppWidgetManager
import androidx.preference.PreferenceManager
import de.jrpie.android.launcher.actions.TorchManager
import de.jrpie.android.launcher.apps.AbstractAppInfo
@ -20,13 +22,22 @@ import de.jrpie.android.launcher.apps.isPrivateSpaceLocked
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.preferences.migratePreferencesToNewVersion
import de.jrpie.android.launcher.preferences.resetPreferences
import de.jrpie.android.launcher.widgets.LauncherWidgetProvider
import de.jrpie.android.launcher.widgets.Widget
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
const val APP_WIDGET_HOST_ID = 42;
class Application : android.app.Application() {
val apps = MutableLiveData<List<AbstractDetailedAppInfo>>()
val widgets = MutableLiveData<Set<Widget>>()
val privateSpaceLocked = MutableLiveData<Boolean>()
lateinit var appWidgetHost: AppWidgetHost
lateinit var appWidgetManager: AppWidgetManager
private val profileAvailabilityBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
@ -90,6 +101,8 @@ class Application : android.app.Application() {
customAppNames = LauncherPreferences.apps().customNames()
} else if (pref == LauncherPreferences.apps().keys().pinnedShortcuts()) {
loadApps()
} else if (pref == LauncherPreferences.widgets().keys().widgets()) {
widgets.postValue(LauncherPreferences.widgets().widgets() ?: setOf())
}
}
@ -103,10 +116,15 @@ class Application : android.app.Application() {
torchManager = TorchManager(this)
}
appWidgetHost = AppWidgetHost(this.applicationContext, APP_WIDGET_HOST_ID)
appWidgetManager = AppWidgetManager.getInstance(this.applicationContext)
appWidgetHost.startListening()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
LauncherPreferences.init(preferences, this.resources)
// Try to restore old preferences
migratePreferencesToNewVersion(this)
@ -157,4 +175,10 @@ class Application : android.app.Application() {
apps.postValue(getApps(packageManager, applicationContext))
}
}
override fun onTerminate() {
appWidgetHost.stopListening()
super.onTerminate()
}
}

View file

@ -6,6 +6,9 @@ import android.app.role.RoleManager
import android.content.ActivityNotFoundException
import android.content.ClipData
import android.content.ClipboardManager
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps

View file

@ -8,6 +8,7 @@ import de.jrpie.android.launcher.actions.lock.LockMethod;
import de.jrpie.android.launcher.preferences.serialization.MapAbstractAppInfoStringPreferenceSerializer;
import de.jrpie.android.launcher.preferences.serialization.SetAbstractAppInfoPreferenceSerializer;
import de.jrpie.android.launcher.preferences.serialization.SetPinnedShortcutInfoPreferenceSerializer;
import de.jrpie.android.launcher.preferences.serialization.SetWidgetSerializer;
import de.jrpie.android.launcher.preferences.theme.Background;
import de.jrpie.android.launcher.preferences.theme.ColorTheme;
import de.jrpie.android.launcher.preferences.theme.Font;
@ -82,5 +83,8 @@ import eu.jonahbauer.android.preference.annotations.Preferences;
@PreferenceGroup(name = "actions", prefix = "settings_actions_", suffix = "_key", value = {
@Preference(name = "lock_method", type = LockMethod.class, defaultValue = "DEVICE_ADMIN"),
}),
@PreferenceGroup(name = "widgets", prefix = "settings_widgets_", suffix= "_key", value = {
@Preference(name = "widgets", type = Set.class, serializer = SetWidgetSerializer.class)
}),
})
public final class LauncherPreferences$Config {}

View file

@ -13,6 +13,9 @@ import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersio
import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersion3
import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersionUnknown
import de.jrpie.android.launcher.ui.HomeActivity
import de.jrpie.android.launcher.widgets.ClockWidget
import de.jrpie.android.launcher.widgets.WidgetPosition
import de.jrpie.android.launcher.widgets.deleteAllWidgets
/* Current version of the structure of preferences.
* Increase when breaking changes are introduced and write an appropriate case in
@ -71,6 +74,13 @@ fun resetPreferences(context: Context) {
Log.i(TAG, "Resetting preferences")
LauncherPreferences.clear()
LauncherPreferences.internal().versionCode(PREFERENCE_VERSION)
deleteAllWidgets(context)
LauncherPreferences.widgets().widgets(
setOf(
ClockWidget(-500, WidgetPosition(1,4,10,3))
)
)
val hidden: MutableSet<AbstractAppInfo> = mutableSetOf()

View file

@ -4,6 +4,7 @@ package de.jrpie.android.launcher.preferences.serialization
import de.jrpie.android.launcher.apps.AbstractAppInfo
import de.jrpie.android.launcher.apps.PinnedShortcutInfo
import de.jrpie.android.launcher.widgets.Widget
import eu.jonahbauer.android.preference.annotations.serializer.PreferenceSerializationException
import eu.jonahbauer.android.preference.annotations.serializer.PreferenceSerializer
import kotlinx.serialization.Serializable
@ -28,6 +29,24 @@ class SetAbstractAppInfoPreferenceSerializer :
}
}
@Suppress("UNCHECKED_CAST")
class SetWidgetSerializer :
PreferenceSerializer<java.util.Set<Widget>?, java.util.Set<java.lang.String>?> {
@Throws(PreferenceSerializationException::class)
override fun serialize(value: java.util.Set<Widget>?): java.util.Set<java.lang.String>? {
return value?.map(Widget::serialize)
?.toHashSet() as? java.util.Set<java.lang.String>
}
@Throws(PreferenceSerializationException::class)
override fun deserialize(value: java.util.Set<java.lang.String>?): java.util.Set<Widget>? {
return value?.map(java.lang.String::toString)?.map(Widget::deserialize)
?.toHashSet() as? java.util.Set<Widget>
}
}
@Suppress("UNCHECKED_CAST")
class SetPinnedShortcutInfoPreferenceSerializer :
PreferenceSerializer<java.util.Set<PinnedShortcutInfo>?, java.util.Set<java.lang.String>?> {

View file

@ -1,6 +1,7 @@
package de.jrpie.android.launcher.ui
import android.annotation.SuppressLint
import android.app.Activity
import android.content.SharedPreferences
import android.content.res.Configuration
import android.content.res.Resources
@ -10,8 +11,7 @@ import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.window.OnBackInvokedDispatcher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture
@ -20,7 +20,6 @@ import de.jrpie.android.launcher.databinding.HomeBinding
import de.jrpie.android.launcher.openTutorial
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.ui.tutorial.TutorialActivity
import java.util.Locale
/**
* [HomeActivity] is the actual application Launcher,
@ -34,7 +33,7 @@ import java.util.Locale
* - Setting global variables (preferences etc.)
* - Opening the [TutorialActivity] on new installations
*/
class HomeActivity : UIObject, AppCompatActivity() {
class HomeActivity : UIObject, Activity() {
private lateinit var binding: HomeBinding
private var touchGestureDetector: TouchGestureDetector? = null
@ -45,15 +44,18 @@ class HomeActivity : UIObject, AppCompatActivity() {
prefKey?.startsWith("display.") == true
) {
recreate()
} else if (prefKey?.startsWith("action.") == true) {
updateSettingsFallbackButtonVisibility()
} else if (prefKey == LauncherPreferences.widgets().keys().widgets()) {
binding.homeWidgetContainer.updateWidgets(this@HomeActivity,
LauncherPreferences.widgets().widgets()
)
}
if (prefKey?.startsWith("action.") == true) {
updateSettingsFallbackButtonVisibility()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState)
super<Activity>.onCreate(savedInstanceState)
super<UIObject>.onCreate()
@ -81,8 +83,7 @@ class HomeActivity : UIObject, AppCompatActivity() {
}
override fun onStart() {
super<AppCompatActivity>.onStart()
super<Activity>.onStart()
super<UIObject>.onStart()
// If the tutorial was not finished, start it
@ -93,6 +94,15 @@ class HomeActivity : UIObject, AppCompatActivity() {
LauncherPreferences.getSharedPreferences()
.registerOnSharedPreferenceChangeListener(sharedPreferencesListener)
(application as Application).appWidgetHost.startListening()
}
override fun onStop() {
(application as Application).appWidgetHost.stopListening()
super.onStop()
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
@ -118,44 +128,6 @@ class HomeActivity : UIObject, AppCompatActivity() {
}
}
private fun initClock() {
val locale = Locale.getDefault()
val dateVisible = LauncherPreferences.clock().dateVisible()
val timeVisible = LauncherPreferences.clock().timeVisible()
var dateFMT = "yyyy-MM-dd"
var timeFMT = "HH:mm"
if (LauncherPreferences.clock().showSeconds()) {
timeFMT += ":ss"
}
if (LauncherPreferences.clock().localized()) {
dateFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, dateFMT)
timeFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, timeFMT)
}
var upperFormat = dateFMT
var lowerFormat = timeFMT
var upperVisible = dateVisible
var lowerVisible = timeVisible
if (LauncherPreferences.clock().flipDateTime()) {
upperFormat = lowerFormat.also { lowerFormat = upperFormat }
upperVisible = lowerVisible.also { lowerVisible = upperVisible }
}
binding.homeUpperView.isVisible = upperVisible
binding.homeLowerView.isVisible = lowerVisible
binding.homeUpperView.setTextColor(LauncherPreferences.clock().color())
binding.homeLowerView.setTextColor(LauncherPreferences.clock().color())
binding.homeLowerView.format24Hour = lowerFormat
binding.homeUpperView.format24Hour = upperFormat
binding.homeLowerView.format12Hour = lowerFormat
binding.homeUpperView.format12Hour = upperFormat
}
override fun getTheme(): Resources.Theme {
val mTheme = modifyTheme(super.getTheme())
mTheme.applyStyle(R.style.backgroundWallpaper, true)
@ -193,9 +165,11 @@ class HomeActivity : UIObject, AppCompatActivity() {
windowInsets
}
}
initClock()
updateSettingsFallbackButtonVisibility()
binding.homeWidgetContainer.updateWidgets(this@HomeActivity,
LauncherPreferences.widgets().widgets()
)
}
override fun onDestroy() {
@ -233,30 +207,11 @@ class HomeActivity : UIObject, AppCompatActivity() {
}
override fun onTouchEvent(event: MotionEvent): Boolean {
android.util.Log.e("Launcher", "on touch")
touchGestureDetector?.onTouchEvent(event)
return true
}
override fun setOnClicks() {
binding.homeUpperView.setOnClickListener {
if (LauncherPreferences.clock().flipDateTime()) {
Gesture.TIME(this)
} else {
Gesture.DATE(this)
}
}
binding.homeLowerView.setOnClickListener {
if (LauncherPreferences.clock().flipDateTime()) {
Gesture.DATE(this)
} else {
Gesture.TIME(this)
}
}
}
private fun handleBack() {
Gesture.BACK(this)
}

View file

@ -49,7 +49,21 @@ class PinShortcutActivity : AppCompatActivity(), UIObject {
val request = launcherApps.getPinItemRequest(intent)
this.request = request
if (request == null || request.requestType != PinItemRequest.REQUEST_TYPE_SHORTCUT) {
if (request == null) {
finish()
return
}
if (request.requestType == PinItemRequest.REQUEST_TYPE_APPWIDGET) {
// TODO
request.getAppWidgetProviderInfo(this)
// startActivity()
finish()
return
}
if (request.requestType != PinItemRequest.REQUEST_TYPE_SHORTCUT) {
finish()
return
}

View file

@ -11,6 +11,7 @@ import de.jrpie.android.launcher.actions.openAppsList
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.preferences.theme.ColorTheme
import de.jrpie.android.launcher.setDefaultHomeScreen
import de.jrpie.android.launcher.ui.widgets.manage.ManageWidgetsActivity
/**
@ -81,6 +82,14 @@ class SettingsFragmentLauncher : PreferenceFragmentCompat() {
true
}
val manageWidgets = findPreference<androidx.preference.Preference>(
LauncherPreferences.widgets().keys().widgets()
)
manageWidgets?.setOnPreferenceClickListener {
startActivity(Intent(requireActivity(), ManageWidgetsActivity::class.java))
true
}
val hiddenApps = findPreference<androidx.preference.Preference>(
LauncherPreferences.apps().keys().hidden()
)

View file

@ -0,0 +1,80 @@
package de.jrpie.android.launcher.ui.widgets
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.databinding.ClockBinding
import de.jrpie.android.launcher.preferences.LauncherPreferences
import java.util.Locale
class ClockView(context: Context, attrs: AttributeSet? = null, val appWidgetId: Int): ConstraintLayout(context, attrs) {
val binding: ClockBinding = ClockBinding.inflate(LayoutInflater.from(context), this, true)
init {
initClock()
setOnClicks()
}
private fun initClock() {
val locale = Locale.getDefault()
val dateVisible = LauncherPreferences.clock().dateVisible()
val timeVisible = LauncherPreferences.clock().timeVisible()
var dateFMT = "yyyy-MM-dd"
var timeFMT = "HH:mm"
if (LauncherPreferences.clock().showSeconds()) {
timeFMT += ":ss"
}
if (LauncherPreferences.clock().localized()) {
dateFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, dateFMT)
timeFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, timeFMT)
}
var upperFormat = dateFMT
var lowerFormat = timeFMT
var upperVisible = dateVisible
var lowerVisible = timeVisible
if (LauncherPreferences.clock().flipDateTime()) {
upperFormat = lowerFormat.also { lowerFormat = upperFormat }
upperVisible = lowerVisible.also { lowerVisible = upperVisible }
}
binding.clockUpperView.isVisible = upperVisible
binding.clockLowerView.isVisible = lowerVisible
binding.clockUpperView.setTextColor(LauncherPreferences.clock().color())
binding.clockLowerView.setTextColor(LauncherPreferences.clock().color())
binding.clockLowerView.format24Hour = lowerFormat
binding.clockUpperView.format24Hour = upperFormat
binding.clockLowerView.format12Hour = lowerFormat
binding.clockUpperView.format12Hour = upperFormat
}
fun setOnClicks() {
binding.clockUpperView.setOnClickListener {
if (LauncherPreferences.clock().flipDateTime()) {
Gesture.TIME(context)
} else {
Gesture.DATE(context)
}
}
binding.clockLowerView.setOnClickListener {
if (LauncherPreferences.clock().flipDateTime()) {
Gesture.DATE(context)
} else {
Gesture.TIME(context)
}
}
}
}

View file

@ -0,0 +1,137 @@
package de.jrpie.android.launcher.ui.widgets
import android.app.Activity
import android.content.Context
import android.graphics.PointF
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.View.MeasureSpec.makeMeasureSpec
import android.view.ViewGroup
import androidx.core.graphics.contains
import androidx.core.view.size
import de.jrpie.android.launcher.widgets.Widget
import de.jrpie.android.launcher.widgets.WidgetPosition
import kotlin.math.max
/**
* This only works in an Activity, not AppCompatActivity
*/
open class WidgetContainerView(context: Context, attrs: AttributeSet? = null) : ViewGroup(context, attrs) {
var widgetViewById = HashMap<Int, View>()
open fun updateWidgets(activity: Activity, widgets: Set<Widget>?) {
if (widgets == null) {
return
}
Log.i("WidgetContainer", "updating ${activity.localClassName}")
widgetViewById.clear()
(0..<size).forEach { removeViewAt(0) }
widgets.forEach { widget ->
widget.createView(activity)?.let {
addView(it, WidgetContainerView.Companion.LayoutParams(widget.position))
widgetViewById.put(widget.id, it)
}
}
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
if (ev == null) {
return false
}
val position = PointF(ev.x, ev.y)
return widgetViewById.filter {
RectF(
it.value.x,
it.value.y,
it.value.x + it.value.width,
it.value.y + it.value.height
).contains(position) == true
}.any {
Widget.byId(context, it.key)?.allowInteraction == false
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var maxHeight = suggestedMinimumHeight
var maxWidth = suggestedMinimumWidth
val mWidth = MeasureSpec.getSize(widthMeasureSpec)
val mHeight = MeasureSpec.getSize(heightMeasureSpec)
(0..<size).map { getChildAt(it) }.forEach {
val position = (it.layoutParams as LayoutParams).position.getAbsoluteRect(mWidth, mHeight)
it.measure(makeMeasureSpec(position.width(), MeasureSpec.EXACTLY), makeMeasureSpec(position.height(), MeasureSpec.EXACTLY))
Log.e("measure", "$position")
}
// Find rightmost and bottom-most child
(0..<size).map { getChildAt(it) }.filter { it.visibility != GONE }.forEach {
val position = (it.layoutParams as LayoutParams).position.getAbsoluteRect(mWidth, mHeight)
maxWidth = max(maxWidth, position.left + it.measuredWidth)
maxHeight = max(maxHeight, position.top + it.measuredHeight)
}
setMeasuredDimension(
resolveSizeAndState(maxWidth.toInt(), widthMeasureSpec, 0),
resolveSizeAndState(maxHeight.toInt(), heightMeasureSpec, 0)
)
}
/**
* Returns a set of layout parameters with a width of
* [ViewGroup.LayoutParams.WRAP_CONTENT],
* a height of [ViewGroup.LayoutParams.WRAP_CONTENT]
* and with the coordinates (0, 0).
*/
override fun generateDefaultLayoutParams(): ViewGroup.LayoutParams {
return LayoutParams(WidgetPosition(0,0,1,1))
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
for (i in 0..<size) {
val child = getChildAt(i)
val lp = child.layoutParams as LayoutParams
val position = lp.position.getAbsoluteRect(r - l, b - t)
child.layout(position.left, position.top, position.right, position.bottom)
child.layoutParams.width = position.width()
child.layoutParams.height = position.height()
}
}
override fun generateLayoutParams(attrs: AttributeSet?): ViewGroup.LayoutParams {
return LayoutParams(context, attrs)
}
// Override to allow type-checking of LayoutParams.
override fun checkLayoutParams(p: ViewGroup.LayoutParams?): Boolean {
return p is LayoutParams
}
override fun generateLayoutParams(p: ViewGroup.LayoutParams?): ViewGroup.LayoutParams {
return LayoutParams(p)
}
override fun shouldDelayChildPressedState(): Boolean {
return false
}
companion object {
class LayoutParams : ViewGroup.LayoutParams {
var position = WidgetPosition(0,0,4,4)
constructor(position: WidgetPosition) : super(WRAP_CONTENT, WRAP_CONTENT) {
this.position = position
}
constructor(c: Context, attrs: AttributeSet?) : super(c, attrs)
constructor(source: ViewGroup.LayoutParams?) : super(source)
}
}
}

View file

@ -0,0 +1,173 @@
package de.jrpie.android.launcher.ui.widgets.manage
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Resources
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.ui.UIObject
import de.jrpie.android.launcher.ui.widgets.WidgetContainerView
import de.jrpie.android.launcher.widgets.AppWidget
import de.jrpie.android.launcher.widgets.WidgetPosition
import kotlin.math.min
// http://coderender.blogspot.com/2012/01/hosting-android-widgets-my.html
const val REQUEST_CREATE_APPWIDGET = 1
const val REQUEST_PICK_APPWIDGET = 2
// We can't use AppCompatActivity, since some AppWidgets don't work there.
class ManageWidgetsActivity : Activity(), UIObject {
private var sharedPreferencesListener =
SharedPreferences.OnSharedPreferenceChangeListener { _, prefKey ->
if (prefKey == LauncherPreferences.widgets().keys().widgets()) {
// We can't observe the livedata because this is not an AppCompatActivity
findViewById<WidgetContainerView>(R.id.manage_widgets_container).updateWidgets(this,
LauncherPreferences.widgets().widgets()
)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super<Activity>.onCreate(savedInstanceState)
super<UIObject>.onCreate()
setContentView(R.layout.activity_manage_widgets)
findViewById<FloatingActionButton>(R.id.manage_widgets_button_add).setOnClickListener {
selectWidget()
}
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
findViewById<WidgetContainerView>(R.id.manage_widgets_container).updateWidgets(this,
(application as Application).widgets.value
)
}
override fun onStart() {
super<Activity>.onStart()
super<UIObject>.onStart()
LauncherPreferences.getSharedPreferences()
.registerOnSharedPreferenceChangeListener(sharedPreferencesListener)
}
override fun onResume() {
super.onResume()
findViewById<WidgetContainerView>(R.id.manage_widgets_container).updateWidgets(this,
LauncherPreferences.widgets().widgets()
)
}
override fun getTheme(): Resources.Theme {
val mTheme = modifyTheme(super.getTheme())
mTheme.applyStyle(R.style.backgroundWallpaper, true)
LauncherPreferences.clock().font().applyToTheme(mTheme)
LauncherPreferences.theme().colorTheme().applyToTheme(
mTheme,
LauncherPreferences.theme().textShadow()
)
return mTheme
}
override fun onDestroy() {
LauncherPreferences.getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(sharedPreferencesListener)
super.onDestroy()
}
fun selectWidget() {
val appWidgetHost = (application as Application).appWidgetHost
startActivityForResult(
Intent(this, SelectWidgetActivity::class.java).also {
it.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetHost.allocateAppWidgetId()
)
}, REQUEST_PICK_APPWIDGET
)
}
fun createWidget(data: Intent) {
Log.i("Launcher", "creating widget")
val appWidgetManager = (application as Application).appWidgetManager
val appWidgetId = data.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: return
val provider = appWidgetManager.getAppWidgetInfo(appWidgetId)
val display = windowManager.defaultDisplay
val position = WidgetPosition.fromAbsoluteRect(
Rect(0,0,
min(400, appWidgetManager.getAppWidgetInfo(appWidgetId).minWidth),
min(400, appWidgetManager.getAppWidgetInfo(appWidgetId).minHeight)
),
display.width,
display.height
)
val widget = AppWidget(appWidgetId, provider, position)
LauncherPreferences.widgets().widgets(
(LauncherPreferences.widgets().widgets() ?: HashSet()).also {
it.add(widget)
}
)
}
private fun configureWidget(data: Intent) {
val extras = data.extras
val appWidgetId = extras!!.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
val widget = AppWidget(appWidgetId)
if (widget.isConfigurable(this)) {
widget.configure(this, REQUEST_CREATE_APPWIDGET)
} else {
createWidget(data)
}
}
override fun onActivityResult(
requestCode: Int, resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_PICK_APPWIDGET) {
configureWidget(data!!)
} else if (requestCode == REQUEST_CREATE_APPWIDGET) {
createWidget(data!!)
}
} else if (resultCode == RESULT_CANCELED && data != null) {
val appWidgetId =
data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
if (appWidgetId != -1) {
AppWidget(appWidgetId).delete(this)
}
}
}
/**
* For a better preview, [ManageWidgetsActivity] should behave exactly like [HomeActivity]
*/
override fun isHomeScreen(): Boolean {
return true
}
}

View file

@ -0,0 +1,168 @@
package de.jrpie.android.launcher.ui.widgets.manage
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Intent
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.databinding.ActivitySelectWidgetBinding
import de.jrpie.android.launcher.ui.UIObject
import de.jrpie.android.launcher.widgets.ClockWidget
import de.jrpie.android.launcher.widgets.LauncherAppWidgetProvider
import de.jrpie.android.launcher.widgets.LauncherClockWidgetProvider
import de.jrpie.android.launcher.widgets.LauncherWidgetProvider
import de.jrpie.android.launcher.widgets.WidgetPosition
import de.jrpie.android.launcher.widgets.bindAppWidgetOrRequestPermission
import de.jrpie.android.launcher.widgets.getAppWidgetHost
import de.jrpie.android.launcher.widgets.getAppWidgetProviders
import de.jrpie.android.launcher.widgets.updateWidget
private const val REQUEST_WIDGET_PERMISSION = 29
/**
* This activity lets the user pick an app widget to add.
* It provides an interface similar to [android.appwidget.AppWidgetManager.ACTION_APPWIDGET_PICK],
* but shows more information and also shows widgets from other user profiles.
*/
class SelectWidgetActivity : AppCompatActivity(), UIObject {
lateinit var binding: ActivitySelectWidgetBinding
var widgetId: Int = -1
private fun tryBindWidget(info: LauncherWidgetProvider) {
when (info) {
is LauncherAppWidgetProvider -> {
if (bindAppWidgetOrRequestPermission(
this,
info.info,
widgetId,
REQUEST_WIDGET_PERMISSION
)
) {
setResult(
RESULT_OK,
Intent().also {
it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId)
}
)
finish()
}
}
is LauncherClockWidgetProvider -> {
updateWidget(ClockWidget(widgetId, WidgetPosition(0,4,12,3)))
finish()
}
}
}
override fun onStart() {
super<AppCompatActivity>.onStart()
super<UIObject>.onStart()
}
override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState)
super<UIObject>.onCreate()
binding = ActivitySelectWidgetBinding.inflate(layoutInflater)
setContentView(binding.root)
widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
if (widgetId == -1) {
widgetId = getAppWidgetHost().allocateAppWidgetId()
}
val viewManager = LinearLayoutManager(this)
val viewAdapter = SelectWidgetRecyclerAdapter()
binding.selectWidgetRecycler.apply {
setHasFixedSize(false)
layoutManager = viewManager
adapter = viewAdapter
}
}
override fun getTheme(): Resources.Theme {
return modifyTheme(super.getTheme())
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_WIDGET_PERMISSION && resultCode == RESULT_OK) {
data ?: return
val provider = (data.getSerializableExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER) as? AppWidgetProviderInfo) ?: return
tryBindWidget(LauncherAppWidgetProvider(provider))
}
}
inner class SelectWidgetRecyclerAdapter() :
RecyclerView.Adapter<SelectWidgetRecyclerAdapter.ViewHolder>() {
private val widgets = getAppWidgetProviders(this@SelectWidgetActivity).toTypedArray()
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener {
var textView: TextView = itemView.findViewById(R.id.list_widgets_row_name)
var descriptionView: TextView = itemView.findViewById(R.id.list_widgets_row_description)
var iconView: ImageView = itemView.findViewById(R.id.list_widgets_row_icon)
var previewView: ImageView = itemView.findViewById(R.id.list_widgets_row_preview)
override fun onClick(v: View) {
tryBindWidget(widgets[bindingAdapterPosition])
}
init {
itemView.setOnClickListener(this)
}
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
val label = widgets[i].loadLabel(this@SelectWidgetActivity)
val description = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
widgets[i].loadDescription(this@SelectWidgetActivity)
} else {
""
}
val preview =
widgets[i].loadPreviewImage(this@SelectWidgetActivity)
val icon =
widgets[i].loadIcon(this@SelectWidgetActivity)
viewHolder.textView.text = label
viewHolder.descriptionView.text = description
viewHolder.descriptionView.visibility =
if (description?.isEmpty() == false) { View.VISIBLE } else { View.GONE }
viewHolder.iconView.setImageDrawable(icon)
viewHolder.previewView.setImageDrawable(preview)
viewHolder.previewView.visibility =
if (preview != null) { View.VISIBLE } else { View.GONE }
viewHolder.previewView.requestLayout()
}
override fun getItemCount(): Int {
return widgets.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view: View = inflater.inflate(R.layout.list_widgets_row, parent, false)
return ViewHolder(view)
}
}
}

View file

@ -0,0 +1,172 @@
package de.jrpie.android.launcher.ui.widgets.manage
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.graphics.RectF
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import androidx.core.graphics.contains
import androidx.core.graphics.minus
import androidx.core.graphics.toRect
import androidx.core.view.children
import de.jrpie.android.launcher.ui.widgets.WidgetContainerView
import de.jrpie.android.launcher.widgets.Widget
import de.jrpie.android.launcher.widgets.WidgetPosition
import de.jrpie.android.launcher.widgets.updateWidget
import kotlin.math.max
import kotlin.math.min
/**
* A variant of the [WidgetContainerView] which allows to manage widgets.
*/
class WidgetManagerView(context: Context, attrs: AttributeSet? = null) :
WidgetContainerView(context, attrs) {
val TOUCH_SLOP: Int
val TOUCH_SLOP_SQUARE: Int
val LONG_PRESS_TIMEOUT: Long
init {
val configuration = ViewConfiguration.get(context)
TOUCH_SLOP = configuration.scaledTouchSlop
TOUCH_SLOP_SQUARE = TOUCH_SLOP * TOUCH_SLOP
LONG_PRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout().toLong()
}
enum class EditMode(val resize: (dx: Int, dy: Int, rect: Rect) -> Rect) {
MOVE({ dx, dy, rect ->
Rect(rect.left + dx, rect.top + dy, rect.right + dx, rect.bottom + dy)
}),
TOP({ dx, dy, rect ->
Rect(rect.left, min(rect.top + dy, rect.bottom - 200), rect.right, rect.bottom)
}),
BOTTOM({ dx, dy, rect ->
Rect(rect.left, rect.top, rect.right, max(rect.top + 200, rect.bottom + dy))
}),
LEFT({ dx, dy, rect ->
Rect(min(rect.left + dx, rect.right - 200), rect.top, rect.right, rect.bottom)
}),
RIGHT({ dx, dy, rect ->
Rect(rect.left, rect.top, max(rect.left + 200, rect.right + dx), rect.bottom)
}),
}
var selectedWidgetOverlayView: WidgetOverlayView? = null
var selectedWidgetView: View? = null
var currentGestureStart: Point? = null
var startWidgetPosition: Rect? = null
var lastPosition = Rect()
private val longPressHandler = Handler(Looper.getMainLooper())
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return true
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event == null) {
return false
}
synchronized(this) {
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
val start = Point(event.x.toInt(), event.y.toInt())
currentGestureStart = start
val view = children.mapNotNull { it as? WidgetOverlayView }.firstOrNull {
RectF(it.x, it.y, it.x + it.width, it.y + it.height).toRect().contains(start) == true
} ?: return false
val position = (view.layoutParams as Companion.LayoutParams).position.getAbsoluteRect(width, height)
selectedWidgetOverlayView = view
selectedWidgetView = widgetViewById.get(view.widgetId) ?: return true
startWidgetPosition = position
val positionInView = start.minus(Point(position.left, position.top))
view.mode = view.getHandles().firstOrNull { it.position.contains(positionInView) }?.mode ?: EditMode.MOVE
longPressHandler.postDelayed({
synchronized(this@WidgetManagerView) {
view.showPopupMenu()
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
endInteraction()
}
}, LONG_PRESS_TIMEOUT)
}
if (event.actionMasked == MotionEvent.ACTION_MOVE ||
event.actionMasked == MotionEvent.ACTION_UP
) {
val distanceX = event.x - (currentGestureStart?.x ?: return true)
val distanceY = event.y - (currentGestureStart?.y ?: return true)
if (distanceX * distanceX + distanceY * distanceY > TOUCH_SLOP_SQUARE) {
longPressHandler.removeCallbacksAndMessages(null)
}
val view = selectedWidgetOverlayView ?: return true
val start = startWidgetPosition ?: return true
val absoluteNewPosition = view.mode?.resize(
distanceX.toInt(),
distanceY.toInt(),
start
) ?: return true
val newPosition = WidgetPosition.fromAbsoluteRect(
absoluteNewPosition, width, height
)
if (newPosition != lastPosition) {
lastPosition = absoluteNewPosition
(view.layoutParams as Companion.LayoutParams).position = newPosition
(selectedWidgetView?.layoutParams as? Companion.LayoutParams)?.position = newPosition
requestLayout()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_PRESS)
}
}
if (event.actionMasked == MotionEvent.ACTION_UP) {
longPressHandler.removeCallbacksAndMessages(null)
val id = selectedWidgetOverlayView?.widgetId ?: return true
val widget = Widget.byId(context, id) ?: return true
widget.position = newPosition
endInteraction()
updateWidget(widget)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END)
}
}
}
}
return true
}
private fun endInteraction() {
startWidgetPosition = null
selectedWidgetOverlayView?.mode = null
}
override fun updateWidgets(activity: Activity, widgets: Set<Widget>?) {
super.updateWidgets(activity, widgets)
if (widgets == null) {
return
}
widgets.forEach { widget ->
WidgetOverlayView(activity).let {
addView(it)
it.widgetId = widget.id
(it.layoutParams as Companion.LayoutParams).position = widget.position
}
}
}
}

View file

@ -0,0 +1,131 @@
package de.jrpie.android.launcher.ui.widgets.manage
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.PopupMenu
import androidx.core.graphics.toRectF
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.widgets.Widget
import de.jrpie.android.launcher.widgets.updateWidget
/**
* An overlay to show configuration options for a widget.
*/
private const val HANDLE_SIZE = 100
private const val HANDLE_EDGE_SIZE = (1.2 * HANDLE_SIZE).toInt()
class WidgetOverlayView : View {
val paint = Paint()
val handlePaint = Paint()
val selectedHandlePaint = Paint()
var mode: WidgetManagerView.EditMode? = null
class Handle(val mode: WidgetManagerView.EditMode, val position: Rect)
init {
handlePaint.style = Paint.Style.STROKE
handlePaint.setARGB(255, 255, 255, 255)
selectedHandlePaint.style = Paint.Style.FILL_AND_STROKE
selectedHandlePaint.setARGB(100, 255, 255, 255)
paint.style = Paint.Style.STROKE
paint.setARGB(255, 255, 255, 255)
}
private var preview: Drawable? = null
var widgetId: Int = -1
set(newId) {
field = newId
preview = Widget.byId(context, widgetId)?.getPreview(context)
}
constructor(context: Context) : super(context) {
init(null, 0)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(attrs, 0)
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context,
attrs,
defStyle
) {
init(attrs, defStyle)
}
private fun init(attrs: AttributeSet?, defStyle: Int) { }
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
getHandles().forEach {
if (it.mode == mode) {
canvas.drawRoundRect(it.position.toRectF(), 5f, 5f, selectedHandlePaint)
} else {
canvas.drawRoundRect(it.position.toRectF(), 5f, 5f, handlePaint)
}
}
val bounds = getBounds()
canvas.drawRoundRect(bounds.toRectF(), 5f, 5f, paint)
if (mode == null) {
return
}
//preview?.bounds = bounds
//preview?.draw(canvas)
}
fun showPopupMenu() {
val widget = Widget.byId(context, widgetId)?: return
val menu = PopupMenu(context, this)
menu.menu.let {
it.add(
context.getString(R.string.widget_menu_remove)
).setOnMenuItemClickListener { _ ->
Widget.byId(context, widgetId)?.delete(context)
return@setOnMenuItemClickListener true
}
it.add(
if (widget.allowInteraction) {
context.getString(R.string.widget_menu_disable_interaction)
} else {
context.getString(R.string.widget_menu_enable_interaction)
}
).setOnMenuItemClickListener { _ ->
widget.allowInteraction = !widget.allowInteraction
updateWidget(widget)
return@setOnMenuItemClickListener true
}
}
menu.show()
}
fun getHandles(): List<Handle> {
return listOf<Handle>(
Handle(WidgetManagerView.EditMode.TOP,
Rect(HANDLE_EDGE_SIZE, 0, width - HANDLE_EDGE_SIZE, HANDLE_SIZE)),
Handle(WidgetManagerView.EditMode.BOTTOM,
Rect(HANDLE_EDGE_SIZE, height - HANDLE_SIZE, width - HANDLE_EDGE_SIZE, height)),
Handle(WidgetManagerView.EditMode.LEFT,
Rect(0, HANDLE_EDGE_SIZE, HANDLE_SIZE, height - HANDLE_EDGE_SIZE)),
Handle(WidgetManagerView.EditMode.RIGHT,
Rect(width - HANDLE_SIZE, HANDLE_EDGE_SIZE, width, height - HANDLE_EDGE_SIZE))
)
}
private fun getBounds(): Rect {
return Rect(0,0, width, height)
}
}

View file

@ -0,0 +1,120 @@
package de.jrpie.android.launcher.widgets;
import android.app.Activity
import android.appwidget.AppWidgetHostView
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.SizeF
import android.view.View
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.ui.HomeActivity
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("widget:app")
class AppWidget(
override val id: Int,
override var position: WidgetPosition = WidgetPosition(0,0,1,1),
override var allowInteraction: Boolean = false,
// We keep track of packageName, className and user to make it possible to restore the widget
// on a new device when restoring settings (currently not implemented)
// In normal operation only id and position are used.
val packageName: String? = null,
val className: String? = null,
val user: Int? = null
): Widget() {
constructor(id: Int, widgetProviderInfo: AppWidgetProviderInfo, position: WidgetPosition) :
this(
id,
position,
false,
widgetProviderInfo.provider.packageName,
widgetProviderInfo.provider.className,
widgetProviderInfo.profile.hashCode()
)
/**
* Get the [AppWidgetProviderInfo] by [id].
* If the widget is not installed, use [restoreAppWidgetProviderInfo] instead.
*/
fun getAppWidgetProviderInfo(context: Context): AppWidgetProviderInfo? {
if (id < 0) {
return null
}
return (context.applicationContext as Application).appWidgetManager
.getAppWidgetInfo(id)
}
/**
* Restore the AppWidgetProviderInfo from [user], [packageName] and [className].
* Only use this when the widget is not installed,
* in normal operation use [getAppWidgetProviderInfo] instead.
*/
/*fun restoreAppWidgetProviderInfo(context: Context): AppWidgetProviderInfo? {
return getAppWidgetProviders(context).firstOrNull {
it.profile.hashCode() == user
&& it.provider.packageName == packageName
&& it.provider.className == className
}
}*/
override fun toString(): String {
return "WidgetInfo(id=$id, position=$position, packageName=$packageName, className=$className, user=$user)"
}
override fun createView(activity: Activity): AppWidgetHostView? {
val providerInfo = activity.getAppWidgetManager().getAppWidgetInfo(id) ?: return null
val view = activity.getAppWidgetHost()
.createView(activity, this.id, providerInfo)
val dp = activity.resources.displayMetrics.density
val screenWidth = activity.resources.displayMetrics.widthPixels
val screenHeight = activity.resources.displayMetrics.heightPixels
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val absolutePosition = position.getAbsoluteRect(screenWidth, screenHeight)
view.updateAppWidgetSize(Bundle.EMPTY,
listOf(SizeF(
absolutePosition.width() / dp,
absolutePosition.height() / dp
)))
}
view.setPadding(0,0,0,0)
return view
}
override fun findView(views: Sequence<View>): AppWidgetHostView? {
return views.mapNotNull { it as? AppWidgetHostView }.firstOrNull { it.appWidgetId == id }
}
override fun getIcon(context: Context): Drawable? {
return context.getAppWidgetManager().getAppWidgetInfo(id)?.loadIcon(context, DisplayMetrics.DENSITY_HIGH)
}
override fun getPreview(context: Context): Drawable? {
return context.getAppWidgetManager().getAppWidgetInfo(id)?.loadPreviewImage(context, DisplayMetrics.DENSITY_HIGH)
}
override fun isConfigurable(context: Context): Boolean {
return context.getAppWidgetManager().getAppWidgetInfo(id)?.configure != null
}
override fun configure(activity: Activity, requestCode: Int) {
if (!isConfigurable(activity)) {
return
}
activity.getAppWidgetHost().startAppWidgetConfigureActivityForResult(
activity,
id,
0,
requestCode,
null
)
}
}

View file

@ -0,0 +1,41 @@
package de.jrpie.android.launcher.widgets
import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import de.jrpie.android.launcher.ui.widgets.ClockView
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("widget:clock")
class ClockWidget(
override val id: Int,
override var position: WidgetPosition,
override var allowInteraction: Boolean = true
) : Widget() {
override fun createView(activity: Activity): View? {
return ClockView(activity, null, id)
}
override fun findView(views: Sequence<View>): ClockView? {
return views.mapNotNull { it as? ClockView }.firstOrNull { it.appWidgetId == id }
}
override fun getPreview(context: Context): Drawable? {
return null
}
override fun getIcon(context: Context): Drawable? {
return null
}
override fun isConfigurable(context: Context): Boolean {
return false
}
override fun configure(activity: Activity, requestCode: Int) { }
}

View file

@ -0,0 +1,58 @@
package de.jrpie.android.launcher.widgets
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.DisplayMetrics
import androidx.appcompat.content.res.AppCompatResources
import de.jrpie.android.launcher.R
sealed class LauncherWidgetProvider {
abstract fun loadLabel(context: Context): CharSequence?
abstract fun loadPreviewImage(context: Context): Drawable?
abstract fun loadIcon(context: Context): Drawable?
abstract fun loadDescription(context: Context): CharSequence?
}
class LauncherAppWidgetProvider(val info: AppWidgetProviderInfo) : LauncherWidgetProvider() {
override fun loadLabel(context: Context): CharSequence? {
return info.loadLabel(context.packageManager)
}
override fun loadPreviewImage(context: Context): Drawable? {
return info.loadPreviewImage(context, DisplayMetrics.DENSITY_DEFAULT)
}
override fun loadIcon(context: Context): Drawable? {
return info.loadIcon(context, DisplayMetrics.DENSITY_DEFAULT)
}
override fun loadDescription(context: Context): CharSequence? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
info.loadDescription(context)
} else {
null
}
}
}
class LauncherClockWidgetProvider : LauncherWidgetProvider() {
override fun loadLabel(context: Context): CharSequence? {
return context.getString(R.string.widget_clock_label)
}
override fun loadDescription(context: Context): CharSequence? {
return context.getString(R.string.widget_clock_description)
}
override fun loadPreviewImage(context: Context): Drawable? {
return null
}
override fun loadIcon(context: Context): Drawable? {
return AppCompatResources.getDrawable(context, R.drawable.baseline_clock_24)
}
}

View file

@ -0,0 +1,60 @@
package de.jrpie.android.launcher.widgets
import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.preferences.LauncherPreferences
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@Serializable
sealed class Widget {
abstract val id: Int
abstract var position: WidgetPosition
abstract var allowInteraction: Boolean
/**
* @param activity The activity where the view will be used. Must not be an AppCompatActivity.
*/
abstract fun createView(activity: Activity): View?
abstract fun findView(views: Sequence<View>): View?
abstract fun getPreview(context: Context): Drawable?
abstract fun getIcon(context: Context): Drawable?
abstract fun isConfigurable(context: Context): Boolean
abstract fun configure(activity: Activity, requestCode: Int)
fun delete(context: Context) {
context.getAppWidgetHost().deleteAppWidgetId(id)
LauncherPreferences.widgets().widgets(
LauncherPreferences.widgets().widgets()?.also {
it.remove(this)
}
)
}
override fun hashCode(): Int {
return id
}
override fun equals(other: Any?): Boolean {
return (other as? Widget)?.id == id
}
fun serialize(): String {
return Json.encodeToString(serializer(), this)
}
companion object {
fun deserialize(serialized: String): Widget {
return Json.decodeFromString(serialized)
}
fun byId(context: Context, id: Int): Widget? {
return (context.applicationContext as Application).widgets.value?.firstOrNull {
it.id == id
}
}
}
}

View file

@ -0,0 +1,58 @@
package de.jrpie.android.launcher.widgets
import android.graphics.Rect
import kotlinx.serialization.Serializable
import kotlin.math.ceil
import kotlin.math.roundToInt
import kotlin.math.max
const val GRID_SIZE: Short = 12
@Serializable
data class WidgetPosition(var x: Short, var y: Short, var width: Short, var height: Short) {
fun getAbsoluteRect(screenWidth: Int, screenHeight: Int): Rect {
val gridWidth = screenWidth / GRID_SIZE.toFloat()
val gridHeight= screenHeight / GRID_SIZE.toFloat()
return Rect(
(x * gridWidth).toInt(),
(y * gridHeight).toInt(),
((x + width) * gridWidth).toInt(),
((y + height) * gridHeight).toInt()
)
}
companion object {
fun fromAbsoluteRect(absolute: Rect, screenWidth: Int, screenHeight: Int): WidgetPosition {
val gridWidth = screenWidth / GRID_SIZE.toFloat()
val gridHeight= screenHeight / GRID_SIZE.toFloat()
val x = (absolute.left / gridWidth).roundToInt().toShort().coerceIn(0, (GRID_SIZE-1).toShort())
val y = (absolute.top / gridHeight).roundToInt().toShort().coerceIn(0, (GRID_SIZE-1).toShort())
val w = max(2, ((absolute.right - absolute.left) / gridWidth).roundToInt()).toShort()
val h = max(2, ((absolute.bottom - absolute.top) / gridHeight).roundToInt()).toShort()
return WidgetPosition(x,y,w,h)
}
fun center(minWidth: Int, minHeight: Int, screenWidth: Int, screenHeight: Int): WidgetPosition {
val gridWidth = screenWidth / GRID_SIZE.toFloat()
val gridHeight= screenHeight / GRID_SIZE.toFloat()
val cellsWidth = ceil(minWidth / gridWidth).toInt().toShort()
val cellsHeight = ceil(minHeight / gridHeight).toInt().toShort()
return WidgetPosition(
((GRID_SIZE - cellsWidth) / 2).toShort(),
((GRID_SIZE - cellsHeight) / 2).toShort(),
cellsWidth,
cellsHeight
)
}
}
}

View file

@ -0,0 +1,87 @@
package de.jrpie.android.launcher.widgets
import android.app.Activity
import android.app.Service
import android.appwidget.AppWidgetHost
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.os.Build
import android.os.UserManager
import android.util.Log
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.preferences.LauncherPreferences
fun deleteAllWidgets(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.getAppWidgetHost().appWidgetIds.forEach { AppWidget(it).delete(context) }
}
}
/**
* Tries to bind [providerInfo] to the id [id].
* @param providerInfo The widget to be bound.
* @param id The id to bind the widget to. If -1 is provided, a new id is allocated.
* @param
* @param requestCode Used to start an activity to request permission to bind the widget.
*
* @return true iff the app widget was bound successfully.
*/
fun bindAppWidgetOrRequestPermission(activity: Activity, providerInfo: AppWidgetProviderInfo, id: Int, requestCode: Int? = null): Boolean {
val appWidgetId = if(id == -1) {
activity.getAppWidgetHost().allocateAppWidgetId()
} else { id }
Log.i("Launcher", "Binding new widget ${appWidgetId}")
if (!activity.getAppWidgetManager().bindAppWidgetIdIfAllowed(
appWidgetId,
providerInfo.provider
)
) {
Log.i("Widgets", "requesting permission for widget")
val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_BIND).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId)
putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, providerInfo.provider)
}
activity.startActivityForResult(intent, requestCode ?: 0)
return false
}
return true
}
fun getAppWidgetProviders( context: Context ): List<LauncherWidgetProvider> {
val list = mutableListOf<LauncherWidgetProvider>(LauncherClockWidgetProvider())
val appWidgetManager = context.getAppWidgetManager()
val profiles =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
(context.getSystemService(Service.LAUNCHER_APPS_SERVICE) as LauncherApps).profiles
} else {
(context.getSystemService(Service.USER_SERVICE) as UserManager).userProfiles
}
list.addAll(
profiles.map {
appWidgetManager.getInstalledProvidersForProfile(it)
.map { LauncherAppWidgetProvider(it) }
}.flatten()
)
return list
}
fun updateWidget(widget: Widget) {
var widgets = LauncherPreferences.widgets().widgets() ?: setOf()
widgets = widgets.minus(widget).plus(widget)
LauncherPreferences.widgets().widgets(widgets)
}
fun Context.getAppWidgetHost(): AppWidgetHost {
return (this.applicationContext as Application).appWidgetHost
}
fun Context.getAppWidgetManager(): AppWidgetManager {
return (this.applicationContext as Application).appWidgetManager
}

View file

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:textColor"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</vector>

View file

@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:textColor"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z" />
<path
android:fillColor="?android:textColor"
android:pathData="M12.5,7H11v6l5.25,3.15 0.75,-1.23 -4.5,-2.67z" />
</vector>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.widgets.manage.ManageWidgetsActivity">
<de.jrpie.android.launcher.ui.widgets.manage.WidgetManagerView
android:id="@+id/manage_widgets_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/manage_widgets_button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:focusable="true"
android:src="@drawable/baseline_add_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/select_widget_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.widgets.manage.SelectWidgetActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/select_widget_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@null"
app:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/select_widget_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/select_widget_title"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textSize="30sp"
app:layout_constraintEnd_toStartOf="@id/select_widget_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/select_widget_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center"
android:includeFontPadding="true"
android:paddingLeft="16sp"
android:paddingRight="16sp"
android:src="@drawable/baseline_close_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/select_widget_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/select_widget_appbar" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="false"
android:fitsSystemWindows="true"
tools:context=".ui.widgets.ClockView">
<TextClock
android:id="@+id/clock_upper_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="2024-12-24" />
<TextClock
android:id="@+id/clock_lower_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:textSize="18sp"
tools:text="18:00:00"
app:layout_constraintTop_toBottomOf="@+id/clock_upper_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -10,29 +10,10 @@
android:fitsSystemWindows="true"
tools:context=".ui.HomeActivity">
<TextClock
android:id="@+id/home_upper_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45"
tools:text="2024-12-24" />
<TextClock
android:id="@+id/home_lower_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:textSize="18sp"
tools:text="18:00:00"
app:layout_constraintTop_toBottomOf="@+id/home_upper_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<de.jrpie.android.launcher.ui.widgets.WidgetContainerView
android:id="@+id/home_widget_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- only shown when µLauncher settings can't be reached by a gesture -->
<ImageView

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_apps_row_container"
android:background="@color/cardview_dark_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15sp">
<ImageView
android:id="@+id/list_widgets_header_icon"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:src="@mipmap/ic_launcher_round"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/list_widgets_header_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:gravity="start"
android:text=""
android:textSize="20sp"
tools:text="some widget"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/list_widgets_header_icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_apps_row_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15sp">
<ImageView
android:id="@+id/list_widgets_row_icon"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:src="@mipmap/ic_launcher_round"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/list_widgets_row_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10sp"
android:layout_marginEnd="10sp"
android:gravity="start"
android:text=""
android:textSize="20sp"
tools:text="some widget"
app:layout_constraintStart_toEndOf="@id/list_widgets_row_icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/list_widgets_row_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10sp"
android:gravity="start"
android:text=""
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/list_widgets_row_name"
app:layout_constraintTop_toBottomOf="@+id/list_widgets_row_name"
tools:text="a longer description of the widget" />
<ImageView
android:id="@+id/list_widgets_row_preview"
android:layout_width="0dp"
android:maxHeight="100dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/list_widgets_row_description"
tools:src="@mipmap/ic_launcher_round"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -11,5 +11,9 @@
<color name="lightTheme_background_color">#fff</color>
<color name="lightTheme_accent_color">#9999ff</color>
<color name="lightTheme_text_color">#000</color>
<color name="light_blue_400">#FF29B6F6</color>
<color name="light_blue_600">#FF039BE5</color>
<color name="gray_400">#FFBDBDBD</color>
<color name="gray_600">#FF757575</color>
</resources>

View file

@ -9,6 +9,7 @@
<string name="settings_internal_started_key" translatable="false">internal.started_before</string>
<string name="settings_internal_started_time_key" translatable="false">internal.first_startup</string>
<string name="settings_internal_version_code_key" translatable="false">internal.version_code</string>
<string name="settings_widgets_widgets_key" translatable="false">widgets.widgets</string>
<string name="settings_apps_favorites_key" translatable="false">apps.favorites</string>
<string name="settings_apps_hidden_key" translatable="false">apps.hidden</string>
<string name="settings_apps_pinned_shortcuts_key" translatable="false">apps.pinned_shortcuts</string>

View file

@ -98,6 +98,8 @@
<string name="settings_gesture_time">Time</string>
<string name="settings_gesture_description_time">Click on time</string>
<string name="settings_widgets_widgets">Manage widgets</string>
<string name="settings_apps_choose">Choose App</string>
@ -388,5 +390,15 @@
<string name="legal_info_title">Open Source Licenses</string>
<string name="toast_activity_not_found_search_web">No app found to handle search.</string>
<string name="toast_activity_not_found_browser">Can\'t open URL: no browser found.</string>
<string name="select_widget_title">Choose Widget</string>
<string name="widget_menu_remove">Remove</string>
<string name="widget_menu_configure">Configure</string>
<string name="widget_menu_enable_interaction">Enable Interaction</string>
<string name="widget_menu_disable_interaction">Disable Interaction</string>
<string name="widget_clock_label">Clock</string>
<string name="widget_clock_description">The default clock of μLauncher</string>
</resources>

View file

@ -66,12 +66,12 @@
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">2</item>
</style>
<style name="textShadowLight" parent="textShadow">
<item name="android:shadowColor">#aaa</item>
</style>
<style name="backgroundWallpaper">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
@ -81,26 +81,30 @@
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
<style name="backgroundSolid">
</style>
<style name="backgroundSolid"></style>
<style name="fontSystemDefault">
<!--<item name="android:textSize">18sp</item>-->
</style>
<style name="fontHack">
<item name="android:fontFamily">@font/hack</item>
<!--<item name="android:textSize">18sp</item>-->
</style>
<style name="fontMonospace">
<item name="android:fontFamily">monospace</item>
</style>
<style name="fontSerifMonospace">
<item name="android:fontFamily">serif-monospace</item>
</style>
<style name="fontSansSerif">
<item name="android:fontFamily">sans-serif</item>
</style>
<style name="fontSerif" tools:keep="@style/fontSerif">
<item name="android:fontFamily">serif</item>
</style>

View file

@ -6,6 +6,10 @@
<Preference
android:key="@string/settings_general_choose_home_screen_key"
android:title="@string/settings_general_choose_home_screen"/>
<Preference
android:key="@string/settings_widgets_widgets_key"
android:title="@string/settings_widgets_widgets"/>
</PreferenceCategory>
<PreferenceCategory