diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt index 42d9a61..fe0f091 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.applauncher.activities import android.content.Intent -import android.content.pm.PackageManager import android.net.Uri import android.os.Bundle import android.view.Menu @@ -9,7 +8,7 @@ import android.view.MenuItem import com.simplemobiletools.applauncher.BuildConfig import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.adapters.RecyclerAdapter -import com.simplemobiletools.applauncher.dialogs.AddAppDialog +import com.simplemobiletools.applauncher.dialogs.AddAppLauncherDialog import com.simplemobiletools.applauncher.extensions.dbHelper import com.simplemobiletools.applauncher.extensions.isAPredefinedApp import com.simplemobiletools.applauncher.models.AppLauncher @@ -32,7 +31,7 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener { checkWhatsNewDialog() fab.setOnClickListener { - AddAppDialog(this, getNotDisplayedLaunchers()) { + AddAppLauncherDialog(this, launchers) { refreshLaunchers() } } @@ -82,23 +81,6 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener { } } - private fun getNotDisplayedLaunchers(): ArrayList { - val allApps = ArrayList() - 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 - val label = componentInfo.loadLabel(packageManager).toString() - allApps.add(AppLauncher(0, label, componentInfo.packageName)) - } - - val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() }) - val unique = sorted.distinctBy { it.packageName } - val filtered = unique.filter { !launchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } - return filtered as ArrayList - } - private fun checkInvalidApps() { val invalidIds = ArrayList() for ((id, name, packageName) in launchers) { diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt index a9cb67d..8701c8b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/RecyclerAdapter.kt @@ -14,8 +14,6 @@ import com.simplemobiletools.applauncher.activities.SimpleActivity import com.simplemobiletools.applauncher.dialogs.EditDialog import com.simplemobiletools.applauncher.extensions.config import com.simplemobiletools.applauncher.extensions.dbHelper -import com.simplemobiletools.applauncher.extensions.getLauncherDrawable -import com.simplemobiletools.applauncher.extensions.isAPredefinedApp import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.applyColorFilter @@ -106,6 +104,8 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: MutableList override fun onBindViewHolder(holder: ViewHolder, position: Int) { itemViews.put(position, holder.bindView(launchers[position], textColor, resources, packageManager)) + toggleItemSelection(selectedPositions.contains(position), position) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { @@ -73,16 +72,10 @@ class RecyclerDialogAdapter(activity: Activity, val launchers: List itemView.apply { launcher_label.text = launcher.name launcher_label.setTextColor(textColor) + launcher_icon.setImageDrawable(launcher.drawable!!) setOnClickListener { viewClicked() } setOnLongClickListener { viewClicked(); true } - - val drawable = if (launcher.packageName.isAPredefinedApp()) { - resources.getLauncherDrawable(launcher.packageName) - } else { - packageManager.getApplicationIcon(launcher.packageName) - } - launcher_icon.setImageDrawable(drawable) } return itemView } diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppDialog.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppDialog.kt deleted file mode 100644 index b36c879..0000000 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppDialog.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.simplemobiletools.applauncher.dialogs - -import android.app.Activity -import android.support.v7.app.AlertDialog -import android.view.ViewGroup -import com.simplemobiletools.applauncher.R -import com.simplemobiletools.applauncher.adapters.RecyclerDialogAdapter -import com.simplemobiletools.applauncher.extensions.dbHelper -import com.simplemobiletools.applauncher.models.AppLauncher -import com.simplemobiletools.commons.extensions.setupDialogStuff -import kotlinx.android.synthetic.main.dialog_pick_launchers.view.* -import java.util.* - -class AddAppDialog(val activity: Activity, val availableLaunchers: ArrayList, val callback: () -> Unit) { - var dialog: AlertDialog - var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup) - lateinit var adapter: RecyclerDialogAdapter - - init { - dialog = AlertDialog.Builder(activity) - .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmSelection() }) - .setNegativeButton(R.string.cancel, null) - .create().apply { - activity.setupDialogStuff(view, this) - adapter = RecyclerDialogAdapter(activity, availableLaunchers) - view.pick_launchers_holder.adapter = adapter - } - } - - private fun confirmSelection() { - adapter.getSelectedLaunchers().forEach { - activity.dbHelper.insertAppLauncher(it) - } - callback() - dialog.dismiss() - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppLauncherDialog.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppLauncherDialog.kt new file mode 100644 index 0000000..f14592c --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/dialogs/AddAppLauncherDialog.kt @@ -0,0 +1,73 @@ +package com.simplemobiletools.applauncher.dialogs + +import android.app.Activity +import android.content.Intent +import android.content.pm.PackageManager +import android.support.v7.app.AlertDialog +import android.view.ViewGroup +import com.simplemobiletools.applauncher.R +import com.simplemobiletools.applauncher.adapters.RecyclerDialogAdapter +import com.simplemobiletools.applauncher.extensions.dbHelper +import com.simplemobiletools.applauncher.extensions.getLauncherDrawable +import com.simplemobiletools.applauncher.extensions.isAPredefinedApp +import com.simplemobiletools.applauncher.models.AppLauncher +import com.simplemobiletools.commons.extensions.setupDialogStuff +import kotlinx.android.synthetic.main.dialog_pick_launchers.view.* +import java.util.* + +class AddAppLauncherDialog(val activity: Activity, val displayedLaunchers: ArrayList, val callback: () -> Unit) { + private var dialog: AlertDialog + private var view = (activity.layoutInflater.inflate(R.layout.dialog_pick_launchers, null) as ViewGroup) + private var adapter: RecyclerDialogAdapter? = null + + init { + dialog = AlertDialog.Builder(activity) + .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmSelection() }) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) + + Thread({ + adapter = RecyclerDialogAdapter(activity, getNotDisplayedLaunchers()) + activity.runOnUiThread { + view.pick_launchers_holder.adapter = adapter + } + }).start() + } + } + + private fun confirmSelection() { + adapter?.getSelectedLaunchers()?.forEach { + activity.dbHelper.insertAppLauncher(it) + } + callback() + dialog.dismiss() + } + + private fun getNotDisplayedLaunchers(): ArrayList { + val resources = activity.resources + val packageManager = activity.packageManager + val allApps = ArrayList() + 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 + val label = componentInfo.loadLabel(packageManager).toString() + val packageName = componentInfo.packageName + + val drawable = if (packageName.isAPredefinedApp()) { + resources.getLauncherDrawable(packageName) + } else { + packageManager.getApplicationIcon(packageName) + } + + allApps.add(AppLauncher(0, label, componentInfo.packageName, drawable)) + } + + val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() }) + val unique = sorted.distinctBy { it.packageName } + val filtered = unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } + return filtered as ArrayList + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt index c5a25fd..a4509eb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt @@ -6,6 +6,8 @@ import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.text.TextUtils import com.simplemobiletools.applauncher.R +import com.simplemobiletools.applauncher.extensions.getLauncherDrawable +import com.simplemobiletools.applauncher.extensions.isAPredefinedApp import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue @@ -15,7 +17,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont private val MAIN_TABLE_NAME = "launchers" private val COL_ID = "id" private val COL_NAME = "name" - private val COL_PKG_NAME = "pkgName" + private val COL_PKG_NAME = "package_name" private val COL_POSITION = "position" private val mDb = writableDatabase @@ -95,6 +97,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } fun getLaunchers(): ArrayList { + val resources = context.resources + val packageManager = context.packageManager val launchers = ArrayList() val cols = arrayOf(COL_ID, COL_NAME, COL_PKG_NAME) val cursor = mDb.query(MAIN_TABLE_NAME, cols, null, null, null, null, COL_NAME) @@ -102,8 +106,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont while (cursor.moveToNext()) { val id = cursor.getIntValue(COL_ID) val name = cursor.getStringValue(COL_NAME) - val pkgName = cursor.getStringValue(COL_PKG_NAME) - val launcher = AppLauncher(id, name, pkgName) + val packageName = cursor.getStringValue(COL_PKG_NAME) + + val drawable = if (packageName.isAPredefinedApp()) { + resources.getLauncherDrawable(packageName) + } else { + packageManager.getApplicationIcon(packageName) + } + + val launcher = AppLauncher(id, name, packageName, drawable) launchers.add(launcher) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt index 4fc65bf..7529061 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt @@ -1,6 +1,8 @@ package com.simplemobiletools.applauncher.models -data class AppLauncher(val id: Int, var name: String, val packageName: String) { +import android.graphics.drawable.Drawable + +data class AppLauncher(val id: Int, var name: String, val packageName: String, val drawable: Drawable? = null) { override fun equals(other: Any?): Boolean { return packageName.equals((other as AppLauncher).packageName, true) } diff --git a/app/src/main/res/layout/app_launcher_item.xml b/app/src/main/res/layout/app_launcher_item.xml index 75f5a5d..2f75fc9 100644 --- a/app/src/main/res/layout/app_launcher_item.xml +++ b/app/src/main/res/layout/app_launcher_item.xml @@ -13,7 +13,7 @@ diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index ed88bb3..3080056 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,3 +1,3 @@ - 50dp + 50dp