Skip to content

Commit

Permalink
Changes in default launcher chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujnotes committed Sep 21, 2023
1 parent 29d6a97 commit 2916d46
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 33 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/app/olauncher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class MainActivity : AppCompatActivity() {
viewModel.launcherResetFailed.observe(this) {
openLauncherChooser(it)
}
viewModel.resetLauncherLiveData.observe(this) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
resetDefaultLauncher()
else
showLauncherSelector(Constants.REQUEST_CODE_LAUNCHER_SELECTOR)
}
viewModel.showDialog.observe(this) {
when (it) {
Constants.Dialog.RATE -> {
Expand Down Expand Up @@ -175,10 +181,14 @@ class MainActivity : AppCompatActivity() {
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode != Activity.RESULT_OK) return
when (requestCode) {
Constants.REQUEST_CODE_ENABLE_ADMIN -> {
if (resultCode == Activity.RESULT_OK)
prefs.lockModeOn = true
prefs.lockModeOn = true
}

Constants.REQUEST_CODE_LAUNCHER_SELECTOR -> {
resetDefaultLauncher()
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/app/olauncher/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import app.olauncher.data.Prefs
import app.olauncher.helper.SingleLiveEvent
import app.olauncher.helper.WallpaperWorker
import app.olauncher.helper.getAppsList
import app.olauncher.helper.getDefaultLauncherPackage
import app.olauncher.helper.getHiddenAppsList
import app.olauncher.helper.isOlauncherDefault
import app.olauncher.helper.resetDefaultLauncher
import app.olauncher.helper.showToast
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
Expand All @@ -42,6 +40,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
val launcherResetFailed = MutableLiveData<Boolean>()
val homeAppAlignment = MutableLiveData<Int>()
val showDialog = SingleLiveEvent<String>()
val resetLauncherLiveData = SingleLiveEvent<Unit?>()

fun selectedApp(appModel: AppModel, flag: Int) {
when (flag) {
Expand Down Expand Up @@ -211,10 +210,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
isOlauncherDefault.value = isOlauncherDefault(appContext)
}

fun resetDefaultLauncherApp(context: Context) {
resetDefaultLauncher(context)
launcherResetFailed.value = getDefaultLauncherPackage(appContext).contains(".")
}
// fun resetDefaultLauncherApp(context: Context) {
// resetDefaultLauncher(context)
// launcherResetFailed.value = getDefaultLauncherPackage(appContext).contains(".")
// }

fun setWallpaperWorker() {
val constraints = Constraints.Builder()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/app/olauncher/data/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ object Constants {
const val FLAG_SET_CALENDAR_APP = 14

const val REQUEST_CODE_ENABLE_ADMIN = 666
const val REQUEST_CODE_LAUNCHER_SELECTOR = 678

const val HINT_RATE_US = 15
const val HINT_SHARE = 22
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/app/olauncher/helper/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package app.olauncher.helper

import android.app.Activity
import android.app.role.RoleManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.RequiresApi

fun View.hideKeyboard() {
this.clearFocus()
Expand All @@ -18,3 +25,35 @@ fun View.showKeyboard(show: Boolean = true) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}, 100)
}


@RequiresApi(Build.VERSION_CODES.Q)
fun Activity.showLauncherSelector(requestCode: Int) {
val roleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager
if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME)) {
val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
startActivityForResult(intent, requestCode)
} else
resetDefaultLauncher()
}

fun Context.resetDefaultLauncher() {
try {
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)
startActivity(selector)
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
} catch (e: Exception) {
e.printStackTrace()
}
}
23 changes: 0 additions & 23 deletions app/src/main/java/app/olauncher/helper/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,6 @@ fun getDefaultLauncherPackage(context: Context): String {
} else "android"
}

// Source: https://stackoverflow.com/a/13239706
fun resetDefaultLauncher(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()
}
}

fun setPlainWallpaperByTheme(context: Context, appTheme: Int) {
when (appTheme) {
AppCompatDelegate.MODE_NIGHT_YES -> setPlainWallpaper(context, android.R.color.black)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/app/olauncher/ui/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
R.id.lock -> {}
R.id.clock -> openClockApp()
R.id.date -> openCalendarApp()
R.id.setDefaultLauncher -> viewModel.resetDefaultLauncherApp(requireContext())
R.id.setDefaultLauncher -> viewModel.resetLauncherLiveData.call()
else -> {
try { // Launch app
val appLocation = view.tag.toString().toInt()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/app/olauncher/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongClickListe
when (view.id) {
R.id.olauncherHiddenApps -> showHiddenApps()
R.id.appInfo -> openAppInfo(requireContext(), android.os.Process.myUserHandle(), BuildConfig.APPLICATION_ID)
R.id.setLauncher -> viewModel.resetDefaultLauncherApp(requireContext())
R.id.setLauncher -> viewModel.resetLauncherLiveData.call()
R.id.toggleLock -> toggleLockMode()
R.id.autoShowKeyboard -> toggleKeyboardText()
R.id.homeAppsNum -> binding.appsNumSelectLayout.visibility = View.VISIBLE
Expand Down

0 comments on commit 2916d46

Please sign in to comment.