Refactor: Moved to a better location.

Moved some of the functions to a better location to suit what they do.
This commit is contained in:
HeCodes2Much
2024-05-26 08:29:48 +01:00
parent 824586ea27
commit d6d7f6f721
3 changed files with 55 additions and 51 deletions

View File

@@ -1,15 +1,19 @@
package com.github.droidworksstudio.ktx
import android.app.SearchManager
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.drawable.AdaptiveIconDrawable
import android.net.Uri
import android.os.Build
import android.os.UserHandle
import android.provider.Settings
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
@@ -23,6 +27,7 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
fun Context.isTabletConfig(): Boolean =
resources.configuration.smallestScreenWidthDp >= SMALLEST_WIDTH_600
@@ -120,6 +125,54 @@ fun Context.openUrl(url: String) {
startActivity(intent)
}
fun Context.resetDefaultLauncher() {
val manufacturer = Build.MANUFACTURER.lowercase()
when (manufacturer) {
"google", "essential" -> runningStockAndroid()
else -> notRunningStockAndroid()
}
}
private fun Context.runningStockAndroid() {
try {
val packageManager = this.packageManager
val componentName = ComponentName(this, FakeHomeActivity::class.java)
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
this.startActivity(selector)
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun Context.notRunningStockAndroid() {
try {
val intent = Intent("android.settings.HOME_SETTINGS")
this.startActivity(intent)
} catch (e: ActivityNotFoundException) {
// Fallback to general settings if specific launcher settings are not found
try {
val intent = Intent(Settings.ACTION_SETTINGS)
this.startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun Context.isPackageInstalled(
packageName: String,
userHandle: UserHandle = android.os.Process.myUserHandle()

View File

@@ -38,56 +38,6 @@ import kotlin.math.pow
import kotlin.math.sqrt
class AppHelper @Inject constructor() {
fun resetDefaultLauncher(context: Context) {
val manufacturer = Build.MANUFACTURER.lowercase()
when (manufacturer) {
"google", "essential" -> runningStockAndroid(context)
else -> notRunningStockAndroid(context)
}
}
private fun runningStockAndroid(context: Context) {
try {
val packageManager = context.packageManager
val componentName = ComponentName(context, FakeHomeActivity::class.java)
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
context.startActivity(selector)
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun notRunningStockAndroid(context: Context) {
try {
val intent = Intent("android.settings.HOME_SETTINGS")
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
// Fallback to general settings if specific launcher settings are not found
try {
val intent = Intent(Settings.ACTION_SETTINGS)
context.startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
@SuppressLint("WrongConstant", "PrivateApi")
fun expandNotificationDrawer(context: Context) {
try {

View File

@@ -11,6 +11,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.github.droidworksstudio.ktx.resetDefaultLauncher
import com.github.droidworksstudio.ktx.restartApp
import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.launcher.R
@@ -102,7 +103,7 @@ class SettingsFragment : Fragment(),
// Click listener for reset default launcher
binding.setLauncherSelector.setOnClickListener {
appHelper.resetDefaultLauncher(requireContext())
requireContext().resetDefaultLauncher()
}
binding.favoriteText.setOnClickListener {