Be more conservative with handlers and references

Expiring message timers could end up leaking references and
executing work even after their conversation item was no longer
visible

Maybe fixes #6898

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2017-09-09 23:46:48 -07:00
parent 6a10c69df8
commit f3d943270c
9 changed files with 67 additions and 77 deletions

View File

@@ -1,16 +1,17 @@
package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import org.thoughtcrime.securesms.util.Util;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
public class ExpirationTimerView extends HourglassView {
private final Handler handler = new Handler();
private long startedAt;
private long expiresIn;
@@ -39,26 +40,11 @@ public class ExpirationTimerView extends HourglassView {
public void startAnimation() {
synchronized (this) {
visible = true;
if (stopped == false) return;
else stopped = false;
if (!stopped) return;
else stopped = false;
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
setPercentage(calculateProgress(startedAt, expiresIn));
synchronized (ExpirationTimerView.this) {
if (!visible) {
stopped = true;
return;
}
}
handler.postDelayed(this, calculateAnimationDelay(startedAt, expiresIn));
}
}, calculateAnimationDelay(this.startedAt, this.expiresIn));
Util.runOnMainDelayed(new AnimationUpdateRunnable(this), calculateAnimationDelay(this.startedAt, this.expiresIn));
}
public void stopAnimation() {
@@ -85,4 +71,30 @@ public class ExpirationTimerView extends HourglassView {
}
}
private static class AnimationUpdateRunnable implements Runnable {
private final WeakReference<ExpirationTimerView> expirationTimerViewReference;
private AnimationUpdateRunnable(@NonNull ExpirationTimerView expirationTimerView) {
this.expirationTimerViewReference = new WeakReference<>(expirationTimerView);
}
@Override
public void run() {
ExpirationTimerView timerView = expirationTimerViewReference.get();
if (timerView == null) return;
timerView.setExpirationTime(timerView.startedAt, timerView.expiresIn);
synchronized (timerView) {
if (!timerView.visible) {
timerView.stopped = true;
return;
}
}
Util.runOnMainDelayed(this, timerView.calculateAnimationDelay(timerView.startedAt, timerView.expiresIn));
}
}
}

View File

@@ -4,7 +4,6 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
@@ -25,11 +24,11 @@ import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.emoji.EmojiDrawer;
import org.thoughtcrime.securesms.components.emoji.EmojiToggle;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener;
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
import org.thoughtcrime.securesms.util.views.Stub;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@@ -288,9 +287,7 @@ public class InputPanel extends LinearLayout
private static class RecordTime implements Runnable {
private final TextView recordTimeView;
private final AtomicLong startTime = new AtomicLong(0);
private final Handler handler = new Handler();
private RecordTime(TextView recordTimeView) {
this.recordTimeView = recordTimeView;
@@ -300,7 +297,7 @@ public class InputPanel extends LinearLayout
this.startTime.set(System.currentTimeMillis());
this.recordTimeView.setText(DateUtils.formatElapsedTime(0));
ViewUtil.fadeIn(this.recordTimeView, FADE_TIME);
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1));
Util.runOnMainDelayed(this, TimeUnit.SECONDS.toMillis(1));
}
public long hide() {
@@ -316,7 +313,7 @@ public class InputPanel extends LinearLayout
if (localStartTime > 0) {
long elapsedTime = System.currentTimeMillis() - localStartTime;
recordTimeView.setText(DateUtils.formatElapsedTime(TimeUnit.MILLISECONDS.toSeconds(elapsedTime)));
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1));
Util.runOnMainDelayed(this, TimeUnit.SECONDS.toMillis(1));
}
}
}

View File

@@ -1,8 +1,6 @@
package org.thoughtcrime.securesms.components.webrtc;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@@ -11,8 +9,9 @@ import android.view.animation.Animation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.thoughtcrime.securesms.components.multiwaveview.MultiWaveView;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.multiwaveview.MultiWaveView;
import org.thoughtcrime.securesms.util.Util;
/**
* Displays the controls at the bottom of the in-call screen.
@@ -26,16 +25,6 @@ public class WebRtcIncomingCallOverlay extends RelativeLayout {
private MultiWaveView incomingCallWidget;
private TextView redphoneLabel;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
if (incomingCallWidget.getVisibility() == View.VISIBLE) {
incomingCallWidget.ping();
handler.sendEmptyMessageDelayed(0, 1200);
}
}
};
public WebRtcIncomingCallOverlay(Context context) {
super(context);
initialize();
@@ -63,7 +52,15 @@ public class WebRtcIncomingCallOverlay extends RelativeLayout {
incomingCallWidget.setVisibility(View.VISIBLE);
redphoneLabel.setVisibility(View.VISIBLE);
handler.sendEmptyMessageDelayed(0, 500);
Util.runOnMainDelayed(new Runnable() {
@Override
public void run() {
if (incomingCallWidget.getVisibility() == View.VISIBLE) {
incomingCallWidget.ping();
Util.runOnMainDelayed(this, 1200);
}
}
}, 500);
}
public void setActiveCall() {