Skip to content

Commit d1abd6e

Browse files
committed
fix for a crash, #137
1 parent 27adfd5 commit d1abd6e

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ private ListenableWorker.Result uploadMessageAttachment(final Context context, f
553553
break;
554554
case ARG_OPERATION_AUDIO:
555555
content = draftyAudio(uploadDetails.mimeType, args.getByteArray(ARG_AUDIO_PREVIEW),
556-
null, ref, uploadDetails.duration, fname, uploadDetails.fileSize);
556+
null, url, uploadDetails.duration, fname, uploadDetails.fileSize);
557557
break;
558558
}
559559
}

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.net.Uri;
2222
import android.os.Build;
2323
import android.os.Bundle;
24-
import android.os.SystemClock;
2524
import android.text.Spannable;
2625
import android.text.SpannableString;
2726
import android.text.Spanned;
@@ -1004,25 +1003,25 @@ static class ViewHolder extends RecyclerView.ViewHolder {
10041003
mGestureDetector = new GestureDetector(itemView.getContext(),
10051004
new GestureDetector.SimpleOnGestureListener() {
10061005
@Override
1007-
public void onLongPress(MotionEvent ev) {
1006+
public void onLongPress(@NonNull MotionEvent ev) {
10081007
itemView.performLongClick();
10091008
}
10101009

10111010
@Override
1012-
public boolean onSingleTapConfirmed(MotionEvent ev) {
1011+
public boolean onSingleTapConfirmed(@NonNull MotionEvent ev) {
10131012
itemView.performClick();
10141013
return super.onSingleTapConfirmed(ev);
10151014
}
10161015

10171016
@Override
1018-
public void onShowPress(MotionEvent ev) {
1017+
public void onShowPress(@NonNull MotionEvent ev) {
10191018
if (mRippleOverlay != null) {
10201019
mRippleOverlay.setPressed(true);
10211020
mRippleOverlay.postDelayed(() -> mRippleOverlay.setPressed(false), 250);
10221021
}
10231022
}
10241023
@Override
1025-
public boolean onDown(MotionEvent ev) {
1024+
public boolean onDown(@NonNull MotionEvent ev) {
10261025
// Convert click coordinates in itemView to TexView.
10271026
int[] item = new int[2];
10281027
int[] text = new int[2];
@@ -1151,13 +1150,15 @@ private boolean clickAudio(Map<String, Object> data, Object params) {
11511150
try {
11521151
FullFormatter.AudioClickAction aca = (FullFormatter.AudioClickAction) params;
11531152
if (aca.action == FullFormatter.AudioClickAction.Action.PLAY) {
1154-
mMediaControl.ensurePlayerReady(mSeqId, data, aca.control);
1155-
mMediaControl.playWhenReady();
1153+
if (mMediaControl.ensurePlayerReady(mSeqId, data, aca.control)) {
1154+
mMediaControl.playWhenReady();
1155+
}
11561156
} else if (aca.action == FullFormatter.AudioClickAction.Action.PAUSE) {
11571157
mMediaControl.pause();
11581158
} else if (aca.seekTo != null) {
1159-
mMediaControl.ensurePlayerReady(mSeqId, data, aca.control);
1160-
mMediaControl.seekToWhenReady(aca.seekTo);
1159+
if (mMediaControl.ensurePlayerReady(mSeqId, data, aca.control)) {
1160+
mMediaControl.seekToWhenReady(aca.seekTo);
1161+
}
11611162
}
11621163
} catch (IOException | ClassCastException ignored) {
11631164
Toast.makeText(mActivity, R.string.unable_to_play_audio, Toast.LENGTH_SHORT).show();
@@ -1305,11 +1306,11 @@ private class MediaControl {
13051306
// Playback fraction to seek to when the player is ready.
13061307
private float mSeekTo = -1f;
13071308

1308-
synchronized void ensurePlayerReady(int seq, Map<String, Object> data,
1309+
synchronized boolean ensurePlayerReady(int seq, Map<String, Object> data,
13091310
FullFormatter.AudioControlCallback control) throws IOException {
13101311
if (mAudioPlayer != null && mPlayingAudioSeq == seq) {
13111312
mAudioControlCallback = control;
1312-
return;
1313+
return true;
13131314
}
13141315

13151316
if (mPlayingAudioSeq > 0 && mAudioControlCallback != null) {
@@ -1383,15 +1384,24 @@ synchronized void ensurePlayerReady(int seq, Map<String, Object> data,
13831384
.build();
13841385
mAudioPlayer.setDataSource(mActivity, uri);
13851386
}
1387+
} else {
1388+
mAudioControlCallback.reset();
1389+
Log.w(TAG, "Invalid ref URL " + val);
1390+
Toast.makeText(mActivity, R.string.unable_to_play_audio, Toast.LENGTH_SHORT).show();
1391+
return false;
13861392
}
13871393
} else if ((val = data.get("val")) instanceof String) {
13881394
byte[] source = Base64.decode((String) val, Base64.DEFAULT);
13891395
mAudioPlayer.setDataSource(new MemoryAudioSource(source));
13901396
} else {
1397+
mAudioControlCallback.reset();
13911398
Log.w(TAG, "Unable to play audio: missing data");
1399+
Toast.makeText(mActivity, R.string.unable_to_play_audio, Toast.LENGTH_SHORT).show();
1400+
return false;
13921401
}
13931402

13941403
mAudioPlayer.prepareAsync();
1404+
return true;
13951405
}
13961406

13971407
synchronized void releasePlayer(int seq) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.view.MotionEvent;
3737
import android.view.View;
3838
import android.view.ViewGroup;
39+
import android.view.WindowManager;
3940
import android.widget.EditText;
4041
import android.widget.ImageView;
4142
import android.widget.TextView;
@@ -626,7 +627,7 @@ public boolean onZoneReached(int id) {
626627
}
627628
});
628629
GestureDetector gd = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
629-
public void onLongPress(MotionEvent e) {
630+
public void onLongPress(@NonNull MotionEvent e) {
630631
if (!UiUtils.isPermissionGranted(activity, Manifest.permission.RECORD_AUDIO)) {
631632
mAudioRecorderPermissionLauncher.launch(new String[] {
632633
Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS
@@ -936,12 +937,14 @@ private void initAudioRecorder(Activity activity) {
936937
mGainControl = AutomaticGainControl.create(MediaRecorder.AudioSource.MIC);
937938
}
938939

940+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
939941
try {
940942
mAudioRecord = File.createTempFile("audio", ".m4a", activity.getCacheDir());
941943
mAudioRecorder.setOutputFile(mAudioRecord.getAbsolutePath());
942944
mAudioRecorder.prepare();
943945
mAudioSampler = new AudioSampler();
944946
} catch (IOException ex) {
947+
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
945948
Log.w(TAG, "Failed to initialize audio recording", ex);
946949
Toast.makeText(activity, R.string.audio_recording_failed, Toast.LENGTH_SHORT).show();
947950
mAudioRecorder.release();
@@ -995,6 +998,8 @@ private void releaseAudio(boolean keepRecord) {
995998
return;
996999
}
9971000

1001+
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1002+
9981003
if (!keepRecord && mAudioRecord != null) {
9991004
mAudioRecord.delete();
10001005
mAudioRecord = null;

0 commit comments

Comments
 (0)