Housekeeping: Cleaned up functions and deleted AppExtensions

This commit is contained in:
HeCodes2Much
2024-05-26 08:57:08 +01:00
parent ce3ee30d07
commit 707e4e01ba
4 changed files with 113 additions and 127 deletions

View File

@@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.LauncherApps import android.content.pm.LauncherApps
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
@@ -32,8 +33,11 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat 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.Constants
import com.github.droidworksstudio.launcher.data.entities.AppInfo import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
import java.io.File
import java.io.IOException
import java.util.Calendar import java.util.Calendar
import java.util.Date import java.util.Date
import kotlin.math.pow import kotlin.math.pow
@@ -264,6 +268,111 @@ fun Context.openBatteryManager() {
} }
} }
fun Context.searchOnPlayStore(query: String? = null): Boolean {
return try {
val playStoreIntent = Intent(Intent.ACTION_VIEW)
playStoreIntent.data = Uri.parse("${Constants.APP_GOOGLE_PLAY_STORE}=$query")
// Check if the Play Store app is installed
if (playStoreIntent.resolveActivity(packageManager) != null) {
startActivity(playStoreIntent)
} else {
// If Play Store app is not installed, open Play Store website in browser
playStoreIntent.data = Uri.parse("${Constants.URL_GOOGLE_PLAY_STORE}=$query")
startActivity(playStoreIntent)
}
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}
fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean {
val searchUrl = Constants.URL_GOOGLE_SEARCH
val encodedQuery = Uri.encode(searchQuery)
val fullUrl = "$searchUrl$encodedQuery"
Log.d("fullUrl", fullUrl)
openUrl(fullUrl)
return true
}
fun Context.backupSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
val allPrefs = sharedPreferences.all
val backupFile = File(filesDir, backupFileName)
println("Backup SharedPreferences to: ${backupFile.absolutePath}")
try {
backupFile.bufferedWriter().use { writer ->
for ((key, value) in allPrefs) {
if (value != null) {
val line = when (value) {
is Boolean -> "$key=${value}\n"
is Int -> "$key=${value}\n"
is Float -> "$key=${value}\n"
is Long -> "$key=${value}\n"
is String -> "$key=${value}\n"
is Set<*> -> "$key=${value.joinToString(",")}\n"
else -> null
}
if (line != null) {
writer.write(line)
println("Writing: $line")
} else {
println("Skipping unsupported type for key: $key")
}
} else {
println("Null value for key: $key")
}
}
}
println("Backup completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to backup SharedPreferences: ${e.message}")
}
}
fun Context.restoreSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
val editor = sharedPreferences.edit()
val backupFile = File(filesDir, backupFileName)
println("Restoring SharedPreferences from: ${backupFile.absolutePath}")
if (backupFile.exists()) {
try {
backupFile.forEachLine { line ->
val (key, value) = line.split("=", limit = 2)
when {
value.toBooleanStrictOrNull() != null -> editor.putBoolean(
key,
value.toBoolean()
)
value.toIntOrNull() != null -> editor.putInt(key, value.toInt())
value.toFloatOrNull() != null -> editor.putFloat(key, value.toFloat())
value.toLongOrNull() != null -> editor.putLong(key, value.toLong())
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
else -> editor.putString(key, value)
}
println("Restoring: $key=$value")
}
editor.apply()
println("Restore completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to restore SharedPreferences: ${e.message}")
}
} else {
println("Backup file does not exist.")
}
}
fun Context.isPackageInstalled( fun Context.isPackageInstalled(
packageName: String, packageName: String,
userHandle: UserHandle = android.os.Process.myUserHandle() userHandle: UserHandle = android.os.Process.myUserHandle()

View File

@@ -1,116 +0,0 @@
package com.github.droidworksstudio.launcher.helper
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.util.Log
import com.github.droidworksstudio.ktx.openUrl
import com.github.droidworksstudio.launcher.Constants
import java.io.File
import java.io.IOException
fun Context.searchOnPlayStore(query: String? = null): Boolean {
return try {
val playStoreIntent = Intent(Intent.ACTION_VIEW)
playStoreIntent.data = Uri.parse("${Constants.APP_GOOGLE_PLAY_STORE}=$query")
// Check if the Play Store app is installed
if (playStoreIntent.resolveActivity(packageManager) != null) {
startActivity(playStoreIntent)
} else {
// If Play Store app is not installed, open Play Store website in browser
playStoreIntent.data = Uri.parse("${Constants.URL_GOOGLE_PLAY_STORE}=$query")
startActivity(playStoreIntent)
}
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}
fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean {
val searchUrl = Constants.URL_GOOGLE_SEARCH
val encodedQuery = Uri.encode(searchQuery)
val fullUrl = "$searchUrl$encodedQuery"
Log.d("fullUrl", fullUrl)
openUrl(fullUrl)
return true
}
fun Context.backupSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
val allPrefs = sharedPreferences.all
val backupFile = File(filesDir, backupFileName)
println("Backup SharedPreferences to: ${backupFile.absolutePath}")
try {
backupFile.bufferedWriter().use { writer ->
for ((key, value) in allPrefs) {
if (value != null) {
val line = when (value) {
is Boolean -> "$key=${value}\n"
is Int -> "$key=${value}\n"
is Float -> "$key=${value}\n"
is Long -> "$key=${value}\n"
is String -> "$key=${value}\n"
is Set<*> -> "$key=${value.joinToString(",")}\n"
else -> null
}
if (line != null) {
writer.write(line)
println("Writing: $line")
} else {
println("Skipping unsupported type for key: $key")
}
} else {
println("Null value for key: $key")
}
}
}
println("Backup completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to backup SharedPreferences: ${e.message}")
}
}
fun Context.restoreSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
val editor = sharedPreferences.edit()
val backupFile = File(filesDir, backupFileName)
println("Restoring SharedPreferences from: ${backupFile.absolutePath}")
if (backupFile.exists()) {
try {
backupFile.forEachLine { line ->
val (key, value) = line.split("=", limit = 2)
when {
value.toBooleanStrictOrNull() != null -> editor.putBoolean(
key,
value.toBoolean()
)
value.toIntOrNull() != null -> editor.putInt(key, value.toInt())
value.toFloatOrNull() != null -> editor.putFloat(key, value.toFloat())
value.toLongOrNull() != null -> editor.putLong(key, value.toLong())
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
else -> editor.putString(key, value)
}
println("Restoring: $key=$value")
}
editor.apply()
println("Restore completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to restore SharedPreferences: ${e.message}")
}
} else {
println("Backup file does not exist.")
}
}

View File

@@ -9,29 +9,22 @@ import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.AlarmClock
import android.provider.CalendarContract
import android.provider.Settings import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity import android.view.Gravity
import android.view.View import android.view.View
import android.view.Window import android.view.Window
import android.view.WindowInsets import android.view.WindowInsets
import android.view.WindowManager
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat import androidx.appcompat.widget.LinearLayoutCompat
import com.github.droidworksstudio.ktx.backupSharedPreferences
import com.github.droidworksstudio.ktx.restoreSharedPreferences
import com.github.droidworksstudio.ktx.showLongToast import com.github.droidworksstudio.ktx.showLongToast
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.github.droidworksstudio.launcher.Constants import com.github.droidworksstudio.launcher.Constants
import com.github.droidworksstudio.launcher.R import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.accessibility.ActionService import com.github.droidworksstudio.launcher.accessibility.ActionService
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import java.util.Calendar import java.util.Calendar
import java.util.Date
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.pow
import kotlin.math.sqrt
class AppHelper @Inject constructor() { class AppHelper @Inject constructor() {
@SuppressLint("WrongConstant", "PrivateApi") @SuppressLint("WrongConstant", "PrivateApi")

View File

@@ -17,6 +17,8 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.droidworksstudio.ktx.hideKeyboard import com.github.droidworksstudio.ktx.hideKeyboard
import com.github.droidworksstudio.ktx.launchApp import com.github.droidworksstudio.ktx.launchApp
import com.github.droidworksstudio.ktx.openSearch import com.github.droidworksstudio.ktx.openSearch
import com.github.droidworksstudio.ktx.searchCustomSearchEngine
import com.github.droidworksstudio.ktx.searchOnPlayStore
import com.github.droidworksstudio.ktx.showKeyboard import com.github.droidworksstudio.ktx.showKeyboard
import com.github.droidworksstudio.ktx.showLongToast import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.launcher.R import com.github.droidworksstudio.launcher.R
@@ -25,8 +27,6 @@ import com.github.droidworksstudio.launcher.databinding.FragmentDrawBinding
import com.github.droidworksstudio.launcher.helper.AppHelper import com.github.droidworksstudio.launcher.helper.AppHelper
import com.github.droidworksstudio.launcher.helper.FingerprintHelper import com.github.droidworksstudio.launcher.helper.FingerprintHelper
import com.github.droidworksstudio.launcher.helper.PreferenceHelper import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.helper.searchCustomSearchEngine
import com.github.droidworksstudio.launcher.helper.searchOnPlayStore
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
import com.github.droidworksstudio.launcher.listener.ScrollEventListener import com.github.droidworksstudio.launcher.listener.ScrollEventListener