-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharkNetApp.java
More file actions
308 lines (251 loc) · 12.7 KB
/
SharkNetApp.java
File metadata and controls
308 lines (251 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package net.sharksystem.sharknet.android;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import net.sharksystem.R;
import net.sharksystem.SharkException;
import net.sharksystem.SharkPeer;
import net.sharksystem.SharkPeerFS;
import net.sharksystem.SharkStatusException;
import net.sharksystem.app.messenger.SharkMessengerComponent;
import net.sharksystem.asap.ASAP;
import net.sharksystem.asap.ASAPSecurityException;
import net.sharksystem.asap.android.Util;
import net.sharksystem.asap.android.apps.ASAPAndroidPeer;
import net.sharksystem.pki.HelperPKITests;
import net.sharksystem.pki.SharkPKIComponent;
import net.sharksystem.pki.SharkPKIComponentFactory;
import net.sharksystem.pki.android.SharkPKIReceivedCredentialMessageHandler;
import net.sharksystem.app.messenger.SharkMessengerComponentFactory;
import java.io.File;
import java.io.IOException;
public class SharkNetApp {
private static final CharSequence APP_FOLDER_NAME = "SharkNet2_AppData";
private static SharkNetApp singleton;
private SharkPeer sharkPeer;
private ASAPAndroidPeer asapAndroidPeer;
private SharkPKIReceivedCredentialMessageHandler receivedCredentialListener;
public static SharkNetApp getSharkNetApp() {
if(SharkNetApp.singleton == null)
throw new SharkStatusException("SharkNetApplication not yet initialized");
return SharkNetApp.singleton;
}
public SharkPeer getSharkPeer() {
if(this.sharkPeer == null)
throw new SharkStatusException("Shark peer not yet initialized");
return this.sharkPeer;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// system setup //
/////////////////////////////////////////////////////////////////////////////////////////////
public static void initializeSharkNetApp(Activity initialActivity, String ownerID)
throws SharkException, IOException {
///////////////////////////////////// restore app data - if any
if(SharkNetApp.singleton == null) {
Log.d(getLogStart(), "going to initialize shark net application");
SharkNetApp.singleton = new SharkNetApp(initialActivity, ownerID);
// produce folder
File rootDir =
Util.getASAPRootDirectory(initialActivity, SharkNetApp.APP_FOLDER_NAME, ownerID);
// produce application side shark peer
SharkNetApp.singleton.sharkPeer = new SharkPeerFS(
SharkNetApp.singleton.getOwnerID(),
rootDir.getAbsolutePath()
);
/////////////////////////// setup PKI /////////////////////////////////////////////
// create Android specific key store
AndroidASAPKeyStore androidASAPKeyStore = null;
try {
androidASAPKeyStore = new AndroidASAPKeyStore(initialActivity, ownerID);
} catch (ASAPSecurityException e) {
Log.e(getLogStart(), "cannot create Android key store, fatal - give up: "
+ e.getLocalizedMessage());
throw new SharkException("cannot create Android key store", e);
}
// create a pki component factory with android key store
SharkPKIComponentFactory pkiComponentFactory = new SharkPKIComponentFactory();
// register this component with shark peer
SharkNetApp.singleton.sharkPeer.addComponent(
pkiComponentFactory, SharkPKIComponent.class);
SharkPKIComponent sharkPKI = (SharkPKIComponent)
SharkNetApp.singleton.sharkPeer.getComponent(SharkPKIComponent.class);
///////////////////////////////////// setup SharkMessenger
// create messenger factory - needs a pki
// get messenger factory with pki component as parameter.
SharkMessengerComponentFactory messengerFactory =
new SharkMessengerComponentFactory(
(SharkPKIComponent) SharkNetApp.singleton.sharkPeer
.getComponent(SharkPKIComponent.class));
// register this component with shark peer
SharkNetApp.singleton.sharkPeer.addComponent(
messengerFactory, SharkMessengerComponent.class);
Log.d(getLogStart(), "shark net components added");
///////////////////////////////////// ignition
// setup android (application side peer)
ASAPAndroidPeer.initializePeer(
ownerID,
SharkNetApp.singleton.sharkPeer.getFormats(),
SharkNetApp.APP_FOLDER_NAME,
initialActivity);
// TODO - need to inject keystore
// launch service side
ASAPAndroidPeer applicationSideASAPPeer = ASAPAndroidPeer.startPeer(initialActivity);
Log.d(getLogStart(), "ASAP had a liftoff");
// remember
SharkNetApp.singleton.setApplicationSideASAPAndroidPeer(applicationSideASAPPeer);
// use asap peer proxy for this app side shark peer
SharkNetApp.singleton.sharkPeer.start(applicationSideASAPPeer);
Log.d(getLogStart(), "shark net application launched");
///////////////////////////////////// testing: example data
//Log.d(getLogStart(), "fill pki with example data");
//HelperPKITests.fillWithExampleData((SharkPKIComponent) sharkPKI);
} else {
Log.d(getLogStart(), "shark net application already initialized - ignore");
}
}
private void setApplicationSideASAPAndroidPeer(ASAPAndroidPeer asapAndroidPeer) {
this.asapAndroidPeer = asapAndroidPeer;
}
public ASAPAndroidPeer getASAPAndroidPeer() {
// return this.sharkPeer.getASAPPeer();
return this.asapAndroidPeer;
}
// debug / tests
SharkPKIReceivedCredentialMessageHandler getReceivedCredentialListener() {
return this.receivedCredentialListener;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// component getter //
/////////////////////////////////////////////////////////////////////////////////////////////
public SharkPKIComponent getSharkPKI() {
try {
return (SharkPKIComponent) this.sharkPeer.getComponent(SharkPKIComponent.class);
} catch (SharkException e) {
// this cannot happen - this component is initialized in init
throw new SharkStatusException("internal error: " + e.getLocalizedMessage());
}
}
public SharkMessengerComponent getSharkMessenger() {
try {
return (SharkMessengerComponent)
this.sharkPeer.getComponent(SharkMessengerComponent.class);
} catch (SharkException e) {
// this cannot happen - this component is initialized in init
throw new SharkStatusException("internal error: " + e.getLocalizedMessage());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// GUI support //
/////////////////////////////////////////////////////////////////////////////////////////////
public void setupDrawerLayout(Activity activity) {
DrawerLayout mDrawerLayout = activity.findViewById(R.id.sharknet_drawer_layout);
if(mDrawerLayout == null) {
Log.d(getLogStart(), "cannot find drawer layout: " + R.id.sharknet_drawer_layout);
return;
}
// add listener to drawer items
NavigationView navigationView = activity.findViewById(R.id.sharknet_drawer_navigation_view);
navigationView.setNavigationItemSelectedListener(
new DrawerOnNavigationItemListener(activity, mDrawerLayout));
}
/////////////////////////////////////////////////////////////////////////////////////////////
// application setting beside ASAP/Shark //
/////////////////////////////////////////////////////////////////////////////////////////////
public final static String PREFERENCES_FILE = "SharkNet2Identity";
private final static String OWNER_NAME = "SharkNet2Identity_OwnerName";
private final static String OWNER_ID = "SharkNet2Identity_OwnerID";
public final static String DEFAULT_OWNER_NAME = "SNUser";
private final static String DEFAULT_OWNER_ID = "Default_SN_USER_ID";
private CharSequence ownerName;
private CharSequence ownerID;
public CharSequence getOwnerID() {
return this.ownerID;
}
public CharSequence getOwnerName() {
return this.ownerName;
}
private SharkNetApp(Context initialContext, String ownerID) {
SharedPreferences sharedPref = initialContext.getSharedPreferences(
PREFERENCES_FILE, Context.MODE_PRIVATE);
if(sharedPref.contains(OWNER_NAME)) {
this.ownerName = sharedPref.getString(OWNER_NAME, DEFAULT_OWNER_NAME);
} else {
this.ownerName = DEFAULT_OWNER_NAME;
}
this.ownerID = ownerID;
}
/**
* Return ownerID. The id is created automatically when the system is initialized
* @param context app context
* @return String - ownerID
* @throws SharkException if ownerID has not been created yet.
* @see #initializeSystem(Context, CharSequence)
*/
public static String getOwnerID(Context context) throws SharkException {
SharedPreferences sharedPref = context.getSharedPreferences(
PREFERENCES_FILE, Context.MODE_PRIVATE);
if(sharedPref.contains(OWNER_ID)) {
return sharedPref.getString(OWNER_ID, DEFAULT_OWNER_ID);
} else throw new SharkException("not yet personalized - no id set");
}
/**
* Static method to initialize system with a name.
* An ID is automatically created and will not be changed.
* @param ctx App context
* @param ownerName The name the User defined for themselves.
* @throws SharkException if owner name is null, empty or default
* @see #DEFAULT_OWNER_NAME
*/
public static void initializeSystem(Context ctx, CharSequence ownerName)
throws SharkException {
checkOwnerName(ownerName);
SharedPreferences sharedPref = ctx.getSharedPreferences(
PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(OWNER_NAME, ownerName.toString());
// create owner id
// if (!sharedPref.contains(OWNER_ID)) {
String ownerID = ASAP.createUniqueID(); // TODO is changed on every startup - correct?
editor.putString(OWNER_ID, ownerID);
// }
editor.apply();
}
/**
* Checks owner name for valid syntax and throws exception on wrong syntax,
* does nothing if syntax is correct
* @param ownerName name to be checked
* @throws SharkException if owner name is null empty or default
* @see #DEFAULT_OWNER_NAME
*/
private static void checkOwnerName(CharSequence ownerName) throws SharkException {
if (ownerName == null || ownerName.equals(""))
throw new SharkException("Owner name is null or empty");
if (ownerName.equals(DEFAULT_OWNER_NAME))
throw new SharkException("Owner name can't be default name: " + DEFAULT_OWNER_NAME);
}
/**
* Change owner name (but not id)
* @param ctx app context
* @param ownerName new owner name
* @throws SharkException if the new owner name is null, empty or equals the default owner name
* @see #DEFAULT_OWNER_NAME
*/
public void changeOwnerName(Context ctx, String ownerName) throws SharkException {
checkOwnerName(ownerName);
SharedPreferences sharedPref = ctx.getSharedPreferences(
PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(OWNER_NAME, ownerName);
editor.apply();
this.ownerName = ownerName;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// utils //
/////////////////////////////////////////////////////////////////////////////////////////////
private static String getLogStart() {
return SharkNetApp.class.getSimpleName();
}
}