Skip to content

Commit

Permalink
Housekeeping: Cleaned up functions and deleted AppExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed May 26, 2024
1 parent ce3ee30 commit 707e4e0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 127 deletions.
109 changes: 109 additions & 0 deletions app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.content.res.Configuration
Expand Down Expand Up @@ -32,8 +33,11 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.github.droidworksstudio.launcher.Constants
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
import java.io.File
import java.io.IOException
import java.util.Calendar
import java.util.Date
import kotlin.math.pow
Expand Down Expand Up @@ -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(
packageName: String,
userHandle: UserHandle = android.os.Process.myUserHandle()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,22 @@ import android.content.res.Configuration
import android.content.res.Resources
import android.net.Uri
import android.os.Build
import android.provider.AlarmClock
import android.provider.CalendarContract
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowInsets
import android.view.WindowManager
import android.widget.TextView
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.google.android.material.dialog.MaterialAlertDialogBuilder
import com.github.droidworksstudio.launcher.Constants
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.accessibility.ActionService
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import java.util.Calendar
import java.util.Date
import javax.inject.Inject
import kotlin.math.pow
import kotlin.math.sqrt

class AppHelper @Inject constructor() {
@SuppressLint("WrongConstant", "PrivateApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.droidworksstudio.ktx.hideKeyboard
import com.github.droidworksstudio.ktx.launchApp
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.showLongToast
import com.github.droidworksstudio.launcher.R
Expand All @@ -25,8 +27,6 @@ import com.github.droidworksstudio.launcher.databinding.FragmentDrawBinding
import com.github.droidworksstudio.launcher.helper.AppHelper
import com.github.droidworksstudio.launcher.helper.FingerprintHelper
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.OnSwipeTouchListener
import com.github.droidworksstudio.launcher.listener.ScrollEventListener
Expand Down

0 comments on commit 707e4e0

Please sign in to comment.