1
1
package com .example .chatfull ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .Intent ;
5
+ import android .database .Cursor ;
4
6
import android .net .Uri ;
5
7
import android .os .Bundle ;
8
+ import android .provider .OpenableColumns ;
9
+ import android .util .Base64 ;
6
10
import android .util .Log ;
7
11
import android .view .View ;
8
12
import android .widget .Button ;
9
13
import android .widget .EditText ;
10
14
import android .widget .ImageButton ;
15
+ import android .widget .ImageView ;
11
16
import android .widget .Toast ;
12
17
13
18
import androidx .annotation .Nullable ;
14
19
import androidx .appcompat .app .AppCompatActivity ;
15
20
21
+ import com .bumptech .glide .Glide ;
22
+ import com .squareup .picasso .Picasso ;
23
+ import com .stfalcon .chatkit .commons .ImageLoader ;
16
24
import com .stfalcon .chatkit .messages .MessagesList ;
17
25
import com .stfalcon .chatkit .messages .MessagesListAdapter ;
18
26
27
+ import java .io .ByteArrayOutputStream ;
28
+ import java .io .File ;
29
+ import java .io .IOException ;
30
+ import java .io .InputStream ;
31
+ import java .io .UnsupportedEncodingException ;
19
32
import java .util .Calendar ;
20
33
21
34
public class ChatActivity extends AppCompatActivity {
@@ -50,7 +63,18 @@ protected void onCreate(Bundle savedInstanceState) {
50
63
messageReceiveServer = new MessageReceiveServer (ShowInfoActivity .getSelfIpAddress (), ShowInfoActivity .getSelfPort (),this );
51
64
52
65
this .messagesList = findViewById (R .id .messagesList );
53
- adapter = new MessagesListAdapter <>(senderId , null );
66
+
67
+ ImageLoader imageLoader = new ImageLoader () {
68
+ @ Override
69
+ public void loadImage (ImageView imageView , @ Nullable String url , @ Nullable Object payload ) {
70
+ byte [] byteArray = Base64 .decode (url ,Base64 .DEFAULT );
71
+ Glide .with (getApplicationContext ())
72
+ .asBitmap ()
73
+ .load (byteArray )
74
+ .into (imageView );
75
+ }
76
+ };
77
+ adapter = new MessagesListAdapter <>(senderId , imageLoader );
54
78
messagesList .setAdapter (adapter );
55
79
56
80
input = findViewById (R .id .et_message );
@@ -60,10 +84,14 @@ protected void onCreate(Bundle savedInstanceState) {
60
84
}
61
85
62
86
public void onBtnSendClick (View view ) {
87
+ if (input .getText ().toString () == null ) return ;
88
+
63
89
Message message = new Message (Integer .toString (++cnt ), me , input .getText ().toString (), Calendar .getInstance ().getTime ());
90
+ message .setIsImage (false );
91
+ message .setFilename (null );
64
92
adapter .addToStart (message , true );
65
93
66
- sender = new SendMessage (user .getIpAddress (), user .getPort (), input . getText (). toString () ,this );
94
+ sender = new SendMessage (user .getIpAddress (), user .getPort (), message ,this );
67
95
sender .execute ();
68
96
Log .e ("SEND" ,input .getText ().toString ());
69
97
input .setText ("" );
@@ -96,11 +124,93 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
96
124
}else if (requestCode == PICK_IMAGE_REQUEST && data !=null ) {
97
125
if (resultCode == RESULT_OK ) {
98
126
Uri file = data .getData ();
99
- Toast .makeText (this , file .getPath () + "IMAGE" , Toast .LENGTH_SHORT ).show ();
127
+ Message message = new Message (Integer .toString (++cnt ), me , null , Calendar .getInstance ().getTime ());
128
+ message .setFilename (getFileName (file ));
129
+ try {
130
+ message .setFile (getBytes (this ,file ));
131
+ } catch (IOException e ) {
132
+ e .printStackTrace ();
133
+ Log .e ("SEND_FILE" ,"COULD NOT CONVERT TO BYTE" );
134
+ }
135
+ message .setIsImage (true );
136
+
137
+ adapter .addToStart (message ,true );
138
+ sender = new SendMessage (user .getIpAddress (), user .getPort (), message ,this );
139
+ sender .execute ();
140
+ // Toast.makeText(this, file.getPath() + "IMAGE", Toast.LENGTH_SHORT).show();
141
+ }
142
+ }
143
+ }
144
+
145
+ public String getFileName (Uri uri ) {
146
+ String result = null ;
147
+ if (uri .getScheme ().equals ("content" )) {
148
+ Cursor cursor = getContentResolver ().query (uri , null , null , null , null );
149
+ try {
150
+ if (cursor != null && cursor .moveToFirst ()) {
151
+ result = cursor .getString (cursor .getColumnIndex (OpenableColumns .DISPLAY_NAME ));
152
+ }
153
+ } finally {
154
+ cursor .close ();
155
+ }
156
+ }
157
+ if (result == null ) {
158
+ result = uri .getPath ();
159
+ int cut = result .lastIndexOf ('/' );
160
+ if (cut != -1 ) {
161
+ result = result .substring (cut + 1 );
100
162
}
101
163
}
164
+ return result ;
102
165
}
103
166
167
+ /**
168
+ * get bytes array from Uri.
169
+ *
170
+ * @param context current context.
171
+ * @param uri uri fo the file to read.
172
+ * @return a bytes array.
173
+ * @throws IOException
174
+ */
175
+ public static byte [] getBytes (Context context , Uri uri ) throws IOException {
176
+ InputStream iStream = context .getContentResolver ().openInputStream (uri );
177
+ try {
178
+ return getBytes (iStream );
179
+ } finally {
180
+ // close the stream
181
+ try {
182
+ iStream .close ();
183
+ } catch (IOException ignored ) { /* do nothing */ }
184
+ }
185
+ }
186
+
187
+
188
+
189
+ /**
190
+ * get bytes from input stream.
191
+ *
192
+ * @param inputStream inputStream.
193
+ * @return byte array read from the inputStream.
194
+ * @throws IOException
195
+ */
196
+ public static byte [] getBytes (InputStream inputStream ) throws IOException {
197
+
198
+ byte [] bytesResult = null ;
199
+ ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream ();
200
+ int bufferSize = 1024 ;
201
+ byte [] buffer = new byte [bufferSize ];
202
+ try {
203
+ int len ;
204
+ while ((len = inputStream .read (buffer )) != -1 ) {
205
+ byteBuffer .write (buffer , 0 , len );
206
+ }
207
+ bytesResult = byteBuffer .toByteArray ();
208
+ } finally {
209
+ // close the stream
210
+ try { byteBuffer .close (); } catch (IOException ignored ){ /* do nothing */ }
211
+ }
212
+ return bytesResult ;
213
+ }
104
214
// @Override
105
215
// public boolean onSubmit(CharSequence input) {
106
216
// Message message = new Message(Integer.toString(++cnt), me, input.toString(), Calendar.getInstance().getTime());
@@ -116,9 +226,9 @@ public void stopSender(){
116
226
sender .cancel (true );
117
227
}
118
228
119
- public void setMessage (final String msg ){
120
- Log .e ("IN_SET" ,msg );
121
- if (msg .equalsIgnoreCase ("OFFLINE" )){
229
+ public void setMessage (final Message msg ){
230
+ Log .e ("IN_SET" ,msg . toString () );
231
+ if (msg .getText ()!= null && msg . getText (). equalsIgnoreCase ("OFFLINE" )){
122
232
if (sender != null )
123
233
sender .cancel (true );
124
234
if (messageReceiveServer != null )
@@ -131,8 +241,13 @@ public void setMessage(final String msg){
131
241
runOnUiThread (new Runnable () {
132
242
@ Override
133
243
public void run () {
134
- Message message = new Message (Integer .toString (++cnt ), user , msg , Calendar .getInstance ().getTime ());
135
- adapter .addToStart (message , true );
244
+ if (msg .getText () != null ) {
245
+ Message message = new Message (Integer .toString (++cnt ), user , msg .getText (), Calendar .getInstance ().getTime ());
246
+ adapter .addToStart (message , true );
247
+ } else if (msg .isImage ()){
248
+ msg .setUser (user );
249
+ adapter .addToStart (msg ,true );
250
+ }
136
251
}
137
252
});
138
253
}
@@ -150,7 +265,7 @@ protected void onDestroy() {
150
265
@ Override
151
266
public void onBackPressed () {
152
267
Log .e ("CHAT_ACTIVITY" , "PAUSE" );
153
- sender = new SendMessage (user .getIpAddress (), user .getPort (), "OFFLINE" ,this );
268
+ sender = new SendMessage (user .getIpAddress (), user .getPort (), new Message ( Integer . toString (++ cnt ), me , "OFFLINE" ) ,this );
154
269
sender .execute ();
155
270
try {
156
271
Thread .sleep (500 );
0 commit comments