@@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException
5
5
import android.content.ComponentName
6
6
import android.content.Context
7
7
import android.content.Intent
8
+ import android.content.SharedPreferences
8
9
import android.content.pm.LauncherApps
9
10
import android.content.pm.PackageManager
10
11
import android.content.res.Configuration
@@ -32,8 +33,11 @@ import androidx.core.graphics.drawable.toBitmap
32
33
import androidx.core.os.ConfigurationCompat
33
34
import androidx.lifecycle.LifecycleObserver
34
35
import androidx.lifecycle.LifecycleOwner
36
+ import com.github.droidworksstudio.launcher.Constants
35
37
import com.github.droidworksstudio.launcher.data.entities.AppInfo
36
38
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
39
+ import java.io.File
40
+ import java.io.IOException
37
41
import java.util.Calendar
38
42
import java.util.Date
39
43
import kotlin.math.pow
@@ -264,6 +268,111 @@ fun Context.openBatteryManager() {
264
268
}
265
269
}
266
270
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
+
267
376
fun Context.isPackageInstalled (
268
377
packageName : String ,
269
378
userHandle : UserHandle = android.os.Process .myUserHandle()
0 commit comments