Skip to content

Commit 5bdd010

Browse files
Refactor: Cleaned up some unneeded code.
1 parent d58fb5a commit 5bdd010

File tree

7 files changed

+32
-42
lines changed

7 files changed

+32
-42
lines changed

app/src/main/java/com/github/droidworksstudio/common/ContextExtensions.kt

+4-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import androidx.annotation.DrawableRes
3131
import androidx.core.content.ContextCompat
3232
import androidx.core.graphics.drawable.IconCompat
3333
import androidx.core.graphics.drawable.toBitmap
34-
import androidx.core.os.ConfigurationCompat
3534
import androidx.lifecycle.LifecycleObserver
3635
import androidx.lifecycle.LifecycleOwner
3736
import com.github.droidworksstudio.launcher.data.entities.AppInfo
@@ -112,8 +111,6 @@ fun Context.createIconWithResourceCompat(
112111
}
113112
}
114113

115-
fun Context.currentLanguage() = ConfigurationCompat.getLocales(resources.configuration)[0]?.language
116-
117114
fun Context.openBrowser(url: String, clearFromRecent: Boolean = true) {
118115
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
119116
if (clearFromRecent) browserIntent.flags =
@@ -182,7 +179,7 @@ fun Context.resetDefaultLauncher() {
182179
try {
183180
val intent = Intent("android.settings.HOME_SETTINGS")
184181
this.startActivity(intent)
185-
} catch (e: ActivityNotFoundException) {
182+
} catch (_: ActivityNotFoundException) {
186183
// Fallback to general settings if specific launcher settings are not found
187184
try {
188185
val intent = Intent(Settings.ACTION_SETTINGS)
@@ -266,7 +263,7 @@ fun Context.launchCalendar() {
266263
builder.appendPath("time")
267264
builder.appendPath(time.toString())
268265
this.startActivity(Intent(Intent.ACTION_VIEW, builder.build()))
269-
} catch (e: Exception) {
266+
} catch (_: Exception) {
270267
try {
271268
val intent = Intent(this, LauncherActivity::class.java)
272269
intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
@@ -281,7 +278,7 @@ fun Context.openBatteryManager() {
281278
try {
282279
val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
283280
this.startActivity(intent)
284-
} catch (e: ActivityNotFoundException) {
281+
} catch (_: ActivityNotFoundException) {
285282
// Battery manager settings cannot be opened
286283
// Handle this case as needed
287284
showLongToast("Battery manager settings are not available on this device.")
@@ -375,7 +372,7 @@ fun Context.isPackageInstalled(
375372
): Boolean {
376373
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
377374
val activityInfo = launcher.getActivityList(packageName, userHandle)
378-
return activityInfo.size > 0
375+
return activityInfo.isNotEmpty()
379376
}
380377

381378
fun Context.getAppNameFromPackageName(packageName: String): String? {

app/src/main/java/com/github/droidworksstudio/launcher/data/AppDatabase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import androidx.room.RoomDatabase
66
import com.github.droidworksstudio.launcher.data.dao.AppInfoDAO
77
import com.github.droidworksstudio.launcher.data.entities.AppInfo
88

9-
@Database(entities = [AppInfo::class], version = 1, exportSchema = false)
9+
@Database(entities = [AppInfo::class], version = 1, exportSchema = true)
1010
abstract class AppDatabase : RoomDatabase() {
1111
abstract fun appDao(): AppInfoDAO
1212
}

app/src/main/java/com/github/droidworksstudio/launcher/data/dao/AppInfoDAO.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ interface AppInfoDAO {
2222
@Insert(onConflict = OnConflictStrategy.REPLACE)
2323
suspend fun restoreAll(apps: List<AppInfo>)
2424

25-
2625
@Query("DELETE FROM app")
2726
suspend fun clearAll()
2827

2928
@Query("DELETE FROM sqlite_sequence WHERE name = 'app'")
3029
suspend fun resetAutoIncrement()
3130

32-
@Update
33-
suspend fun update(app: AppInfo)
34-
3531
@Delete
3632
fun delete(app: AppInfo)
3733

@@ -65,21 +61,21 @@ interface AppInfoDAO {
6561
@Transaction
6662
suspend fun updateAppName(appInfo: AppInfo, newAppName: String) {
6763
appInfo.appName = newAppName
68-
update(appInfo)
64+
updateAppInfo(appInfo)
6965
logUpdate("App name updated", appInfo)
7066
}
7167

7268
@Transaction
7369
suspend fun updateAppHidden(appInfo: AppInfo, appHidden: Boolean) {
7470
appInfo.hidden = appHidden
75-
update(appInfo)
71+
updateAppInfo(appInfo)
7672
logUpdate("App hidden status updated", appInfo)
7773
}
7874

7975
@Transaction
8076
suspend fun updateLockApp(appInfo: AppInfo, appLock: Boolean) {
8177
appInfo.lock = appLock
82-
update(appInfo)
78+
updateAppInfo(appInfo)
8379
logUpdate("App lock status updated", appInfo)
8480
}
8581

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import android.os.Vibrator
1616
import android.os.VibratorManager
1717
import android.text.SpannableStringBuilder
1818
import android.text.style.ImageSpan
19+
import android.util.JsonReader
1920
import android.util.Log
2021
import android.util.TypedValue
2122
import android.view.Gravity
@@ -41,6 +42,7 @@ import com.google.gson.reflect.TypeToken
4142
import kotlinx.coroutines.flow.first
4243
import retrofit2.Retrofit
4344
import retrofit2.converter.gson.GsonConverterFactory
45+
import java.io.StringReader
4446
import java.net.UnknownHostException
4547
import java.text.SimpleDateFormat
4648
import java.util.Calendar
@@ -384,6 +386,10 @@ class AppHelper @Inject constructor() {
384386
// Read the content from the InputStream
385387
val jsonString = inputStream.bufferedReader().use { it.readText() }
386388

389+
// Create a JsonReader with lenient parsing
390+
val jsonReader = JsonReader(StringReader(jsonString))
391+
jsonReader.isLenient = true // Enable lenient mode
392+
387393
// Convert JSON to List<AppInfo>
388394
val gson = Gson()
389395
val type = object : TypeToken<List<AppInfo>>() {}.type

app/src/main/java/com/github/droidworksstudio/launcher/repository/AppInfoRepository.kt

+3-17
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AppInfoRepository @Inject constructor(
4444
}
4545

4646
suspend fun updateInfo(appInfo: AppInfo) {
47-
appDao.update(appInfo)
47+
appDao.updateAppInfo(appInfo)
4848
}
4949

5050
suspend fun updateAppOrder(appInfoList: List<AppInfo>) {
@@ -175,22 +175,8 @@ class AppInfoRepository @Inject constructor(
175175
launcherApps.getActivityList(null, profile)
176176
.mapNotNull { app ->
177177
val packageName = app.applicationInfo.packageName
178-
val currentDateTime = LocalDateTime.now()
179-
if (packageName !in existingPackageNames && packageName !in excludedPackageNames) {
180-
AppInfo(
181-
appName = app.label.toString(),
182-
packageName = packageName,
183-
favorite = false,
184-
hidden = false,
185-
lock = false,
186-
createTime = currentDateTime.toString(),
187-
userHandle = userId,
188-
)
189-
} else {
190-
val existingApp = getAppByPackageNameWork(packageName)
191-
existingApp?.let { appList.add(it) }
192-
existingApp
193-
}
178+
val existingApp = getAppByPackageNameWork(packageName)
179+
existingApp
194180
}
195181
}
196182
}

app/src/main/java/com/github/droidworksstudio/launcher/ui/bottomsheetdialog/AppInfoBottomSheetFragment.kt

+15-8
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
4747

4848
private var appStateClickListener: OnItemClickedListener.OnAppStateClickListener? = null
4949

50-
5150
fun setOnAppStateClickListener(listener: OnItemClickedListener.OnAppStateClickListener) {
5251
appStateClickListener = listener
5352
}
5453

5554
override fun onCreateView(
5655
inflater: LayoutInflater, container: ViewGroup?,
57-
savedInstanceState: Bundle?
56+
savedInstanceState: Bundle?,
5857
): View {
5958
_binding = BottomsheetDialogBinding.inflate(inflater, container, false)
6059
return binding.root
6160
}
6261

63-
@RequiresApi(Build.VERSION_CODES.O)
62+
@RequiresApi(Build.VERSION_CODES.R)
6463
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
6564
super.onViewCreated(view, savedInstanceState)
6665

@@ -89,13 +88,21 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
8988

9089
}
9190

92-
@RequiresApi(Build.VERSION_CODES.O)
91+
92+
@RequiresApi(Build.VERSION_CODES.R)
9393
private fun observeClickListener() {
9494
val packageName = appInfo.packageName
95+
var appName = appInfo.packageName
9596

96-
val packageManager = context?.packageManager
97-
val applicationInfo = packageManager?.getApplicationInfo(packageName, 0)
98-
val appName = applicationInfo?.let { packageManager.getApplicationLabel(it).toString() }
97+
try {
98+
// Get the context's PackageManager
99+
val packageManager = context?.packageManager
100+
101+
val applicationInfo = packageManager?.getApplicationInfo(packageName, 0)
102+
appName = applicationInfo?.let { packageManager.getApplicationLabel(it).toString() }.toString()
103+
} catch (e: Exception) {
104+
e.printStackTrace()
105+
}
99106

100107
binding.apply {
101108
bottomSheetFavHidden.setOnClickListener {
@@ -135,7 +142,7 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
135142
)
136143
)
137144
binding.bottomSheetRename.hint = appName
138-
appInfo.appName = appName ?: ""
145+
appInfo.appName = appName
139146
} else {
140147
appInfo.appName = s.toString()
141148
}

app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt

-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ class DrawFragment : Fragment(),
348348
}
349349

350350
private fun showSelectedApp(appInfo: AppInfo) {
351-
binding.searchViewText.setQuery("", false)
352-
353351
val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo)
354352
bottomSheetFragment.setOnAppStateClickListener(this)
355353
bottomSheetFragment.show(parentFragmentManager, "BottomSheetDialog")

0 commit comments

Comments
 (0)