From 8eea00b7b4925fc6f41bb2fa88b64a6f4c50930d Mon Sep 17 00:00:00 2001 From: DrKLO Date: Mon, 1 Sep 2014 19:43:03 +0400 Subject: [PATCH] Added other upload queue for small files --- TMessagesProj/build.gradle | 2 +- .../org/telegram/android/MediaController.java | 17 ++- .../telegram/android/MessagesController.java | 2 +- .../telegram/android/SendMessagesHelper.java | 20 ++-- .../org/telegram/messenger/FileLoader.java | 109 +++++++++++++----- .../org/telegram/ui/Views/AvatarUpdater.java | 2 +- 6 files changed, 107 insertions(+), 45 deletions(-) diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index 78ffb1925..7e1713307 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -80,7 +80,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 319 + versionCode 320 versionName "1.8.0" } } diff --git a/TMessagesProj/src/main/java/org/telegram/android/MediaController.java b/TMessagesProj/src/main/java/org/telegram/android/MediaController.java index c13c8e91e..c617e56a5 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/android/MediaController.java @@ -583,9 +583,10 @@ public class MediaController implements NotificationCenter.NotificationCenterDel queue = documentDownloadQueue; } queue.addAll(objects); - for (DownloadObject downloadObject : queue) { - String path = FileLoader.getAttachFileName(downloadObject.object); - downloadQueueKeys.put(path, downloadObject); + for (int a = 0; a < queue.size(); a++) { + DownloadObject downloadObject = queue.get(a); + + boolean added = true; if (downloadObject.object instanceof TLRPC.Audio) { FileLoader.getInstance().loadFile((TLRPC.Audio)downloadObject.object, false); } else if (downloadObject.object instanceof TLRPC.PhotoSize) { @@ -594,6 +595,15 @@ public class MediaController implements NotificationCenter.NotificationCenterDel FileLoader.getInstance().loadFile((TLRPC.Video)downloadObject.object); } else if (downloadObject.object instanceof TLRPC.Document) { FileLoader.getInstance().loadFile((TLRPC.Document)downloadObject.object); + } else { + added = false; + queue.remove(a); + a--; + } + if (added) { + String path = FileLoader.getAttachFileName(downloadObject.object); + downloadQueueKeys.put(path, downloadObject); + FileLog.e("tmessages", "download file " + path); } } } @@ -617,6 +627,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel private void checkDownloadFinished(String fileName, boolean canceled) { DownloadObject downloadObject = downloadQueueKeys.get(fileName); if (downloadObject != null) { + FileLog.e("tmessages", "check download finished " + fileName + " canceled = " + canceled); downloadQueueKeys.remove(fileName); if (!canceled) { MessagesStorage.getInstance().removeFromDownloadQueue(downloadObject.id, downloadObject.type); diff --git a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java index a5efd8426..457e10ec3 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java @@ -909,7 +909,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter public void uploadAndApplyUserAvatar(TLRPC.PhotoSize bigPhoto) { if (bigPhoto != null) { uploadingAvatar = AndroidUtilities.getCacheDir() + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg"; - FileLoader.getInstance().uploadFile(uploadingAvatar, false); + FileLoader.getInstance().uploadFile(uploadingAvatar, false, true); } } diff --git a/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java b/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java index ed4f128f7..68518aa41 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java +++ b/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java @@ -766,9 +766,9 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter String location = AndroidUtilities.getCacheDir() + "/" + message.location.volume_id + "_" + message.location.local_id + ".jpg"; putToDelayedMessages(location, message); if (message.sendRequest != null) { - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, true); } else { - FileLoader.getInstance().uploadFile(location, true); + FileLoader.getInstance().uploadFile(location, true, true); } } else if (message.type == 1) { if (message.sendRequest != null) { @@ -781,14 +781,14 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter if (media.thumb == null) { String location = AndroidUtilities.getCacheDir() + "/" + message.location.volume_id + "_" + message.location.local_id + ".jpg"; putToDelayedMessages(location, message); - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, true); } else { String location = message.videoLocation.path; if (location == null) { location = AndroidUtilities.getCacheDir() + "/" + message.videoLocation.id + ".mp4"; } putToDelayedMessages(location, message); - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, false); } } else { String location = message.videoLocation.path; @@ -796,7 +796,7 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter location = AndroidUtilities.getCacheDir() + "/" + message.videoLocation.id + ".mp4"; } putToDelayedMessages(location, message); - FileLoader.getInstance().uploadFile(location, true); + FileLoader.getInstance().uploadFile(location, true, false); } } else if (message.type == 2) { TLRPC.InputMedia media = null; @@ -808,23 +808,23 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter if (message.sendRequest != null && media.thumb == null && message.location != null) { String location = AndroidUtilities.getCacheDir() + "/" + message.location.volume_id + "_" + message.location.local_id + ".jpg"; putToDelayedMessages(location, message); - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, true); } else { String location = message.documentLocation.path; putToDelayedMessages(location, message); if (message.sendRequest != null) { - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, false); } else { - FileLoader.getInstance().uploadFile(location, true); + FileLoader.getInstance().uploadFile(location, true, false); } } } else if (message.type == 3) { String location = message.audioLocation.path; putToDelayedMessages(location, message); if (message.sendRequest != null) { - FileLoader.getInstance().uploadFile(location, false); + FileLoader.getInstance().uploadFile(location, false, true); } else { - FileLoader.getInstance().uploadFile(location, true); + FileLoader.getInstance().uploadFile(location, true, true); } } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/FileLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/FileLoader.java index 91b04d427..256666ef5 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/FileLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/FileLoader.java @@ -29,6 +29,7 @@ public class FileLoader { private volatile DispatchQueue fileLoaderQueue = new DispatchQueue("fileUploadQueue"); private LinkedList uploadOperationQueue = new LinkedList(); + private LinkedList uploadSmallOperationQueue = new LinkedList(); private LinkedList loadOperationQueue = new LinkedList(); private LinkedList audioLoadOperationQueue = new LinkedList(); private LinkedList photoLoadOperationQueue = new LinkedList(); @@ -43,6 +44,7 @@ public class FileLoader { private int currentAudioLoadOperationsCount = 0; private int currentPhotoLoadOperationsCount = 0; private int currentUploadOperationsCount = 0; + private int currentUploadSmallOperationsCount = 0; private static volatile FileLoader Instance = null; public static FileLoader getInstance() { @@ -62,18 +64,16 @@ public class FileLoader { fileLoaderQueue.postRunnable(new Runnable() { @Override public void run() { + FileUploadOperation operation = null; if (!enc) { - FileUploadOperation operation = uploadOperationPaths.get(location); - if (operation != null) { - uploadOperationQueue.remove(operation); - operation.cancel(); - } + operation = uploadOperationPaths.get(location); } else { - FileUploadOperation operation = uploadOperationPathsEnc.get(location); - if (operation != null) { - uploadOperationQueue.remove(operation); - operation.cancel(); - } + operation = uploadOperationPathsEnc.get(location); + } + if (operation != null) { + uploadOperationQueue.remove(operation); + uploadSmallOperationQueue.remove(operation); + operation.cancel(); } } }); @@ -83,7 +83,7 @@ public class FileLoader { return fileProgresses.get(location); } - public void uploadFile(final String location, final boolean encrypted) { + public void uploadFile(final String location, final boolean encrypted, final boolean small) { fileLoaderQueue.postRunnable(new Runnable() { @Override public void run() { @@ -113,12 +113,23 @@ public class FileLoader { } else { uploadOperationPaths.remove(location); } - currentUploadOperationsCount--; - if (currentUploadOperationsCount < 2) { - FileUploadOperation operation = uploadOperationQueue.poll(); - if (operation != null) { - currentUploadOperationsCount++; - operation.start(); + if (small) { + currentUploadSmallOperationsCount--; + if (currentUploadSmallOperationsCount < 2) { + FileUploadOperation operation = uploadSmallOperationQueue.poll(); + if (operation != null) { + currentUploadSmallOperationsCount++; + operation.start(); + } + } + } else { + currentUploadOperationsCount--; + if (currentUploadOperationsCount < 2) { + FileUploadOperation operation = uploadOperationQueue.poll(); + if (operation != null) { + currentUploadOperationsCount++; + operation.start(); + } } } if (delegate != null) { @@ -153,12 +164,23 @@ public class FileLoader { fileProgresses.remove(location); } }); - currentUploadOperationsCount--; - if (currentUploadOperationsCount < 2) { - FileUploadOperation operation = uploadOperationQueue.poll(); - if (operation != null) { - currentUploadOperationsCount++; - operation.start(); + if (small) { + currentUploadSmallOperationsCount--; + if (currentUploadSmallOperationsCount < 2) { + FileUploadOperation operation = uploadSmallOperationQueue.poll(); + if (operation != null) { + currentUploadSmallOperationsCount++; + operation.start(); + } + } + } else { + currentUploadOperationsCount--; + if (currentUploadOperationsCount < 2) { + FileUploadOperation operation = uploadOperationQueue.poll(); + if (operation != null) { + currentUploadOperationsCount++; + operation.start(); + } } } } @@ -175,11 +197,20 @@ public class FileLoader { } } }; - if (currentUploadOperationsCount < 2) { - currentUploadOperationsCount++; - operation.start(); + if (small) { + if (currentUploadSmallOperationsCount < 2) { + currentUploadSmallOperationsCount++; + operation.start(); + } else { + uploadSmallOperationQueue.add(operation); + } } else { - uploadOperationQueue.add(operation); + if (currentUploadOperationsCount < 2) { + currentUploadOperationsCount++; + operation.start(); + } else { + uploadOperationQueue.add(operation); + } } } }); @@ -296,10 +327,30 @@ public class FileLoader { if (fileName == null || fileName.contains("" + Integer.MIN_VALUE)) { return; } - if (loadOperationPaths.containsKey(fileName)) { + + FileLoadOperation operation = null; + operation = loadOperationPaths.get(fileName); + if (operation != null) { + if (force) { + LinkedList downloadQueue = null; + if (audio != null) { + downloadQueue = audioLoadOperationQueue; + } else if (location != null) { + downloadQueue = photoLoadOperationQueue; + } else { + downloadQueue = loadOperationQueue; + } + if (downloadQueue != null) { + int index = downloadQueue.indexOf(operation); + if (index != -1) { + downloadQueue.remove(index); + downloadQueue.add(0, operation); + } + } + } return; } - FileLoadOperation operation = null; + if (video != null) { operation = new FileLoadOperation(video); } else if (location != null) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java b/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java index ddbb8c0d7..2dc73ff3e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Views/AvatarUpdater.java @@ -131,7 +131,7 @@ public class AvatarUpdater implements NotificationCenter.NotificationCenterDeleg uploadingAvatar = AndroidUtilities.getCacheDir() + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg"; NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload); NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload); - FileLoader.getInstance().uploadFile(uploadingAvatar, false); + FileLoader.getInstance().uploadFile(uploadingAvatar, false, true); } } }