From e53824306292f69f2fc354faa92a6d02c78868fa Mon Sep 17 00:00:00 2001 From: DrKLO Date: Sun, 23 Mar 2014 04:09:24 +0400 Subject: [PATCH 1/3] remove main thread block before recording audio --- .../telegram/messenger/MediaController.java | 47 +++++++++++-------- .../java/org/telegram/ui/ChatActivity.java | 16 ++++++- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index a55cd5d08..3b3cb8cad 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -75,6 +75,8 @@ public class MediaController implements NotificationCenter.NotificationCenterDel public final static int audioProgressDidChanged = 50001; public final static int audioDidReset = 50002; public final static int recordProgressChanged = 50003; + public final static int recordStarted = 50004; + public final static int recordStartError = 50005; private HashMap>> loadingFileObservers = new HashMap>>(); private HashMap observersByTag = new HashMap(); @@ -782,10 +784,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel return isPaused; } - public boolean startRecording(final long dialog_id) { - final Semaphore semaphore = new Semaphore(0); - final Boolean[] result = new Boolean[1]; - + public void startRecording(final long dialog_id) { try { Vibrator v = (Vibrator) ApplicationLoader.applicationContext.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(20); @@ -797,8 +796,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel @Override public void run() { if (audioRecorder != null) { - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } @@ -813,8 +816,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel try { if (startRecord(recordingAudioFile.getAbsolutePath()) == 0) { - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } audioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, recordBufferSize * 10); @@ -870,22 +877,24 @@ public class MediaController implements NotificationCenter.NotificationCenterDel } } - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } recordQueue.postRunnable(recordRunnable); - result[0] = true; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStarted); + } + }); } - }, 120); - try { - semaphore.acquire(); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - return result[0]; + }, 100); } private void stopRecordingInternal(final boolean send) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index c9355a1ba..44fa0ddbe 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -342,6 +342,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().addObserver(this, MediaController.audioProgressDidChanged); NotificationCenter.getInstance().addObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().addObserver(this, MediaController.recordProgressChanged); + NotificationCenter.getInstance().addObserver(this, MediaController.recordStarted); + NotificationCenter.getInstance().addObserver(this, MediaController.recordStartError); NotificationCenter.getInstance().addObserver(this, 997); loading = true; @@ -388,6 +390,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().removeObserver(this, MediaController.audioProgressDidChanged); NotificationCenter.getInstance().removeObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().removeObserver(this, MediaController.recordProgressChanged); + NotificationCenter.getInstance().removeObserver(this, MediaController.recordStarted); + NotificationCenter.getInstance().removeObserver(this, MediaController.recordStartError); NotificationCenter.getInstance().removeObserver(this, 997); if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.delegate = null; @@ -677,7 +681,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { startedDraggingX = -1; - recordingAudio = MediaController.getInstance().startRecording(dialog_id); + MediaController.getInstance().startRecording(dialog_id); updateAudioRecordIntefrace(); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { startedDraggingX = -1; @@ -2229,6 +2233,16 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa mActionMode.finish(); mActionMode = null; } + } else if (id == MediaController.recordStartError) { + if (recordingAudio) { + recordingAudio = false; + updateAudioRecordIntefrace(); + } + } else if (id == MediaController.recordStarted) { + if (!recordingAudio) { + recordingAudio = true; + updateAudioRecordIntefrace(); + } } } From 6be743c9c29402fc1438bac6a587fe021b9bfe8a Mon Sep 17 00:00:00 2001 From: DrKLO Date: Sun, 23 Mar 2014 04:24:19 +0400 Subject: [PATCH 2/3] fixed previous commit --- .../telegram/messenger/MediaController.java | 9 +++++++- .../java/org/telegram/ui/ChatActivity.java | 22 +++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index 3b3cb8cad..1650af236 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -77,6 +77,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel public final static int recordProgressChanged = 50003; public final static int recordStarted = 50004; public final static int recordStartError = 50005; + public final static int recordStopped = 50006; private HashMap>> loadingFileObservers = new HashMap>>(); private HashMap observersByTag = new HashMap(); @@ -894,7 +895,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel } }); } - }, 100); + }); } private void stopRecordingInternal(final boolean send) { @@ -972,6 +973,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel } catch (Exception e) { FileLog.e("tmessages", e); } + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStopped); + } + }); } }); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 44fa0ddbe..920c4b74e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -344,6 +344,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().addObserver(this, MediaController.recordProgressChanged); NotificationCenter.getInstance().addObserver(this, MediaController.recordStarted); NotificationCenter.getInstance().addObserver(this, MediaController.recordStartError); + NotificationCenter.getInstance().addObserver(this, MediaController.recordStopped); NotificationCenter.getInstance().addObserver(this, 997); loading = true; @@ -392,6 +393,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().removeObserver(this, MediaController.recordProgressChanged); NotificationCenter.getInstance().removeObserver(this, MediaController.recordStarted); NotificationCenter.getInstance().removeObserver(this, MediaController.recordStartError); + NotificationCenter.getInstance().removeObserver(this, MediaController.recordStopped); NotificationCenter.getInstance().removeObserver(this, 997); if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.delegate = null; @@ -409,6 +411,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa if (mWakeLock != null) { try { mWakeLock.release(); + mWakeLock = null; } catch (Exception e) { FileLog.e("tmessages", e); } @@ -685,11 +688,9 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa updateAudioRecordIntefrace(); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { startedDraggingX = -1; - if (recordingAudio) { - MediaController.getInstance().stopRecording(true); - recordingAudio = false; - updateAudioRecordIntefrace(); - } + MediaController.getInstance().stopRecording(true); + recordingAudio = false; + updateAudioRecordIntefrace(); } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE && recordingAudio) { float x = motionEvent.getX(); if (x < -distCanMove) { @@ -889,9 +890,11 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa } if (recordingAudio) { try { - PowerManager pm = (PowerManager)parentActivity.getSystemService(Context.POWER_SERVICE); - mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "audio record lock"); - mWakeLock.acquire(); + if (mWakeLock == null) { + PowerManager pm = (PowerManager) parentActivity.getSystemService(Context.POWER_SERVICE); + mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "audio record lock"); + mWakeLock.acquire(); + } } catch (Exception e) { FileLog.e("tmessages", e); } @@ -940,6 +943,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa if (mWakeLock != null) { try { mWakeLock.release(); + mWakeLock = null; } catch (Exception e) { FileLog.e("tmessages", e); } @@ -2233,7 +2237,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa mActionMode.finish(); mActionMode = null; } - } else if (id == MediaController.recordStartError) { + } else if (id == MediaController.recordStartError || id == MediaController.recordStopped) { if (recordingAudio) { recordingAudio = false; updateAudioRecordIntefrace(); From 4b6fb69f309dc24714d0b0d025c047c5d6e4e2cf Mon Sep 17 00:00:00 2001 From: DrKLO Date: Sun, 23 Mar 2014 16:12:13 +0400 Subject: [PATCH 3/3] update to 1.4.7 Pebble notifications (thanks to https://github.com/DrKLO/Telegram/pull/318) Fixed native lib load on android 4.0 --- TMessagesProj/build.gradle | 4 +-- .../messenger/MessagesController.java | 25 ++++++++++++++++ .../org/telegram/messenger/NativeLoader.java | 13 +++++++-- .../ui/SettingsNotificationsActivity.java | 29 ++++++++++++++----- TMessagesProj/src/main/res/values/strings.xml | 1 + 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index f3c1a2c25..1f1941ed5 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -82,7 +82,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 207 - versionName "1.4.6" + versionCode 208 + versionName "1.4.7" } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index f03afaa9e..aa698fa20 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -34,6 +34,8 @@ import android.support.v7.app.ActionBarActivity; import android.text.Html; import android.util.SparseArray; +import org.json.JSONArray; +import org.json.JSONObject; import org.telegram.objects.MessageObject; import org.telegram.objects.PhotoObject; import org.telegram.ui.LaunchActivity; @@ -4516,6 +4518,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter notification.flags |= Notification.FLAG_SHOW_LIGHTS; try { mNotificationManager.notify(1, notification); + if (preferences.getBoolean("EnablePebbleNotifications", false)) { + sendAlertToPebble(msg); + } currentPushMessage = messageObject; } catch (Exception e) { FileLog.e("tmessages", e); @@ -4523,6 +4528,26 @@ public class MessagesController implements NotificationCenter.NotificationCenter } } + public void sendAlertToPebble(String message) { + try { + final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION"); + + final HashMap data = new HashMap(); + data.put("title", LocaleController.getString("AppName", R.string.AppName)); + data.put("body", message); + final JSONObject jsonData = new JSONObject(data); + final String notificationData = new JSONArray().put(jsonData).toString(); + + i.putExtra("messageType", "PEBBLE_ALERT"); + i.putExtra("sender", "MyAndroidApp"); + i.putExtra("notificationData", notificationData); + + ApplicationLoader.applicationContext.sendBroadcast(i); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + } + public void dialogsUnreadCountIncr(final HashMap values) { Utilities.RunOnUIThread(new Runnable() { @Override diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java index b4e57b406..2c00ee014 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java @@ -33,10 +33,12 @@ public class NativeLoader { try { String folder = null; long libSize = 0; + long libSize2 = 0; if (Build.CPU_ABI.equalsIgnoreCase("armeabi-v7a")) { folder = "armeabi-v7a"; libSize = sizes[1]; + libSize2 = sizes[0]; } else if (Build.CPU_ABI.equalsIgnoreCase("armeabi")) { folder = "armeabi"; libSize = sizes[0]; @@ -53,10 +55,14 @@ public class NativeLoader { } File destFile = new File(context.getApplicationInfo().nativeLibraryDir + "/libtmessages.so"); - if (destFile.exists() && destFile.length() == libSize) { + if (destFile.exists() && (destFile.length() == libSize || libSize2 != 0 && destFile.length() == libSize2)) { Log.d("tmessages", "Load normal lib"); - System.loadLibrary("tmessages"); - return; + try { + System.loadLibrary("tmessages"); + return; + } catch (Exception e) { + e.printStackTrace(); + } } File destLocalFile = new File(context.getFilesDir().getAbsolutePath() + "/libtmessages.so"); @@ -64,6 +70,7 @@ public class NativeLoader { if (destLocalFile.length() == libSize) { Log.d("tmessages", "Load local lib"); System.load(destLocalFile.getAbsolutePath()); + return; } else { destLocalFile.delete(); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java index 2c3fea275..b72101955 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsNotificationsActivity.java @@ -141,7 +141,7 @@ public class SettingsNotificationsActivity extends BaseFragment { } catch (Exception e) { FileLog.e("tmessages", e); } - } else if (i == 17) { + } else if (i == 19) { if (reseting) { return; } @@ -202,6 +202,13 @@ public class SettingsNotificationsActivity extends BaseFragment { editor.putBoolean("EnableContactJoined", !enabled); editor.commit(); listView.invalidateViews(); + } else if (i == 17) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + boolean enabled = preferences.getBoolean("EnablePebbleNotifications", false); + editor.putBoolean("EnablePebbleNotifications", !enabled); + editor.commit(); + listView.invalidateViews(); } } }); @@ -334,15 +341,15 @@ public class SettingsNotificationsActivity extends BaseFragment { public boolean isEnabled(int i) { SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); boolean enabledAll = preferences.getBoolean("EnableAll", true); - if (i == 17 || i == 15) { + if (i == 19 || i == 15) { return true; } - return !(i != 1 && !enabledAll && i != 13) && (i > 0 && i < 5 || i > 5 && i < 10 || i > 10 && i < 14); + return !(i != 1 && !enabledAll && i != 13) && (i > 0 && i < 5 || i > 5 && i < 10 || i > 10 && i < 14) || i == 17; } @Override public int getCount() { - return 18; + return 20; } @Override @@ -378,6 +385,8 @@ public class SettingsNotificationsActivity extends BaseFragment { } else if (i == 14) { textView.setText(LocaleController.getString("Events", R.string.Events)); } else if (i == 16) { + textView.setText(LocaleController.getString("Pebble", R.string.Pebble)); + } else if (i == 18) { textView.setText(LocaleController.getString("Reset", R.string.Reset)); } } if (type == 1) { @@ -433,6 +442,10 @@ public class SettingsNotificationsActivity extends BaseFragment { enabled = preferences.getBoolean("EnableContactJoined", true); textView.setText(LocaleController.getString("ContactJoined", R.string.ContactJoined)); divider.setVisibility(View.INVISIBLE); + } else if (i == 17) { + enabled = preferences.getBoolean("EnablePebbleNotifications", false); + textView.setText(LocaleController.getString("Alert", R.string.Alert)); + divider.setVisibility(View.INVISIBLE); } if (enabled) { checkButton.setImageResource(R.drawable.btn_check_on); @@ -480,12 +493,12 @@ public class SettingsNotificationsActivity extends BaseFragment { } textView.setText(LocaleController.getString("Sound", R.string.Sound)); divider.setVisibility(View.INVISIBLE); - } else if (i == 17) { + } else if (i == 19) { textView.setText(LocaleController.getString("ResetAllNotifications", R.string.ResetAllNotifications)); textViewDetail.setText(LocaleController.getString("UndoAllCustom", R.string.UndoAllCustom)); divider.setVisibility(View.INVISIBLE); } - if (i != 17 && !enabledAll) { + if (i != 19 && !enabledAll) { view.setEnabled(false); if(android.os.Build.VERSION.SDK_INT >= 11) { textView.setAlpha(0.3f); @@ -507,9 +520,9 @@ public class SettingsNotificationsActivity extends BaseFragment { @Override public int getItemViewType(int i) { - if (i == 0 || i == 5 || i == 10 || i == 14 || i == 16) { + if (i == 0 || i == 5 || i == 10 || i == 14 || i == 16 || i == 18) { return 0; - } else if (i > 0 && i < 4 || i > 5 && i < 9 || i > 10 && i < 14 || i == 15) { + } else if (i > 0 && i < 4 || i > 5 && i < 9 || i > 10 && i < 14 || i == 15 || i == 17) { return 1; } else { return 2; diff --git a/TMessagesProj/src/main/res/values/strings.xml b/TMessagesProj/src/main/res/values/strings.xml index 60121d32c..429138bed 100644 --- a/TMessagesProj/src/main/res/values/strings.xml +++ b/TMessagesProj/src/main/res/values/strings.xml @@ -249,6 +249,7 @@ Private Chats EVENTS Contact joined Telegram + PEBBLE No shared media yet