Skip to content

Commit c483629

Browse files
committed
data race on login, enable stickers, bix sharing to tinode
1 parent b2382f3 commit c483629

File tree

6 files changed

+70
-21
lines changed

6 files changed

+70
-21
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@
141141

142142
<activity
143143
android:name=".SendToActivity"
144-
android:label="@string/send_to"
145144
android:theme="@style/AppTheme"
146145
android:exported="true">
147146
<intent-filter>

app/src/main/java/co/tinode/tindroid/AttachmentHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import androidx.work.ListenableWorker;
4848
import androidx.work.NetworkType;
4949
import androidx.work.OneTimeWorkRequest;
50+
import androidx.work.Operation;
5051
import androidx.work.WorkManager;
5152
import androidx.work.Worker;
5253
import androidx.work.WorkerParameters;
@@ -172,7 +173,7 @@ static UploadDetails getFileDetails(@NonNull final Context context, @NonNull Uri
172173
return result;
173174
}
174175

175-
static void enqueueMsgAttachmentUploadRequest(AppCompatActivity activity, String operation, Bundle args) {
176+
static Operation enqueueMsgAttachmentUploadRequest(AppCompatActivity activity, String operation, Bundle args) {
176177
String topicName = args.getString(AttachmentHandler.ARG_TOPIC_NAME);
177178
// Create a new message which will be updated with upload progress.
178179
Drafty content = new Drafty();
@@ -210,8 +211,11 @@ static void enqueueMsgAttachmentUploadRequest(AppCompatActivity activity, String
210211
.addTag(TAG_UPLOAD_WORK)
211212
.build();
212213

213-
WorkManager.getInstance(activity).enqueueUniqueWork(Long.toString(msg.getDbId()), ExistingWorkPolicy.REPLACE, upload);
214+
return WorkManager.getInstance(activity).enqueueUniqueWork(Long.toString(msg.getDbId()),
215+
ExistingWorkPolicy.REPLACE, upload);
214216
}
217+
218+
return null;
215219
}
216220

217221
@SuppressWarnings("UnusedReturnValue")

app/src/main/java/co/tinode/tindroid/FindFragment.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class FindFragment extends Fragment implements UiUtils.ProgressIndicator
6161
private FndTopic<VxCard> mFndTopic;
6262
private FndListener mFndListener;
6363

64+
private LoginEventListener mLoginListener;
65+
6466
private String mSearchTerm; // Stores the current search query term
6567
private FindAdapter mAdapter = null;
6668

@@ -135,6 +137,20 @@ public void onResume() {
135137
return;
136138
}
137139

140+
final Tinode tinode = Cache.getTinode();
141+
mLoginListener = new LoginEventListener(tinode.isConnected());
142+
tinode.addListener(mLoginListener);
143+
144+
if (!tinode.isAuthenticated()) {
145+
// If connection is not ready, wait for completion. This method will be called again
146+
// from the onLogin callback;
147+
return;
148+
}
149+
150+
topicAttach();
151+
}
152+
153+
private void topicAttach() {
138154
Cache.attachFndTopic(mFndListener)
139155
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
140156
@Override
@@ -172,6 +188,8 @@ public void onPause() {
172188
if (mFndTopic != null) {
173189
mFndTopic.setListener(null);
174190
}
191+
192+
Cache.getTinode().removeListener(mLoginListener);
175193
}
176194

177195
@Override
@@ -440,4 +458,16 @@ public void onClick(String topicName) {
440458
activity.finish();
441459
}
442460
}
461+
462+
private class LoginEventListener extends UiUtils.EventListener {
463+
LoginEventListener(boolean online) {
464+
super(getActivity(), online);
465+
}
466+
467+
@Override
468+
public void onLogin(int code, String txt) {
469+
super.onLogin(code, txt);
470+
topicAttach();
471+
}
472+
}
443473
}

app/src/main/java/co/tinode/tindroid/MessageActivity.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ public void onResume() {
225225
return;
226226
}
227227

228+
// Resume message sender.
229+
mMessageSender.resume();
230+
228231
CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
229232
//noinspection ConstantConditions
230233
mMessageText = TextUtils.isEmpty(text) ? null : text.toString();
@@ -243,6 +246,7 @@ public void onResume() {
243246
showFragment(FRAGMENT_FILE_PREVIEW, args, true);
244247
}
245248
}
249+
intent.setData(null);
246250

247251
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
248252

@@ -385,8 +389,9 @@ private String readTopicNameFromIntent(Intent intent) {
385389
public void onPause() {
386390
super.onPause();
387391

388-
Cache.getTinode().removeListener(mTinodeListener);
392+
mMessageSender.pause();
389393

394+
Cache.getTinode().removeListener(mTinodeListener);
390395
topicDetach();
391396

392397
// Stop handling read messages
@@ -410,16 +415,13 @@ private Fragment maybeShowMessagesFragmentOnAttach() {
410415
}
411416

412417
private void topicAttach(boolean interactive) {
413-
setRefreshing(true);
414-
415-
Tinode tinode = Cache.getTinode();
416-
if (!tinode.isAuthenticated()) {
418+
if (!Cache.getTinode().isAuthenticated()) {
417419
// If connection is not ready, wait for completion. This method will be called again
418420
// from the onLogin callback;
419-
Cache.getTinode().reconnectNow(interactive, false, false);
420421
return;
421422
}
422423

424+
setRefreshing(true);
423425
Topic.MetaGetBuilder builder = mTopic.getMetaGetBuilder()
424426
.withDesc()
425427
.withSub()
@@ -453,9 +455,8 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
453455
mTopicName, mTopic.getOnline(), mTopic.getLastSeen(), mTopic.isDeleted());
454456
}
455457
});
456-
// Resume message sender and submit pending messages for processing:
457-
// publish queued, delete marked for deletion.
458-
mMessageSender.resume();
458+
// Submit pending messages for processing: publish queued,
459+
// delete marked for deletion.
459460
syncAllMessages(true);
460461
return null;
461462
}
@@ -485,7 +486,6 @@ public void onFinally() {
485486

486487
// Clean up everything related to the topic being replaced of removed.
487488
private void topicDetach() {
488-
mMessageSender.pause();
489489
if (mTypingAnimationTimer != null) {
490490
mTypingAnimationTimer.cancel();
491491
mTypingAnimationTimer = null;

app/src/main/java/co/tinode/tindroid/MessagesFragment.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import androidx.appcompat.app.AlertDialog;
6262
import androidx.appcompat.app.AppCompatActivity;
6363
import androidx.appcompat.widget.AppCompatImageButton;
64+
import androidx.appcompat.widget.AppCompatImageView;
65+
import androidx.core.content.ContextCompat;
6466
import androidx.core.content.FileProvider;
6567
import androidx.core.view.ContentInfoCompat;
6668
import androidx.core.view.OnReceiveContentListener;
@@ -70,6 +72,7 @@
7072
import androidx.recyclerview.widget.RecyclerView;
7173
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
7274
import androidx.work.Data;
75+
import androidx.work.Operation;
7376
import androidx.work.WorkInfo;
7477
import androidx.work.WorkManager;
7578

@@ -947,7 +950,7 @@ private void initAudioRecorder(Activity activity) {
947950
mAudioRecorder.prepare();
948951
mAudioSampler = new AudioSampler();
949952
} catch (IOException ex) {
950-
Log.i(TAG, "Failed to initialize audio recording", ex);
953+
Log.w(TAG, "Failed to initialize audio recording", ex);
951954
Toast.makeText(activity, R.string.audio_recording_failed, Toast.LENGTH_SHORT).show();
952955
mAudioRecorder.release();
953956
mAudioRecorder = null;
@@ -1376,18 +1379,34 @@ private int findItemPositionById(long id) {
13761379
return mMessagesAdapter.findItemPositionById(id, first, last);
13771380
}
13781381

1379-
private static class StickerReceiver implements OnReceiveContentListener {
1382+
private class StickerReceiver implements OnReceiveContentListener {
13801383
@Nullable
13811384
@Override
13821385
public ContentInfoCompat onReceiveContent(@NonNull View view, @NonNull ContentInfoCompat payload) {
13831386
Pair<ContentInfoCompat, ContentInfoCompat> split = payload.partition(item -> item.getUri() != null);
13841387

1385-
if (split.first != null) {
1388+
final MessageActivity activity = (MessageActivity) getActivity();
1389+
if (split.first != null && activity != null) {
13861390
// Handle posted URIs.
13871391
ClipData data = split.first.getClip();
13881392
if (data.getItemCount() > 0) {
1389-
Uri uri = data.getItemAt(0).getUri();
1390-
Log.i(TAG, "Uri from IME: " + uri.toString() + "; total=" + data.getItemCount());
1393+
Uri stickerUri = data.getItemAt(0).getUri();
1394+
Bundle args = new Bundle();
1395+
args.putParcelable(AttachmentHandler.ARG_LOCAL_URI, stickerUri);
1396+
args.putString(AttachmentHandler.ARG_OPERATION, AttachmentHandler.ARG_OPERATION_IMAGE);
1397+
args.putString(AttachmentHandler.ARG_TOPIC_NAME, mTopicName);
1398+
1399+
Operation op = AttachmentHandler.enqueueMsgAttachmentUploadRequest(activity,
1400+
AttachmentHandler.ARG_OPERATION_IMAGE, args);
1401+
if (op != null) {
1402+
op.getResult().addListener((Runnable) () -> {
1403+
if (activity.isFinishing() || activity.isDestroyed()) {
1404+
return;
1405+
}
1406+
activity.syncAllMessages(true);
1407+
notifyDataSetChanged(false);
1408+
}, ContextCompat.getMainExecutor(activity));
1409+
}
13911410
}
13921411
}
13931412

app/src/main/java/co/tinode/tindroid/TindroidApp.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,11 @@ public void onReceive(Context context, Intent intent) {
205205
Cache.getTinode().addListener(new Tinode.EventListener() {
206206
@Override
207207
public void onDataMessage(MsgServerData data) {
208-
Log.i(TAG, "Data message for " + data.topic);
209208
if (Cache.getTinode().isMe(data.from)) {
210209
return;
211210
}
212211
String webrtc = data.getStringHeader("webrtc");
213-
Log.i(TAG, "Data.head.webrtc=" + webrtc);
214212
if (MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) {
215-
Log.i(TAG, "Data.head.webrtc NOT started=" + MsgServerData.parseWebRTC(webrtc));
216213
return;
217214
}
218215
ComTopic topic = (ComTopic) Cache.getTinode().getTopic(data.topic);

0 commit comments

Comments
 (0)