moving All apps display handling into a separate fragment
This commit is contained in:
parent
4c079b2832
commit
64403c2385
|
@ -1,26 +1,14 @@
|
||||||
package com.simplemobiletools.launcher.activities
|
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.content.res.Configuration
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.drawable.Drawable
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Surface
|
|
||||||
import android.view.WindowManager
|
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.appLaunched
|
||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.extensions.statusBarHeight
|
||||||
import com.simplemobiletools.commons.helpers.isRPlus
|
|
||||||
import com.simplemobiletools.commons.views.MyGridLayoutManager
|
|
||||||
import com.simplemobiletools.launcher.BuildConfig
|
import com.simplemobiletools.launcher.BuildConfig
|
||||||
import com.simplemobiletools.launcher.R
|
import com.simplemobiletools.launcher.R
|
||||||
import com.simplemobiletools.launcher.adapters.LaunchersAdapter
|
import com.simplemobiletools.launcher.fragments.AllAppsFragment
|
||||||
import com.simplemobiletools.launcher.extensions.getColumnCount
|
|
||||||
import com.simplemobiletools.launcher.models.AppLauncher
|
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
|
@ -31,116 +19,19 @@ class MainActivity : SimpleActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
appLaunched(BuildConfig.APPLICATION_ID)
|
appLaunched(BuildConfig.APPLICATION_ID)
|
||||||
getLaunchers()
|
|
||||||
window.setDecorFitsSystemWindows(false)
|
window.setDecorFitsSystemWindows(false)
|
||||||
|
(all_apps_fragment as AllAppsFragment).setupFragment(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
launchers_fastscroller.updateColors(getProperPrimaryColor())
|
(main_holder.layoutParams as FrameLayout.LayoutParams).topMargin = statusBarHeight
|
||||||
(launchers_holder.layoutParams as FrameLayout.LayoutParams).topMargin = statusBarHeight
|
|
||||||
updateStatusbarColor(Color.TRANSPARENT)
|
updateStatusbarColor(Color.TRANSPARENT)
|
||||||
setupNavigationBar()
|
(all_apps_fragment as AllAppsFragment).setupViews()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||||
super.onConfigurationChanged(newConfig)
|
super.onConfigurationChanged(newConfig)
|
||||||
launchers_grid.scrollToPosition(0)
|
(all_apps_fragment as AllAppsFragment).onConfigurationChanged()
|
||||||
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<AppLauncher>()
|
|
||||||
val allPackageNames = ArrayList<String>()
|
|
||||||
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<AppLauncher>
|
|
||||||
launchers.sortBy { it.title.toLowerCase() }
|
|
||||||
|
|
||||||
val layoutManager = launchers_grid.layoutManager as MyGridLayoutManager
|
|
||||||
layoutManager.spanCount = getColumnCount()
|
|
||||||
setupAdapter(launchers)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<AppLauncher>()
|
||||||
|
val allPackageNames = ArrayList<String>()
|
||||||
|
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<AppLauncher>
|
||||||
|
launchers.sortBy { it.title.toLowerCase() }
|
||||||
|
|
||||||
|
val layoutManager = all_apps_grid.layoutManager as MyGridLayoutManager
|
||||||
|
layoutManager.spanCount = context.getColumnCount()
|
||||||
|
setupAdapter(launchers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +1,11 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
android:id="@+id/main_holder"
|
||||||
android:id="@+id/launchers_holder"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
|
<include
|
||||||
android:id="@+id/launchers_fastscroller"
|
android:id="@+id/all_apps_fragment"
|
||||||
android:layout_width="match_parent"
|
layout="@layout/all_apps_fragment" />
|
||||||
android:layout_height="wrap_content">
|
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
|
||||||
android:id="@+id/launchers_grid"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:clipToPadding="false"
|
|
||||||
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager"
|
|
||||||
app:spanCount="@integer/portrait_column_count" />
|
|
||||||
|
|
||||||
</com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller>
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.simplemobiletools.launcher.fragments.AllAppsFragment xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/all_apps_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
|
||||||
|
android:id="@+id/all_apps_fastscroller"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
|
android:id="@+id/all_apps_grid"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:clipToPadding="false"
|
||||||
|
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager"
|
||||||
|
app:spanCount="@integer/portrait_column_count" />
|
||||||
|
|
||||||
|
</com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller>
|
||||||
|
</com.simplemobiletools.launcher.fragments.AllAppsFragment>
|
Loading…
Reference in New Issue