Skip to content

Commit bc7c5dc

Browse files
committed
Fix restoring crop and zoom on profile changes
1 parent d9737f4 commit bc7c5dc

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

app/src/main/kotlin/de/markusfisch/android/binaryeye/activity/CameraActivity.kt

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class CameraActivity : AppCompatActivity() {
7171
private lateinit var zoomBar: SeekBar
7272
private lateinit var flashFab: FloatingActionButton
7373

74+
private var currentProfile = prefs.profile
75+
private var shouldStoreSettings = true
7476
private var formatsToRead = setOf<BarcodeFormat>()
7577
private var frameMetrics = FrameMetrics()
7678
private var decoding = true
@@ -159,8 +161,11 @@ class CameraActivity : AppCompatActivity() {
159161
startActivity(Intent(this, WelcomeActivity::class.java))
160162
return
161163
}
164+
loadPreferences()
165+
if (refreshIfProfileChanged()) {
166+
return
167+
}
162168
System.gc()
163-
updateFromPreferences()
164169
setReturnTarget(intent)
165170
// Avoid asking multiple times when the user has denied access
166171
// for this session. Otherwise ActivityCompat.requestPermissions()
@@ -183,7 +188,23 @@ class CameraActivity : AppCompatActivity() {
183188
return installedSince < 86400000
184189
}
185190

186-
private fun updateFromPreferences() {
191+
private fun refreshIfProfileChanged(): Boolean {
192+
if (currentProfile == prefs.profile) {
193+
return false
194+
}
195+
shouldStoreSettings = false
196+
zoomBar.max = 0 // Reset zoom when there are no preferences yet.
197+
currentProfile = prefs.profile
198+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
199+
recreate()
200+
} else {
201+
finish()
202+
startActivity(Intent(this, CameraActivity::class.java))
203+
}
204+
return true
205+
}
206+
207+
private fun loadPreferences() {
187208
detectorView.updateCropHandlePos()
188209
updateHintsAndTitle()
189210
if (prefs.bulkMode && bulkMode != prefs.bulkMode) {
@@ -246,14 +267,21 @@ class CameraActivity : AppCompatActivity() {
246267
override fun onPause() {
247268
super.onPause()
248269
closeCamera()
249-
saveZoom()
250-
detectorView.storeCropHandlePos()
270+
if (shouldStoreSettings) {
271+
storeSettings();
272+
}
273+
shouldStoreSettings = true
251274
}
252275

253276
private fun closeCamera() {
254277
cameraView.close()
255278
}
256279

280+
private fun storeSettings() {
281+
storeZoomBarSettings()
282+
detectorView.storeCropHandlePos()
283+
}
284+
257285
override fun onRestoreInstanceState(savedState: Bundle) {
258286
super.onRestoreInstanceState(savedState)
259287
zoomBar.max = savedState.getInt(ZOOM_MAX)
@@ -431,19 +459,15 @@ class CameraActivity : AppCompatActivity() {
431459
AlertDialog.Builder(this)
432460
.setTitle(R.string.profile)
433461
.setItems(profiles) { _, which ->
434-
val old = prefs.profile
435-
val new = when (which) {
462+
val newProfile = when (which) {
436463
0 -> null
437464
else -> profiles[which]
438465
}
439-
if (old != new) {
440-
prefs.load(this, new)
441-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
442-
recreate()
443-
} else {
444-
finish()
445-
startActivity(Intent(this, CameraActivity::class.java))
446-
}
466+
if (currentProfile != newProfile) {
467+
storeSettings()
468+
prefs.load(this, newProfile)
469+
loadPreferences()
470+
refreshIfProfileChanged()
447471
}
448472
}
449473
.show()
@@ -536,11 +560,12 @@ class CameraActivity : AppCompatActivity() {
536560
parameters: Camera.Parameters
537561
) {
538562
zoomBar.visibility = if (parameters.isZoomSupported) {
563+
restoreZoomBarSettings()
539564
val max = parameters.maxZoom
540565
if (zoomBar.max != max) {
541566
zoomBar.max = max
542567
zoomBar.progress = max / 10
543-
saveZoom()
568+
storeZoomBarSettings()
544569
}
545570
parameters.zoom = zoomBar.progress
546571
View.VISIBLE
@@ -651,7 +676,6 @@ class CameraActivity : AppCompatActivity() {
651676

652677
override fun onStopTrackingTouch(seekBar: SeekBar) {}
653678
})
654-
restoreZoom()
655679
}
656680

657681
@Suppress("DEPRECATION")
@@ -665,14 +689,14 @@ class CameraActivity : AppCompatActivity() {
665689
}
666690
}
667691

668-
private fun saveZoom() {
692+
private fun storeZoomBarSettings() {
669693
val editor = prefs.preferences.edit()
670694
editor.putInt(ZOOM_MAX, zoomBar.max)
671695
editor.putInt(ZOOM_LEVEL, zoomBar.progress)
672696
editor.apply()
673697
}
674698

675-
private fun restoreZoom() {
699+
private fun restoreZoomBarSettings() {
676700
zoomBar.max = prefs.preferences.getInt(ZOOM_MAX, zoomBar.max)
677701
zoomBar.progress = prefs.preferences.getInt(
678702
ZOOM_LEVEL,

0 commit comments

Comments
 (0)