Skip to content

Commit

Permalink
Refactor: Cleaned up some unneeded code.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed Dec 2, 2024
1 parent d58fb5a commit 5bdd010
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.github.droidworksstudio.launcher.data.entities.AppInfo
Expand Down Expand Up @@ -112,8 +111,6 @@ fun Context.createIconWithResourceCompat(
}
}

fun Context.currentLanguage() = ConfigurationCompat.getLocales(resources.configuration)[0]?.language

fun Context.openBrowser(url: String, clearFromRecent: Boolean = true) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
if (clearFromRecent) browserIntent.flags =
Expand Down Expand Up @@ -182,7 +179,7 @@ fun Context.resetDefaultLauncher() {
try {
val intent = Intent("android.settings.HOME_SETTINGS")
this.startActivity(intent)
} catch (e: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
// Fallback to general settings if specific launcher settings are not found
try {
val intent = Intent(Settings.ACTION_SETTINGS)
Expand Down Expand Up @@ -266,7 +263,7 @@ fun Context.launchCalendar() {
builder.appendPath("time")
builder.appendPath(time.toString())
this.startActivity(Intent(Intent.ACTION_VIEW, builder.build()))
} catch (e: Exception) {
} catch (_: Exception) {
try {
val intent = Intent(this, LauncherActivity::class.java)
intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
Expand All @@ -281,7 +278,7 @@ fun Context.openBatteryManager() {
try {
val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
this.startActivity(intent)
} catch (e: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
// Battery manager settings cannot be opened
// Handle this case as needed
showLongToast("Battery manager settings are not available on this device.")
Expand Down Expand Up @@ -375,7 +372,7 @@ fun Context.isPackageInstalled(
): Boolean {
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityInfo = launcher.getActivityList(packageName, userHandle)
return activityInfo.size > 0
return activityInfo.isNotEmpty()
}

fun Context.getAppNameFromPackageName(packageName: String): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.room.RoomDatabase
import com.github.droidworksstudio.launcher.data.dao.AppInfoDAO
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 fun appDao(): AppInfoDAO
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ interface AppInfoDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun restoreAll(apps: List<AppInfo>)


@Query("DELETE FROM app")
suspend fun clearAll()

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

@Update
suspend fun update(app: AppInfo)

@Delete
fun delete(app: AppInfo)

Expand Down Expand Up @@ -65,21 +61,21 @@ interface AppInfoDAO {
@Transaction
suspend fun updateAppName(appInfo: AppInfo, newAppName: String) {
appInfo.appName = newAppName
update(appInfo)
updateAppInfo(appInfo)
logUpdate("App name updated", appInfo)
}

@Transaction
suspend fun updateAppHidden(appInfo: AppInfo, appHidden: Boolean) {
appInfo.hidden = appHidden
update(appInfo)
updateAppInfo(appInfo)
logUpdate("App hidden status updated", appInfo)
}

@Transaction
suspend fun updateLockApp(appInfo: AppInfo, appLock: Boolean) {
appInfo.lock = appLock
update(appInfo)
updateAppInfo(appInfo)
logUpdate("App lock status updated", appInfo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.os.Vibrator
import android.os.VibratorManager
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import android.util.JsonReader
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
Expand All @@ -41,6 +42,7 @@ import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.first
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.StringReader
import java.net.UnknownHostException
import java.text.SimpleDateFormat
import java.util.Calendar
Expand Down Expand Up @@ -384,6 +386,10 @@ class AppHelper @Inject constructor() {
// Read the content from the InputStream
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>
val gson = Gson()
val type = object : TypeToken<List<AppInfo>>() {}.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AppInfoRepository @Inject constructor(
}

suspend fun updateInfo(appInfo: AppInfo) {
appDao.update(appInfo)
appDao.updateAppInfo(appInfo)
}

suspend fun updateAppOrder(appInfoList: List<AppInfo>) {
Expand Down Expand Up @@ -175,22 +175,8 @@ class AppInfoRepository @Inject constructor(
launcherApps.getActivityList(null, profile)
.mapNotNull { app ->
val packageName = app.applicationInfo.packageName
val currentDateTime = LocalDateTime.now()
if (packageName !in existingPackageNames && packageName !in excludedPackageNames) {
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
}
val existingApp = getAppByPackageNameWork(packageName)
existingApp
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,19 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial

private var appStateClickListener: OnItemClickedListener.OnAppStateClickListener? = null


fun setOnAppStateClickListener(listener: OnItemClickedListener.OnAppStateClickListener) {
appStateClickListener = listener
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {
_binding = BottomsheetDialogBinding.inflate(inflater, container, false)
return binding.root
}

@RequiresApi(Build.VERSION_CODES.O)
@RequiresApi(Build.VERSION_CODES.R)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

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

}

@RequiresApi(Build.VERSION_CODES.O)

@RequiresApi(Build.VERSION_CODES.R)
private fun observeClickListener() {
val packageName = appInfo.packageName
var appName = appInfo.packageName

val packageManager = context?.packageManager
val applicationInfo = packageManager?.getApplicationInfo(packageName, 0)
val appName = applicationInfo?.let { packageManager.getApplicationLabel(it).toString() }
try {
// Get the context's PackageManager
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 {
bottomSheetFavHidden.setOnClickListener {
Expand Down Expand Up @@ -135,7 +142,7 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial
)
)
binding.bottomSheetRename.hint = appName
appInfo.appName = appName ?: ""
appInfo.appName = appName
} else {
appInfo.appName = s.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,6 @@ class DrawFragment : Fragment(),
}

private fun showSelectedApp(appInfo: AppInfo) {
binding.searchViewText.setQuery("", false)

val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo)
bottomSheetFragment.setOnAppStateClickListener(this)
bottomSheetFragment.show(parentFragmentManager, "BottomSheetDialog")
Expand Down

0 comments on commit 5bdd010

Please sign in to comment.