adding Thank You + improving handling of invalid packages

This commit is contained in:
tibbi 2017-11-12 15:16:31 +01:00
parent b6aa726762
commit c0a50d94f1
22 changed files with 77 additions and 58 deletions

View File

@ -11,6 +11,7 @@ import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.adapters.RecyclerAdapter import com.simplemobiletools.applauncher.adapters.RecyclerAdapter
import com.simplemobiletools.applauncher.dialogs.AddAppDialog import com.simplemobiletools.applauncher.dialogs.AddAppDialog
import com.simplemobiletools.applauncher.extensions.dbHelper import com.simplemobiletools.applauncher.extensions.dbHelper
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.checkWhatsNew import com.simplemobiletools.commons.extensions.checkWhatsNew
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
@ -23,7 +24,6 @@ import java.util.*
class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener { class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
private var launchers = ArrayList<AppLauncher>() private var launchers = ArrayList<AppLauncher>()
private var remainingLaunchers = ArrayList<AppLauncher>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -70,18 +70,16 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
launchers = dbHelper.getLaunchers() launchers = dbHelper.getLaunchers()
checkInvalidApps() checkInvalidApps()
launchers_holder.adapter = RecyclerAdapter(this, launchers, this) { launchers_holder.adapter = RecyclerAdapter(this, launchers, this) {
val launchIntent = packageManager.getLaunchIntentForPackage(it.pkgName) val launchIntent = packageManager.getLaunchIntentForPackage(it.packageName)
if (launchIntent != null) { if (launchIntent != null) {
startActivity(launchIntent) startActivity(launchIntent)
finish() finish()
} else { } else {
val url = "https://play.google.com/store/apps/details?id=${it.pkgName}" val url = "https://play.google.com/store/apps/details?id=${it.packageName}"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent) startActivity(intent)
} }
} }
remainingLaunchers = getNotDisplayedLaunchers()
} }
private fun getNotDisplayedLaunchers(): ArrayList<AppLauncher> { private fun getNotDisplayedLaunchers(): ArrayList<AppLauncher> {
@ -92,21 +90,20 @@ class MainActivity : SimpleActivity(), RecyclerAdapter.AppLaunchersListener {
for (info in list) { for (info in list) {
val componentInfo = info.activityInfo.applicationInfo val componentInfo = info.activityInfo.applicationInfo
val label = componentInfo.loadLabel(packageManager).toString() val label = componentInfo.loadLabel(packageManager).toString()
val pkgName = componentInfo.packageName allApps.add(AppLauncher(0, label, componentInfo.packageName))
allApps.add(AppLauncher(0, label, pkgName, 0))
} }
val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() }) val sorted = allApps.sortedWith(compareBy { it.name.toLowerCase() })
val unique = sorted.distinctBy { it.pkgName } val unique = sorted.distinctBy { it.packageName }
val filtered = unique.filter { !launchers.contains(it) } val filtered = unique.filter { !launchers.contains(it) && it.packageName != "com.simplemobiletools.applauncher" }
return filtered as ArrayList<AppLauncher> return filtered as ArrayList<AppLauncher>
} }
private fun checkInvalidApps() { private fun checkInvalidApps() {
val invalidIds = ArrayList<String>() val invalidIds = ArrayList<String>()
for ((id, name, pkgName) in launchers) { for ((id, name, packageName) in launchers) {
val launchIntent = packageManager.getLaunchIntentForPackage(pkgName) val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
if (launchIntent == null && !pkgName.startsWith("com.simplemobiletools")) { if (launchIntent == null && !packageName.isAPredefinedApp()) {
invalidIds.add(id.toString()) invalidIds.add(id.toString())
} }
} }

View File

@ -14,6 +14,8 @@ import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.activities.SimpleActivity import com.simplemobiletools.applauncher.activities.SimpleActivity
import com.simplemobiletools.applauncher.extensions.config import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.dbHelper 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.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
@ -165,7 +167,7 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLaunc
private fun getRealAppName(launcher: AppLauncher): String { private fun getRealAppName(launcher: AppLauncher): String {
return try { return try {
val applicationInfo = activity.packageManager.getApplicationInfo(launcher.pkgName, 0) val applicationInfo = activity.packageManager.getApplicationInfo(launcher.packageName, 0)
activity.packageManager.getApplicationLabel(applicationInfo).toString() activity.packageManager.getApplicationLabel(applicationInfo).toString()
} catch (e: PackageManager.NameNotFoundException) { } catch (e: PackageManager.NameNotFoundException) {
"" ""
@ -182,10 +184,10 @@ class RecyclerAdapter(val activity: SimpleActivity, val launchers: List<AppLaunc
setOnClickListener { viewClicked(launcher) } setOnClickListener { viewClicked(launcher) }
setOnLongClickListener { viewLongClicked(); true } setOnLongClickListener { viewLongClicked(); true }
val drawable = if (launcher.iconId != 0) { val drawable = if (launcher.packageName.isAPredefinedApp()) {
resources.getDrawable(launcher.iconId) resources.getLauncherDrawable(launcher.packageName)
} else { } else {
packageManager.getApplicationIcon(launcher.pkgName) packageManager.getApplicationIcon(launcher.packageName)
} }
launcher_icon.setImageDrawable(drawable) launcher_icon.setImageDrawable(drawable)
} }

View File

@ -10,6 +10,8 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.extensions.config import com.simplemobiletools.applauncher.extensions.config
import com.simplemobiletools.applauncher.extensions.getLauncherDrawable
import com.simplemobiletools.applauncher.extensions.isAPredefinedApp
import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
@ -67,10 +69,10 @@ class RecyclerDialogAdapter(activity: Activity, val launchers: List<AppLauncher>
setOnClickListener { viewClicked() } setOnClickListener { viewClicked() }
setOnLongClickListener { viewClicked(); true } setOnLongClickListener { viewClicked(); true }
val drawable = if (launcher.iconId != 0) { val drawable = if (launcher.packageName.isAPredefinedApp()) {
resources.getDrawable(launcher.iconId) resources.getLauncherDrawable(launcher.packageName)
} else { } else {
packageManager.getApplicationIcon(launcher.pkgName) packageManager.getApplicationIcon(launcher.packageName)
} }
launcher_icon.setImageDrawable(drawable) launcher_icon.setImageDrawable(drawable)
} }

View File

@ -0,0 +1,21 @@
package com.simplemobiletools.applauncher.extensions
import android.content.res.Resources
import android.graphics.drawable.Drawable
import com.simplemobiletools.applauncher.R
fun Resources.getLauncherDrawable(packageName: String): Drawable {
return getDrawable(when (packageName) {
"com.simplemobiletools.calculator" -> R.drawable.ic_launcher_calculator
"com.simplemobiletools.calendar" -> R.drawable.ic_launcher_calendar
"com.simplemobiletools.camera" -> R.drawable.ic_launcher_camera
"com.simplemobiletools.draw" -> R.drawable.ic_launcher_draw
"com.simplemobiletools.filemanager" -> R.drawable.ic_launcher_filemanager
"com.simplemobiletools.flashlight" -> R.drawable.ic_launcher_flashlight
"com.simplemobiletools.gallery" -> R.drawable.ic_launcher_gallery
"com.simplemobiletools.musicplayer" -> R.drawable.ic_launcher_musicplayer
"com.simplemobiletools.notes" -> R.drawable.ic_launcher_notes
"com.simplemobiletools.thankyou" -> R.drawable.ic_launcher_thankyou
else -> throw RuntimeException("Invalid launcher package name $packageName")
})
}

View File

@ -0,0 +1,6 @@
package com.simplemobiletools.applauncher.extensions
import com.simplemobiletools.applauncher.helpers.predefinedPackageNames
// treat apps like "com.simplemobiletools.notes.debug" as any third party app
fun String.isAPredefinedApp() = predefinedPackageNames.contains(this)

View File

@ -0,0 +1,14 @@
package com.simplemobiletools.applauncher.helpers
val predefinedPackageNames = arrayListOf(
"com.simplemobiletools.calculator",
"com.simplemobiletools.calendar",
"com.simplemobiletools.camera",
"com.simplemobiletools.draw",
"com.simplemobiletools.filemanager",
"com.simplemobiletools.flashlight",
"com.simplemobiletools.gallery",
"com.simplemobiletools.musicplayer",
"com.simplemobiletools.notes",
"com.simplemobiletools.thankyou"
)

View File

@ -5,7 +5,6 @@ import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils import android.text.TextUtils
import android.util.Log
import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.applauncher.models.AppLauncher
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
@ -17,7 +16,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val COL_ID = "id" private val COL_ID = "id"
private val COL_NAME = "name" private val COL_NAME = "name"
private val COL_PKG_NAME = "pkgName" private val COL_PKG_NAME = "pkgName"
private val COL_ICON_ID = "icon"
private val COL_POSITION = "position" private val COL_POSITION = "position"
private val mDb = writableDatabase private val mDb = writableDatabase
@ -36,14 +34,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
Log.e("DEBUG", "create") db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_NAME TEXT, $COL_PKG_NAME TEXT UNIQUE, $COL_POSITION INTEGER)")
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_NAME TEXT, $COL_PKG_NAME TEXT UNIQUE, " +
"$COL_ICON_ID INTEGER, $COL_POSITION INTEGER)")
addInitialLaunchers(db) addInitialLaunchers(db)
} }
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
Log.e("DEBUG", "upgrade")
} }
private fun addInitialLaunchers(db: SQLiteDatabase) { private fun addInitialLaunchers(db: SQLiteDatabase) {
@ -56,37 +51,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
R.string.flashlight, R.string.flashlight,
R.string.gallery, R.string.gallery,
R.string.music_player, R.string.music_player,
R.string.notes R.string.notes,
) R.string.thank_you
val packages = arrayListOf(
"calculator",
"calendar",
"camera",
"draw",
"filemanager",
"flashlight",
"gallery",
"musicplayer",
"notes"
)
val icons = arrayListOf(
R.drawable.ic_launcher_calculator,
R.drawable.ic_launcher_calendar,
R.drawable.ic_launcher_camera,
R.drawable.ic_launcher_draw,
R.drawable.ic_launcher_filemanager,
R.drawable.ic_launcher_flashlight,
R.drawable.ic_launcher_gallery,
R.drawable.ic_launcher_musicplayer,
R.drawable.ic_launcher_notes
) )
val cnt = titles.size val cnt = titles.size
val resources = context.resources val resources = context.resources
val packages = predefinedPackageNames
for (i in 0 until cnt) { for (i in 0 until cnt) {
val appLauncher = AppLauncher(0, resources.getString(titles[i]), "com.simplemobiletools.${packages[i]}", icons[i]) val appLauncher = AppLauncher(0, resources.getString(titles[i]), packages[i])
addAppLauncher(appLauncher, db) addAppLauncher(appLauncher, db)
} }
} }
@ -103,8 +76,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun fillAppLauncherValues(appLauncher: AppLauncher): ContentValues { private fun fillAppLauncherValues(appLauncher: AppLauncher): ContentValues {
return ContentValues().apply { return ContentValues().apply {
put(COL_NAME, appLauncher.name) put(COL_NAME, appLauncher.name)
put(COL_PKG_NAME, appLauncher.pkgName) put(COL_PKG_NAME, appLauncher.packageName)
put(COL_ICON_ID, appLauncher.iconId)
} }
} }
@ -124,15 +96,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun getLaunchers(): ArrayList<AppLauncher> { fun getLaunchers(): ArrayList<AppLauncher> {
val launchers = ArrayList<AppLauncher>() val launchers = ArrayList<AppLauncher>()
val cols = arrayOf(COL_ID, COL_NAME, COL_PKG_NAME, COL_ICON_ID) val cols = arrayOf(COL_ID, COL_NAME, COL_PKG_NAME)
val cursor = mDb.query(MAIN_TABLE_NAME, cols, null, null, null, null, COL_NAME) val cursor = mDb.query(MAIN_TABLE_NAME, cols, null, null, null, null, COL_NAME)
cursor.use { cursor.use {
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val id = cursor.getIntValue(COL_ID) val id = cursor.getIntValue(COL_ID)
val name = cursor.getStringValue(COL_NAME) val name = cursor.getStringValue(COL_NAME)
val pkgName = cursor.getStringValue(COL_PKG_NAME) val pkgName = cursor.getStringValue(COL_PKG_NAME)
val icon = cursor.getIntValue(COL_ICON_ID) val launcher = AppLauncher(id, name, pkgName)
val launcher = AppLauncher(id, name, pkgName, icon)
launchers.add(launcher) launchers.add(launcher)
} }
} }

View File

@ -1,7 +1,7 @@
package com.simplemobiletools.applauncher.models package com.simplemobiletools.applauncher.models
data class AppLauncher(val id: Int, var name: String, val pkgName: String, val iconId: Int) { data class AppLauncher(val id: Int, var name: String, val packageName: String) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return pkgName.equals((other as AppLauncher).pkgName, true) return packageName.equals((other as AppLauncher).packageName, true)
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@ -13,6 +13,7 @@
<string name="gallery">Galerie</string> <string name="gallery">Galerie</string>
<string name="music_player">Musikplayer</string> <string name="music_player">Musikplayer</string>
<string name="notes">Notizblock</string> <string name="notes">Notizblock</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->

View File

@ -13,6 +13,7 @@
<string name="gallery">Galería</string> <string name="gallery">Galería</string>
<string name="music_player">Reproductor de música</string> <string name="music_player">Reproductor de música</string>
<string name="notes">Notas</string> <string name="notes">Notas</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->

View File

@ -13,6 +13,7 @@
<string name="gallery">ギャラリー</string> <string name="gallery">ギャラリー</string>
<string name="music_player">ミュージック プレイヤー</string> <string name="music_player">ミュージック プレイヤー</string>
<string name="notes">メモ</string> <string name="notes">メモ</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->

View File

@ -13,6 +13,7 @@
<string name="gallery">Galeria</string> <string name="gallery">Galeria</string>
<string name="music_player">Odtwarzacz Muzyki</string> <string name="music_player">Odtwarzacz Muzyki</string>
<string name="notes">Notatki</string> <string name="notes">Notatki</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->

View File

@ -13,6 +13,7 @@
<string name="gallery">Galeria</string> <string name="gallery">Galeria</string>
<string name="music_player">Reprodutor de músicas</string> <string name="music_player">Reprodutor de músicas</string>
<string name="notes">Notas</string> <string name="notes">Notas</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->

View File

@ -13,6 +13,7 @@
<string name="gallery">Gallery</string> <string name="gallery">Gallery</string>
<string name="music_player">Music Player</string> <string name="music_player">Music Player</string>
<string name="notes">Notes</string> <string name="notes">Notes</string>
<string name="thank_you">Thank You</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have --> <!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- Short description has to have less than 80 chars --> <!-- Short description has to have less than 80 chars -->