rework the launcher adder, show items in 1 column with titles

This commit is contained in:
tibbi
2021-11-07 23:01:46 +01:00
parent ab782a00a9
commit 48ab67f164
7 changed files with 107 additions and 35 deletions

View File

@ -11,7 +11,7 @@ import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog
import com.simplemobiletools.applauncher.dialogs.ChangeSortingDialog
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.getNotDisplayedLaunchers
import com.simplemobiletools.applauncher.extensions.getAllLaunchers
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.*
@ -29,7 +29,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private val MAX_COLUMN_COUNT = 20
private var displayedLaunchers = ArrayList<AppLauncher>()
private var notDisplayedLaunchers: ArrayList<AppLauncher>? = null
private var allLaunchers: ArrayList<AppLauncher>? = null
private var zoomListener: MyRecyclerView.MyZoomListener? = null
private var mStoredPrimaryColor = 0
@ -45,8 +45,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
setupGridLayoutManager()
fab.setOnClickListener {
if (notDisplayedLaunchers != null) {
AddAppLauncherDialog(this, notDisplayedLaunchers!!) {
if (allLaunchers != null) {
val shownLaunchers = (launchers_grid.adapter as LaunchersAdapter).launchers
AddAppLauncherDialog(this, allLaunchers!!, shownLaunchers) {
setupLaunchers()
}
}
@ -158,7 +159,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
ensureBackgroundThread {
notDisplayedLaunchers = getNotDisplayedLaunchers(launchers)
allLaunchers = getAllLaunchers()
}
}

View File

@ -8,20 +8,26 @@ import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beInvisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import kotlinx.android.synthetic.main.item_app_launcher.view.*
import kotlinx.android.synthetic.main.item_add_app_launcher.view.*
import kotlinx.android.synthetic.main.item_app_launcher.view.launcher_icon
import java.util.*
class LaunchersDialogAdapter(activity: Activity, val launchers: ArrayList<AppLauncher>) : RecyclerView.Adapter<LaunchersDialogAdapter.ViewHolder>() {
class LaunchersDialogAdapter(activity: Activity, val allLaunchers: ArrayList<AppLauncher>, val shownLaunchers: ArrayList<AppLauncher>) :
RecyclerView.Adapter<LaunchersDialogAdapter.ViewHolder>() {
private val config = activity.config
private var textColor = config.textColor
private var adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
private var selectedKeys = HashSet<Int>()
init {
shownLaunchers.forEach {
selectedKeys.add(it.packageName.hashCode())
}
}
fun toggleItemSelection(select: Boolean, pos: Int) {
val itemKey = launchers.getOrNull(pos)?.packageName?.hashCode() ?: return
val itemKey = allLaunchers.getOrNull(pos)?.packageName?.hashCode() ?: return
if (select) {
selectedKeys.add(itemKey)
@ -32,18 +38,18 @@ class LaunchersDialogAdapter(activity: Activity, val launchers: ArrayList<AppLau
notifyItemChanged(pos)
}
fun getSelectedLaunchers() = launchers.filter { selectedKeys.contains(it.packageName.hashCode()) } as ArrayList<AppLauncher>
fun getSelectedLaunchers() = allLaunchers.filter { selectedKeys.contains(it.packageName.hashCode()) } as ArrayList<AppLauncher>
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(launchers[position])
holder.bindView(allLaunchers[position])
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_app_launcher, parent, false)
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_add_app_launcher, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = launchers.size
override fun getItemCount() = allLaunchers.size
private fun isKeySelected(key: Int) = selectedKeys.contains(key)
@ -51,15 +57,13 @@ class LaunchersDialogAdapter(activity: Activity, val launchers: ArrayList<AppLau
fun bindView(launcher: AppLauncher): View {
val isSelected = isKeySelected(launcher.packageName.hashCode())
itemView.apply {
launcher_check?.beInvisibleIf(!isSelected)
launcher_label.text = launcher.title
launcher_label.setTextColor(textColor)
launcher_icon.setImageDrawable(launcher.drawable!!)
if (isSelected) {
launcher_check?.background?.applyColorFilter(adjustedPrimaryColor)
launcher_checkbox.apply {
isChecked = isSelected
text = launcher.title
setColors(textColor, adjustedPrimaryColor, 0)
}
launcher_icon.setImageDrawable(launcher.drawable!!)
setOnClickListener { viewClicked(launcher) }
setOnLongClickListener { viewClicked(launcher); true }
}

View File

@ -5,33 +5,45 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.LaunchersDialogAdapter
import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.areSystemAnimationsEnabled
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.views.MyGridLayoutManager
import kotlinx.android.synthetic.main.dialog_pick_launchers.view.*
class AddAppLauncherDialog(val activity: Activity, val notDisplayedLaunchers: ArrayList<AppLauncher>, val callback: () -> Unit) {
class AddAppLauncherDialog(
val activity: Activity,
val allLaunchers: ArrayList<AppLauncher>,
val shownLaunchers: ArrayList<AppLauncher>,
val callback: () -> Unit
) {
private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup)
private var adapter: LaunchersDialogAdapter? = null
init {
(view.pick_launchers_holder.layoutManager as MyGridLayoutManager).spanCount = activity.config.columnCnt
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmSelection() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this) {
adapter = LaunchersDialogAdapter(activity, notDisplayedLaunchers)
adapter = LaunchersDialogAdapter(activity, allLaunchers, shownLaunchers)
view.pick_launchers_holder.adapter = adapter
if (activity.areSystemAnimationsEnabled) {
view.pick_launchers_holder.scheduleLayoutAnimation()
}
}
}
}
private fun confirmSelection() {
adapter?.getSelectedLaunchers()?.forEach {
val selectedLaunchers = adapter?.getSelectedLaunchers() as ArrayList<AppLauncher> ?: return
val selectedPackageNames = selectedLaunchers.map { it.packageName }
val filtered = shownLaunchers.map { it.packageName }.filter { !selectedPackageNames.contains(it) }
filtered.forEach {
activity.dbHelper.deleteLauncher(it)
}
selectedLaunchers.forEach {
activity.dbHelper.insertAppLauncher(it)
}
callback()

View File

@ -14,10 +14,12 @@ val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>): ArrayList<AppLauncher> {
fun Context.getAllLaunchers(): ArrayList<AppLauncher> {
val allApps = ArrayList<AppLauncher>()
val allPackageNames = ArrayList<String>()
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED)
for (info in list) {
val componentInfo = info.activityInfo.applicationInfo
@ -46,9 +48,16 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>)
}
}
allPackageNames.add(packageName)
allApps.add(AppLauncher(0, label, packageName, 0, drawable))
}
dbHelper.getLaunchers().forEach { launcher ->
if (!allPackageNames.contains(launcher.packageName)) {
allApps.add(launcher)
}
}
if (config.sorting and SORT_BY_CUSTOM != 0) {
allApps.sortBy { it.title.toLowerCase() }
} else {
@ -57,5 +66,5 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>)
}
val unique = allApps.distinctBy { it.packageName }
return unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } as ArrayList<AppLauncher>
return unique.filter { it.packageName != "com.simplemobiletools.applauncher" } as ArrayList<AppLauncher>
}

View File

@ -42,8 +42,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_NAME TEXT, $COL_PKG_NAME TEXT UNIQUE, $COL_POSITION INTEGER," +
"$COL_WAS_RENAMED INTEGER, $COL_APP_ORDER INTEGER)")
db.execSQL(
"CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_NAME TEXT, $COL_PKG_NAME TEXT UNIQUE, $COL_POSITION INTEGER," +
"$COL_WAS_RENAMED INTEGER, $COL_APP_ORDER INTEGER)"
)
addInitialLaunchers(db)
}
@ -127,6 +129,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mDb.delete(MAIN_TABLE_NAME, selection, null)
}
fun deleteLauncher(packageName: String) {
val selection = "$COL_PKG_NAME LIKE \"$packageName\""
mDb.delete(MAIN_TABLE_NAME, selection, null)
}
fun updateLauncherName(id: Int, newName: String): Boolean {
val values = ContentValues().apply {
put(COL_NAME, newName)

View File

@ -4,7 +4,7 @@
android:id="@+id/pick_launchers_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/medium_margin"
android:layoutAnimation="@anim/layout_animation"
android:paddingTop="@dimen/medium_margin"
android:scrollbars="vertical"
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager"
app:spanCount="@integer/column_count" />
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/add_launchers_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingStart="@dimen/normal_margin"
android:paddingTop="@dimen/small_margin"
android:paddingEnd="@dimen/normal_margin"
android:paddingBottom="@dimen/small_margin">
<ImageView
android:id="@+id/launcher_icon"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_centerVertical="true"
android:padding="@dimen/tiny_margin"
tools:src="@drawable/ic_person_vector" />
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/launcher_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/launcher_icon"
android:layout_alignBottom="@+id/launcher_icon"
android:layout_toEndOf="@+id/launcher_icon"
android:background="@null"
android:button="@null"
android:clickable="false"
android:drawableEnd="?android:attr/listChoiceIndicatorMultiple"
android:drawablePadding="@dimen/normal_margin"
android:ellipsize="end"
android:lines="1"
android:paddingStart="@dimen/normal_margin"
android:textSize="@dimen/bigger_text_size"
tools:text="Email" />
</RelativeLayout>