Skip to content

Commit 707e4e0

Browse files
Housekeeping: Cleaned up functions and deleted AppExtensions
1 parent ce3ee30 commit 707e4e0

File tree

4 files changed

+113
-127
lines changed

4 files changed

+113
-127
lines changed

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException
55
import android.content.ComponentName
66
import android.content.Context
77
import android.content.Intent
8+
import android.content.SharedPreferences
89
import android.content.pm.LauncherApps
910
import android.content.pm.PackageManager
1011
import android.content.res.Configuration
@@ -32,8 +33,11 @@ import androidx.core.graphics.drawable.toBitmap
3233
import androidx.core.os.ConfigurationCompat
3334
import androidx.lifecycle.LifecycleObserver
3435
import androidx.lifecycle.LifecycleOwner
36+
import com.github.droidworksstudio.launcher.Constants
3537
import com.github.droidworksstudio.launcher.data.entities.AppInfo
3638
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
39+
import java.io.File
40+
import java.io.IOException
3741
import java.util.Calendar
3842
import java.util.Date
3943
import kotlin.math.pow
@@ -264,6 +268,111 @@ fun Context.openBatteryManager() {
264268
}
265269
}
266270

271+
fun Context.searchOnPlayStore(query: String? = null): Boolean {
272+
return try {
273+
val playStoreIntent = Intent(Intent.ACTION_VIEW)
274+
playStoreIntent.data = Uri.parse("${Constants.APP_GOOGLE_PLAY_STORE}=$query")
275+
276+
// Check if the Play Store app is installed
277+
if (playStoreIntent.resolveActivity(packageManager) != null) {
278+
startActivity(playStoreIntent)
279+
} else {
280+
// If Play Store app is not installed, open Play Store website in browser
281+
playStoreIntent.data = Uri.parse("${Constants.URL_GOOGLE_PLAY_STORE}=$query")
282+
startActivity(playStoreIntent)
283+
}
284+
true
285+
} catch (e: Exception) {
286+
e.printStackTrace()
287+
false
288+
}
289+
}
290+
291+
fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean {
292+
val searchUrl = Constants.URL_GOOGLE_SEARCH
293+
val encodedQuery = Uri.encode(searchQuery)
294+
val fullUrl = "$searchUrl$encodedQuery"
295+
Log.d("fullUrl", fullUrl)
296+
openUrl(fullUrl)
297+
return true
298+
}
299+
300+
fun Context.backupSharedPreferences(backupFileName: String) {
301+
val sharedPreferences: SharedPreferences =
302+
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
303+
val allPrefs = sharedPreferences.all
304+
val backupFile = File(filesDir, backupFileName)
305+
306+
println("Backup SharedPreferences to: ${backupFile.absolutePath}")
307+
308+
try {
309+
backupFile.bufferedWriter().use { writer ->
310+
for ((key, value) in allPrefs) {
311+
if (value != null) {
312+
val line = when (value) {
313+
is Boolean -> "$key=${value}\n"
314+
is Int -> "$key=${value}\n"
315+
is Float -> "$key=${value}\n"
316+
is Long -> "$key=${value}\n"
317+
is String -> "$key=${value}\n"
318+
is Set<*> -> "$key=${value.joinToString(",")}\n"
319+
else -> null
320+
}
321+
if (line != null) {
322+
writer.write(line)
323+
println("Writing: $line")
324+
} else {
325+
println("Skipping unsupported type for key: $key")
326+
}
327+
} else {
328+
println("Null value for key: $key")
329+
}
330+
}
331+
}
332+
println("Backup completed successfully.")
333+
} catch (e: IOException) {
334+
e.printStackTrace()
335+
println("Failed to backup SharedPreferences: ${e.message}")
336+
}
337+
}
338+
339+
fun Context.restoreSharedPreferences(backupFileName: String) {
340+
val sharedPreferences: SharedPreferences =
341+
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
342+
val editor = sharedPreferences.edit()
343+
val backupFile = File(filesDir, backupFileName)
344+
345+
println("Restoring SharedPreferences from: ${backupFile.absolutePath}")
346+
347+
if (backupFile.exists()) {
348+
try {
349+
backupFile.forEachLine { line ->
350+
val (key, value) = line.split("=", limit = 2)
351+
when {
352+
value.toBooleanStrictOrNull() != null -> editor.putBoolean(
353+
key,
354+
value.toBoolean()
355+
)
356+
357+
value.toIntOrNull() != null -> editor.putInt(key, value.toInt())
358+
value.toFloatOrNull() != null -> editor.putFloat(key, value.toFloat())
359+
value.toLongOrNull() != null -> editor.putLong(key, value.toLong())
360+
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
361+
else -> editor.putString(key, value)
362+
}
363+
println("Restoring: $key=$value")
364+
}
365+
editor.apply()
366+
println("Restore completed successfully.")
367+
} catch (e: IOException) {
368+
e.printStackTrace()
369+
println("Failed to restore SharedPreferences: ${e.message}")
370+
}
371+
} else {
372+
println("Backup file does not exist.")
373+
}
374+
}
375+
267376
fun Context.isPackageInstalled(
268377
packageName: String,
269378
userHandle: UserHandle = android.os.Process.myUserHandle()

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

Lines changed: 0 additions & 116 deletions
This file was deleted.

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,22 @@ import android.content.res.Configuration
99
import android.content.res.Resources
1010
import android.net.Uri
1111
import android.os.Build
12-
import android.provider.AlarmClock
13-
import android.provider.CalendarContract
1412
import android.provider.Settings
15-
import android.util.DisplayMetrics
16-
import android.util.Log
1713
import android.view.Gravity
1814
import android.view.View
1915
import android.view.Window
2016
import android.view.WindowInsets
21-
import android.view.WindowManager
2217
import android.widget.TextView
2318
import androidx.appcompat.widget.LinearLayoutCompat
19+
import com.github.droidworksstudio.ktx.backupSharedPreferences
20+
import com.github.droidworksstudio.ktx.restoreSharedPreferences
2421
import com.github.droidworksstudio.ktx.showLongToast
2522
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2623
import com.github.droidworksstudio.launcher.Constants
2724
import com.github.droidworksstudio.launcher.R
2825
import com.github.droidworksstudio.launcher.accessibility.ActionService
29-
import com.github.droidworksstudio.launcher.data.entities.AppInfo
3026
import java.util.Calendar
31-
import java.util.Date
3227
import javax.inject.Inject
33-
import kotlin.math.pow
34-
import kotlin.math.sqrt
3528

3629
class AppHelper @Inject constructor() {
3730
@SuppressLint("WrongConstant", "PrivateApi")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
1717
import com.github.droidworksstudio.ktx.hideKeyboard
1818
import com.github.droidworksstudio.ktx.launchApp
1919
import com.github.droidworksstudio.ktx.openSearch
20+
import com.github.droidworksstudio.ktx.searchCustomSearchEngine
21+
import com.github.droidworksstudio.ktx.searchOnPlayStore
2022
import com.github.droidworksstudio.ktx.showKeyboard
2123
import com.github.droidworksstudio.ktx.showLongToast
2224
import com.github.droidworksstudio.launcher.R
@@ -25,8 +27,6 @@ import com.github.droidworksstudio.launcher.databinding.FragmentDrawBinding
2527
import com.github.droidworksstudio.launcher.helper.AppHelper
2628
import com.github.droidworksstudio.launcher.helper.FingerprintHelper
2729
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
28-
import com.github.droidworksstudio.launcher.helper.searchCustomSearchEngine
29-
import com.github.droidworksstudio.launcher.helper.searchOnPlayStore
3030
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
3131
import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
3232
import com.github.droidworksstudio.launcher.listener.ScrollEventListener

0 commit comments

Comments
 (0)