From 172ded5ea551325b0761643da2c5155a16379b26 Mon Sep 17 00:00:00 2001 From: DrKLO Date: Thu, 10 Jul 2014 16:14:36 +0400 Subject: [PATCH] FIxed android 2.x crashes --- .../messenger/ConnectionsManager.java | 32 ++++++++--- .../ui/PopupNotificationActivity.java | 56 +++++++++---------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java index 40b5ca17..c8944094 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java @@ -214,6 +214,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. PowerManager pm = (PowerManager)ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "lock"); + wakeLock.setReferenceCounted(false); } public int getConnectionState() { @@ -2074,9 +2075,14 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. } else { if (resultContainer.result instanceof TLRPC.updates_Difference) { pushMessagesReceived = true; - if (wakeLock.isHeld()) { - wakeLock.release(); - } + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + if (wakeLock.isHeld()) { + wakeLock.release(); + } + } + }); } request.completionBlock.run(resultContainer.result, null); } @@ -2250,15 +2256,23 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. if (paused) { pushMessagesReceived = false; } - if (!wakeLock.isHeld()) { - wakeLock.acquire(20000); - } + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + wakeLock.acquire(20000); + } + }); resumeNetworkInternal(); } else { pushMessagesReceived = true; - if (wakeLock.isHeld()) { - wakeLock.release(); - } + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + if (wakeLock.isHeld()) { + wakeLock.release(); + } + } + }); MessagesController.getInstance().processUpdates((TLRPC.Updates) message, false); } } else { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PopupNotificationActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/PopupNotificationActivity.java index 6aa0bbd9..c6f1b7dc 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/PopupNotificationActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/PopupNotificationActivity.java @@ -19,6 +19,7 @@ import android.os.Bundle; import android.os.PowerManager; import android.util.AttributeSet; import android.util.TypedValue; +import android.view.Gravity; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; @@ -50,6 +51,7 @@ import org.telegram.ui.Views.ActionBar.ActionBarLayer; import org.telegram.ui.Views.ActionBar.ActionBarMenu; import org.telegram.ui.Views.BackupImageView; import org.telegram.ui.Views.ChatActivityEnterView; +import org.telegram.ui.Views.FrameLayoutFixed; import org.telegram.ui.Views.PopupAudioView; import java.io.File; @@ -85,9 +87,8 @@ public class PopupNotificationActivity extends Activity implements NotificationC private float moveStartX = -1; private boolean startedMoving = false; private Runnable onAnimationEndRunnable = null; - private Runnable wakeLockRunnable = null; - private class FrameLayoutTouch extends FrameLayout { + private class FrameLayoutTouch extends FrameLayoutFixed { public FrameLayoutTouch(Context context) { super(context); } @@ -117,7 +118,7 @@ public class PopupNotificationActivity extends Activity implements NotificationC } } - public class FrameLayoutAnimationListener extends FrameLayout { + public class FrameLayoutAnimationListener extends FrameLayoutFixed { public FrameLayoutAnimationListener(Context context) { super(context); } @@ -215,6 +216,7 @@ public class PopupNotificationActivity extends Activity implements NotificationC PowerManager pm = (PowerManager)ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "screen"); + wakeLock.setReferenceCounted(false); handleIntent(getIntent()); } @@ -305,7 +307,7 @@ public class PopupNotificationActivity extends Activity implements NotificationC } } else if (motionEvent == null || motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { if (motionEvent != null && startedMoving) { - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) centerView.getLayoutParams(); + FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) centerView.getLayoutParams(); int diff = (int)(motionEvent.getX() - moveStartX); int width = AndroidUtilities.displaySize.x - AndroidUtilities.dp(24); int moveDiff = 0; @@ -380,24 +382,27 @@ public class PopupNotificationActivity extends Activity implements NotificationC } private void applyViewsLayoutParams(int xOffset) { - ViewGroup.MarginLayoutParams layoutParams = null; + FrameLayout.LayoutParams layoutParams = null; int widht = AndroidUtilities.displaySize.x - AndroidUtilities.dp(24); if (leftView != null) { - layoutParams = (ViewGroup.MarginLayoutParams) leftView.getLayoutParams(); + layoutParams = (FrameLayout.LayoutParams) leftView.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; layoutParams.leftMargin = -widht + xOffset; leftView.setLayoutParams(layoutParams); } if (centerView != null) { - layoutParams = (ViewGroup.MarginLayoutParams) centerView.getLayoutParams(); + layoutParams = (FrameLayout.LayoutParams) centerView.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; layoutParams.leftMargin = xOffset; centerView.setLayoutParams(layoutParams); } if (rightView != null) { - layoutParams = (ViewGroup.MarginLayoutParams) rightView.getLayoutParams(); + layoutParams = (FrameLayout.LayoutParams) rightView.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; layoutParams.leftMargin = widht + xOffset; @@ -523,7 +528,8 @@ public class PopupNotificationActivity extends Activity implements NotificationC if (applyOffset) { int widht = AndroidUtilities.displaySize.x - AndroidUtilities.dp(24); - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); + FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; if (num == currentMessageNum) { @@ -581,11 +587,12 @@ public class PopupNotificationActivity extends Activity implements NotificationC rightView = getViewForMessage(currentMessageNum + 1, true); } else if (move == 3) { if (rightView != null) { - int offset = ((ViewGroup.MarginLayoutParams) rightView.getLayoutParams()).leftMargin; + int offset = ((FrameLayout.LayoutParams) rightView.getLayoutParams()).leftMargin; reuseView(rightView); rightView = getViewForMessage(currentMessageNum + 1, false); int widht = AndroidUtilities.displaySize.x - AndroidUtilities.dp(24); - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) rightView.getLayoutParams(); + FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) rightView.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; layoutParams.leftMargin = offset; @@ -594,11 +601,12 @@ public class PopupNotificationActivity extends Activity implements NotificationC } } else if (move == 4) { if (leftView != null) { - int offset = ((ViewGroup.MarginLayoutParams) leftView.getLayoutParams()).leftMargin; + int offset = ((FrameLayout.LayoutParams) leftView.getLayoutParams()).leftMargin; reuseView(leftView); leftView = getViewForMessage(0, false); int widht = AndroidUtilities.displaySize.x - AndroidUtilities.dp(24); - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) leftView.getLayoutParams(); + FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) leftView.getLayoutParams(); + layoutParams.gravity = Gravity.TOP | Gravity.LEFT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = widht; layoutParams.leftMargin = offset; @@ -651,21 +659,6 @@ public class PopupNotificationActivity extends Activity implements NotificationC currentMessageNum = 0; } getNewMessage(); - wakeLock.acquire(); - Utilities.stageQueue.postRunnable(wakeLockRunnable = new Runnable() { - @Override - public void run() { - Utilities.RunOnUIThread(new Runnable() { - @Override - public void run() { - wakeLockRunnable = null; - if (wakeLock.isHeld()) { - wakeLock.release(); - } - } - }); - } - }, 7000); } private void getNewMessage() { @@ -868,6 +861,7 @@ public class PopupNotificationActivity extends Activity implements NotificationC } ConnectionsManager.getInstance().setAppPaused(false, false); fixLayout(); + wakeLock.acquire(7000); } @Override @@ -879,6 +873,9 @@ public class PopupNotificationActivity extends Activity implements NotificationC chatActivityEnterView.setFieldFocused(false); } ConnectionsManager.getInstance().setAppPaused(true, false); + if (wakeLock.isHeld()) { + wakeLock.release(); + } } @Override @@ -972,8 +969,5 @@ public class PopupNotificationActivity extends Activity implements NotificationC if (wakeLock.isHeld()) { wakeLock.release(); } - if (wakeLockRunnable != null) { - Utilities.stageQueue.cancelRunnable(wakeLockRunnable); - } } }