Refactor: Cleaned up some unneeded code.

This commit is contained in:
HeCodes2Much
2024-12-02 18:57:59 +00:00
parent d58fb5a4c2
commit 5bdd01076e
7 changed files with 32 additions and 42 deletions

View File

@@ -31,7 +31,6 @@ import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import com.github.droidworksstudio.launcher.data.entities.AppInfo import com.github.droidworksstudio.launcher.data.entities.AppInfo
@@ -112,8 +111,6 @@ fun Context.createIconWithResourceCompat(
} }
} }
fun Context.currentLanguage() = ConfigurationCompat.getLocales(resources.configuration)[0]?.language
fun Context.openBrowser(url: String, clearFromRecent: Boolean = true) { fun Context.openBrowser(url: String, clearFromRecent: Boolean = true) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
if (clearFromRecent) browserIntent.flags = if (clearFromRecent) browserIntent.flags =
@@ -182,7 +179,7 @@ fun Context.resetDefaultLauncher() {
try { try {
val intent = Intent("android.settings.HOME_SETTINGS") val intent = Intent("android.settings.HOME_SETTINGS")
this.startActivity(intent) this.startActivity(intent)
} catch (e: ActivityNotFoundException) { } catch (_: ActivityNotFoundException) {
// Fallback to general settings if specific launcher settings are not found // Fallback to general settings if specific launcher settings are not found
try { try {
val intent = Intent(Settings.ACTION_SETTINGS) val intent = Intent(Settings.ACTION_SETTINGS)
@@ -266,7 +263,7 @@ fun Context.launchCalendar() {
builder.appendPath("time") builder.appendPath("time")
builder.appendPath(time.toString()) builder.appendPath(time.toString())
this.startActivity(Intent(Intent.ACTION_VIEW, builder.build())) this.startActivity(Intent(Intent.ACTION_VIEW, builder.build()))
} catch (e: Exception) { } catch (_: Exception) {
try { try {
val intent = Intent(this, LauncherActivity::class.java) val intent = Intent(this, LauncherActivity::class.java)
intent.addCategory(Intent.CATEGORY_APP_CALENDAR) intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
@@ -281,7 +278,7 @@ fun Context.openBatteryManager() {
try { try {
val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY) val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
this.startActivity(intent) this.startActivity(intent)
} catch (e: ActivityNotFoundException) { } catch (_: ActivityNotFoundException) {
// Battery manager settings cannot be opened // Battery manager settings cannot be opened
// Handle this case as needed // Handle this case as needed
showLongToast("Battery manager settings are not available on this device.") showLongToast("Battery manager settings are not available on this device.")
@@ -375,7 +372,7 @@ fun Context.isPackageInstalled(
): Boolean { ): Boolean {
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityInfo = launcher.getActivityList(packageName, userHandle) val activityInfo = launcher.getActivityList(packageName, userHandle)
return activityInfo.size > 0 return activityInfo.isNotEmpty()
} }
fun Context.getAppNameFromPackageName(packageName: String): String? { fun Context.getAppNameFromPackageName(packageName: String): String? {

View File

@@ -6,7 +6,7 @@ import androidx.room.RoomDatabase
import com.github.droidworksstudio.launcher.data.dao.AppInfoDAO import com.github.droidworksstudio.launcher.data.dao.AppInfoDAO
import com.github.droidworksstudio.launcher.data.entities.AppInfo import com.github.droidworksstudio.launcher.data.entities.AppInfo
@Database(entities = [AppInfo::class], version = 1, exportSchema = false) @Database(entities = [AppInfo::class], version = 1, exportSchema = true)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
abstract fun appDao(): AppInfoDAO abstract fun appDao(): AppInfoDAO
} }

View File

@@ -22,16 +22,12 @@ interface AppInfoDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun restoreAll(apps: List<AppInfo>) suspend fun restoreAll(apps: List<AppInfo>)
@Query("DELETE FROM app") @Query("DELETE FROM app")
suspend fun clearAll() suspend fun clearAll()
@Query("DELETE FROM sqlite_sequence WHERE name = 'app'") @Query("DELETE FROM sqlite_sequence WHERE name = 'app'")
suspend fun resetAutoIncrement() suspend fun resetAutoIncrement()
@Update
suspend fun update(app: AppInfo)
@Delete @Delete
fun delete(app: AppInfo) fun delete(app: AppInfo)
@@ -65,21 +61,21 @@ interface AppInfoDAO {
@Transaction @Transaction
suspend fun updateAppName(appInfo: AppInfo, newAppName: String) { suspend fun updateAppName(appInfo: AppInfo, newAppName: String) {
appInfo.appName = newAppName appInfo.appName = newAppName
update(appInfo) updateAppInfo(appInfo)
logUpdate("App name updated", appInfo) logUpdate("App name updated", appInfo)
} }
@Transaction @Transaction
suspend fun updateAppHidden(appInfo: AppInfo, appHidden: Boolean) { suspend fun updateAppHidden(appInfo: AppInfo, appHidden: Boolean) {
appInfo.hidden = appHidden appInfo.hidden = appHidden
update(appInfo) updateAppInfo(appInfo)
logUpdate("App hidden status updated", appInfo) logUpdate("App hidden status updated", appInfo)
} }
@Transaction @Transaction
suspend fun updateLockApp(appInfo: AppInfo, appLock: Boolean) { suspend fun updateLockApp(appInfo: AppInfo, appLock: Boolean) {
appInfo.lock = appLock appInfo.lock = appLock
update(appInfo) updateAppInfo(appInfo)
logUpdate("App lock status updated", appInfo) logUpdate("App lock status updated", appInfo)
} }

View File

@@ -16,6 +16,7 @@ import android.os.Vibrator
import android.os.VibratorManager import android.os.VibratorManager
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.text.style.ImageSpan import android.text.style.ImageSpan
import android.util.JsonReader
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.Gravity import android.view.Gravity
@@ -41,6 +42,7 @@ import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import java.io.StringReader
import java.net.UnknownHostException import java.net.UnknownHostException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Calendar import java.util.Calendar
@@ -384,6 +386,10 @@ class AppHelper @Inject constructor() {
// Read the content from the InputStream // Read the content from the InputStream
val jsonString = inputStream.bufferedReader().use { it.readText() } val jsonString = inputStream.bufferedReader().use { it.readText() }
// Create a JsonReader with lenient parsing
val jsonReader = JsonReader(StringReader(jsonString))
jsonReader.isLenient = true // Enable lenient mode
// Convert JSON to List<AppInfo> // Convert JSON to List<AppInfo>
val gson = Gson() val gson = Gson()
val type = object : TypeToken<List<AppInfo>>() {}.type val type = object : TypeToken<List<AppInfo>>() {}.type

View File

@@ -44,7 +44,7 @@ class AppInfoRepository @Inject constructor(
} }
suspend fun updateInfo(appInfo: AppInfo) { suspend fun updateInfo(appInfo: AppInfo) {
appDao.update(appInfo) appDao.updateAppInfo(appInfo)
} }
suspend fun updateAppOrder(appInfoList: List<AppInfo>) { suspend fun updateAppOrder(appInfoList: List<AppInfo>) {
@@ -175,22 +175,8 @@ class AppInfoRepository @Inject constructor(
launcherApps.getActivityList(null, profile) launcherApps.getActivityList(null, profile)
.mapNotNull { app -> .mapNotNull { app ->
val packageName = app.applicationInfo.packageName val packageName = app.applicationInfo.packageName
val currentDateTime = LocalDateTime.now() val existingApp = getAppByPackageNameWork(packageName)
if (packageName !in existingPackageNames && packageName !in excludedPackageNames) { existingApp
AppInfo(
appName = app.label.toString(),
packageName = packageName,
favorite = false,
hidden = false,
lock = false,
createTime = currentDateTime.toString(),
userHandle = userId,
)
} else {
val existingApp = getAppByPackageNameWork(packageName)
existingApp?.let { appList.add(it) }
existingApp
}
} }
} }
} }

View File

@@ -47,20 +47,19 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
private var appStateClickListener: OnItemClickedListener.OnAppStateClickListener? = null private var appStateClickListener: OnItemClickedListener.OnAppStateClickListener? = null
fun setOnAppStateClickListener(listener: OnItemClickedListener.OnAppStateClickListener) { fun setOnAppStateClickListener(listener: OnItemClickedListener.OnAppStateClickListener) {
appStateClickListener = listener appStateClickListener = listener
} }
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?,
): View { ): View {
_binding = BottomsheetDialogBinding.inflate(inflater, container, false) _binding = BottomsheetDialogBinding.inflate(inflater, container, false)
return binding.root return binding.root
} }
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.R)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@@ -89,13 +88,21 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
} }
@RequiresApi(Build.VERSION_CODES.O)
@RequiresApi(Build.VERSION_CODES.R)
private fun observeClickListener() { private fun observeClickListener() {
val packageName = appInfo.packageName val packageName = appInfo.packageName
var appName = appInfo.packageName
val packageManager = context?.packageManager try {
val applicationInfo = packageManager?.getApplicationInfo(packageName, 0) // Get the context's PackageManager
val appName = applicationInfo?.let { packageManager.getApplicationLabel(it).toString() } val packageManager = context?.packageManager
val applicationInfo = packageManager?.getApplicationInfo(packageName, 0)
appName = applicationInfo?.let { packageManager.getApplicationLabel(it).toString() }.toString()
} catch (e: Exception) {
e.printStackTrace()
}
binding.apply { binding.apply {
bottomSheetFavHidden.setOnClickListener { bottomSheetFavHidden.setOnClickListener {
@@ -135,7 +142,7 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
) )
) )
binding.bottomSheetRename.hint = appName binding.bottomSheetRename.hint = appName
appInfo.appName = appName ?: "" appInfo.appName = appName
} else { } else {
appInfo.appName = s.toString() appInfo.appName = s.toString()
} }

View File

@@ -348,8 +348,6 @@ class DrawFragment : Fragment(),
} }
private fun showSelectedApp(appInfo: AppInfo) { private fun showSelectedApp(appInfo: AppInfo) {
binding.searchViewText.setQuery("", false)
val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo) val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo)
bottomSheetFragment.setOnAppStateClickListener(this) bottomSheetFragment.setOnAppStateClickListener(this)
bottomSheetFragment.show(parentFragmentManager, "BottomSheetDialog") bottomSheetFragment.show(parentFragmentManager, "BottomSheetDialog")