Преглед изворни кода

bug fix for create note crash, start clock crash

sduduzog пре 7 година
родитељ
комит
1c039eda4b

+ 10 - 5
app/src/main/java/com/sduduzog/slimlauncher/data/DataRepository.kt

@@ -6,11 +6,13 @@ import android.content.pm.PackageManager
 import android.content.pm.ResolveInfo
 import android.os.AsyncTask
 import androidx.lifecycle.LiveData
+import com.sduduzog.slimlauncher.BuildConfig
 import java.util.*
 
 class DataRepository(application: Application) {
 
     private val db: DataRoomDatabase = DataRoomDatabase.getDatabase(application)!!
+    private val packageName = application.packageName
     private val appDao: AppDao = db.appDao()
     private val noteDao: NoteDao = db.noteDao()
     private var _apps: LiveData<List<App>> = appDao.apps
@@ -106,14 +108,17 @@ class DataRepository(application: Application) {
 
             main.addCategory(Intent.CATEGORY_LAUNCHER)
 
-            val launchables = pm.queryIntentActivities(main, 0)
-            Collections.sort(launchables,
+            val activitiesList = pm.queryIntentActivities(main, 0)
+            Collections.sort(activitiesList.filter {
+
+                it.activityInfo.packageName != BuildConfig.APPLICATION_ID
+            },
                     ResolveInfo.DisplayNameComparator(pm))
             mAsyncTaskDao.deleteAll() // Need to find a less expensive way of doing this
-            for (i in launchables.indices) {
-                val item = launchables[i]
+            for (i in activitiesList.indices) {
+                val item = activitiesList[i]
                 val activity = item.activityInfo
-                val app = App(launchables[i].loadLabel(pm).toString(), activity.applicationInfo.packageName, activity.name)
+                val app = App(activitiesList[i].loadLabel(pm).toString(), activity.applicationInfo.packageName, activity.name)
                 mAsyncTaskDao.insert(app)
             }
             return null

+ 14 - 9
app/src/main/java/com/sduduzog/slimlauncher/ui/main/MainFragment.kt

@@ -98,11 +98,7 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
 
         clockTextView.setOnClickListener {
             try {
-                val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-                    Intent(AlarmClock.ACTION_SHOW_ALARMS)
-                } else {
-                    alternativeClockIntent()
-                }
+                val intent = alternativeClockIntent()
                 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                 val left = 0
                 val top = 0
@@ -243,7 +239,7 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
 
     private fun alternativeClockIntent(): Intent {
         val alarmClockIntent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
-
+        val pm = activity!!.packageManager
 // Verify clock implementation
         val clockImpls = arrayOf(arrayOf("HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl"), arrayOf("Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"), arrayOf("Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"), arrayOf("Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"), arrayOf("Samsung Galaxy Clock", "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage"), arrayOf("Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock"), arrayOf("ASUS Tablets", "com.asus.deskclock", "com.asus.deskclock.DeskClock"))
 
@@ -254,16 +250,25 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
             val className = clockImpls[i][2]
             val cn = ComponentName(packageName, className)
             alarmClockIntent.component = cn
-            foundClockImpl = true
+            if (alarmClockIntent.resolveActivity(pm) != null)
+                foundClockImpl = true
         }
 
         if (!foundClockImpl) {
             throw Exception()
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+            val i = Intent(AlarmClock.ACTION_SHOW_ALARMS)
+            if (alarmClockIntent.resolveActivity(pm) != null) {
+                return i
+            } else {
+                throw Exception("No clock activity found for the intent")
+            }
+        } else {
+            throw Exception("No clock activity found for the intent")
         }
-        return alarmClockIntent
-
     }
 
+
     private fun doBounceAnimation(targetView: View) {
         targetView.animate()
                 .setStartDelay(500)

+ 1 - 1
app/src/main/java/com/sduduzog/slimlauncher/ui/main/notes/NoteFragment.kt

@@ -28,7 +28,7 @@ class NoteFragment : Fragment(), Observer {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         arguments.let {
-            note = if (it!!.containsKey("note")) {
+            note = if (it !=null && it.containsKey("note")) {
                 it.get("note") as Note
             } else {
                 Note("", -1L)