Skip to content

Commit

Permalink
Refactor: Moved to a better location.
Browse files Browse the repository at this point in the history
Moved some of the functions to a better location to suit what they do.
  • Loading branch information
CreativeCodeCat committed May 26, 2024
1 parent 824586e commit d6d7f6f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,7 +103,7 @@ class SettingsFragment : Fragment(),

// Click listener for reset default launcher
binding.setLauncherSelector.setOnClickListener {
appHelper.resetDefaultLauncher(requireContext())
requireContext().resetDefaultLauncher()
}

binding.favoriteText.setOnClickListener {
Expand Down

0 comments on commit d6d7f6f

Please sign in to comment.