diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index bb138c880..d0a742de8 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -82,7 +82,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 229 - versionName "1.4.12" + versionCode 230 + versionName "1.4.13" } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index d2b8b185f..515b303b6 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -21,6 +21,7 @@ import android.media.MediaRecorder; import android.media.audiofx.AutomaticGainControl; import android.net.Uri; import android.os.Environment; +import android.os.ParcelFileDescriptor; import android.os.Vibrator; import android.provider.MediaStore; import android.view.View; @@ -38,6 +39,7 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; +import java.util.Locale; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Semaphore; @@ -1424,4 +1426,81 @@ public class MediaController implements NotificationCenter.NotificationCenterDel currentGifMessageObject = null; } } + + public static boolean isGif(Uri uri) { + ParcelFileDescriptor parcelFD = null; + FileInputStream input = null; + try { + parcelFD = ApplicationLoader.applicationContext.getContentResolver().openFileDescriptor(uri, "r"); + input = new FileInputStream(parcelFD.getFileDescriptor()); + if (input.getChannel().size() > 3) { + byte[] header = new byte[3]; + input.read(header, 0, 3); + String str = new String(header); + if (str != null && str.equalsIgnoreCase("gif")) { + return true; + } + } + } catch (Exception e) { + FileLog.e("tmessages", e); + } finally { + try { + if (parcelFD != null) { + parcelFD.close(); + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + try { + if (input != null) { + input.close(); + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + } + return false; + } + + public static String copyDocumentToCache(Uri uri) { + ParcelFileDescriptor parcelFD = null; + FileInputStream input = null; + FileOutputStream output = null; + try { + int id = UserConfig.lastLocalId; + UserConfig.lastLocalId--; + parcelFD = ApplicationLoader.applicationContext.getContentResolver().openFileDescriptor(uri, "r"); + input = new FileInputStream(parcelFD.getFileDescriptor()); + File f = new File(Utilities.getCacheDir(), String.format(Locale.US, "%d.gif", id)); + output = new FileOutputStream(f); + input.getChannel().transferTo(0, input.getChannel().size(), output.getChannel()); + UserConfig.saveConfig(false); + return f.getAbsolutePath(); + } catch (Exception e) { + FileLog.e("tmessages", e); + } finally { + try { + if (parcelFD != null) { + parcelFD.close(); + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + try { + if (input != null) { + input.close(); + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + try { + if (output != null) { + output.close(); + } + } catch (Exception e2) { + FileLog.e("tmessages", e2); + } + } + return null; + } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index 51812cfe7..5b8e7c519 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -2134,8 +2134,22 @@ public class MessagesController implements NotificationCenter.NotificationCenter size2.location = size.location; } } - sentMessage.message = newMsg.message; - sentMessage.attachPath = newMsg.attachPath; + if (newMsg.attachPath != null && newMsg.attachPath.startsWith(Utilities.getCacheDir().getAbsolutePath())) { + File cacheFile = new File(newMsg.attachPath); + File cacheFile2 = new File(Utilities.getCacheDir(), MessageObject.getAttachFileName(sentMessage.media.document)); + boolean result = cacheFile.renameTo(cacheFile2); + if (result) { + newMsg.attachPath = null; + newMsg.media.document.dc_id = sentMessage.media.document.dc_id; + newMsg.media.document.id = sentMessage.media.document.id; + } else { + sentMessage.attachPath = newMsg.attachPath; + sentMessage.message = newMsg.message; + } + } else { + sentMessage.attachPath = newMsg.attachPath; + sentMessage.message = newMsg.message; + } } else if (sentMessage.media instanceof TLRPC.TL_messageMediaAudio && sentMessage.media.audio != null && newMsg.media instanceof TLRPC.TL_messageMediaAudio && newMsg.media.audio != null) { sentMessage.message = newMsg.message; sentMessage.attachPath = newMsg.attachPath; @@ -2205,6 +2219,13 @@ public class MessagesController implements NotificationCenter.NotificationCenter newMsg.media.document.path = document.path; newMsg.media.document.thumb = document.thumb; newMsg.media.document.dc_id = file.dc_id; + + if (document.path != null && document.path.startsWith(Utilities.getCacheDir().getAbsolutePath())) { + File cacheFile = new File(document.path); + File cacheFile2 = new File(Utilities.getCacheDir(), MessageObject.getAttachFileName(newMsg.media.document)); + cacheFile.renameTo(cacheFile2); + } + ArrayList arr = new ArrayList(); arr.add(newMsg); MessagesStorage.getInstance().putMessages(arr, false, true); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java index 52a14210f..0785977d7 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/NativeLoader.java @@ -34,7 +34,7 @@ public class NativeLoader { File f = null; if (context != null) { try { - f = (File) ApplicationInfo.class.getField("nativeLibraryDir").get(context.getApplicationInfo()); + f = new File((String)ApplicationInfo.class.getField("nativeLibraryDir").get(context.getApplicationInfo())); } catch (Throwable th) { th.printStackTrace(); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 5da655958..eaf125f6c 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -1556,7 +1556,18 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa return; } String tempPath = Utilities.getPath(data.getData()); + + boolean isGif = false; if (tempPath != null && tempPath.endsWith(".gif")) { + isGif = true; + } else if (tempPath == null) { + isGif = MediaController.isGif(data.getData()); + if (isGif) { + tempPath = MediaController.copyDocumentToCache(data.getData()); + } + } + + if (tempPath != null && isGif) { processSendingDocument(tempPath); } else { processSendingPhoto(null, data.getData()); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index 0bc6f2fad..1dcdf76c9 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -38,6 +38,7 @@ import org.telegram.PhoneFormat.PhoneFormat; import org.telegram.messenger.BuildVars; import org.telegram.messenger.ConnectionsManager; import org.telegram.messenger.FileLog; +import org.telegram.messenger.MediaController; import org.telegram.messenger.MessagesController; import org.telegram.messenger.NotificationCenter; import org.telegram.messenger.R; @@ -336,22 +337,24 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen if (!(parcelable instanceof Uri)) { parcelable = Uri.parse(parcelable.toString()); } - if (parcelable != null && type != null && type.startsWith("image/")) { - String tempPath = Utilities.getPath((Uri)parcelable); - if (type.equals("image/gif") || tempPath != null && tempPath.endsWith(".gif")) { - try { - documentPath = Utilities.getPath((Uri)parcelable); - } catch (Exception e) { - FileLog.e("tmessages", e); + Uri uri = (Uri)parcelable; + if (uri != null && type != null && type.startsWith("image/")) { + String tempPath = Utilities.getPath(uri); + boolean isGif = false; + if (tempPath != null && tempPath.endsWith(".gif")) { + isGif = true; + documentPath = tempPath; + } else if (tempPath == null) { + isGif = MediaController.isGif(uri); + if (isGif) { + documentPath = MediaController.copyDocumentToCache(uri); } - if (documentPath == null) { - photoPath = (Uri) parcelable; - } - } else { - photoPath = (Uri) parcelable; + } + if (!isGif || documentPath == null) { + photoPath = uri; } } else { - path = Utilities.getPath((Uri)parcelable); + path = Utilities.getPath(uri); if (path != null) { if (path.startsWith("file:")) { path = path.replace("file://", ""); @@ -380,13 +383,24 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen if (!(parcelable instanceof Uri)) { parcelable = Uri.parse(parcelable.toString()); } - String tempPath = Utilities.getPath((Uri)parcelable); - if (type.equals("image/gif") || tempPath != null && tempPath.endsWith(".gif")) { + Uri uri = (Uri)parcelable; + String tempPath = Utilities.getPath(uri); + + boolean isGif = false; + if (tempPath != null && tempPath.endsWith(".gif")) { + isGif = true; + } else if (tempPath == null) { + isGif = MediaController.isGif(uri); + if (isGif) { + tempPath = MediaController.copyDocumentToCache(uri); + } + } + if (isGif && tempPath != null) { if (documentsPathArray == null) { documentsPathArray = new ArrayList(); } try { - documentsPathArray.add(Utilities.getPath((Uri) parcelable)); + documentsPathArray.add(tempPath); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -394,7 +408,7 @@ public class LaunchActivity extends ActionBarActivity implements NotificationCen if (imagesPathArray == null) { imagesPathArray = new ArrayList(); } - imagesPathArray.add((Uri) parcelable); + imagesPathArray.add(uri); } } } else {