Skip to content

Commit 71211d1

Browse files
committed
ready to start debugging sn channels - towards a working version
1 parent 7a02da3 commit 71211d1

File tree

3 files changed

+233
-23
lines changed

3 files changed

+233
-23
lines changed

app/src/main/java/net/sharksystem/asap/sharknet/android/SNChannelAddMessageActivity.java

Lines changed: 134 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.sharksystem.asap.sharknet.android;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.util.Log;
56
import android.view.View;
7+
import android.widget.CheckBox;
68
import android.widget.EditText;
79
import android.widget.TextView;
810
import android.widget.Toast;
@@ -13,6 +15,8 @@
1315
import net.sharksystem.asap.ASAPException;
1416
import net.sharksystem.asap.sharknet.InMemoSNMessage;
1517
import net.sharksystem.crypto.BasicKeyStore;
18+
import net.sharksystem.persons.android.PersonListSelectionActivity;
19+
import net.sharksystem.persons.android.PersonsStorageAndroidComponent;
1620

1721
import java.io.IOException;
1822
import java.util.HashSet;
@@ -21,6 +25,7 @@
2125
public class SNChannelAddMessageActivity extends SNChannelsActivity {
2226
private CharSequence name = null;
2327
private CharSequence uri;
28+
private Set<CharSequence> selectedRecipients = null;
2429

2530
@Override
2631
protected void onCreate(Bundle savedInstanceState) {
@@ -47,7 +52,46 @@ protected void onCreate(Bundle savedInstanceState) {
4752
topicTextView.setText(this.name);
4853
}
4954

50-
public void onAddClick(View view) {
55+
public void onSelectRecipients(View view) {
56+
PersonsStorageAndroidComponent.getPersonsStorage().setPreselectionSet(this.selectedRecipients);
57+
//Log.d(this.getLogStart(), "setPreselected: " + this.selectedRecipients);
58+
Intent intent = new Intent(this, PersonListSelectionActivity.class);
59+
this.startActivity(intent);
60+
}
61+
62+
public void onEncryptedClick(View view) {
63+
this.redrawComments();
64+
}
65+
66+
private void redrawComments() {
67+
TextView commentsTextView = findViewById(R.id.comments);
68+
commentsTextView.setText("");
69+
70+
CheckBox encryptedCheckBox = findViewById(R.id.snEncrypted);
71+
72+
if(this.selectedRecipients != null && this.selectedRecipients.size() > 1) {
73+
if (encryptedCheckBox.isChecked()) {
74+
commentsTextView.setText("each recipient gets its own encrypted message");
75+
}
76+
}
77+
78+
if(this.selectedRecipients == null || this.selectedRecipients.isEmpty()) {
79+
if(encryptedCheckBox.isChecked()) {
80+
encryptedCheckBox.setChecked(false);
81+
Toast.makeText(this,
82+
"select recipient(s) - cannot encrypt message to anybody",
83+
Toast.LENGTH_SHORT).show();
84+
}
85+
}
86+
}
87+
88+
public void onRemoveRecipients(View view) {
89+
this.selectedRecipients = null;
90+
PersonsStorageAndroidComponent.getPersonsStorage().setPreselectionSet(null);
91+
this.redrawRecipientList();
92+
}
93+
94+
public void onSendClick(View view) {
5195
// get new message
5296
EditText messageTextView = (EditText) findViewById(R.id.snMessage);
5397

@@ -57,28 +101,32 @@ public void onAddClick(View view) {
57101
Toast.makeText(this, "message is empty", Toast.LENGTH_SHORT).show();
58102
} else {
59103
try {
60-
61104
// let's sort things out.
62-
CharSequence sender =
63-
SNChannelsComponent.getSharkNetChannelComponent().getOwnerID();
64-
Set<CharSequence> recipients = new HashSet<>();
65-
recipients.add(sender); // TODO must come from GUI
66-
boolean sign = true; // TODO must come from GUI
67-
boolean encrypt = true; // TODO must come from GUI
68-
BasicKeyStore basicKeyStore =
69-
SNChannelsComponent.getSharkNetChannelComponent().getBasicKeyStore();
70-
71-
byte[] content = messageText.getBytes();
105+
CheckBox signCheckBox = findViewById(R.id.snSigned);
106+
boolean sign = signCheckBox.isChecked();
72107

73-
byte[] serializedMessage = null;
74-
serializedMessage = InMemoSNMessage.serializeMessage(
75-
content, sender, recipients, sign, encrypt, basicKeyStore);
108+
CheckBox encryptedCheckBox = findViewById(R.id.snEncrypted);
109+
boolean encrypt = encryptedCheckBox.isChecked();
76110

77-
// deliver as asap message
78-
this.sendASAPMessage(SNChannelsComponent.APP_NAME, this.uri,
79-
serializedMessage, true);
111+
byte[] content = messageText.getBytes();
80112

113+
// lets produce and send asap messages
114+
// 1) no recipients
115+
if(this.selectedRecipients == null || this.selectedRecipients.isEmpty()) {
116+
this.compileAndSendASAPMessage(content, null, sign, encrypt);
117+
} else {
118+
// 2) no encryption or just a single recipient
119+
if(!encrypt || this.selectedRecipients.size() == 1) {
120+
this.compileAndSendASAPMessage(content, sign, encrypt);
121+
} else {
122+
// 3) encrypt more than one recipient - each gets its own message
123+
for(CharSequence receiver : this.selectedRecipients) {
124+
this.compileAndSendASAPMessage(content, receiver, sign, encrypt);
125+
}
126+
}
127+
}
81128
} catch (ASAPException | IOException e) {
129+
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
82130
Log.d(this.getLogStart(), "problems when sending message in SNChannel: "
83131
+ e.getLocalizedMessage());
84132
}
@@ -88,9 +136,77 @@ public void onAddClick(View view) {
88136
this.finish();
89137
}
90138

139+
private void compileAndSendASAPMessage(byte[] content, boolean sign, boolean encrypt)
140+
throws IOException, ASAPException {
141+
CharSequence sender =
142+
SNChannelsComponent.getSharkNetChannelComponent().getOwnerID();
143+
BasicKeyStore basicKeyStore =
144+
SNChannelsComponent.getSharkNetChannelComponent().getBasicKeyStore();
145+
byte[] serializedMessage = InMemoSNMessage.serializeMessage(
146+
content, sender, this.selectedRecipients, sign, encrypt, basicKeyStore);
147+
// deliver as asap message
148+
this.sendASAPMessage(SNChannelsComponent.APP_NAME, this.uri,
149+
serializedMessage, true);
150+
}
151+
152+
private void compileAndSendASAPMessage(byte[] content, CharSequence recipient,
153+
boolean sign, boolean encrypt)
154+
throws IOException, ASAPException {
155+
CharSequence sender =
156+
SNChannelsComponent.getSharkNetChannelComponent().getOwnerID();
157+
BasicKeyStore basicKeyStore =
158+
SNChannelsComponent.getSharkNetChannelComponent().getBasicKeyStore();
159+
byte[] serializedMessage = InMemoSNMessage.serializeMessage(
160+
content, sender, recipient, sign, encrypt, basicKeyStore);
161+
// deliver as asap message
162+
this.sendASAPMessage(SNChannelsComponent.APP_NAME, this.uri,
163+
serializedMessage, true);
164+
}
165+
91166
public void onAbortClick(View view) {
92167
ASAPChannelIntent intent = new ASAPChannelIntent(this, this.name, this.uri,
93168
SNChannelViewActivity.class);
94169
startActivity(intent);
95170
}
171+
172+
private void redrawRecipientList() {
173+
String recipientsString = "anybody";
174+
175+
if(this.selectedRecipients != null && !this.selectedRecipients.isEmpty()) {
176+
StringBuilder sb = new StringBuilder();
177+
boolean first = true;
178+
for(CharSequence recipient : this.selectedRecipients) {
179+
CharSequence recipientName = recipient;
180+
try {
181+
recipientName = SNChannelsComponent.getSharkNetChannelComponent().
182+
getPersonName(recipient);
183+
} catch (ASAPException e) {
184+
// name not found
185+
}
186+
187+
if(first) {
188+
first = false;
189+
} else {
190+
sb.append(" | ");
191+
}
192+
sb.append(recipientName);
193+
}
194+
recipientsString = sb.toString();
195+
}
196+
197+
TextView tv = this.findViewById(R.id.snMessageRecipients);
198+
tv.setText(recipientsString);
199+
200+
this.redrawComments();
201+
}
202+
203+
@Override
204+
protected void onResume() {
205+
super.onResume();
206+
207+
this.selectedRecipients =
208+
PersonsStorageAndroidComponent.getPersonsStorage().getLastPersonsSelection();
209+
210+
this.redrawRecipientList();
211+
}
96212
}

app/src/main/res/layout/sn_channel_add_message.xml

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
android:orientation="vertical" android:layout_width="match_parent"
44
android:layout_height="match_parent">
55

6+
<!-- line 1 -->
67
<TextView
78
android:id="@+id/snChannelName"
89
android:layout_width="258dp"
910
android:layout_height="26dp"
1011
android:text="no ownerName"
1112
/>
1213

14+
<!-- line 2 -->
1315
<EditText
1416
android:id="@+id/snMessage"
1517
android:layout_width="258dp"
@@ -18,17 +20,109 @@
1820
android:inputType="textMultiLine"
1921
/>
2022

23+
<!-- recipients -->
2124
<LinearLayout
22-
android:layout_width="match_parent"
23-
android:layout_height="match_parent"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:orientation="horizontal">
28+
29+
<TextView
30+
android:id="@+id/recipientsText"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:text="recipients: "
34+
/>
35+
36+
<TextView
37+
android:id="@+id/snMessageRecipients"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:text="anybody"
41+
/>
42+
</LinearLayout>
43+
44+
<LinearLayout
45+
android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:orientation="horizontal">
48+
49+
<Button
50+
android:id="@+id/selectRecipients"
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:onClick="onSelectRecipients"
54+
android:text="Select"
55+
/>
56+
57+
<Button
58+
android:id="@+id/removeAllRecipients"
59+
android:layout_width="wrap_content"
60+
android:layout_height="wrap_content"
61+
android:onClick="onRemoveRecipients"
62+
android:text="to anybody"
63+
/>
64+
</LinearLayout>
65+
66+
<!-- line : encryption -->
67+
<!-- https://developer.android.com/guide/topics/ui/controls/checkbox -->
68+
<LinearLayout
69+
android:layout_width="wrap_content"
70+
android:layout_height="wrap_content"
71+
android:orientation="horizontal">
72+
73+
<TextView
74+
android:id="@+id/end2end"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:text="E2E security: "
78+
/>
79+
80+
<CheckBox
81+
android:id="@+id/snSigned"
82+
android:layout_width="wrap_content"
83+
android:layout_height="wrap_content"
84+
android:layout_weight="1"
85+
android:checked="false"
86+
android:text="sign" />
87+
88+
<CheckBox
89+
android:id="@+id/snEncrypted"
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:layout_weight="1"
93+
android:checked="false"
94+
android:onClick="onEncryptedClick"
95+
android:text="encrypt" />
96+
</LinearLayout>
97+
98+
99+
<!-- comments -->
100+
<LinearLayout
101+
android:layout_width="wrap_content"
102+
android:layout_height="wrap_content"
103+
android:orientation="horizontal">
104+
105+
<TextView
106+
android:id="@+id/comments"
107+
android:layout_width="wrap_content"
108+
android:layout_height="wrap_content"
109+
android:text=""
110+
/>
111+
112+
</LinearLayout>
113+
114+
<!-- line last -->
115+
<LinearLayout
116+
android:layout_width="wrap_content"
117+
android:layout_height="wrap_content"
24118
android:orientation="horizontal">
25119

26120
<Button
27121
android:id="@+id/addButton"
28122
android:layout_width="wrap_content"
29123
android:layout_height="wrap_content"
30-
android:onClick="onAddClick"
31-
android:text="@string/add"
124+
android:onClick="onSendClick"
125+
android:text="@string/send"
32126
/>
33127

34128
<Button

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<string name="defaultOwnerName">SNUser</string>
33

44
<string name="app_name">SN2</string>
5-
<string name="send">hello</string>
5+
<string name="send">Send</string>
66
<string name="hello">hello</string>
77

88
<string name="bubbleAddBubbleText">bubble</string>

0 commit comments

Comments
 (0)