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 c09a6e4..b1325cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt @@ -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() - private var notDisplayedLaunchers: ArrayList? = null + private var allLaunchers: ArrayList? = 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() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersDialogAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersDialogAdapter.kt index 62604e9..4713778 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersDialogAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersDialogAdapter.kt @@ -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) : RecyclerView.Adapter() { +class LaunchersDialogAdapter(activity: Activity, val allLaunchers: ArrayList, val shownLaunchers: ArrayList) : + RecyclerView.Adapter() { private val config = activity.config private var textColor = config.textColor private var adjustedPrimaryColor = activity.getAdjustedPrimaryColor() private var selectedKeys = HashSet() + 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 + fun getSelectedLaunchers() = allLaunchers.filter { selectedKeys.contains(it.packageName.hashCode()) } as ArrayList 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, val callback: () -> Unit) { +class AddAppLauncherDialog( + val activity: Activity, + val allLaunchers: ArrayList, + val shownLaunchers: ArrayList, + 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 ?: 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() diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt index 972b390..6dc62d2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt @@ -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): ArrayList { +fun Context.getAllLaunchers(): ArrayList { val allApps = ArrayList() + val allPackageNames = 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 @@ -46,9 +48,16 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList) } } + 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) } val unique = allApps.distinctBy { it.packageName } - return unique.filter { !displayedLaunchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" } as ArrayList + return unique.filter { it.packageName != "com.simplemobiletools.applauncher" } 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 21b5b8d..54037b7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt @@ -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) diff --git a/app/src/main/res/layout/dialog_pick_launchers.xml b/app/src/main/res/layout/dialog_pick_launchers.xml index fe134c0..2ca0081 100644 --- a/app/src/main/res/layout/dialog_pick_launchers.xml +++ b/app/src/main/res/layout/dialog_pick_launchers.xml @@ -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" /> diff --git a/app/src/main/res/layout/item_add_app_launcher.xml b/app/src/main/res/layout/item_add_app_launcher.xml new file mode 100644 index 0000000..4ba9a95 --- /dev/null +++ b/app/src/main/res/layout/item_add_app_launcher.xml @@ -0,0 +1,39 @@ + + + + + + + +