From 44be21dd0f1f2294cd20fe8041c619d3679b1874 Mon Sep 17 00:00:00 2001 From: xaxtix Date: Tue, 9 Nov 2021 04:49:31 +0300 Subject: [PATCH 1/2] Update to 8.2.3 --- .../telegram/messenger/AndroidUtilities.java | 35 ++- .../org/telegram/messenger/BuildVars.java | 7 +- .../org/telegram/messenger/ImageLoader.java | 33 +-- .../telegram/messenger/MediaController.java | 246 +++++++++++------- .../org/telegram/messenger/MessageObject.java | 1 + .../messenger/MessagesController.java | 7 + .../telegram/ui/Cells/ChatMessageCell.java | 12 +- .../java/org/telegram/ui/ChatActivity.java | 70 +++-- .../ChatAttachAlertDocumentLayout.java | 14 +- .../java/org/telegram/ui/PhotoViewer.java | 4 +- 10 files changed, 278 insertions(+), 151 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java b/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java index a847e6832..4d236416d 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java @@ -38,6 +38,7 @@ import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -95,6 +96,7 @@ import android.widget.ScrollView; import android.widget.TextView; import com.android.internal.telephony.ITelephony; +import com.google.android.exoplayer2.util.Log; import com.google.android.gms.auth.api.phone.SmsRetriever; import com.google.android.gms.auth.api.phone.SmsRetrieverClient; import com.google.android.gms.tasks.Task; @@ -2267,11 +2269,11 @@ public class AndroidUtilities { return; } File f = new File(fromPath); - Uri contentUri = Uri.fromFile(f); - addMediaToGallery(contentUri); + addMediaToGallery(f); } - public static void addMediaToGallery(Uri uri) { + public static void addMediaToGallery(File file) { + Uri uri = Uri.fromFile(file); if (uri == null) { return; } @@ -2285,8 +2287,8 @@ public class AndroidUtilities { } private static File getAlbumDir(boolean secretChat) { - if (secretChat || !BuildVars.NO_SCOPED_STORAGE || (Build.VERSION.SDK_INT >= 23 && ApplicationLoader.applicationContext.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { - return FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE); + if (secretChat || !BuildVars.NO_SCOPED_STORAGE ||(Build.VERSION.SDK_INT >= 23 && ApplicationLoader.applicationContext.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { + return FileLoader.getDirectory(FileLoader.MEDIA_DIR_IMAGE); } File storageDir = null; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { @@ -2399,17 +2401,25 @@ public class AndroidUtilities { public static File generatePicturePath(boolean secretChat, String ext) { try { - File storageDir = getAlbumDir(secretChat); - Date date = new Date(); - date.setTime(System.currentTimeMillis() + Utilities.random.nextInt(1000) + 1); - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", Locale.US).format(date); - return new File(storageDir, "IMG_" + timeStamp + "." + (TextUtils.isEmpty(ext) ? "jpg" : ext)); + File storageDir = ApplicationLoader.applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES); + return new File(storageDir, generateFileName(0, ext)); } catch (Exception e) { FileLog.e(e); } return null; } + public static String generateFileName(int type, String ext) { + Date date = new Date(); + date.setTime(System.currentTimeMillis() + Utilities.random.nextInt(1000) + 1); + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", Locale.US).format(date); + if (type == 0) { + return "IMG_" + timeStamp + "." + (TextUtils.isEmpty(ext) ? "jpg" : ext); + } else { + return "VID_" + timeStamp + ".mp4"; + } + } + public static CharSequence generateSearchName(String name, String name2, String q) { if (name == null && name2 == null || TextUtils.isEmpty(q)) { return ""; @@ -2701,7 +2711,10 @@ public class AndroidUtilities { } public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException { - OutputStream out = new FileOutputStream(destFile); + return copyFile(sourceFile, new FileOutputStream(destFile)); + } + + public static boolean copyFile(InputStream sourceFile, OutputStream out) throws IOException { byte[] buf = new byte[4096]; int len; while ((len = sourceFile.read(buf)) > 0) { diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/BuildVars.java b/TMessagesProj/src/main/java/org/telegram/messenger/BuildVars.java index f9d464a68..393e4c176 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/BuildVars.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/BuildVars.java @@ -15,15 +15,16 @@ import android.os.Build; public class BuildVars { public static boolean DEBUG_VERSION = false; - public static boolean LOGS_ENABLED = false; public static boolean DEBUG_PRIVATE_VERSION = false; + public static boolean LOGS_ENABLED = false; public static boolean USE_CLOUD_STRINGS = true; public static boolean CHECK_UPDATES = true; public static boolean NO_SCOPED_STORAGE = Build.VERSION.SDK_INT <= 29; - public static int BUILD_VERSION = 2462; - public static String BUILD_VERSION_STRING = "8.2.1"; + public static int BUILD_VERSION = 2466; + public static String BUILD_VERSION_STRING = "8.2.3"; public static int APP_ID = 4; public static String APP_HASH = "014b35b6184100b085b0d0572f9b5103"; + public static String SMS_HASH = isStandaloneApp() ? "w0lkcmTZkKh" : (DEBUG_VERSION ? "O2P2z+/jBpJ" : "oLeq9AcOZkT"); public static String PLAYSTORE_APP_URL = "https://play.google.com/store/apps/details?id=org.telegram.messenger"; diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ImageLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/ImageLoader.java index 95cf9d0d5..fdbaca2a2 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ImageLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ImageLoader.java @@ -1931,7 +1931,7 @@ public class ImageLoader { @TargetApi(26) private static void moveDirectory(File source, File target) { - if (!target.exists() && !target.mkdir()) { + if (!source.exists() || (!target.exists() && !target.mkdir())) { return; } try (Stream files = Files.list(source.toPath())) { @@ -1984,27 +1984,20 @@ public class ImageLoader { } } } - telegramPath = new File(path, "Telegram"); - telegramPath.mkdirs(); - /*int version = 0; - try { - PackageManager pm = ApplicationLoader.applicationContext.getPackageManager(); - ApplicationInfo applicationInfo = pm.getApplicationInfo(ApplicationLoader.applicationContext.getPackageName(), 0); - if (applicationInfo != null) { - version = applicationInfo.targetSdkVersion; - } - } catch (Throwable ignore) { + if (Build.VERSION.SDK_INT >= 30) { + File newPath = ApplicationLoader.applicationContext.getExternalFilesDir(null); + telegramPath = new File(newPath, "Telegram"); +// File oldPath = new File(path, "Telegram"); +// long moveStart = System.currentTimeMillis(); +// moveDirectory(oldPath, telegramPath); +// long dt = System.currentTimeMillis() - moveStart; +// FileLog.d("move time = " + dt); + } else { + telegramPath = new File(path, "Telegram"); } - File newPath = ApplicationLoader.applicationContext.getExternalFilesDir(null); - telegramPath = new File(newPath, "Telegram"); //TODO - if (Build.VERSION.SDK_INT >= 29 && version < 30) { - File oldPath = new File(path, "Telegram"); - long moveStart = SystemClock.elapsedRealtime(); - moveDirectory(oldPath, telegramPath); - long dt = SystemClock.elapsedRealtime() - moveStart; - FileLog.d("move time = " + dt); - }*/ + telegramPath.mkdirs(); + if (Build.VERSION.SDK_INT >= 19 && !telegramPath.isDirectory()) { ArrayList dirs = AndroidUtilities.getDataDirs(); for (int a = 0, N = dirs.size(); a < N; a++) { diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index d54f91081..538d91e3a 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -16,6 +16,7 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.app.DownloadManager; import android.content.ContentResolver; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -81,8 +82,10 @@ import org.telegram.ui.PhotoViewer; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.lang.reflect.Method; import java.net.URLEncoder; import java.nio.ByteBuffer; @@ -99,10 +102,15 @@ import java.util.concurrent.CountDownLatch; public class MediaController implements AudioManager.OnAudioFocusChangeListener, NotificationCenter.NotificationCenterDelegate, SensorEventListener { private native int startRecord(String path, int sampleRate); + private native int writeFrame(ByteBuffer frame, int len); + private native void stopRecord(); + public static native int isOpusFile(String path); + public native byte[] getWaveform(String path); + public native byte[] getWaveform2(short[] array, int length); public boolean isBuffering() { @@ -454,7 +462,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, private static final float VOLUME_NORMAL = 1.0f; private static final int AUDIO_NO_FOCUS_NO_DUCK = 0; private static final int AUDIO_NO_FOCUS_CAN_DUCK = 1; - private static final int AUDIO_FOCUSED = 2; + private static final int AUDIO_FOCUSED = 2; private static class VideoConvertMessage { public MessageObject messageObject; @@ -745,7 +753,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, Cursor cursor = null; try { if (ApplicationLoader.applicationContext.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - cursor = MediaStore.Images.Media.query(ApplicationLoader.applicationContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] {"COUNT(_id)"}, null, null, null); + cursor = MediaStore.Images.Media.query(ApplicationLoader.applicationContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"COUNT(_id)"}, null, null, null); if (cursor != null) { if (cursor.moveToNext()) { count += cursor.getInt(0); @@ -761,7 +769,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, } try { if (ApplicationLoader.applicationContext.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - cursor = MediaStore.Images.Media.query(ApplicationLoader.applicationContext.getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[] {"COUNT(_id)"}, null, null, null); + cursor = MediaStore.Images.Media.query(ApplicationLoader.applicationContext.getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{"COUNT(_id)"}, null, null, null); if (cursor != null) { if (cursor.moveToNext()) { count += cursor.getInt(0); @@ -1971,6 +1979,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, } return audioPlayer.getDuration(); } + public MessageObject getPlayingMessageObject() { return playingMessageObject; } @@ -2496,7 +2505,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, final MessageObject currentMessage = playingMessageObject; AndroidUtilities.runOnUIThread(() -> { if (audioPlayer != null && playingMessageObject != null && !isPaused) { - if (isSamePlayingMessage(currentMessage )) { + if (isSamePlayingMessage(currentMessage)) { seekToProgress(playingMessageObject, p); } audioPlayer.play(); @@ -2853,7 +2862,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, /*if (Build.VERSION.SDK_INT >= 26) { ApplicationLoader.applicationContext.startForegroundService(intent); } else {*/ - ApplicationLoader.applicationContext.startService(intent); + ApplicationLoader.applicationContext.startService(intent); //} } catch (Throwable e) { FileLog.e(e); @@ -3245,7 +3254,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, /*if (Build.VERSION.SDK_INT >= 26) { ApplicationLoader.applicationContext.startForegroundService(intent); } else {*/ - ApplicationLoader.applicationContext.startService(intent); + ApplicationLoader.applicationContext.startService(intent); //} } catch (Throwable e) { FileLog.e(e); @@ -3793,7 +3802,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, } if (!cancelled) { if (isMusic) { - AndroidUtilities.addMediaToGallery(Uri.fromFile(destFile)); + AndroidUtilities.addMediaToGallery(destFile); } else { DownloadManager downloadManager = (DownloadManager) ApplicationLoader.applicationContext.getSystemService(Context.DOWNLOAD_SERVICE); String mimeType = mime; @@ -3870,7 +3879,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, } public static void saveFile(String fullPath, Context context, final int type, final String name, final String mime, final Runnable onSaved) { - if (fullPath == null) { + if (fullPath == null || context == null) { return; } @@ -3887,8 +3896,9 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, } final File sourceFile = file; - final boolean[] cancelled = new boolean[] {false}; + final boolean[] cancelled = new boolean[]{false}; if (sourceFile.exists()) { + AlertDialog progressDialog = null; final boolean[] finished = new boolean[1]; if (context != null && type != 0) { @@ -3913,99 +3923,152 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, new Thread(() -> { try { - File destFile; - if (type == 0) { - destFile = AndroidUtilities.generatePicturePath(false, FileLoader.getFileExtension(sourceFile)); - } else if (type == 1) { - destFile = AndroidUtilities.generateVideoPath(); - } else { - File dir; - if (type == 2) { - dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); - } else { - dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); - } - dir.mkdir(); - destFile = new File(dir, name); - if (destFile.exists()) { - int idx = name.lastIndexOf('.'); - for (int a = 0; a < 10; a++) { - String newName; - if (idx != -1) { - newName = name.substring(0, idx) + "(" + (a + 1) + ")" + name.substring(idx); - } else { - newName = name + "(" + (a + 1) + ")"; + boolean result = true; + if (Build.VERSION.SDK_INT >= 29) { + try { + ContentValues contentValues = new ContentValues(); + String extension = MimeTypeMap.getFileExtensionFromUrl(sourceFile.getAbsolutePath()); + String mimeType = null; + if (extension != null) { + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); + } + Uri uriToInsert = null; + if (type == 0) { + uriToInsert = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + File dirDest = new File(Environment.DIRECTORY_PICTURES, "Telegram"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, dirDest + File.separator); } - destFile = new File(dir, newName); - if (!destFile.exists()) { + contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, AndroidUtilities.generateFileName(0, extension)); + contentValues.put(MediaStore.Images.Media.MIME_TYPE, mimeType); + } else if (type == 1) { + File dirDest = new File(Environment.DIRECTORY_MOVIES, "Telegram"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, dirDest + File.separator); + uriToInsert = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); + contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, AndroidUtilities.generateFileName(1, extension)); + } else if (type == 2) { + File dirDest = new File(Environment.DIRECTORY_DOWNLOADS, "Telegram"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, dirDest + File.separator); + uriToInsert = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); + contentValues.put(MediaStore.Downloads.DISPLAY_NAME, sourceFile.getName()); + } else { + File dirDest = new File(Environment.DIRECTORY_MUSIC, "Telegram"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, dirDest + File.separator); + uriToInsert = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); + contentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, sourceFile.getName()); + } + + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); + + Uri dstUri = context.getContentResolver().insert(uriToInsert, contentValues); + if (dstUri != null) { + FileInputStream fileInputStream = new FileInputStream(sourceFile); + OutputStream outputStream = context.getContentResolver().openOutputStream(dstUri); + AndroidUtilities.copyFile(fileInputStream, outputStream); + fileInputStream.close(); + } + } catch (Exception e) { + FileLog.e(e); + result = false; + } + } else { + File destFile; + if (type == 0) { + destFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Telegram"); + destFile.mkdirs(); + destFile = new File(destFile, AndroidUtilities.generateFileName(0, FileLoader.getFileExtension(sourceFile))); + } else if (type == 1) { + destFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "Telegram"); + destFile.mkdirs(); + destFile = new File(destFile, AndroidUtilities.generateFileName(1, FileLoader.getFileExtension(sourceFile))); + } else { + File dir; + if (type == 2) { + dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); + } else { + dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); + } + dir = new File(dir, "Telegram"); + dir.mkdirs(); + destFile = new File(dir, name); + if (destFile.exists()) { + int idx = name.lastIndexOf('.'); + for (int a = 0; a < 10; a++) { + String newName; + if (idx != -1) { + newName = name.substring(0, idx) + "(" + (a + 1) + ")" + name.substring(idx); + } else { + newName = name + "(" + (a + 1) + ")"; + } + destFile = new File(dir, newName); + if (!destFile.exists()) { + break; + } + } + } + } + if (!destFile.exists()) { + destFile.createNewFile(); + } + long lastProgress = System.currentTimeMillis() - 500; + try (FileInputStream inputStream = new FileInputStream(sourceFile); FileChannel source = inputStream.getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel()) { + long size = source.size(); + try { + @SuppressLint("DiscouragedPrivateApi") Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$"); + int fdint = (Integer) getInt.invoke(inputStream.getFD()); + if (AndroidUtilities.isInternalUri(fdint)) { + if (finalProgress != null) { + AndroidUtilities.runOnUIThread(() -> { + try { + finalProgress.dismiss(); + } catch (Exception e) { + FileLog.e(e); + } + }); + } + return; + } + } catch (Throwable e) { + FileLog.e(e); + } + for (long a = 0; a < size; a += 4096) { + if (cancelled[0]) { break; } - } - } - } - if (!destFile.exists()) { - destFile.createNewFile(); - } - boolean result = true; - long lastProgress = System.currentTimeMillis() - 500; - try (FileInputStream inputStream = new FileInputStream(sourceFile); FileChannel source = inputStream.getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel()) { - long size = source.size(); - try { - @SuppressLint("DiscouragedPrivateApi") Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$"); - int fdint = (Integer) getInt.invoke(inputStream.getFD()); - if (AndroidUtilities.isInternalUri(fdint)) { + destination.transferFrom(source, a, Math.min(4096, size - a)); if (finalProgress != null) { - AndroidUtilities.runOnUIThread(() -> { - try { - finalProgress.dismiss(); - } catch (Exception e) { - FileLog.e(e); - } - }); + if (lastProgress <= System.currentTimeMillis() - 500) { + lastProgress = System.currentTimeMillis(); + final int progress = (int) ((float) a / (float) size * 100); + AndroidUtilities.runOnUIThread(() -> { + try { + finalProgress.setProgress(progress); + } catch (Exception e) { + FileLog.e(e); + } + }); + } } - return; } - } catch (Throwable e) { + } catch (Exception e) { FileLog.e(e); + result = false; } - for (long a = 0; a < size; a += 4096) { - if (cancelled[0]) { - break; - } - destination.transferFrom(source, a, Math.min(4096, size - a)); - if (finalProgress != null) { - if (lastProgress <= System.currentTimeMillis() - 500) { - lastProgress = System.currentTimeMillis(); - final int progress = (int) ((float) a / (float) size * 100); - AndroidUtilities.runOnUIThread(() -> { - try { - finalProgress.setProgress(progress); - } catch (Exception e) { - FileLog.e(e); - } - }); - } + if (cancelled[0]) { + destFile.delete(); + result = false; + } + if (result) { + if (type == 2) { + DownloadManager downloadManager = (DownloadManager) ApplicationLoader.applicationContext.getSystemService(Context.DOWNLOAD_SERVICE); + downloadManager.addCompletedDownload(destFile.getName(), destFile.getName(), false, mime, destFile.getAbsolutePath(), destFile.length(), true); + } else { + AndroidUtilities.addMediaToGallery(destFile.getAbsoluteFile()); } } - } catch (Exception e) { - FileLog.e(e); - result = false; } - if (cancelled[0]) { - destFile.delete(); - result = false; - } - - if (result) { - if (type == 2) { - DownloadManager downloadManager = (DownloadManager) ApplicationLoader.applicationContext.getSystemService(Context.DOWNLOAD_SERVICE); - downloadManager.addCompletedDownload(destFile.getName(), destFile.getName(), false, mime, destFile.getAbsolutePath(), destFile.length(), true); - } else { - AndroidUtilities.addMediaToGallery(Uri.fromFile(destFile)); - } - if (onSaved != null) { - AndroidUtilities.runOnUIThread(onSaved); - } + if (result && onSaved != null) { + AndroidUtilities.runOnUIThread(onSaved); } } catch (Exception e) { FileLog.e(e); @@ -4833,6 +4896,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener, public interface VideoConvertorListener { boolean checkConversionCanceled(); + void didWriteData(long availableSize, float progress); } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java index 4c8361665..2a4fd184c 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java @@ -5967,6 +5967,7 @@ public class MessageObject { attachPathExists = f.exists(); } if (!attachPathExists) { + File file = FileLoader.getPathToMessage(messageOwner); if (type == 3 && needDrawBluredPreview()) { mediaExists = new File(file.getAbsolutePath() + ".enc").exists(); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index 382b5a36b..a1b7595da 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -14606,6 +14606,13 @@ public class MessagesController extends BaseController implements NotificationCe .apply(); } + public void markSponsoredAsRead(long dialog_id, MessageObject object) { + ArrayList messages = getSponsoredMessages(dialog_id); + if (messages != null) { + messages.remove(object); + } + } + public interface MessagesLoadedCallback { void onMessagesLoaded(boolean fromCache); void onError(); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Cells/ChatMessageCell.java b/TMessagesProj/src/main/java/org/telegram/ui/Cells/ChatMessageCell.java index 6fec1e773..8e57276bd 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Cells/ChatMessageCell.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Cells/ChatMessageCell.java @@ -5834,7 +5834,11 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate if (messageObject.isSponsored()) { drawInstantView = true; - drawInstantViewType = 1; + if (messageObject.sponsoredChannelPost != 0) { + drawInstantViewType = 12; + } else { + drawInstantViewType = 1; + } long id = MessageObject.getPeerId(messageObject.messageOwner.from_id); if (id > 0) { TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(id); @@ -6848,7 +6852,9 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate if (drawInstantView && instantViewLayout == null) { String str; instantWidth = AndroidUtilities.dp(12 + 9 + 12); - if (drawInstantViewType == 1) { + if (drawInstantViewType == 12) { + str = LocaleController.getString("OpenChannelPost", R.string.OpenChannelPost); + } else if (drawInstantViewType == 1) { str = LocaleController.getString("OpenChannel", R.string.OpenChannel); } else if (drawInstantViewType == 10) { str = LocaleController.getString("OpenBot", R.string.OpenBot); @@ -6889,7 +6895,7 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate if (currentMessageObject.type == 12) { totalHeight += AndroidUtilities.dp(14); } - if (hasNewLineForTime) { + if (currentMessageObject.isSponsored() && hasNewLineForTime) { totalHeight += AndroidUtilities.dp(16); } if (instantViewLayout != null && instantViewLayout.getLineCount() > 0) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 37652bd6f..dec3ba227 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -5754,7 +5754,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } else if (returnToMessageId > 0) { scrollToMessageId(returnToMessageId, 0, true, returnToLoadIndex, true, 0); } else { - scrollToLastMessage(); + scrollToLastMessage(false); if (!pinnedMessageIds.isEmpty()) { forceScrollToFirst = true; forceNextPinnedMessageId = pinnedMessageIds.get(0); @@ -9332,7 +9332,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } else { SendMessagesHelper.getInstance(currentAccount).sendMessage(getUserConfig().getCurrentUser(), dialog_id, messageObject, getThreadMessage(), null, null, true, 0); if (chatMode == 0) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } hideFieldPanel(false); } @@ -10892,9 +10892,18 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } } - private void moveScrollToLastMessage() { + private void moveScrollToLastMessage(boolean skipSponsored) { if (chatListView != null && !messages.isEmpty() && !pinchToZoomHelper.isInOverlayMode()) { - chatLayoutManager.scrollToPositionWithOffset(0, 0); + int position = 0; + if (skipSponsored) { + while (position < messages.size()) { + if (!messages.get(position).isSponsored()) { + break; + } + position++; + } + } + chatLayoutManager.scrollToPositionWithOffset(position, 0); chatListView.stopScroll(); } } @@ -10967,7 +10976,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } } - private void scrollToLastMessage() { + private void scrollToLastMessage(boolean skipSponsored) { if (chatListView.isFastScrollAnimationRunning()) { return; } @@ -10984,7 +10993,16 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } else { chatAdapter.updateRowsSafe(); chatScrollHelperCallback.scrollTo = null; - chatScrollHelper.scrollToPosition(0, 0, true, true); + int position = 0; + if (skipSponsored) { + while (position < messages.size()) { + if (!messages.get(position).isSponsored()) { + break; + } + position++; + } + } + chatScrollHelper.scrollToPosition(position, 0, true, true); } } else { if (progressDialog != null) { @@ -13802,7 +13820,8 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not scrollToMessagePosition = -10000; scrollToMessage = null; } else { - moveScrollToLastMessage(); + addSponsoredMessages(!isFirstLoading); + moveScrollToLastMessage(true); } if (loaded_mentions_count != 0) { showMentionDownButton(true, true); @@ -14473,7 +14492,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } if (chatLayoutManager != null) { if (mediaUpdated && chatLayoutManager.findFirstVisibleItemPosition() == 0) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } } getNotificationsController().playOutChatSound(); @@ -16248,7 +16267,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not needAnimateToMessage = obj; } if (obj.isOut() && obj.wasJustSent) { - scrollToLastMessage(); + scrollToLastMessage(true); return; } if (obj.type < 0 || messagesDict[0].indexOfKey(messageId) >= 0) { @@ -16481,7 +16500,12 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } maxDate[0] = Math.max(maxDate[0], obj.messageOwner.date); messagesDict[0].put(messageId, obj); - ArrayList dayArray = messagesByDays.get(obj.dateKey); + ArrayList dayArray; + if (isAd && !messages.isEmpty()) { + dayArray = messagesByDays.get(messages.get(0).dateKey); + } else { + dayArray = messagesByDays.get(obj.dateKey); + } if (placeToPaste > messages.size()) { placeToPaste = messages.size(); } @@ -16632,7 +16656,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not scrollToTopOnResume = true; } else { forceScrollToTop = true; - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } } } else { @@ -16663,7 +16687,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not chatListItemAnimator.setShouldAnimateEnterFromBottom(needMoveScrollToLastMessage); } if (needMoveScrollToLastMessage) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } else { int index = messages.indexOf(messageObject); if (chatLayoutManager != null && index > 0 && (chatLayoutManager.findViewByPosition(chatAdapter.messagesStartRow + index) != null || chatLayoutManager.findViewByPosition(chatAdapter.messagesStartRow + index - 1) != null)) { @@ -18828,7 +18852,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not chatLayoutManager.scrollToPositionWithOffset(chatAdapter.messagesStartRow + messages.indexOf(scrollToMessage), yOffset, bottom); } } else { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } scrollToTopUnReadOnResume = false; scrollToTopOnResume = false; @@ -18945,8 +18969,15 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not int messageId = 0; int offset = 0; if (chatLayoutManager != null) { + boolean sponsoredMessageFound = false; + for (int i = 0; i < chatListView.getChildCount(); i++) { + if (chatListView.getChildAt(i) instanceof ChatMessageCell && ((ChatMessageCell) chatListView.getChildAt(i)).getMessageObject().isSponsored()) { + sponsoredMessageFound = true; + break; + } + } int position = chatLayoutManager.findFirstVisibleItemPosition(); - if (position != 0) { + if (position != 0 && !sponsoredMessageFound) { RecyclerListView.Holder holder = (RecyclerListView.Holder) chatListView.findViewHolderForAdapterPosition(position); if (holder != null) { int mid = 0; @@ -20582,13 +20613,13 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } } if (success && chatMode == 0) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } } else { if (getSendMessagesHelper().retrySendMessage(selectedObject, false)) { updateVisibleRows(); if (chatMode == 0) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } } } @@ -21255,7 +21286,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } } else { fragment.finishFragment(); - moveScrollToLastMessage(); + moveScrollToLastMessage(false); showFieldPanelForForward(true, fmessages); if (AndroidUtilities.isTablet()) { hideActionMode(); @@ -21545,7 +21576,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not public void didSelectLocation(TLRPC.MessageMedia location, int locationType, boolean notify, int scheduleDate) { getSendMessagesHelper().sendMessage(location, dialog_id, replyingMessageObject, getThreadMessage(), null, null, notify, scheduleDate); if (chatMode == 0) { - moveScrollToLastMessage(); + moveScrollToLastMessage(false); } if (locationType == LocationActivity.LOCATION_TYPE_SEND || locationType == LocationActivity.LOCATION_TYPE_SEND_WITH_LIVE) { afterMessageSend(); @@ -22431,8 +22462,9 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not req.channel = MessagesController.getInputChannel(currentChat); req.random_id = object.sponsoredId; getConnectionsManager().sendRequest(req, (response, error) -> { - + }); + getMessagesController().markSponsoredAsRead(dialog_id, object); } @Override diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlertDocumentLayout.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlertDocumentLayout.java index ed2cf4234..da3dd4329 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlertDocumentLayout.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlertDocumentLayout.java @@ -326,6 +326,10 @@ public class ChatAttachAlertDocumentLayout extends ChatAttachAlert.AttachAlertLa if (object instanceof ListItem) { ListItem item = (ListItem) object; File file = item.file; + boolean isExternalStorageManager = false; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { + isExternalStorageManager = Environment.isExternalStorageManager(); + } if (file == null) { if (item.icon == R.drawable.files_gallery) { HashMap selectedPhotos = new HashMap<>(); @@ -369,7 +373,7 @@ public class ChatAttachAlertDocumentLayout extends ChatAttachAlert.AttachAlertLa if (delegate != null) { delegate.startMusicSelectActivity(); } - } else if (!BuildVars.NO_SCOPED_STORAGE && item.icon == R.drawable.files_storage) { + } else if (!BuildVars.NO_SCOPED_STORAGE && item.icon == R.drawable.files_storage && !isExternalStorageManager) { delegate.startDocumentSelectActivity(); } else { int top = getTopForScroll(); @@ -942,7 +946,11 @@ public class ChatAttachAlertDocumentLayout extends ChatAttachAlert.AttachAlertLa items.clear(); HashSet paths = new HashSet<>(); - if (!BuildVars.NO_SCOPED_STORAGE) { + boolean isExternalStorageManager = false; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { + isExternalStorageManager = Environment.isExternalStorageManager(); + } + if (!BuildVars.NO_SCOPED_STORAGE && !isExternalStorageManager) { ListItem ext = new ListItem(); ext.title = LocaleController.getString("InternalStorage", R.string.InternalStorage); ext.icon = R.drawable.files_storage; @@ -1027,7 +1035,7 @@ public class ChatAttachAlertDocumentLayout extends ChatAttachAlert.AttachAlertLa ListItem fs; try { - File telegramPath = new File(Environment.getExternalStorageDirectory(), "Telegram"); + File telegramPath = new File(ApplicationLoader.applicationContext.getExternalFilesDir(null), "Telegram"); if (telegramPath.exists()) { fs = new ListItem(); fs.title = "Telegram"; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java index e3e570140..fa49254d7 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java @@ -9865,7 +9865,9 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat MessageObject openingObject = imagesArr.get(index); if (!openingObject.scheduled && (parentChatActivity == null || !parentChatActivity.isThreadChat())) { opennedFromMedia = true; - startOffset = object.starOffset; + if (object != null) { + startOffset = object.starOffset; + } menuItem.showSubItem(gallery_menu_showinchat); if (openingObject.canForwardMessage()) { setItemVisible(sendItem, true, false); From 3b971647e2c5eff7390b4d0ab953314932d41b77 Mon Sep 17 00:00:00 2001 From: xaxtix Date: Tue, 9 Nov 2021 05:38:47 +0300 Subject: [PATCH 2/2] Update to 8.2.3 --- TMessagesProj/src/main/res/values/strings.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/TMessagesProj/src/main/res/values/strings.xml b/TMessagesProj/src/main/res/values/strings.xml index b16c674b6..b5a03e95a 100644 --- a/TMessagesProj/src/main/res/values/strings.xml +++ b/TMessagesProj/src/main/res/values/strings.xml @@ -4809,4 +4809,5 @@ un1 was accepted into the group %1$s was accepted into the group %2$s|%1$s was accepted into the group + VIEW POST