From 64403c2385bc291674fe6afaecfc0abe208ba721 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 13 Aug 2022 23:08:23 +0200 Subject: [PATCH] moving All apps display handling into a separate fragment --- .../launcher/activities/MainActivity.kt | 123 +--------------- .../launcher/fragments/AllAppsFragment.kt | 138 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 19 +-- app/src/main/res/layout/all_apps_fragment.xml | 22 +++ 4 files changed, 171 insertions(+), 131 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt create mode 100644 app/src/main/res/layout/all_apps_fragment.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 3331c19..c5e3142 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -1,26 +1,14 @@ package com.simplemobiletools.launcher.activities -import android.annotation.SuppressLint -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.Color -import android.graphics.drawable.Drawable import android.os.Bundle -import android.view.Surface -import android.view.WindowManager import android.widget.FrameLayout -import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.ensureBackgroundThread -import com.simplemobiletools.commons.helpers.isRPlus -import com.simplemobiletools.commons.views.MyGridLayoutManager +import com.simplemobiletools.commons.extensions.appLaunched +import com.simplemobiletools.commons.extensions.statusBarHeight import com.simplemobiletools.launcher.BuildConfig import com.simplemobiletools.launcher.R -import com.simplemobiletools.launcher.adapters.LaunchersAdapter -import com.simplemobiletools.launcher.extensions.getColumnCount -import com.simplemobiletools.launcher.models.AppLauncher +import com.simplemobiletools.launcher.fragments.AllAppsFragment import kotlinx.android.synthetic.main.activity_main.* class MainActivity : SimpleActivity() { @@ -31,116 +19,19 @@ class MainActivity : SimpleActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) appLaunched(BuildConfig.APPLICATION_ID) - getLaunchers() window.setDecorFitsSystemWindows(false) + (all_apps_fragment as AllAppsFragment).setupFragment(this) } override fun onResume() { super.onResume() - launchers_fastscroller.updateColors(getProperPrimaryColor()) - (launchers_holder.layoutParams as FrameLayout.LayoutParams).topMargin = statusBarHeight + (main_holder.layoutParams as FrameLayout.LayoutParams).topMargin = statusBarHeight updateStatusbarColor(Color.TRANSPARENT) - setupNavigationBar() + (all_apps_fragment as AllAppsFragment).setupViews() } override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - launchers_grid.scrollToPosition(0) - launchers_fastscroller.resetManualScrolling() - setupNavigationBar() - - val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager - layoutManager.spanCount = getColumnCount() - val launchers = (launchers_grid.adapter as LaunchersAdapter).launchers - setupAdapter(launchers) - } - - @SuppressLint("WrongConstant") - private fun getLaunchers() { - val allApps = ArrayList() - val allPackageNames = ArrayList() - val intent = Intent(Intent.ACTION_MAIN, null) - intent.addCategory(Intent.CATEGORY_LAUNCHER) - - ensureBackgroundThread { - val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED) - for (info in list) { - val componentInfo = info.activityInfo.applicationInfo - val label = componentInfo.loadLabel(packageManager).toString() - val packageName = componentInfo.packageName - - var drawable: Drawable? = null - try { - // try getting the properly colored launcher icons - val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps - val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0] - drawable = activityList.getBadgedIcon(0) - } catch (e: Exception) { - } catch (e: Error) { - } - - if (drawable == null) { - drawable = try { - packageManager.getApplicationIcon(packageName) - } catch (ignored: Exception) { - continue - } - } - - allPackageNames.add(packageName) - allApps.add(AppLauncher(0, label, packageName, 0, drawable)) - } - - val launchers = allApps.distinctBy { it.packageName } as ArrayList - launchers.sortBy { it.title.toLowerCase() } - - val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager - layoutManager.spanCount = getColumnCount() - setupAdapter(launchers) - } - } - - private fun setupAdapter(launchers: ArrayList) { - runOnUiThread { - LaunchersAdapter(this, launchers, launchers_fastscroller) { - val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName) - try { - startActivity(launchIntent) - } catch (e: Exception) { - showErrorToast(e) - } - }.apply { - launchers_grid.adapter = this - } - } - } - - private fun setupNavigationBar() { - var bottomListPadding = 0 - var leftListPadding = 0 - var rightListPadding = 0 - - if (navigationBarOnBottom) { - bottomListPadding = navigationBarHeight - leftListPadding = 0 - rightListPadding = 0 - } else if (navigationBarOnSide) { - bottomListPadding = 0 - - val display = if (isRPlus()) { - display!! - } else { - (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay - } - - if (display.rotation == Surface.ROTATION_90) { - rightListPadding = navigationBarWidth - } else if (display.rotation == Surface.ROTATION_270) { - leftListPadding = navigationBarWidth - } - } - - launchers_grid.setPadding(0, 0, resources.getDimension(R.dimen.medium_margin).toInt(), bottomListPadding) - launchers_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0) + (all_apps_fragment as AllAppsFragment).onConfigurationChanged() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt new file mode 100644 index 0000000..5d5fe8d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -0,0 +1,138 @@ +package com.simplemobiletools.launcher.fragments + +import android.annotation.SuppressLint +import android.content.Context +import android.content.Intent +import android.content.pm.LauncherApps +import android.content.pm.PackageManager +import android.graphics.drawable.Drawable +import android.os.Process +import android.util.AttributeSet +import android.view.Surface +import android.view.WindowManager +import android.widget.RelativeLayout +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.commons.helpers.isRPlus +import com.simplemobiletools.commons.views.MyGridLayoutManager +import com.simplemobiletools.launcher.R +import com.simplemobiletools.launcher.activities.SimpleActivity +import com.simplemobiletools.launcher.adapters.LaunchersAdapter +import com.simplemobiletools.launcher.extensions.getColumnCount +import com.simplemobiletools.launcher.models.AppLauncher +import kotlinx.android.synthetic.main.all_apps_fragment.view.* + +class AllAppsFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { + private var activity: SimpleActivity? = null + + fun setupFragment(activity: SimpleActivity) { + this.activity = activity + getLaunchers() + } + + fun onConfigurationChanged() { + all_apps_grid.scrollToPosition(0) + all_apps_fastscroller.resetManualScrolling() + setupViews() + + val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager + layoutManager.spanCount = context.getColumnCount() + val launchers = (all_apps_grid.adapter as LaunchersAdapter).launchers + setupAdapter(launchers) + } + + @SuppressLint("WrongConstant") + private fun getLaunchers() { + val allApps = ArrayList() + val allPackageNames = ArrayList() + val intent = Intent(Intent.ACTION_MAIN, null) + intent.addCategory(Intent.CATEGORY_LAUNCHER) + + ensureBackgroundThread { + val list = context.packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED) + for (info in list) { + val componentInfo = info.activityInfo.applicationInfo + val label = componentInfo.loadLabel(context.packageManager).toString() + val packageName = componentInfo.packageName + + var drawable: Drawable? = null + try { + // try getting the properly colored launcher icons + val launcher = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val activityList = launcher.getActivityList(packageName, Process.myUserHandle())[0] + drawable = activityList.getBadgedIcon(0) + } catch (e: Exception) { + } catch (e: Error) { + } + + if (drawable == null) { + drawable = try { + context.packageManager.getApplicationIcon(packageName) + } catch (ignored: Exception) { + continue + } + } + + allPackageNames.add(packageName) + allApps.add(AppLauncher(0, label, packageName, 0, drawable)) + } + + val launchers = allApps.distinctBy { it.packageName } as ArrayList + launchers.sortBy { it.title.toLowerCase() } + + val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager + layoutManager.spanCount = context.getColumnCount() + setupAdapter(launchers) + } + } + + private fun setupAdapter(launchers: ArrayList) { + activity?.runOnUiThread { + LaunchersAdapter(activity!!, launchers, all_apps_fastscroller) { + val launchIntent = context.packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName) + try { + activity!!.startActivity(launchIntent) + } catch (e: Exception) { + activity?.showErrorToast(e) + } + }.apply { + all_apps_grid.adapter = this + } + } + } + + fun setupViews() { + if (activity == null) { + return + } + + all_apps_fastscroller.updateColors(context.getProperPrimaryColor()) + + var bottomListPadding = 0 + var leftListPadding = 0 + var rightListPadding = 0 + + if (activity!!.navigationBarOnBottom) { + bottomListPadding = activity!!.navigationBarHeight + leftListPadding = 0 + rightListPadding = 0 + } else if (activity!!.navigationBarOnSide) { + bottomListPadding = 0 + + val display = if (isRPlus()) { + display!! + } else { + (activity!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay + } + + if (display.rotation == Surface.ROTATION_90) { + rightListPadding = activity!!.navigationBarWidth + } else if (display.rotation == Surface.ROTATION_270) { + leftListPadding = activity!!.navigationBarWidth + } + } + + all_apps_grid.setPadding(0, 0, resources.getDimension(R.dimen.medium_margin).toInt(), bottomListPadding) + all_apps_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0) + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d83bee2..97be365 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,22 +1,11 @@ - + - - - diff --git a/app/src/main/res/layout/all_apps_fragment.xml b/app/src/main/res/layout/all_apps_fragment.xml new file mode 100644 index 0000000..a3c8d66 --- /dev/null +++ b/app/src/main/res/layout/all_apps_fragment.xml @@ -0,0 +1,22 @@ + + + + + + + + +