Skip to content

Commit 4a72876

Browse files
committed
Exclude cache_prefs from backup
1 parent dd90974 commit 4a72876

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
<application
1111
android:allowBackup="true"
12+
android:fullBackupContent="@xml/backup_rules"
13+
android:dataExtractionRules="@xml/data_extraction_rules"
1214
android:icon="@mipmap/ic_launcher"
1315
android:label="@string/app_name"
1416
android:roundIcon="@mipmap/ic_launcher_round"

app/src/main/java/org/nitri/opentopo/CacheSettingsFragment.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import androidx.appcompat.widget.SwitchCompat
1616
import androidx.fragment.app.DialogFragment
1717
import androidx.localbroadcastmanager.content.LocalBroadcastManager
1818
import androidx.preference.PreferenceManager
19+
import android.content.SharedPreferences
1920
import org.osmdroid.config.Configuration
2021
import java.io.File
22+
import androidx.core.content.edit
2123

2224

2325
class CacheSettingsFragment : DialogFragment() {
@@ -30,18 +32,21 @@ class CacheSettingsFragment : DialogFragment() {
3032
val inflater = requireActivity().layoutInflater
3133
@SuppressLint("InflateParams") val view =
3234
inflater.inflate(R.layout.fragment_cache_settings, null)
33-
val prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().applicationContext)
35+
val defaultPrefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().applicationContext)
36+
val cachePrefs: SharedPreferences = requireActivity().getSharedPreferences("cache_prefs", Context.MODE_PRIVATE)
3437
val tvExternalStorageRoot = view.findViewById<TextView>(R.id.tvExternalStorageRoot)
3538
val swExternalStorage = view.findViewById<SwitchCompat>(R.id.swExternalStorage)
3639
etTileCache = view.findViewById(R.id.etTileCache)
3740
etCacheSize = view.findViewById(R.id.etCacheSize)
3841
val basePath = Configuration.getInstance().osmdroidBasePath
3942
val storageRoot = basePath?.absolutePath ?: getString(R.string.unknown_symbol)
4043
tvExternalStorageRoot.text = getString(R.string.storage_root, storageRoot)
41-
val currentExternalStorage = prefs.getBoolean(PREF_EXTERNAL_STORAGE, false)
44+
val currentExternalStorage = cachePrefs.getBoolean(PREF_EXTERNAL_STORAGE,
45+
defaultPrefs.getBoolean(PREF_EXTERNAL_STORAGE, false)
46+
)
4247
swExternalStorage.isChecked = currentExternalStorage
43-
val currentTileCache = prefs.getString(PREF_TILE_CACHE, DEFAULT_TILE_CACHE)
44-
val currentCacheSize = prefs.getInt(PREF_CACHE_SIZE, DEFAULT_CACHE_SIZE)
48+
val currentTileCache = defaultPrefs.getString(PREF_TILE_CACHE, DEFAULT_TILE_CACHE)
49+
val currentCacheSize = defaultPrefs.getInt(PREF_CACHE_SIZE, DEFAULT_CACHE_SIZE)
4550
etTileCache.setText(currentTileCache)
4651
etCacheSize.setText(currentCacheSize.toString())
4752
builder.setView(view)
@@ -56,13 +61,12 @@ class CacheSettingsFragment : DialogFragment() {
5661
}
5762

5863
if (newCacheSize > 0) {
59-
val editor = prefs.edit()
60-
editor.apply {
61-
putBoolean(PREF_EXTERNAL_STORAGE, newExternalStorage)
64+
defaultPrefs.edit().apply {
6265
putString(PREF_TILE_CACHE, newTileCache)
6366
putInt(PREF_CACHE_SIZE, newCacheSize)
6467
apply()
6568
}
69+
cachePrefs.edit { putBoolean(PREF_EXTERNAL_STORAGE, newExternalStorage) }
6670
val cacheDir = File("$storageRoot/$newTileCache")
6771
if (cacheDir.mkdirs()) {
6872
Log.i(TAG, "Tile cache created: $newTileCache")
@@ -71,7 +75,7 @@ class CacheSettingsFragment : DialogFragment() {
7175
configuration.osmdroidTileCache = cacheDir
7276
configuration.tileFileSystemCacheMaxBytes =
7377
newCacheSize.toLong() * 1024 * 1024
74-
configuration.save(requireActivity().applicationContext, prefs)
78+
configuration.save(requireActivity().applicationContext, defaultPrefs)
7579
val intent = Intent(ACTION_CACHE_CHANGED);
7680
val localBroadcastManager = LocalBroadcastManager.getInstance(
7781
requireActivity()
@@ -94,7 +98,6 @@ class CacheSettingsFragment : DialogFragment() {
9498
return dialog
9599
}
96100

97-
98101
override fun onResume() {
99102
super.onResume()
100103
val dialog = dialog as AlertDialog?

app/src/main/java/org/nitri/opentopo/MapFragment.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,13 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
160160
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
161161
val configuration = Configuration.getInstance()
162162
configuration.userAgentValue = BuildConfig.APPLICATION_ID
163-
val basePath = Utils.getOsmdroidBasePath(context, sharedPreferences.getBoolean(CacheSettingsFragment.PREF_EXTERNAL_STORAGE, false))
163+
val externalPrefs = requireActivity().getSharedPreferences("cache_prefs", Context.MODE_PRIVATE)
164+
val externalStorage = if (externalPrefs.contains(CacheSettingsFragment.PREF_EXTERNAL_STORAGE)) {
165+
externalPrefs.getBoolean(CacheSettingsFragment.PREF_EXTERNAL_STORAGE, false)
166+
} else {
167+
sharedPreferences.getBoolean(CacheSettingsFragment.PREF_EXTERNAL_STORAGE, false)
168+
}
169+
val basePath = Utils.getOsmdroidBasePath(context, externalStorage)
164170
configuration.osmdroidBasePath = basePath
165171
val tileCache = File(configuration.osmdroidBasePath.absolutePath,
166172
sharedPreferences.getString(CacheSettingsFragment.PREF_TILE_CACHE, CacheSettingsFragment.DEFAULT_TILE_CACHE)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<full-backup-content>
3+
<!-- Exclude the dedicated SharedPreferences file that holds the external storage toggle -->
4+
<exclude domain="sharedpref" path="cache_prefs.xml" />
5+
</full-backup-content>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<data-extraction-rules>
3+
<cloud-backup>
4+
<!-- Exclude the dedicated SharedPreferences file from cloud backups on Android 12+ -->
5+
<exclude domain="sharedpref" path="cache_prefs.xml" />
6+
</cloud-backup>
7+
<device-transfer>
8+
<!-- Exclude from device-to-device transfer as well -->
9+
<exclude domain="sharedpref" path="cache_prefs.xml" />
10+
</device-transfer>
11+
</data-extraction-rules>

0 commit comments

Comments
 (0)