Remove obsolete API level checks

This commit is contained in:
MM20 2022-01-29 19:32:53 +01:00
parent 05680afcd5
commit 97ba10d3a7
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
17 changed files with 105 additions and 135 deletions

View File

@ -3,7 +3,6 @@ package de.mm20.launcher2.activity;
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.pm.LauncherApps import android.content.pm.LauncherApps
import android.os.Build
import android.os.Bundle import android.os.Bundle
import de.mm20.launcher2.favorites.FavoritesRepository import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.search.data.AppShortcut import de.mm20.launcher2.search.data.AppShortcut
@ -15,18 +14,16 @@ class AddItemActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val pinRequest = launcherApps.getPinItemRequest(intent) ?: return run { finish() }
val pinRequest = launcherApps.getPinItemRequest(intent) ?: return run { finish() } val shortcutInfo = pinRequest.shortcutInfo ?: return run { finish() }
val shortcutInfo = pinRequest.shortcutInfo ?: return run { finish() } val shortcut = AppShortcut(
val shortcut = AppShortcut( this.applicationContext, shortcutInfo,
this.applicationContext, shortcutInfo, packageManager.getApplicationInfo(shortcutInfo.`package`, 0)
packageManager.getApplicationInfo(shortcutInfo.`package`, 0) .loadLabel(packageManager).toString()
.loadLabel(packageManager).toString() )
) if (pinRequest.accept()) {
if (pinRequest.accept()) { favoritesRepository.pinItem(shortcut)
favoritesRepository.pinItem(shortcut)
}
} }
finish() finish()
} }

View File

@ -96,11 +96,6 @@ class PreferencesAppearanceFragment : PreferenceFragmentCompat() {
.show() .show()
true true
} }
val systemBarsCategory = findPreference<PreferenceCategory>("system_bars")!!
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
systemBarsCategory.removePreference(findPreference("light_nav_bar"))
}
} }

View File

@ -40,11 +40,7 @@ internal class AppRepositoryImpl(
private val profiles: List<UserHandle> = private val profiles: List<UserHandle> =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { launcherApps.profiles.takeIf { it.isNotEmpty() } ?: listOf(Process.myUserHandle())
launcherApps.profiles.takeIf { it.isNotEmpty() } ?: listOf(Process.myUserHandle())
} else {
listOf(Process.myUserHandle())
}
private val installingPackages = mutableMapOf<Int, String>() private val installingPackages = mutableMapOf<Int, String>()

View File

@ -48,7 +48,6 @@ class LauncherAppDeserializer(val context: Context) : SearchableDeserializer {
} }
class AppShortcutSerializer : SearchableSerializer { class AppShortcutSerializer : SearchableSerializer {
@RequiresApi(Build.VERSION_CODES.N_MR1)
override fun serialize(searchable: Searchable): String { override fun serialize(searchable: Searchable): String {
searchable as AppShortcut searchable as AppShortcut
return jsonObjectOf( return jsonObjectOf(
@ -67,7 +66,6 @@ class AppShortcutDeserializer(
val context: Context val context: Context
) : SearchableDeserializer, KoinComponent { ) : SearchableDeserializer, KoinComponent {
@RequiresApi(Build.VERSION_CODES.N_MR1)
override fun deserialize(serialized: String): Searchable? { override fun deserialize(serialized: String): Searchable? {
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
if (!launcherApps.hasShortcutHostPermission()) return null if (!launcherApps.hasShortcutHostPermission()) return null

View File

@ -22,7 +22,6 @@ import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@RequiresApi(Build.VERSION_CODES.N_MR1)
class AppShortcut( class AppShortcut(
context: Context, context: Context,
val launcherShortcut: ShortcutInfo, val launcherShortcut: ShortcutInfo,
@ -78,7 +77,7 @@ class AppShortcut(
context.resources.displayMetrics.densityDpi context.resources.displayMetrics.densityDpi
) )
} ?: return null } ?: return null
if (isAtLeastApiLevel(Build.VERSION_CODES.O) && icon is AdaptiveIconDrawable) { if (icon is AdaptiveIconDrawable) {
return LauncherIcon( return LauncherIcon(
foreground = icon.foreground, foreground = icon.foreground,
background = icon.background, background = icon.background,

View File

@ -8,15 +8,12 @@ import android.content.pm.LauncherApps
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo import android.content.pm.ShortcutInfo
import android.graphics.drawable.AdaptiveIconDrawable import android.graphics.drawable.AdaptiveIconDrawable
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Process import android.os.Process
import android.os.UserHandle import android.os.UserHandle
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import de.mm20.launcher2.icons.LauncherIcon import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.ktx.getSerialNumber import de.mm20.launcher2.ktx.getSerialNumber
import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.preferences.Settings
import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -36,26 +33,24 @@ class LauncherApp(
version = getPackageVersionName(context, launcherActivityInfo.applicationInfo.packageName), version = getPackageVersionName(context, launcherActivityInfo.applicationInfo.packageName),
shortcuts = run { shortcuts = run {
val appShortcuts = mutableListOf<AppShortcut>() val appShortcuts = mutableListOf<AppShortcut>()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { val launcherApps = context.getSystemService<LauncherApps>()!!
val launcherApps = context.getSystemService<LauncherApps>()!! if (!launcherApps.hasShortcutHostPermission()) return@run appShortcuts
if (!launcherApps.hasShortcutHostPermission()) return@run appShortcuts val query = LauncherApps.ShortcutQuery()
val query = LauncherApps.ShortcutQuery() .setPackage(launcherActivityInfo.applicationInfo.packageName)
.setPackage(launcherActivityInfo.applicationInfo.packageName) .setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC or LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST)
.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC or LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST) val shortcuts = try {
val shortcuts = try { launcherApps.getShortcuts(query, launcherActivityInfo.user)
launcherApps.getShortcuts(query, launcherActivityInfo.user) } catch (e: IllegalStateException) {
} catch (e: IllegalStateException) { emptyList<ShortcutInfo>()
emptyList<ShortcutInfo>()
}
appShortcuts.addAll(shortcuts?.map {
AppShortcut(
context,
it,
launcherActivityInfo.label.toString()
)
}
?: emptyList())
} }
appShortcuts.addAll(shortcuts?.map {
AppShortcut(
context,
it,
launcherActivityInfo.label.toString()
)
}
?: emptyList())
appShortcuts appShortcuts
} }
), KoinComponent { ), KoinComponent {
@ -73,7 +68,11 @@ class LauncherApp(
return launcherActivityInfo.user return launcherActivityInfo.user
} }
override suspend fun loadIcon(context: Context, size: Int, legacyIconBackground: LegacyIconBackground): LauncherIcon? { override suspend fun loadIcon(
context: Context,
size: Int,
legacyIconBackground: LegacyIconBackground
): LauncherIcon? {
try { try {
val icon = val icon =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {

View File

@ -17,7 +17,7 @@ class BadgeDrawable(context: Context, drawable: Drawable) : Drawable() {
init { init {
val size = (28.8 * context.dp).roundToInt() val size = (28.8 * context.dp).roundToInt()
val drw: Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable) { val drw: Drawable = if (drawable is AdaptiveIconDrawable) {
LayerDrawable(arrayOf( LayerDrawable(arrayOf(
drawable.background, drawable.background,
drawable.foreground drawable.foreground

View File

@ -124,13 +124,11 @@ public class CrashUtil {
} }
private static void createNotificationChannel(NotificationManager notificationManager, Context context) { private static void createNotificationChannel(NotificationManager notificationManager, Context context) {
if (Build.VERSION.SDK_INT >= 26) { CharSequence name = context.getString(R.string.notification_crash_report_title);
CharSequence name = context.getString(R.string.notification_crash_report_title); String description = "";
String description = ""; NotificationChannel channel = new NotificationChannel(CHANNEL_NOTIFICATION_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
NotificationChannel channel = new NotificationChannel(CHANNEL_NOTIFICATION_ID, name, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(description);
channel.setDescription(description); notificationManager.createNotificationChannel(channel);
notificationManager.createNotificationChannel(channel);
}
} }
private static String getStackTrace(Throwable e) { private static String getStackTrace(Throwable e) {

View File

@ -10,7 +10,6 @@ import androidx.annotation.RequiresApi
import java.util.* import java.util.*
import kotlin.math.roundToInt import kotlin.math.roundToInt
@RequiresApi(Build.VERSION_CODES.O)
class ClockDynamicLauncherIcon( class ClockDynamicLauncherIcon(
foreground: LayerDrawable, foreground: LayerDrawable,
background: Drawable?, background: Drawable?,

View File

@ -14,7 +14,6 @@ import de.mm20.launcher2.search.data.Searchable
class GoogleClockIconProvider(val context: Context) : IconProvider { class GoogleClockIconProvider(val context: Context) : IconProvider {
override suspend fun getIcon(searchable: Searchable, size: Int): LauncherIcon? { override suspend fun getIcon(searchable: Searchable, size: Int): LauncherIcon? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return null
if (searchable !is Application) return null if (searchable !is Application) return null
if (searchable.`package` != "com.google.android.deskclock") return null if (searchable.`package` != "com.google.android.deskclock") return null
val pm = context.packageManager val pm = context.packageManager

View File

@ -48,7 +48,7 @@ class IconPackIconProvider(
?: return generateIcon(context, searchable.launcherActivityInfo, size) ?: return generateIcon(context, searchable.launcherActivityInfo, size)
val drawable = ResourcesCompat.getDrawable(res, resId, context.theme) ?: return null val drawable = ResourcesCompat.getDrawable(res, resId, context.theme) ?: return null
return when { return when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable -> { drawable is AdaptiveIconDrawable -> {
LauncherIcon( LauncherIcon(
foreground = drawable.foreground, foreground = drawable.foreground,
background = drawable.background, background = drawable.background,

View File

@ -81,9 +81,6 @@ class ThemedIconProvider(
} }
private fun getClockIcon(resources: Resources, resId: Int): LauncherIcon? { private fun getClockIcon(resources: Resources, resId: Int): LauncherIcon? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null
}
try { try {
val array = resources.obtainTypedArrayOrNull(resId) ?: return null val array = resources.obtainTypedArrayOrNull(resId) ?: return null
var i = 0 var i = 0

View File

@ -33,7 +33,6 @@ object LauncherIconShape {
} }
val platformDefault: Shape = run { val platformDefault: Shape = run {
val platformShape = getSystemShape() val platformShape = getSystemShape()
if (platformShape == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return@run CircleShape
GenericShape { size, _ -> GenericShape { size, _ ->
Log.d("MM20", "GenericShape {}") Log.d("MM20", "GenericShape {}")
val matrix = Matrix() val matrix = Matrix()
@ -45,11 +44,7 @@ object LauncherIconShape {
} }
} }
private fun getSystemShape(): Path? { private fun getSystemShape(): Path {
return if (isAtLeastApiLevel(Build.VERSION_CODES.O)) { return AdaptiveIconDrawable(null, null).iconMask
AdaptiveIconDrawable(null, null).iconMask
} else {
null
}
} }
} }

View File

@ -18,7 +18,6 @@ import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.ShapeDrawable import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape import android.graphics.drawable.shapes.OvalShape
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.Process import android.os.Process
import android.service.notification.StatusBarNotification import android.service.notification.StatusBarNotification
@ -37,8 +36,10 @@ import de.mm20.launcher2.badges.BadgeRepository
import de.mm20.launcher2.crashreporter.CrashReporter import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.favorites.FavoritesRepository import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.icons.IconRepository import de.mm20.launcher2.icons.IconRepository
import de.mm20.launcher2.ktx.* import de.mm20.launcher2.ktx.castToOrNull
import de.mm20.launcher2.ui.legacy.helper.ActivityStarter import de.mm20.launcher2.ktx.dp
import de.mm20.launcher2.ktx.getBadgeIcon
import de.mm20.launcher2.ktx.lifecycleOwner
import de.mm20.launcher2.notifications.NotificationRepository import de.mm20.launcher2.notifications.NotificationRepository
import de.mm20.launcher2.search.data.AppInstallation import de.mm20.launcher2.search.data.AppInstallation
import de.mm20.launcher2.search.data.Application import de.mm20.launcher2.search.data.Application
@ -46,11 +47,13 @@ import de.mm20.launcher2.search.data.LauncherApp
import de.mm20.launcher2.search.data.Searchable import de.mm20.launcher2.search.data.Searchable
import de.mm20.launcher2.transition.ChangingLayoutTransition import de.mm20.launcher2.transition.ChangingLayoutTransition
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.legacy.helper.ActivityStarter
import de.mm20.launcher2.ui.legacy.searchable.SearchableView import de.mm20.launcher2.ui.legacy.searchable.SearchableView
import de.mm20.launcher2.ui.legacy.view.* import de.mm20.launcher2.ui.legacy.view.*
import kotlinx.coroutines.* import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -176,7 +179,10 @@ class ApplicationDetailRepresentation : Representation, KoinComponent {
return scene return scene
} }
private fun updateNotifications(chipGroup: ChipGroup, notifications: List<StatusBarNotification>) { private fun updateNotifications(
chipGroup: ChipGroup,
notifications: List<StatusBarNotification>
) {
val context = chipGroup.context val context = chipGroup.context
chipGroup.removeAllViews() chipGroup.removeAllViews()
notifications.forEach { notifications.forEach {
@ -299,69 +305,67 @@ class ApplicationDetailRepresentation : Representation, KoinComponent {
private fun setupShortcuts(appShortcuts: ChipGroup, app: Application) { private fun setupShortcuts(appShortcuts: ChipGroup, app: Application) {
val context = appShortcuts.context val context = appShortcuts.context
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { val launcherApps =
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps if (launcherApps.hasShortcutHostPermission()) {
if (launcherApps.hasShortcutHostPermission()) { val shortcuts = app.shortcuts
val shortcuts = app.shortcuts
val repository: FavoritesRepository by inject() val repository: FavoritesRepository by inject()
var count = 0 var count = 0
for (si in shortcuts) { for (si in shortcuts) {
if (count > 4) break if (count > 4) break
count++ count++
val view = Chip(context) val view = Chip(context)
view.text = si.label view.text = si.label
view.chipIcon = createShortcutDrawable( view.chipIcon = createShortcutDrawable(
launcherApps.getShortcutBadgedIconDrawable( launcherApps.getShortcutBadgedIconDrawable(
si.launcherShortcut, si.launcherShortcut,
context.resources.displayMetrics.densityDpi context.resources.displayMetrics.densityDpi
)
) )
)
view.chipIconSize = 24 * context.dp view.chipIconSize = 24 * context.dp
view.chipIconTint = null view.chipIconTint = null
view.chipStrokeWidth = 1 * context.dp view.chipStrokeWidth = 1 * context.dp
view.chipStrokeColor = view.chipStrokeColor =
ContextCompat.getColorStateList(context, R.color.chip_stroke) ContextCompat.getColorStateList(context, R.color.chip_stroke)
view.chipBackgroundColor = view.chipBackgroundColor =
ContextCompat.getColorStateList(context, R.color.chip_background) ContextCompat.getColorStateList(context, R.color.chip_background)
view.setTextAppearanceResource(R.style.ChipTextAppearance) view.setTextAppearanceResource(R.style.ChipTextAppearance)
view.closeIcon = context.getDrawable(R.drawable.ic_star_solid) view.closeIcon = context.getDrawable(R.drawable.ic_star_solid)
view.closeIconTint = ColorStateList.valueOf( view.closeIconTint = ColorStateList.valueOf(
ContextCompat.getColor( ContextCompat.getColor(
context, context,
R.color.text_color_primary R.color.text_color_primary
)
) )
val isPinned = repository.isPinned(si).asLiveData() )
val isPinned = repository.isPinned(si).asLiveData()
isPinned.observe(context as LifecycleOwner, Observer { isPinned.observe(context as LifecycleOwner, Observer {
view.isCloseIconVisible = isPinned.value == true view.isCloseIconVisible = isPinned.value == true
}) })
view.setOnClickListener { view.setOnClickListener {
ActivityStarter.start(context, view) ActivityStarter.start(context, view)
launcherApps.startShortcut(si.launcherShortcut, null, null) launcherApps.startShortcut(si.launcherShortcut, null, null)
}
view.setOnLongClickListener {
if (isPinned.value == true) {
repository.unpinItem(si)
} else {
repository.pinItem(si)
}
true
}
view.setOnCloseIconClickListener {
repository.unpinItem(si)
view.isCloseIconVisible = false
}
appShortcuts.addView(view)
} }
view.setOnLongClickListener {
if (isPinned.value == true) {
repository.unpinItem(si)
} else {
repository.pinItem(si)
}
true
}
view.setOnCloseIconClickListener {
repository.unpinItem(si)
view.isCloseIconVisible = false
}
appShortcuts.addView(view)
} }
} }
} }

View File

@ -25,7 +25,6 @@ import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
@RequiresApi(Build.VERSION_CODES.N_MR1)
class AppShortcutDetailRepresentation : Representation, KoinComponent { class AppShortcutDetailRepresentation : Representation, KoinComponent {
private val iconRepository: IconRepository by inject() private val iconRepository: IconRepository by inject()

View File

@ -101,11 +101,7 @@ open class SearchableView(context: Context, representation: Int) : FrameLayout(c
is Contact -> ContactDetailRepresentation() is Contact -> ContactDetailRepresentation()
is CalendarEvent -> CalendarDetailRepresentation() is CalendarEvent -> CalendarDetailRepresentation()
is Wikipedia -> WikipediaDetailRepresentation() is Wikipedia -> WikipediaDetailRepresentation()
is AppShortcut -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { is AppShortcut -> AppShortcutDetailRepresentation()
AppShortcutDetailRepresentation()
} else {
return
}
else -> return else -> return
} }
applyScene(representation.getScene(this, searchable, previousRepresentation)) applyScene(representation.getScene(this, searchable, previousRepresentation))

View File

@ -59,7 +59,6 @@ class LauncherIconView : View, KoinComponent {
private var platformShapeBounds: RectF? = null private var platformShapeBounds: RectF? = null
@RequiresApi(Build.VERSION_CODES.O)
private fun getSystemShape(): Path { private fun getSystemShape(): Path {
return AdaptiveIconDrawable(null, null).iconMask return AdaptiveIconDrawable(null, null).iconMask
} }