@@ -3,7 +3,6 @@ package app.grapheneos.camera.ui.activities
3
3
import android.animation.ArgbEvaluator
4
4
import android.animation.ValueAnimator
5
5
import android.annotation.SuppressLint
6
- import android.app.Activity
7
6
import android.app.AlertDialog
8
7
import android.content.ActivityNotFoundException
9
8
import android.content.Context
@@ -12,6 +11,7 @@ import android.graphics.Color
12
11
import android.graphics.drawable.ColorDrawable
13
12
import android.media.MediaMetadataRetriever
14
13
import android.net.Uri
14
+ import android.os.Build
15
15
import android.os.Bundle
16
16
import android.os.Handler
17
17
import android.provider.DocumentsContract
@@ -23,9 +23,9 @@ import android.view.Menu
23
23
import android.view.MenuItem
24
24
import android.view.View
25
25
import android.widget.Toast
26
- import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
27
26
import androidx.appcompat.app.AppCompatActivity
28
27
import androidx.core.content.ContextCompat
28
+ import androidx.core.os.BundleCompat
29
29
import androidx.viewpager2.widget.ViewPager2
30
30
import androidxc.exifinterface.media.ExifInterface
31
31
import app.grapheneos.camera.CapturedItem
@@ -39,7 +39,6 @@ import app.grapheneos.camera.util.getParcelableArrayListExtra
39
39
import app.grapheneos.camera.util.getParcelableExtra
40
40
import app.grapheneos.camera.util.storageLocationToUiString
41
41
import com.google.android.material.snackbar.Snackbar
42
- import java.io.FileNotFoundException
43
42
import java.text.SimpleDateFormat
44
43
import java.util.Date
45
44
import java.util.Locale
@@ -64,12 +63,16 @@ class InAppGallery : AppCompatActivity() {
64
63
65
64
private lateinit var rootView: View
66
65
66
+ private var lastViewedMediaItem : CapturedItem ? = null
67
+
67
68
companion object {
68
69
const val INTENT_KEY_SECURE_MODE = " is_secure_mode"
69
70
const val INTENT_KEY_VIDEO_ONLY_MODE = " video_only_mode"
70
71
const val INTENT_KEY_LIST_OF_SECURE_MODE_CAPTURED_ITEMS = " secure_mode_items"
71
72
const val INTENT_KEY_LAST_CAPTURED_ITEM = " last_captured_item"
72
73
74
+ const val LAST_VIEWED_ITEM_KEY = " LAST_VIEWED_ITEM_KEY"
75
+
73
76
@SuppressLint(" SimpleDateFormat" )
74
77
fun convertTime (time : Long , showTimeZone : Boolean = true): String {
75
78
val date = Date (time)
@@ -431,6 +434,10 @@ class InAppGallery : AppCompatActivity() {
431
434
snackBar = Snackbar .make(gallerySlider, " " , Snackbar .LENGTH_LONG )
432
435
gallerySlider.setPageTransformer(GSlideTransformer ())
433
436
437
+ if (savedInstanceState != null ) {
438
+ lastViewedMediaItem = BundleCompat .getParcelable(savedInstanceState, LAST_VIEWED_ITEM_KEY , CapturedItem ::class .java)
439
+ }
440
+
434
441
val intent = this .intent
435
442
436
443
val showVideosOnly = intent.getBooleanExtra(INTENT_KEY_VIDEO_ONLY_MODE , false )
@@ -468,23 +475,25 @@ class InAppGallery : AppCompatActivity() {
468
475
mainExecutor.execute { asyncResultReady(items) }
469
476
}
470
477
471
- val lastCapturedItem = getParcelableExtra<CapturedItem >(intent, INTENT_KEY_LAST_CAPTURED_ITEM )
478
+ if (lastViewedMediaItem == null ) {
479
+ val lastCapturedItem = getParcelableExtra<CapturedItem >(intent, INTENT_KEY_LAST_CAPTURED_ITEM )
472
480
473
- if (lastCapturedItem != null ) {
474
- val list = ArrayList <CapturedItem >()
475
- list.add(lastCapturedItem)
476
- GallerySliderAdapter (this , list).let {
477
- gallerySliderAdapter = it
478
- gallerySlider.adapter = it
479
- }
480
- } else {
481
- Handler (mainLooper).postDelayed({
482
- if (gallerySliderAdapter == null ) {
483
- binding.placeholderText.root.visibility = View .VISIBLE
481
+ if (lastCapturedItem != null ) {
482
+ val list = ArrayList <CapturedItem >()
483
+ list.add(lastCapturedItem)
484
+ GallerySliderAdapter (this , list).let {
485
+ gallerySliderAdapter = it
486
+ gallerySlider.adapter = it
484
487
}
485
- }, 500 )
488
+ } else {
489
+ Handler (mainLooper).postDelayed({
490
+ if (gallerySliderAdapter == null ) {
491
+ binding.placeholderText.root.visibility = View .VISIBLE
492
+ }
493
+ }, 500 )
486
494
487
- hideActionBar()
495
+ hideActionBar()
496
+ }
488
497
}
489
498
}
490
499
@@ -499,6 +508,17 @@ class InAppGallery : AppCompatActivity() {
499
508
return
500
509
}
501
510
511
+ var capturedItemPosition = 0
512
+
513
+ if (lastViewedMediaItem != null ) {
514
+ for (i in 0 .. < items.size) {
515
+ val capturedItem = items[i]
516
+ if (capturedItem == lastViewedMediaItem) {
517
+ capturedItemPosition = i
518
+ }
519
+ }
520
+ }
521
+
502
522
binding.placeholderText.root.visibility = View .GONE
503
523
504
524
val existingAdapter = gallerySliderAdapter
@@ -523,6 +543,7 @@ class InAppGallery : AppCompatActivity() {
523
543
}
524
544
existingAdapter.notifyItemRangeInserted(1 , items.size - 1 )
525
545
}
546
+ gallerySlider.setCurrentItem(capturedItemPosition, false )
526
547
showActionBar()
527
548
}
528
549
@@ -560,4 +581,12 @@ class InAppGallery : AppCompatActivity() {
560
581
snackBar.setText(msg)
561
582
snackBar.show()
562
583
}
584
+
585
+ override fun onSaveInstanceState (outState : Bundle ) {
586
+ super .onSaveInstanceState(outState)
587
+
588
+ gallerySliderAdapter?.let {
589
+ outState.putParcelable(LAST_VIEWED_ITEM_KEY , it.items[gallerySlider.currentItem])
590
+ }
591
+ }
563
592
}
0 commit comments