@@ -711,31 +711,41 @@ class SnapshotHandler {
711
711
712
712
private ExecutorService mExecutorService ;
713
713
private Future <Serializable > mStateSnapshot ;
714
- private File mFile ;
715
- private Cipher mEncryptCipher ;
716
- private Cipher mDecryptCipher ;
714
+ private NavConfiguration mNavConfiguration ;
717
715
718
716
SnapshotHandler (NavConfiguration navConfiguration ) {
719
- mFile = navConfiguration .getSaveStateFile ();
720
- mEncryptCipher = navConfiguration .getSaveStateEncryptCipher ();
721
- mDecryptCipher = navConfiguration .getSaveStateDecryptCipher ();
722
- if (mFile != null ) {
717
+ mNavConfiguration = navConfiguration ;
718
+ if (getFile () != null ) {
723
719
loadSnapshot (); // start load as early as possible
724
720
}
725
721
}
726
722
723
+ private File getFile () {
724
+ return mNavConfiguration .getSaveStateFile ();
725
+ }
726
+
727
+ private Cipher getEncryptCipher () {
728
+ return mNavConfiguration .getSaveStateEncryptCipher ();
729
+ }
730
+
731
+ private Cipher getDecryptCipher () {
732
+ return mNavConfiguration .getSaveStateDecryptCipher ();
733
+ }
734
+
727
735
void saveState (Serializable serializable ) {
728
- if (mFile != null ) {
736
+ final File file = getFile ();
737
+ final Cipher encryptCipher = getEncryptCipher ();
738
+ if (file != null ) {
729
739
mStateSnapshot = getExecutorService ().submit (() -> {
730
- if (!mFile .exists ()) {
731
- mFile .getParentFile ().mkdirs ();
732
- mFile .createNewFile ();
740
+ if (!file .exists ()) {
741
+ file .getParentFile ().mkdirs ();
742
+ file .createNewFile ();
733
743
}
734
744
try {
735
- FileOutputStream fileOutputStream = new FileOutputStream (mFile );
745
+ FileOutputStream fileOutputStream = new FileOutputStream (file );
736
746
BufferedOutputStream bos = new BufferedOutputStream (fileOutputStream );
737
747
ObjectOutputStream oos = new ObjectOutputStream (bos );
738
- oos .writeObject (new SealedObject (serializable , mEncryptCipher ));
748
+ oos .writeObject (new SealedObject (serializable , encryptCipher ));
739
749
oos .close ();
740
750
bos .close ();
741
751
fileOutputStream .close ();
@@ -748,7 +758,7 @@ void saveState(Serializable serializable) {
748
758
}
749
759
750
760
Serializable loadState () {
751
- if (mFile != null ) {
761
+ if (getFile () != null ) {
752
762
if (mStateSnapshot != null ) {
753
763
return getState ();
754
764
}
@@ -759,17 +769,19 @@ Serializable loadState() {
759
769
}
760
770
761
771
private void loadSnapshot () {
772
+ final File file = getFile ();
773
+ final Cipher decryptCipher = getDecryptCipher ();
762
774
mStateSnapshot = getExecutorService ().submit (() -> {
763
- if (!mFile .exists ()) {
775
+ if (!file .exists ()) {
764
776
return null ;
765
777
}
766
778
Serializable result = null ;
767
779
try {
768
- FileInputStream fis = new FileInputStream (mFile );
780
+ FileInputStream fis = new FileInputStream (file );
769
781
BufferedInputStream bis = new BufferedInputStream (fis );
770
782
ObjectInputStream ois = new ObjectInputStream (bis );
771
783
result = (Serializable )
772
- ((SealedObject ) ois .readObject ()).getObject (mDecryptCipher );
784
+ ((SealedObject ) ois .readObject ()).getObject (decryptCipher );
773
785
ois .close ();
774
786
bis .close ();
775
787
fis .close ();
@@ -785,9 +797,12 @@ void clearState() {
785
797
mStateSnapshot .cancel (false );
786
798
mStateSnapshot = null ;
787
799
}
788
- getExecutorService ().submit (() -> {
789
- mFile .delete ();
790
- });
800
+ final File file = getFile ();
801
+ if (file != null ) {
802
+ getExecutorService ().submit (() -> {
803
+ file .delete ();
804
+ });
805
+ }
791
806
}
792
807
793
808
private ExecutorService getExecutorService () {
0 commit comments