11package net .sharksystem .asap .sharknet .android ;
22
3+ import android .content .Intent ;
34import android .os .Bundle ;
45import android .util .Log ;
56import android .view .View ;
7+ import android .widget .CheckBox ;
68import android .widget .EditText ;
79import android .widget .TextView ;
810import android .widget .Toast ;
1315import net .sharksystem .asap .ASAPException ;
1416import net .sharksystem .asap .sharknet .InMemoSNMessage ;
1517import net .sharksystem .crypto .BasicKeyStore ;
18+ import net .sharksystem .persons .android .PersonListSelectionActivity ;
19+ import net .sharksystem .persons .android .PersonsStorageAndroidComponent ;
1620
1721import java .io .IOException ;
1822import java .util .HashSet ;
2125public 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}
0 commit comments