Skip to content

Commit 440874c

Browse files
committed
simple example works again after refactoring.
1 parent 7245602 commit 440874c

File tree

4 files changed

+88
-27
lines changed

4 files changed

+88
-27
lines changed

app/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,24 @@ public class ASAPActivity extends AppCompatActivity implements
4343

4444
private ASAPAndroidPeer asapAndroidPeer;
4545

46-
/**
47-
* Create a new activity object. Make sure your application object is fully
48-
* instantiated
49-
* @param asapAndroidPeer
50-
*/
51-
ASAPActivity(ASAPAndroidPeer asapAndroidPeer) {
52-
this.asapAndroidPeer = asapAndroidPeer;
53-
}
54-
5546
/**
5647
* @throws ASAPComponentNotYetInitializedException if ASAPAndroidPeer was not initialized
5748
*/
5849
public ASAPActivity() {
59-
this(ASAPAndroidPeer.getASAPAndroidPeer());
60-
}
50+
if(!ASAPAndroidPeer.peerInitialized()) {
51+
// weired. Should be with initial activity - anyway try to recover from memento
52+
Log.d(this.getLogStart(), "application side peer not yet initialized - try to restore from memory");
53+
Log.d(this.getLogStart(), "this == " + this);
54+
ASAPAndroidPeer.restoreFromMemento(this); // can throw exception
55+
Log.d(this.getLogStart(), "application side peer restored from memory");
56+
}
6157

62-
protected ASAPAndroidPeer getASAPAndroidPeer() {
63-
return this.asapAndroidPeer;
58+
this.asapAndroidPeer = ASAPAndroidPeer.getASAPAndroidPeer();
6459
}
6560

66-
protected ASAPPeer getASAPPeer() {
67-
return this.asapAndroidPeer;
68-
}
61+
protected ASAPAndroidPeer getASAPAndroidPeer() { return this.asapAndroidPeer; }
62+
63+
protected ASAPPeer getASAPPeer() { return this.asapAndroidPeer; }
6964

7065
/**
7166
* Create a closed asap channel. Ensure to call this method before ever sending a message into

app/src/main/java/net/sharksystem/asap/android/apps/ASAPAndroidPeer.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.BroadcastReceiver;
66
import android.content.Context;
77
import android.content.Intent;
8+
import android.content.SharedPreferences;
89
import android.content.pm.PackageManager;
910
import androidx.annotation.CallSuper;
1011
import androidx.core.app.ActivityCompat;
@@ -21,6 +22,7 @@
2122
import net.sharksystem.asap.android.ASAPChunkReceivedBroadcastIntent;
2223
import net.sharksystem.asap.android.ASAPServiceCreationIntent;
2324
import net.sharksystem.asap.android.Util;
25+
import net.sharksystem.asap.util.Helper;
2426

2527
import java.io.IOException;
2628
import java.util.ArrayList;
@@ -36,6 +38,10 @@
3638

3739
public 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
}

app/src/main/java/net/sharksystem/asap/android/example/ASAPExampleMessagingActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ private void doHandleReceivedMessages(ASAPMessages asapMessages) {
123123
sb.append(e.getLocalizedMessage());
124124
}
125125

126-
TextView receivedMessagesTV = this.findViewById(R.id.exampleMessagingMessages);
127-
receivedMessagesTV.setText(sb.toString());
126+
this.runOnUiThread(new Runnable() {
127+
@Override
128+
public void run() {
129+
TextView receivedMessagesTV =
130+
ASAPExampleMessagingActivity.this.findViewById(R.id.exampleMessagingMessages);
131+
receivedMessagesTV.setText(sb.toString());
132+
}
133+
});
128134
}
129135
}

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import java.util.Set;
3232

3333
/**
34-
* Service that searches for and creates layer 2 point-to-point connections
35-
* to run an ASAP session on.
34+
* This class controls layer 2 connections
3635
*/
3736

3837
public class ASAPService extends Service

0 commit comments

Comments
 (0)