55import android .content .BroadcastReceiver ;
66import android .content .Context ;
77import android .content .Intent ;
8+ import android .content .SharedPreferences ;
89import android .content .pm .PackageManager ;
910import androidx .annotation .CallSuper ;
1011import androidx .core .app .ActivityCompat ;
2122import net .sharksystem .asap .android .ASAPChunkReceivedBroadcastIntent ;
2223import net .sharksystem .asap .android .ASAPServiceCreationIntent ;
2324import net .sharksystem .asap .android .Util ;
25+ import net .sharksystem .asap .util .Helper ;
2426
2527import java .io .IOException ;
2628import java .util .ArrayList ;
3638
3739public class ASAPAndroidPeer extends BroadcastReceiver implements ASAPPeer {
3840 private static final int MY_ASK_FOR_PERMISSIONS_REQUEST = 100 ;
41+ private static final String PREFERENCES_FILE = "ASAPPeerApplicationSideInitSettings" ;
42+ private static final String SUPPORTED_FORMATS = "ASAPPeer_SupportedFormats" ;
43+ private static final String OWNER = "ASAPPeer_Owner" ;
44+ private static final String ROOT_FOLDER = "ASAPPeer_RootFolder" ;
3945 private static ASAPAndroidPeer singleton ;
4046 private ASAPPeerFS asapPeerApplicationSide = null ;
4147 private Collection <CharSequence > supportedFormats ;
@@ -68,6 +74,62 @@ static ASAPAndroidPeer getASAPAndroidPeer() {
6874 return ASAPAndroidPeer .singleton ;
6975 }
7076
77+ /////////////////////////////////////////////////////////////////////////////////////////////
78+ // memento mori //
79+ /////////////////////////////////////////////////////////////////////////////////////////////
80+
81+ private static void writeMemento (Activity activity , Collection <CharSequence > supportedFormats ,
82+ CharSequence asapOwner , CharSequence rootFolder ) {
83+
84+ SharedPreferences sharedPref = activity .getSharedPreferences (
85+ ASAPAndroidPeer .PREFERENCES_FILE , Context .MODE_PRIVATE );
86+
87+ SharedPreferences .Editor editor = sharedPref .edit ();
88+
89+ String supportFormatsString = Helper .collection2String (supportedFormats );
90+ Log .d (net .sharksystem .asap .util .Log .startLog (ASAPAndroidPeer .class ).toString (),
91+ "write memento: " + supportFormatsString + " | " + asapOwner
92+ + " | " + rootFolder );
93+
94+ editor .putString (ASAPAndroidPeer .SUPPORTED_FORMATS , supportFormatsString );
95+ editor .putString (ASAPAndroidPeer .OWNER , asapOwner .toString ());
96+ editor .putString (ASAPAndroidPeer .ROOT_FOLDER , rootFolder .toString ());
97+
98+ editor .commit ();
99+ }
100+
101+ static void restoreFromMemento (Activity activity )
102+ throws ASAPComponentNotYetInitializedException {
103+
104+ Log .d (net .sharksystem .asap .util .Log .startLog (ASAPAndroidPeer .class ).toString (),
105+ "restore from memento with activity == " + activity );
106+
107+ SharedPreferences sharedPref = activity .getSharedPreferences (
108+ ASAPAndroidPeer .PREFERENCES_FILE , Context .MODE_PRIVATE );
109+
110+ if (sharedPref != null || !sharedPref .contains (ASAPAndroidPeer .SUPPORTED_FORMATS )) {
111+ throw new ASAPComponentNotYetInitializedException (
112+ "ASAP peer was never initialized - nothing found in shared preferences: " );
113+ }
114+
115+ try {
116+ new ASAPAndroidPeer (
117+ // supported formats
118+ Helper .string2CharSequenceSet (
119+ sharedPref .getString (ASAPAndroidPeer .SUPPORTED_FORMATS , "" )),
120+ // owner
121+ sharedPref .getString (ASAPAndroidPeer .OWNER , ASAPPeer .UNKNOWN_USER .toString ()),
122+ // rootFolder
123+ sharedPref .getString (ASAPAndroidPeer .ROOT_FOLDER , ASAPPeerFS .DEFAULT_ROOT_FOLDER_NAME .toString ()),
124+ ASAPPeer .ONLINE_EXCHANGE_DEFAULT ,
125+ activity );
126+ } catch (IOException | ASAPException e ) {
127+ // that's highly unlikely
128+ throw new ASAPComponentNotYetInitializedException (
129+ "could not restore app side peer from shared preferences: " + e .getLocalizedMessage ());
130+ }
131+ }
132+
71133 public static boolean peerInitialized () {
72134 return ASAPAndroidPeer .singleton != null ;
73135 }
@@ -81,20 +143,17 @@ public static void initializePeer(
81143 Collection <CharSequence > supportedFormats , Activity initialActivity )
82144 throws IOException , ASAPException {
83145
84- new ASAPAndroidPeer (supportedFormats , ASAPPeer .UNKNOWN_USER ,
85- ASAPPeerFS .DEFAULT_ROOT_FOLDER_NAME ,
86- ASAPPeer .ONLINE_EXCHANGE_DEFAULT , initialActivity );
87-
146+ ASAPAndroidPeer .initializePeer (ASAPPeer .UNKNOWN_USER , supportedFormats ,
147+ ASAPPeerFS .DEFAULT_ROOT_FOLDER_NAME , initialActivity );
88148 }
89149
90150 public static void initializePeer (CharSequence asapOwner ,
91151 Collection <CharSequence > supportedFormats ,
92152 Activity initialActivity )
93153 throws IOException , ASAPException {
94154
95- new ASAPAndroidPeer (supportedFormats , asapOwner ,
96- ASAPPeerFS .DEFAULT_ROOT_FOLDER_NAME ,
97- ASAPPeer .ONLINE_EXCHANGE_DEFAULT , initialActivity );
155+ ASAPAndroidPeer .initializePeer (asapOwner , supportedFormats ,
156+ ASAPPeerFS .DEFAULT_ROOT_FOLDER_NAME , initialActivity );
98157 }
99158
100159 public static void initializePeer (CharSequence asapOwner ,
@@ -103,6 +162,8 @@ public static void initializePeer(CharSequence asapOwner,
103162 Activity initialActivity )
104163 throws IOException , ASAPException {
105164
165+ // write memento
166+ ASAPAndroidPeer .writeMemento (initialActivity , supportedFormats , asapOwner , rootFolder );
106167 new ASAPAndroidPeer (supportedFormats , asapOwner , rootFolder ,
107168 ASAPPeer .ONLINE_EXCHANGE_DEFAULT , initialActivity );
108169 }
0 commit comments