Skip to content

Commit

Permalink
chore: update deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Jul 17, 2023
1 parent 9857db8 commit 9eac2a0
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class IconsCounter(override val count: Int = 0) :
Counter(R.string.icons, R.drawable.ic_icons, count)

data class WallpapersCounter(override val count: Int = 0) :
Counter(R.string.wallpapers, R.drawable.ic_wallpapers, count)
Counter(dev.jahir.frames.R.string.wallpapers, dev.jahir.frames.R.drawable.ic_wallpapers, count)

data class KustomCounter(override val count: Int = 0) :
Counter(R.string.templates, R.drawable.ic_kustom, count)
Counter(dev.jahir.kuper.R.string.templates, dev.jahir.kuper.R.drawable.ic_kustom, count)
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
}
}
val openIcon = if (isAnApp)
if (isInstalled) R.drawable.ic_open_app else R.drawable.ic_download
if (isInstalled) R.drawable.ic_open_app else dev.jahir.frames.R.drawable.ic_download
else R.drawable.ic_open_app
HomeItem(
it.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.jahir.blueprint.data.viewmodels
import android.app.Application
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LifecycleOwner
Expand All @@ -19,6 +18,7 @@ import dev.jahir.blueprint.extensions.blueprintFormat
import dev.jahir.blueprint.extensions.clean
import dev.jahir.blueprint.extensions.drawableRes
import dev.jahir.blueprint.extensions.getLocalizedName
import dev.jahir.blueprint.extensions.queryIntentActivitiesCompat
import dev.jahir.frames.extensions.context.getAppName
import dev.jahir.frames.extensions.context.integer
import dev.jahir.frames.extensions.context.withXml
Expand Down Expand Up @@ -145,12 +145,12 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application
val installedApps = ArrayList<RequestApp>()

val packagesList = try {
context.packageManager.queryIntentActivities(
context.packageManager.queryIntentActivitiesCompat(
Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER),
PackageManager.GET_RESOLVED_FILTER
)
} catch (e: Exception) {
ArrayList<ResolveInfo>()
ArrayList()
}

var loaded = 0
Expand Down Expand Up @@ -279,7 +279,7 @@ class RequestsViewModel(application: Application) : AndroidViewModel(application
}

internal fun deselectApp(app: RequestApp): Boolean {
if (selectedApps.isNullOrEmpty()) return false
if (selectedApps.isEmpty()) return false
if (selectedApps.contains(app))
selectedAppsData.postValue(ArrayList(selectedApps.apply { remove(app) }))
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fun getDefaultAppIcon(): Drawable? {
return Resources.getSystem().getAppIcon(android.R.mipmap.sym_def_app_icon)
}

@Suppress("USELESS_ELVIS")
fun Context.getAppIcon(pkg: String): Drawable? {
if (!pkg.hasContent()) return null
var icon: Drawable? = null
Expand Down Expand Up @@ -58,13 +57,12 @@ private fun Resources.getAppIcon(iconId: Int): Drawable? {
}
}

private fun Context.getAppInfo(pkg: String): ApplicationInfo? {
return try {
packageManager.getApplicationInfo(pkg, 0)
private fun Context.getAppInfo(pkg: String): ApplicationInfo? =
try {
packageManager.getApplicationInfoCompat(pkg, 0)
} catch (e: Exception) {
null
}
}

private fun Context.getResources(ai: ApplicationInfo): Resources? {
return try {
Expand Down
15 changes: 11 additions & 4 deletions library/src/main/kotlin/dev/jahir/blueprint/extensions/Context.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.jahir.blueprint.extensions

import android.annotation.SuppressLint
import android.content.Context
import androidx.annotation.DrawableRes
import dev.jahir.blueprint.R
import dev.jahir.frames.extensions.resources.lower
import java.util.concurrent.TimeUnit

@SuppressLint("DiscouragedApi")
@DrawableRes
fun Context.drawableRes(name: String): Int =
try {
Expand All @@ -17,7 +19,7 @@ fun Context.drawableRes(name: String): Int =
internal fun Context.getLocalizedName(packageName: String, defaultName: String): String {
var appName: String? = null
try {
val appInfo = packageManager.getApplicationInfo(
val appInfo = packageManager.getApplicationInfoCompat(
packageName, android.content.pm.PackageManager.GET_META_DATA
)
try {
Expand All @@ -28,10 +30,10 @@ internal fun Context.getLocalizedName(packageName: String, defaultName: String):
configuration.setLocale(java.util.Locale("en-US"))
appName =
altCntxt.createConfigurationContext(configuration).getString(appInfo.labelRes)
} catch (e: Exception) {
} catch (_: Exception) {
}
if (appName == null) appName = packageManager.getApplicationLabel(appInfo).toString()
} catch (e: Exception) {
} catch (_: Exception) {
}
return appName ?: defaultName
}
Expand All @@ -41,19 +43,24 @@ internal fun Context.millisToText(millis: Long): String =
TimeUnit.MILLISECONDS.toSeconds(millis) < 60 ->
(TimeUnit.MILLISECONDS.toSeconds(millis)
.toString() + " " + getString(R.string.seconds).lower())

TimeUnit.MILLISECONDS.toMinutes(millis) < 60 ->
(TimeUnit.MILLISECONDS.toMinutes(millis)
.toString() + " " + getString(R.string.minutes).lower())

TimeUnit.MILLISECONDS.toHours(millis) < 24 ->
(TimeUnit.MILLISECONDS.toHours(millis)
.toString() + " " + getString(R.string.hours).lower())

TimeUnit.MILLISECONDS.toDays(millis) < 7 ->
(TimeUnit.MILLISECONDS.toDays(millis)
.toString() + " " + getString(R.string.days)).lower()

millis.toWeeks() < 4 ->
(millis.toWeeks().toString() + " " + getString(R.string.weeks).lower())

else -> (millis.toMonths().toString() + " " + getString(R.string.months).lower())
}

private fun Long.toWeeks() = TimeUnit.MILLISECONDS.toDays(this) / 7
private fun Long.toMonths() = toWeeks() / 4
private fun Long.toMonths() = toWeeks() / 4
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@file:Suppress("DEPRECATION")

package dev.jahir.blueprint.extensions

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.WindowManager
import androidx.annotation.AttrRes
Expand All @@ -20,19 +17,16 @@ import dev.jahir.frames.extensions.views.setPaddingTop
import kotlin.math.min
import kotlin.math.roundToInt


internal fun DrawerBlueprintActivity.enableTranslucentStatusBar(enable: Boolean = true) {
if (Build.VERSION.SDK_INT >= 21) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
val params: WindowManager.LayoutParams = window.attributes
if (enable) {
params.flags = params.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS.inv()
} else {
params.flags = params.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
}
window.attributes = params
if (Build.VERSION.SDK_INT >= 21) statusBarColor = Color.TRANSPARENT
statusBarColor = Color.TRANSPARENT
}

@SuppressLint("PrivateResource")
Expand All @@ -42,11 +36,9 @@ internal fun DrawerBlueprintActivity.setOptimalDrawerHeaderHeight(headerView: Vi
val statusBarHeight = 24.dpToPx
var height = getOptimalDrawerWidth() * ratio

if (Build.VERSION.SDK_INT >= 21) {
headerView.setPaddingTop(headerView.paddingTop + statusBarHeight)
if ((height - statusBarHeight) <= defaultHeaderMinHeight) {
height = (defaultHeaderMinHeight + statusBarHeight).toDouble()
}
headerView.setPaddingTop(headerView.paddingTop + statusBarHeight)
if ((height - statusBarHeight) <= defaultHeaderMinHeight) {
height = (defaultHeaderMinHeight + statusBarHeight).toDouble()
}

val finalHeight = ((height - statusBarHeight)).roundToInt()
Expand All @@ -64,9 +56,11 @@ internal fun DrawerBlueprintActivity.setOptimalDrawerHeaderHeight(headerView: Vi

@SuppressLint("PrivateResource")
internal fun DrawerBlueprintActivity.getOptimalDrawerWidth(): Int {
var actionBarHeight: Int = getThemeAttributeDimensionSize(R.attr.actionBarSize)
var actionBarHeight: Int =
getThemeAttributeDimensionSize(androidx.appcompat.R.attr.actionBarSize)
if (actionBarHeight == 0) {
actionBarHeight = dimenPixelSize(R.dimen.abc_action_bar_default_height_material)
actionBarHeight =
dimenPixelSize(androidx.appcompat.R.dimen.abc_action_bar_default_height_material)
}
val possibleMinDrawerWidth = resources.displayMetrics.widthPixels - actionBarHeight
return min(possibleMinDrawerWidth, 320.dpToPx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal class EmailBuilder(
}
resultIntent = Intent.createChooser(intent, context.string(R.string.send_using))
} ?: run {
context.toast(R.string.error)
context.toast(dev.jahir.frames.R.string.error)
}
return resultIntent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dev.jahir.blueprint.extensions

import androidx.annotation.DrawableRes
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import dev.jahir.blueprint.R
import dev.jahir.frames.extensions.context.color
import dev.jahir.frames.extensions.context.drawable
import dev.jahir.frames.extensions.context.resolveColor
Expand All @@ -17,7 +16,12 @@ internal fun ExtendedFloatingActionButton.setup(
) {
this.text = text
this.icon = context.drawable(icon)
?.tint(context.resolveColor(R.attr.colorOnSecondary, context.color(R.color.onAccent)))
?.tint(
context.resolveColor(
com.google.android.material.R.attr.colorOnSecondary,
context.color(dev.jahir.frames.R.color.onAccent)
)
)
invalidate()
post {
if (show) {
Expand All @@ -26,4 +30,4 @@ internal fun ExtendedFloatingActionButton.setup(
if (!isVisible) show()
} else hide()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private fun Context.showLauncherApplyError(
) {
try {
mdDialog {
title(R.string.error)
title(dev.jahir.frames.R.string.error)
message(
customContent ?: launcher?.cleanAppName?.let {
string(R.string.direct_apply_not_supported, it, getAppName())
Expand Down Expand Up @@ -210,18 +210,21 @@ private fun Context.executeLineageOSThemeEngineIntent() {
"org.cyanogenmod.theme.chooser.ChooserActivity"
)
}

isAppInstalled("org.cyanogenmod.theme.chooser2") -> {
component = ComponentName(
"org.cyanogenmod.theme.chooser2",
"org.cyanogenmod.theme.chooser2.ChooserActivity"
)
}

isAppInstalled("com.cyngn.theme.chooser") -> {
component = ComponentName(
"com.cyngn.theme.chooser",
"com.cyngn.theme.chooser.ChooserActivity"
)
}

else -> themesAppInstalled = false
}
if (themesAppInstalled) putExtra("pkgName", packageName)
Expand Down Expand Up @@ -339,7 +342,8 @@ private fun Context.executeMotoLauncherIntent() {
internal val Context.defaultLauncher: Launcher?
get() = try {
val intent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
val resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
val resolveInfo =
packageManager.resolveActivityCompat(intent, PackageManager.MATCH_DEFAULT_ONLY)
val launcherPackage = resolveInfo?.activityInfo?.packageName
Launcher.getSupportedLaunchers(this)
.filter { it.first.isActuallySupported }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.jahir.blueprint.extensions

import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.os.Build

fun PackageManager.getApplicationInfoCompat(pkg: String, flags: Int = 0): ApplicationInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getApplicationInfo(pkg, PackageManager.ApplicationInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") getApplicationInfo(pkg, flags)
}

fun PackageManager.queryIntentActivitiesCompat(intent: Intent, flags: Int = 0): List<ResolveInfo> =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") queryIntentActivities(intent, flags)
}

fun PackageManager.resolveActivityCompat(intent: Intent, flags: Int = 0): ResolveInfo? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
resolveActivity(intent, PackageManager.ResolveInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") resolveActivity(intent, flags)
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
shouldCallSuper = false
startActivity(Intent(this, BlueprintSettingsActivity::class.java))
}

R.id.about -> {
shouldCallSuper = false
startActivity(Intent(this, BlueprintAboutActivity::class.java))
Expand Down Expand Up @@ -245,8 +246,8 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
when (itemId) {
R.id.home -> getAppName()
R.id.icons -> string(R.string.icons)
R.id.wallpapers -> string(R.string.wallpapers)
R.id.apply -> string(R.string.apply)
R.id.wallpapers -> string(dev.jahir.frames.R.string.wallpapers)
R.id.apply -> string(dev.jahir.frames.R.string.apply)
R.id.request -> string(R.string.request)
else -> super.getToolbarTitleForItem(itemId)
}
Expand Down Expand Up @@ -278,16 +279,17 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
R.id.icons -> string(R.string.search_icons)
R.id.request -> string(R.string.search_apps)
R.id.apply -> string(R.string.search_launchers)
R.id.wallpapers -> string(R.string.search_wallpapers)
else -> string(R.string.search_x)
R.id.wallpapers -> string(dev.jahir.frames.R.string.search_wallpapers)
else -> string(dev.jahir.frames.R.string.search_x)
}

override fun getLayoutRes(): Int = R.layout.activity_blueprint
override val initialFragmentTag: String =
if (isIconsPicker) IconsCategoriesFragment.TAG else HomeFragment.TAG
override val initialItemId: Int = if (isIconsPicker) R.id.icons else R.id.home
override fun getMenuRes(): Int =
if (isIconsPicker) R.menu.toolbar_menu_simple else R.menu.blueprint_toolbar_menu
if (isIconsPicker) dev.jahir.frames.R.menu.toolbar_menu_simple
else R.menu.blueprint_toolbar_menu

override fun shouldLoadCollections(): Boolean = false
override fun shouldLoadFavorites(): Boolean = false
Expand Down Expand Up @@ -367,11 +369,12 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
val defText = string(R.string.quick_apply)
fabBtn?.setup(
if (customText.hasContent()) customText else defText,
R.drawable.ic_apply,
dev.jahir.frames.R.drawable.ic_apply,
defaultLauncher != null,
!canShowText
)
}

R.id.request -> {
val selectedAppsCount = requestsViewModel?.selectedApps?.size ?: 0
fabBtn?.setup(
Expand All @@ -386,6 +389,7 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
selectedAppsCount > 0
)
}

else -> fabBtn?.hide()
}
bottomNavigation?.post {
Expand Down Expand Up @@ -500,6 +504,7 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
val contentExtra = when {
TimeUnit.MILLISECONDS.toSeconds(state.timeLeft) >= 60 ->
string(R.string.apps_limit_dialog_day_extra, millisToText(state.timeLeft))

else -> string(R.string.apps_limit_dialog_day_extra_sec)
}
"$preContent $contentExtra"
Expand All @@ -508,6 +513,7 @@ abstract class BlueprintActivity : FramesActivity(), RequestCallback {
when (val requestsLeft = state.requestsLeft) {
maxAppsToRequest, -1 ->
string(R.string.apps_limit_dialog, maxAppsToRequest.toString())

else -> string(R.string.apps_limit_dialog_more, requestsLeft.toString())
}
}
Expand Down
Loading

0 comments on commit 9eac2a0

Please sign in to comment.