2018-07-30 04:07:02 +02:00
/ *
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v . 5 . x . x .
2018-07-30 04:07:02 +02:00
* It is licensed under GNU GPL v . 2 or later .
* You should have received a copy of the license in this archive ( see LICENSE ) .
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov , 2013 - 2018 .
2018-07-30 04:07:02 +02:00
* /
package org.telegram.messenger ;
import android.content.BroadcastReceiver ;
import android.content.Context ;
import android.content.Intent ;
import android.content.IntentFilter ;
import android.content.SharedPreferences ;
import android.net.ConnectivityManager ;
2020-07-26 10:03:38 +02:00
import android.util.Pair ;
2018-07-30 04:07:02 +02:00
import android.util.SparseArray ;
2022-03-20 21:22:37 +01:00
import androidx.collection.LongSparseArray ;
import org.telegram.SQLite.SQLiteCursor ;
import org.telegram.SQLite.SQLitePreparedStatement ;
import org.telegram.tgnet.NativeByteBuffer ;
2018-07-30 04:07:02 +02:00
import org.telegram.tgnet.TLRPC ;
2022-03-20 21:22:37 +01:00
import org.telegram.ui.Components.Bulletin ;
2018-07-30 04:07:02 +02:00
2022-03-20 21:22:37 +01:00
import java.io.File ;
2018-07-30 04:07:02 +02:00
import java.lang.ref.WeakReference ;
import java.util.ArrayList ;
import java.util.HashMap ;
2022-03-20 21:22:37 +01:00
import java.util.Locale ;
2021-09-20 00:10:42 +02:00
2021-03-05 09:38:58 +01:00
import cn.hutool.core.util.StrUtil ;
2022-01-17 08:45:37 +01:00
import tw.nekomimi.nekogram.NekoConfig ;
2021-03-05 09:38:58 +01:00
2019-07-18 15:01:39 +02:00
public class DownloadController extends BaseController implements NotificationCenter . NotificationCenterDelegate {
2018-07-30 04:07:02 +02:00
public interface FileDownloadProgressListener {
2019-01-23 18:03:33 +01:00
void onFailedDownload ( String fileName , boolean canceled ) ;
2018-07-30 04:07:02 +02:00
void onSuccessDownload ( String fileName ) ;
2020-01-23 07:15:40 +01:00
void onProgressDownload ( String fileName , long downloadSize , long totalSize ) ;
void onProgressUpload ( String fileName , long downloadSize , long totalSize , boolean isEncrypted ) ;
2018-07-30 04:07:02 +02:00
int getObserverTag ( ) ;
}
2019-03-03 21:40:48 +01:00
public static final int AUTODOWNLOAD_TYPE_PHOTO = 1 ;
public static final int AUTODOWNLOAD_TYPE_AUDIO = 2 ;
public static final int AUTODOWNLOAD_TYPE_VIDEO = 4 ;
public static final int AUTODOWNLOAD_TYPE_DOCUMENT = 8 ;
public static final int PRESET_NUM_CONTACT = 0 ;
public static final int PRESET_NUM_PM = 1 ;
public static final int PRESET_NUM_GROUP = 2 ;
public static final int PRESET_NUM_CHANNEL = 3 ;
public static final int PRESET_SIZE_NUM_PHOTO = 0 ;
public static final int PRESET_SIZE_NUM_VIDEO = 1 ;
public static final int PRESET_SIZE_NUM_DOCUMENT = 2 ;
public static final int PRESET_SIZE_NUM_AUDIO = 3 ;
2018-07-30 04:07:02 +02:00
private int lastCheckMask = 0 ;
private ArrayList < DownloadObject > photoDownloadQueue = new ArrayList < > ( ) ;
private ArrayList < DownloadObject > audioDownloadQueue = new ArrayList < > ( ) ;
private ArrayList < DownloadObject > documentDownloadQueue = new ArrayList < > ( ) ;
private ArrayList < DownloadObject > videoDownloadQueue = new ArrayList < > ( ) ;
private HashMap < String , DownloadObject > downloadQueueKeys = new HashMap < > ( ) ;
2020-07-26 10:03:38 +02:00
private HashMap < Pair < Long , Integer > , DownloadObject > downloadQueuePairs = new HashMap < > ( ) ;
2018-07-30 04:07:02 +02:00
private HashMap < String , ArrayList < WeakReference < FileDownloadProgressListener > > > loadingFileObservers = new HashMap < > ( ) ;
private HashMap < String , ArrayList < MessageObject > > loadingFileMessagesObservers = new HashMap < > ( ) ;
private SparseArray < String > observersByTag = new SparseArray < > ( ) ;
private boolean listenerInProgress = false ;
private HashMap < String , FileDownloadProgressListener > addLaterArray = new HashMap < > ( ) ;
private ArrayList < FileDownloadProgressListener > deleteLaterArray = new ArrayList < > ( ) ;
private int lastTag = 0 ;
2019-03-03 21:40:48 +01:00
private boolean loadingAutoDownloadConfig ;
2019-01-23 18:03:33 +01:00
private LongSparseArray < Long > typingTimes = new LongSparseArray < > ( ) ;
2022-03-20 21:22:37 +01:00
public final ArrayList < MessageObject > downloadingFiles = new ArrayList < > ( ) ;
public final ArrayList < MessageObject > recentDownloadingFiles = new ArrayList < > ( ) ;
public final SparseArray < MessageObject > unviewedDownloads = new SparseArray < > ( ) ;
2019-03-03 21:40:48 +01:00
public static class Preset {
public int [ ] mask = new int [ 4 ] ;
2022-06-21 04:51:00 +02:00
public long [ ] sizes = new long [ 4 ] ;
2019-03-03 21:40:48 +01:00
public boolean preloadVideo ;
public boolean preloadMusic ;
public boolean lessCallData ;
public boolean enabled ;
2019-12-31 14:08:08 +01:00
public int maxVideoBitrate ;
2019-03-03 21:40:48 +01:00
2022-06-21 04:51:00 +02:00
public Preset ( int [ ] m , long p , long v , long f , boolean pv , boolean pm , boolean e , boolean l , int bitrate ) {
2019-03-03 21:40:48 +01:00
System . arraycopy ( m , 0 , mask , 0 , mask . length ) ;
sizes [ PRESET_SIZE_NUM_PHOTO ] = p ;
sizes [ PRESET_SIZE_NUM_VIDEO ] = v ;
sizes [ PRESET_SIZE_NUM_DOCUMENT ] = f ;
sizes [ PRESET_SIZE_NUM_AUDIO ] = 512 * 1024 ;
preloadVideo = pv ;
preloadMusic = pm ;
lessCallData = l ;
2019-12-31 14:08:08 +01:00
maxVideoBitrate = bitrate ;
2019-03-03 21:40:48 +01:00
enabled = e ;
}
2019-12-31 14:08:08 +01:00
public Preset ( String str , String deafultValue ) {
2019-03-03 21:40:48 +01:00
String [ ] args = str . split ( " _ " ) ;
2019-12-31 14:08:08 +01:00
String [ ] defaultArgs = null ;
2019-03-03 21:40:48 +01:00
if ( args . length > = 11 ) {
mask [ 0 ] = Utilities . parseInt ( args [ 0 ] ) ;
mask [ 1 ] = Utilities . parseInt ( args [ 1 ] ) ;
mask [ 2 ] = Utilities . parseInt ( args [ 2 ] ) ;
mask [ 3 ] = Utilities . parseInt ( args [ 3 ] ) ;
sizes [ PRESET_SIZE_NUM_PHOTO ] = Utilities . parseInt ( args [ 4 ] ) ;
sizes [ PRESET_SIZE_NUM_VIDEO ] = Utilities . parseInt ( args [ 5 ] ) ;
sizes [ PRESET_SIZE_NUM_DOCUMENT ] = Utilities . parseInt ( args [ 6 ] ) ;
sizes [ PRESET_SIZE_NUM_AUDIO ] = Utilities . parseInt ( args [ 7 ] ) ;
preloadVideo = Utilities . parseInt ( args [ 8 ] ) = = 1 ;
preloadMusic = Utilities . parseInt ( args [ 9 ] ) = = 1 ;
enabled = Utilities . parseInt ( args [ 10 ] ) = = 1 ;
if ( args . length > = 12 ) {
lessCallData = Utilities . parseInt ( args [ 11 ] ) = = 1 ;
2019-12-31 14:08:08 +01:00
} else {
defaultArgs = deafultValue . split ( " _ " ) ;
lessCallData = Utilities . parseInt ( defaultArgs [ 11 ] ) = = 1 ;
}
if ( args . length > = 13 ) {
maxVideoBitrate = Utilities . parseInt ( args [ 12 ] ) ;
} else {
if ( defaultArgs = = null ) {
defaultArgs = deafultValue . split ( " _ " ) ;
}
maxVideoBitrate = Utilities . parseInt ( defaultArgs [ 12 ] ) ;
2019-03-03 21:40:48 +01:00
}
}
}
public void set ( Preset preset ) {
System . arraycopy ( preset . mask , 0 , mask , 0 , mask . length ) ;
System . arraycopy ( preset . sizes , 0 , sizes , 0 , sizes . length ) ;
preloadVideo = preset . preloadVideo ;
preloadMusic = preset . preloadMusic ;
lessCallData = preset . lessCallData ;
2019-12-31 14:08:08 +01:00
maxVideoBitrate = preset . maxVideoBitrate ;
2019-03-03 21:40:48 +01:00
}
public void set ( TLRPC . TL_autoDownloadSettings settings ) {
preloadMusic = settings . audio_preload_next ;
preloadVideo = settings . video_preload_large ;
lessCallData = settings . phonecalls_less_data ;
2019-12-31 14:08:08 +01:00
maxVideoBitrate = settings . video_upload_maxbitrate ;
2019-03-03 21:40:48 +01:00
sizes [ PRESET_SIZE_NUM_PHOTO ] = Math . max ( 500 * 1024 , settings . photo_size_max ) ;
sizes [ PRESET_SIZE_NUM_VIDEO ] = Math . max ( 500 * 1024 , settings . video_size_max ) ;
sizes [ PRESET_SIZE_NUM_DOCUMENT ] = Math . max ( 500 * 1024 , settings . file_size_max ) ;
for ( int a = 0 ; a < mask . length ; a + + ) {
if ( settings . photo_size_max ! = 0 & & ! settings . disabled ) {
mask [ a ] | = AUTODOWNLOAD_TYPE_PHOTO ;
} else {
2021-12-21 07:05:11 +01:00
mask [ a ] & = ~ AUTODOWNLOAD_TYPE_PHOTO ;
2019-03-03 21:40:48 +01:00
}
if ( settings . video_size_max ! = 0 & & ! settings . disabled ) {
mask [ a ] | = AUTODOWNLOAD_TYPE_VIDEO ;
} else {
2021-12-21 07:05:11 +01:00
mask [ a ] & = ~ AUTODOWNLOAD_TYPE_VIDEO ;
2019-03-03 21:40:48 +01:00
}
if ( settings . file_size_max ! = 0 & & ! settings . disabled ) {
mask [ a ] | = AUTODOWNLOAD_TYPE_DOCUMENT ;
} else {
2021-12-21 07:05:11 +01:00
mask [ a ] & = ~ AUTODOWNLOAD_TYPE_DOCUMENT ;
2019-03-03 21:40:48 +01:00
}
}
}
@Override
public String toString ( ) {
return mask [ 0 ] + " _ " + mask [ 1 ] + " _ " + mask [ 2 ] + " _ " + mask [ 3 ] +
" _ " + sizes [ PRESET_SIZE_NUM_PHOTO ] +
" _ " + sizes [ PRESET_SIZE_NUM_VIDEO ] +
" _ " + sizes [ PRESET_SIZE_NUM_DOCUMENT ] +
" _ " + sizes [ PRESET_SIZE_NUM_AUDIO ] +
" _ " + ( preloadVideo ? 1 : 0 ) +
" _ " + ( preloadMusic ? 1 : 0 ) +
" _ " + ( enabled ? 1 : 0 ) +
2019-12-31 14:08:08 +01:00
" _ " + ( lessCallData ? 1 : 0 ) +
" _ " + maxVideoBitrate ;
2019-03-03 21:40:48 +01:00
}
public boolean equals ( Preset obj ) {
return mask [ 0 ] = = obj . mask [ 0 ] & &
mask [ 1 ] = = obj . mask [ 1 ] & &
mask [ 2 ] = = obj . mask [ 2 ] & &
mask [ 3 ] = = obj . mask [ 3 ] & &
sizes [ 0 ] = = obj . sizes [ 0 ] & &
sizes [ 1 ] = = obj . sizes [ 1 ] & &
sizes [ 2 ] = = obj . sizes [ 2 ] & &
sizes [ 3 ] = = obj . sizes [ 3 ] & &
preloadVideo = = obj . preloadVideo & &
2019-12-31 14:08:08 +01:00
preloadMusic = = obj . preloadMusic & &
maxVideoBitrate = = obj . maxVideoBitrate ;
2019-03-03 21:40:48 +01:00
}
2019-05-14 14:08:05 +02:00
public boolean isEnabled ( ) {
for ( int a = 0 ; a < mask . length ; a + + ) {
if ( mask [ a ] ! = 0 ) {
return true ;
}
}
return false ;
}
2019-03-03 21:40:48 +01:00
}
public Preset lowPreset ;
public Preset mediumPreset ;
public Preset highPreset ;
public Preset mobilePreset ;
public Preset wifiPreset ;
public Preset roamingPreset ;
public int currentMobilePreset ;
public int currentWifiPreset ;
public int currentRoamingPreset ;
2021-03-05 09:38:58 +01:00
2021-03-12 13:37:39 +01:00
private static SparseArray < DownloadController > Instance = new SparseArray < > ( ) ;
2018-07-30 04:07:02 +02:00
public static DownloadController getInstance ( int num ) {
2021-03-12 13:37:39 +01:00
DownloadController localInstance = Instance . get ( num ) ;
2018-07-30 04:07:02 +02:00
if ( localInstance = = null ) {
synchronized ( DownloadController . class ) {
2021-03-12 13:37:39 +01:00
localInstance = Instance . get ( num ) ;
2018-07-30 04:07:02 +02:00
if ( localInstance = = null ) {
2021-03-12 13:37:39 +01:00
Instance . put ( num , localInstance = new DownloadController ( num ) ) ;
2018-07-30 04:07:02 +02:00
}
}
}
return localInstance ;
}
public DownloadController ( int instance ) {
2019-07-18 15:01:39 +02:00
super ( instance ) ;
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = MessagesController . getMainSettings ( currentAccount ) ;
2019-12-31 14:08:08 +01:00
String defaultLow = " 1_1_1_1_1048576_512000_512000_524288_0_0_1_1_50 " ;
String defaultMedium = " 13_13_13_13_1048576_10485760_1048576_524288_1_1_1_0_100 " ;
String defaultHigh = " 13_13_13_13_1048576_15728640_3145728_524288_1_1_1_0_100 " ;
lowPreset = new Preset ( preferences . getString ( " preset0 " , defaultLow ) , defaultLow ) ;
mediumPreset = new Preset ( preferences . getString ( " preset1 " , defaultMedium ) , defaultMedium ) ;
highPreset = new Preset ( preferences . getString ( " preset2 " , defaultHigh ) , defaultHigh ) ;
2019-03-03 21:40:48 +01:00
boolean newConfig ;
2019-08-22 01:53:26 +02:00
if ( ( newConfig = preferences . contains ( " newConfig " ) ) | | ! getUserConfig ( ) . isClientActivated ( ) ) {
2019-12-31 14:08:08 +01:00
mobilePreset = new Preset ( preferences . getString ( " mobilePreset " , defaultMedium ) , defaultMedium ) ;
wifiPreset = new Preset ( preferences . getString ( " wifiPreset " , defaultHigh ) , defaultHigh ) ;
roamingPreset = new Preset ( preferences . getString ( " roamingPreset " , defaultLow ) , defaultLow ) ;
2019-03-03 21:40:48 +01:00
currentMobilePreset = preferences . getInt ( " currentMobilePreset " , 3 ) ;
currentWifiPreset = preferences . getInt ( " currentWifiPreset " , 3 ) ;
currentRoamingPreset = preferences . getInt ( " currentRoamingPreset " , 3 ) ;
if ( ! newConfig ) {
preferences . edit ( ) . putBoolean ( " newConfig " , true ) . commit ( ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
} else {
2019-06-04 12:14:50 +02:00
int [ ] mobileDataDownloadMask = new int [ 4 ] ;
int [ ] wifiDownloadMask = new int [ 4 ] ;
int [ ] roamingDownloadMask = new int [ 4 ] ;
2022-06-21 04:51:00 +02:00
long [ ] mobileMaxFileSize = new long [ 7 ] ;
long [ ] wifiMaxFileSize = new long [ 7 ] ;
long [ ] roamingMaxFileSize = new long [ 7 ] ;
2019-03-03 21:40:48 +01:00
for ( int a = 0 ; a < 4 ; a + + ) {
String key = " mobileDataDownloadMask " + ( a = = 0 ? " " : a ) ;
if ( a = = 0 | | preferences . contains ( key ) ) {
mobileDataDownloadMask [ a ] = preferences . getInt ( key , AUTODOWNLOAD_TYPE_PHOTO | AUTODOWNLOAD_TYPE_VIDEO | AUTODOWNLOAD_TYPE_DOCUMENT ) ;
wifiDownloadMask [ a ] = preferences . getInt ( " wifiDownloadMask " + ( a = = 0 ? " " : a ) , AUTODOWNLOAD_TYPE_PHOTO | AUTODOWNLOAD_TYPE_VIDEO | AUTODOWNLOAD_TYPE_DOCUMENT ) ;
roamingDownloadMask [ a ] = preferences . getInt ( " roamingDownloadMask " + ( a = = 0 ? " " : a ) , AUTODOWNLOAD_TYPE_PHOTO ) ;
} else {
mobileDataDownloadMask [ a ] = mobileDataDownloadMask [ 0 ] ;
wifiDownloadMask [ a ] = wifiDownloadMask [ 0 ] ;
roamingDownloadMask [ a ] = roamingDownloadMask [ 0 ] ;
}
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
2022-06-21 04:51:00 +02:00
mobileMaxFileSize [ 2 ] = preferences . getLong ( " mobileMaxDownloadSize " + 2 , mediumPreset . sizes [ PRESET_SIZE_NUM_VIDEO ] ) ;
mobileMaxFileSize [ 3 ] = preferences . getLong ( " mobileMaxDownloadSize " + 3 , mediumPreset . sizes [ PRESET_SIZE_NUM_DOCUMENT ] ) ;
wifiMaxFileSize [ 2 ] = preferences . getLong ( " wifiMaxDownloadSize " + 2 , highPreset . sizes [ PRESET_SIZE_NUM_VIDEO ] ) ;
wifiMaxFileSize [ 3 ] = preferences . getLong ( " wifiMaxDownloadSize " + 3 , highPreset . sizes [ PRESET_SIZE_NUM_DOCUMENT ] ) ;
roamingMaxFileSize [ 2 ] = preferences . getLong ( " roamingMaxDownloadSize " + 2 , lowPreset . sizes [ PRESET_SIZE_NUM_VIDEO ] ) ;
roamingMaxFileSize [ 3 ] = preferences . getLong ( " roamingMaxDownloadSize " + 3 , lowPreset . sizes [ PRESET_SIZE_NUM_DOCUMENT ] ) ;
2019-03-03 21:40:48 +01:00
boolean globalAutodownloadEnabled = preferences . getBoolean ( " globalAutodownloadEnabled " , true ) ;
2019-12-31 14:08:08 +01:00
mobilePreset = new Preset ( mobileDataDownloadMask , mediumPreset . sizes [ PRESET_SIZE_NUM_PHOTO ] , mobileMaxFileSize [ 2 ] , mobileMaxFileSize [ 3 ] , true , true , globalAutodownloadEnabled , false , 100 ) ;
wifiPreset = new Preset ( wifiDownloadMask , highPreset . sizes [ PRESET_SIZE_NUM_PHOTO ] , wifiMaxFileSize [ 2 ] , wifiMaxFileSize [ 3 ] , true , true , globalAutodownloadEnabled , false , 100 ) ;
roamingPreset = new Preset ( roamingDownloadMask , lowPreset . sizes [ PRESET_SIZE_NUM_PHOTO ] , roamingMaxFileSize [ 2 ] , roamingMaxFileSize [ 3 ] , false , false , globalAutodownloadEnabled , true , 50 ) ;
2019-03-03 21:40:48 +01:00
SharedPreferences . Editor editor = preferences . edit ( ) ;
editor . putBoolean ( " newConfig " , true ) ;
editor . putString ( " mobilePreset " , mobilePreset . toString ( ) ) ;
editor . putString ( " wifiPreset " , wifiPreset . toString ( ) ) ;
editor . putString ( " roamingPreset " , roamingPreset . toString ( ) ) ;
editor . putInt ( " currentMobilePreset " , currentMobilePreset = 3 ) ;
editor . putInt ( " currentWifiPreset " , currentWifiPreset = 3 ) ;
editor . putInt ( " currentRoamingPreset " , currentRoamingPreset = 3 ) ;
editor . commit ( ) ;
2018-07-30 04:07:02 +02:00
}
2019-01-23 18:03:33 +01:00
AndroidUtilities . runOnUIThread ( ( ) - > {
2021-06-25 02:43:10 +02:00
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . fileLoadFailed ) ;
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . fileLoaded ) ;
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . fileLoadProgressChanged ) ;
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . fileUploadProgressChanged ) ;
2019-07-18 15:01:39 +02:00
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . httpFileDidLoad ) ;
getNotificationCenter ( ) . addObserver ( DownloadController . this , NotificationCenter . httpFileDidFailedLoad ) ;
2019-03-03 21:40:48 +01:00
loadAutoDownloadConfig ( false ) ;
2018-07-30 04:07:02 +02:00
} ) ;
BroadcastReceiver networkStateReceiver = new BroadcastReceiver ( ) {
@Override
public void onReceive ( Context context , Intent intent ) {
checkAutodownloadSettings ( ) ;
}
} ;
IntentFilter filter = new IntentFilter ( ConnectivityManager . CONNECTIVITY_ACTION ) ;
ApplicationLoader . applicationContext . registerReceiver ( networkStateReceiver , filter ) ;
2019-07-18 15:01:39 +02:00
if ( getUserConfig ( ) . isClientActivated ( ) ) {
2018-07-30 04:07:02 +02:00
checkAutodownloadSettings ( ) ;
}
}
2019-03-03 21:40:48 +01:00
public void loadAutoDownloadConfig ( boolean force ) {
2019-07-18 15:01:39 +02:00
if ( loadingAutoDownloadConfig | | ! force & & Math . abs ( System . currentTimeMillis ( ) - getUserConfig ( ) . autoDownloadConfigLoadTime ) < 24 * 60 * 60 * 1000 ) {
2019-03-03 21:40:48 +01:00
return ;
}
loadingAutoDownloadConfig = true ;
TLRPC . TL_account_getAutoDownloadSettings req = new TLRPC . TL_account_getAutoDownloadSettings ( ) ;
2019-07-18 15:01:39 +02:00
getConnectionsManager ( ) . sendRequest ( req , ( response , error ) - > AndroidUtilities . runOnUIThread ( ( ) - > {
2019-03-03 21:40:48 +01:00
loadingAutoDownloadConfig = false ;
2019-07-18 15:01:39 +02:00
getUserConfig ( ) . autoDownloadConfigLoadTime = System . currentTimeMillis ( ) ;
getUserConfig ( ) . saveConfig ( false ) ;
2019-03-03 21:40:48 +01:00
if ( response ! = null ) {
TLRPC . TL_account_autoDownloadSettings res = ( TLRPC . TL_account_autoDownloadSettings ) response ;
lowPreset . set ( res . low ) ;
mediumPreset . set ( res . medium ) ;
highPreset . set ( res . high ) ;
for ( int a = 0 ; a < 3 ; a + + ) {
Preset preset ;
if ( a = = 0 ) {
preset = mobilePreset ;
} else if ( a = = 1 ) {
preset = wifiPreset ;
} else {
preset = roamingPreset ;
}
if ( preset . equals ( lowPreset ) ) {
preset . set ( res . low ) ;
} else if ( preset . equals ( mediumPreset ) ) {
preset . set ( res . medium ) ;
} else if ( preset . equals ( highPreset ) ) {
preset . set ( res . high ) ;
}
}
SharedPreferences . Editor editor = MessagesController . getMainSettings ( currentAccount ) . edit ( ) ;
editor . putString ( " mobilePreset " , mobilePreset . toString ( ) ) ;
editor . putString ( " wifiPreset " , wifiPreset . toString ( ) ) ;
editor . putString ( " roamingPreset " , roamingPreset . toString ( ) ) ;
editor . putString ( " preset0 " , lowPreset . toString ( ) ) ;
editor . putString ( " preset1 " , mediumPreset . toString ( ) ) ;
editor . putString ( " preset2 " , highPreset . toString ( ) ) ;
editor . commit ( ) ;
String str1 = lowPreset . toString ( ) ;
String str2 = mediumPreset . toString ( ) ;
String str3 = highPreset . toString ( ) ;
checkAutodownloadSettings ( ) ;
}
} ) ) ;
}
public Preset getCurrentMobilePreset ( ) {
if ( currentMobilePreset = = 0 ) {
return lowPreset ;
} else if ( currentMobilePreset = = 1 ) {
return mediumPreset ;
} else if ( currentMobilePreset = = 2 ) {
return highPreset ;
} else {
return mobilePreset ;
}
}
public Preset getCurrentWiFiPreset ( ) {
if ( currentWifiPreset = = 0 ) {
return lowPreset ;
} else if ( currentWifiPreset = = 1 ) {
return mediumPreset ;
} else if ( currentWifiPreset = = 2 ) {
return highPreset ;
} else {
return wifiPreset ;
}
}
public Preset getCurrentRoamingPreset ( ) {
if ( currentRoamingPreset = = 0 ) {
return lowPreset ;
} else if ( currentRoamingPreset = = 1 ) {
return mediumPreset ;
} else if ( currentRoamingPreset = = 2 ) {
return highPreset ;
} else {
return roamingPreset ;
}
}
public static int typeToIndex ( int type ) {
if ( type = = AUTODOWNLOAD_TYPE_PHOTO ) {
return PRESET_SIZE_NUM_PHOTO ;
} else if ( type = = AUTODOWNLOAD_TYPE_AUDIO ) {
2021-04-14 03:44:46 +02:00
return PRESET_SIZE_NUM_DOCUMENT ;
2019-03-03 21:40:48 +01:00
} else if ( type = = AUTODOWNLOAD_TYPE_VIDEO ) {
return PRESET_SIZE_NUM_VIDEO ;
} else if ( type = = AUTODOWNLOAD_TYPE_DOCUMENT ) {
return PRESET_SIZE_NUM_DOCUMENT ;
}
return PRESET_SIZE_NUM_PHOTO ;
2018-07-30 04:07:02 +02:00
}
public void cleanup ( ) {
photoDownloadQueue . clear ( ) ;
audioDownloadQueue . clear ( ) ;
documentDownloadQueue . clear ( ) ;
videoDownloadQueue . clear ( ) ;
downloadQueueKeys . clear ( ) ;
2020-07-26 10:03:38 +02:00
downloadQueuePairs . clear ( ) ;
2018-07-30 04:07:02 +02:00
typingTimes . clear ( ) ;
}
2019-12-31 14:08:08 +01:00
public int getMaxVideoBitrate ( ) {
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
return getCurrentWiFiPreset ( ) . maxVideoBitrate ;
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
return getCurrentRoamingPreset ( ) . maxVideoBitrate ;
} else {
return getCurrentMobilePreset ( ) . maxVideoBitrate ;
}
}
2019-01-23 18:03:33 +01:00
public int getAutodownloadMask ( ) {
2018-07-30 04:07:02 +02:00
int result = 0 ;
2019-06-04 12:14:50 +02:00
int [ ] masksArray ;
2019-12-31 14:08:08 +01:00
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
2019-03-03 21:40:48 +01:00
if ( ! wifiPreset . enabled ) {
return 0 ;
}
masksArray = getCurrentWiFiPreset ( ) . mask ;
2019-12-31 14:08:08 +01:00
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
2019-03-03 21:40:48 +01:00
if ( ! roamingPreset . enabled ) {
return 0 ;
}
masksArray = getCurrentRoamingPreset ( ) . mask ;
2018-07-30 04:07:02 +02:00
} else {
2019-03-03 21:40:48 +01:00
if ( ! mobilePreset . enabled ) {
return 0 ;
}
masksArray = getCurrentMobilePreset ( ) . mask ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
for ( int a = 0 ; a < masksArray . length ; a + + ) {
2018-07-30 04:07:02 +02:00
int mask = 0 ;
2019-03-03 21:40:48 +01:00
if ( ( masksArray [ a ] & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_PHOTO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( masksArray [ a ] & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_AUDIO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( masksArray [ a ] & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_VIDEO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( masksArray [ a ] & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_DOCUMENT ;
2018-07-30 04:07:02 +02:00
}
result | = mask < < ( a * 8 ) ;
}
return result ;
}
protected int getAutodownloadMaskAll ( ) {
2019-03-03 21:40:48 +01:00
if ( ! mobilePreset . enabled & & ! roamingPreset . enabled & & ! wifiPreset . enabled ) {
2018-07-30 04:07:02 +02:00
return 0 ;
}
int mask = 0 ;
for ( int a = 0 ; a < 4 ; a + + ) {
2019-03-03 21:40:48 +01:00
if ( ( getCurrentMobilePreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 | | ( getCurrentWiFiPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 | | ( getCurrentRoamingPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_PHOTO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( getCurrentMobilePreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 | | ( getCurrentWiFiPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 | | ( getCurrentRoamingPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_AUDIO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( getCurrentMobilePreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 | | ( getCurrentWiFiPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 | | ( getCurrentRoamingPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_VIDEO ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( getCurrentMobilePreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 | | ( getCurrentWiFiPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 | | ( getCurrentRoamingPreset ( ) . mask [ a ] & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 ) {
mask | = AUTODOWNLOAD_TYPE_DOCUMENT ;
2018-07-30 04:07:02 +02:00
}
}
return mask ;
}
public void checkAutodownloadSettings ( ) {
int currentMask = getCurrentDownloadMask ( ) ;
if ( currentMask = = lastCheckMask ) {
return ;
}
lastCheckMask = currentMask ;
2019-03-03 21:40:48 +01:00
if ( ( currentMask & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 ) {
2018-07-30 04:07:02 +02:00
if ( photoDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_PHOTO ) ;
2018-07-30 04:07:02 +02:00
}
} else {
for ( int a = 0 ; a < photoDownloadQueue . size ( ) ; a + + ) {
DownloadObject downloadObject = photoDownloadQueue . get ( a ) ;
2019-05-14 14:08:05 +02:00
if ( downloadObject . object instanceof TLRPC . Photo ) {
TLRPC . Photo photo = ( TLRPC . Photo ) downloadObject . object ;
TLRPC . PhotoSize photoSize = FileLoader . getClosestPhotoSizeWithSize ( photo . sizes , AndroidUtilities . getPhotoSize ( ) ) ;
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . cancelLoadFile ( photoSize ) ;
2019-01-23 18:03:33 +01:00
} else if ( downloadObject . object instanceof TLRPC . Document ) {
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . cancelLoadFile ( ( TLRPC . Document ) downloadObject . object ) ;
2019-01-23 18:03:33 +01:00
}
2018-07-30 04:07:02 +02:00
}
photoDownloadQueue . clear ( ) ;
}
2019-03-03 21:40:48 +01:00
if ( ( currentMask & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 ) {
2018-07-30 04:07:02 +02:00
if ( audioDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_AUDIO ) ;
2018-07-30 04:07:02 +02:00
}
} else {
for ( int a = 0 ; a < audioDownloadQueue . size ( ) ; a + + ) {
DownloadObject downloadObject = audioDownloadQueue . get ( a ) ;
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . cancelLoadFile ( ( TLRPC . Document ) downloadObject . object ) ;
2018-07-30 04:07:02 +02:00
}
audioDownloadQueue . clear ( ) ;
}
2019-03-03 21:40:48 +01:00
if ( ( currentMask & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 ) {
2018-07-30 04:07:02 +02:00
if ( documentDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_DOCUMENT ) ;
2018-07-30 04:07:02 +02:00
}
} else {
for ( int a = 0 ; a < documentDownloadQueue . size ( ) ; a + + ) {
DownloadObject downloadObject = documentDownloadQueue . get ( a ) ;
TLRPC . Document document = ( TLRPC . Document ) downloadObject . object ;
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . cancelLoadFile ( document ) ;
2018-07-30 04:07:02 +02:00
}
documentDownloadQueue . clear ( ) ;
}
2019-03-03 21:40:48 +01:00
if ( ( currentMask & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 ) {
2018-07-30 04:07:02 +02:00
if ( videoDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_VIDEO ) ;
2018-07-30 04:07:02 +02:00
}
} else {
for ( int a = 0 ; a < videoDownloadQueue . size ( ) ; a + + ) {
DownloadObject downloadObject = videoDownloadQueue . get ( a ) ;
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . cancelLoadFile ( ( TLRPC . Document ) downloadObject . object ) ;
2018-07-30 04:07:02 +02:00
}
videoDownloadQueue . clear ( ) ;
}
int mask = getAutodownloadMaskAll ( ) ;
if ( mask = = 0 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . clearDownloadQueue ( 0 ) ;
2018-07-30 04:07:02 +02:00
} else {
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_PHOTO ) = = 0 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . clearDownloadQueue ( AUTODOWNLOAD_TYPE_PHOTO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_AUDIO ) = = 0 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . clearDownloadQueue ( AUTODOWNLOAD_TYPE_AUDIO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_VIDEO ) = = 0 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . clearDownloadQueue ( AUTODOWNLOAD_TYPE_VIDEO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_DOCUMENT ) = = 0 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . clearDownloadQueue ( AUTODOWNLOAD_TYPE_DOCUMENT ) ;
2018-07-30 04:07:02 +02:00
}
}
}
public boolean canDownloadMedia ( MessageObject messageObject ) {
2021-03-05 09:38:58 +01:00
if ( messageObject . getDocument ( ) ! = null ) {
String documentName = messageObject . getDocument ( ) . file_name ;
2021-03-09 11:32:48 +01:00
if ( StrUtil . isNotBlank ( documentName ) ) {
2022-01-17 08:45:37 +01:00
if ( ( NekoConfig . disableAutoDownloadingWin32Executable . Bool ( ) & &
2021-03-09 11:32:48 +01:00
documentName . toLowerCase ( ) . matches ( " .* \\ .(cmd|bat|com|exe|lnk|msi|ps1|reg|vb|vbe|vbs|vbscript) " )
2022-01-17 08:45:37 +01:00
) | | ( NekoConfig . disableAutoDownloadingArchive . Bool ( ) & &
2021-03-09 11:32:48 +01:00
documentName . toLowerCase ( ) . matches ( " .* \\ .(apk|zip|7z|tar|gz|zst|iso|xz|lha|lzh) " )
)
) return false ;
2021-03-05 09:38:58 +01:00
}
}
2019-03-03 21:40:48 +01:00
return canDownloadMedia ( messageObject . messageOwner ) = = 1 ;
2018-07-30 04:07:02 +02:00
}
2022-06-21 04:51:00 +02:00
public boolean canDownloadMedia ( int type , long size ) {
2019-06-04 12:14:50 +02:00
Preset preset ;
2019-12-31 14:08:08 +01:00
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
2019-06-04 12:14:50 +02:00
if ( ! wifiPreset . enabled ) {
return false ;
}
preset = getCurrentWiFiPreset ( ) ;
2019-12-31 14:08:08 +01:00
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
2019-06-04 12:14:50 +02:00
if ( ! roamingPreset . enabled ) {
return false ;
}
preset = getCurrentRoamingPreset ( ) ;
} else {
if ( ! mobilePreset . enabled ) {
return false ;
}
preset = getCurrentMobilePreset ( ) ;
}
int mask = preset . mask [ 1 ] ;
2022-06-21 04:51:00 +02:00
long maxSize = preset . sizes [ typeToIndex ( type ) ] ;
2019-06-04 12:14:50 +02:00
return ( type = = AUTODOWNLOAD_TYPE_PHOTO | | size ! = 0 & & size < = maxSize ) & & ( type = = AUTODOWNLOAD_TYPE_AUDIO | | ( mask & type ) ! = 0 ) ;
}
2019-03-03 21:40:48 +01:00
public int canDownloadMedia ( TLRPC . Message message ) {
2019-05-14 14:08:05 +02:00
if ( message = = null ) {
return 0 ;
}
2018-07-30 04:07:02 +02:00
int type ;
2019-03-03 21:40:48 +01:00
boolean isVideo ;
2019-05-14 14:08:05 +02:00
if ( ( isVideo = MessageObject . isVideoMessage ( message ) ) | | MessageObject . isGifMessage ( message ) | | MessageObject . isRoundVideoMessage ( message ) | | MessageObject . isGameMessage ( message ) ) {
2019-03-03 21:40:48 +01:00
type = AUTODOWNLOAD_TYPE_VIDEO ;
2018-07-30 04:07:02 +02:00
} else if ( MessageObject . isVoiceMessage ( message ) ) {
2019-03-03 21:40:48 +01:00
type = AUTODOWNLOAD_TYPE_AUDIO ;
2019-07-18 15:01:39 +02:00
} else if ( MessageObject . isPhoto ( message ) | | MessageObject . isStickerMessage ( message ) | | MessageObject . isAnimatedStickerMessage ( message ) ) {
2019-03-03 21:40:48 +01:00
type = AUTODOWNLOAD_TYPE_PHOTO ;
2019-05-14 14:08:05 +02:00
} else if ( MessageObject . getDocument ( message ) ! = null ) {
2019-03-03 21:40:48 +01:00
type = AUTODOWNLOAD_TYPE_DOCUMENT ;
2019-05-14 14:08:05 +02:00
} else {
return 0 ;
2018-07-30 04:07:02 +02:00
}
int index ;
2020-09-30 15:48:47 +02:00
TLRPC . Peer peer = message . peer_id ;
2018-07-30 04:07:02 +02:00
if ( peer ! = null ) {
if ( peer . user_id ! = 0 ) {
2019-07-18 15:01:39 +02:00
if ( getContactsController ( ) . contactsDict . containsKey ( peer . user_id ) ) {
2018-07-30 04:07:02 +02:00
index = 0 ;
} else {
index = 1 ;
}
} else if ( peer . chat_id ! = 0 ) {
2020-09-30 15:48:47 +02:00
if ( message . from_id instanceof TLRPC . TL_peerUser & & getContactsController ( ) . contactsDict . containsKey ( message . from_id . user_id ) ) {
2019-03-03 21:40:48 +01:00
index = 0 ;
} else {
index = 2 ;
}
2018-07-30 04:07:02 +02:00
} else {
2021-09-20 00:10:42 +02:00
TLRPC . Chat chat = message . peer_id . channel_id ! = 0 ? getMessagesController ( ) . getChat ( message . peer_id . channel_id ) : null ;
2020-12-23 08:48:30 +01:00
if ( ChatObject . isChannel ( chat ) & & chat . megagroup ) {
2020-09-30 15:48:47 +02:00
if ( message . from_id instanceof TLRPC . TL_peerUser & & getContactsController ( ) . contactsDict . containsKey ( message . from_id . user_id ) ) {
2019-03-03 21:40:48 +01:00
index = 0 ;
} else {
index = 2 ;
}
2018-07-30 04:07:02 +02:00
} else {
index = 3 ;
}
}
} else {
index = 1 ;
}
2019-03-03 21:40:48 +01:00
Preset preset ;
2019-12-31 14:08:08 +01:00
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
2019-03-03 21:40:48 +01:00
if ( ! wifiPreset . enabled ) {
return 0 ;
}
preset = getCurrentWiFiPreset ( ) ;
2019-12-31 14:08:08 +01:00
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
2019-03-03 21:40:48 +01:00
if ( ! roamingPreset . enabled ) {
return 0 ;
}
preset = getCurrentRoamingPreset ( ) ;
} else {
if ( ! mobilePreset . enabled ) {
return 0 ;
}
preset = getCurrentMobilePreset ( ) ;
}
2019-06-04 12:14:50 +02:00
int mask = preset . mask [ index ] ;
2022-06-21 04:51:00 +02:00
long maxSize ;
2021-04-14 03:44:46 +02:00
if ( type = = AUTODOWNLOAD_TYPE_AUDIO ) {
maxSize = Math . max ( 512 * 1024 , preset . sizes [ typeToIndex ( type ) ] ) ;
} else {
maxSize = preset . sizes [ typeToIndex ( type ) ] ;
}
2022-06-21 04:51:00 +02:00
long size = MessageObject . getMessageSize ( message ) ;
2019-03-03 21:40:48 +01:00
if ( isVideo & & preset . preloadVideo & & size > maxSize & & maxSize > 2 * 1024 * 1024 ) {
return ( mask & type ) ! = 0 ? 2 : 0 ;
} else {
return ( type = = AUTODOWNLOAD_TYPE_PHOTO | | size ! = 0 & & size < = maxSize ) & & ( type = = AUTODOWNLOAD_TYPE_AUDIO | | ( mask & type ) ! = 0 ) ? 1 : 0 ;
}
}
protected boolean canDownloadNextTrack ( ) {
2019-12-31 14:08:08 +01:00
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
2019-03-03 21:40:48 +01:00
return wifiPreset . enabled & & getCurrentWiFiPreset ( ) . preloadMusic ;
2019-12-31 14:08:08 +01:00
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
2019-03-03 21:40:48 +01:00
return roamingPreset . enabled & & getCurrentRoamingPreset ( ) . preloadMusic ;
2018-07-30 04:07:02 +02:00
} else {
2019-03-03 21:40:48 +01:00
return mobilePreset . enabled & & getCurrentMobilePreset ( ) . preloadMusic ;
2018-07-30 04:07:02 +02:00
}
}
2019-06-04 12:14:50 +02:00
public int getCurrentDownloadMask ( ) {
2019-12-31 14:08:08 +01:00
int networkType = ApplicationLoader . getAutodownloadNetworkType ( ) ;
if ( networkType = = StatsController . TYPE_WIFI ) {
2019-03-03 21:40:48 +01:00
if ( ! wifiPreset . enabled ) {
return 0 ;
}
2018-07-30 04:07:02 +02:00
int mask = 0 ;
for ( int a = 0 ; a < 4 ; a + + ) {
2019-03-03 21:40:48 +01:00
mask | = getCurrentWiFiPreset ( ) . mask [ a ] ;
2018-07-30 04:07:02 +02:00
}
return mask ;
2019-12-31 14:08:08 +01:00
} else if ( networkType = = StatsController . TYPE_ROAMING ) {
2019-03-03 21:40:48 +01:00
if ( ! roamingPreset . enabled ) {
return 0 ;
}
2018-07-30 04:07:02 +02:00
int mask = 0 ;
for ( int a = 0 ; a < 4 ; a + + ) {
2019-03-03 21:40:48 +01:00
mask | = getCurrentRoamingPreset ( ) . mask [ a ] ;
2018-07-30 04:07:02 +02:00
}
return mask ;
} else {
2019-03-03 21:40:48 +01:00
if ( ! mobilePreset . enabled ) {
return 0 ;
}
2018-07-30 04:07:02 +02:00
int mask = 0 ;
for ( int a = 0 ; a < 4 ; a + + ) {
2019-03-03 21:40:48 +01:00
mask | = getCurrentMobilePreset ( ) . mask [ a ] ;
2018-07-30 04:07:02 +02:00
}
return mask ;
}
}
2019-03-03 21:40:48 +01:00
public void savePresetToServer ( int type ) {
TLRPC . TL_account_saveAutoDownloadSettings req = new TLRPC . TL_account_saveAutoDownloadSettings ( ) ;
Preset preset ;
boolean enabled ;
if ( type = = 0 ) {
preset = getCurrentMobilePreset ( ) ;
enabled = mobilePreset . enabled ;
} else if ( type = = 1 ) {
preset = getCurrentWiFiPreset ( ) ;
enabled = wifiPreset . enabled ;
} else {
preset = getCurrentRoamingPreset ( ) ;
enabled = roamingPreset . enabled ;
}
req . settings = new TLRPC . TL_autoDownloadSettings ( ) ;
req . settings . audio_preload_next = preset . preloadMusic ;
req . settings . video_preload_large = preset . preloadVideo ;
req . settings . phonecalls_less_data = preset . lessCallData ;
2019-12-31 14:08:08 +01:00
req . settings . video_upload_maxbitrate = preset . maxVideoBitrate ;
2019-03-03 21:40:48 +01:00
req . settings . disabled = ! enabled ;
boolean photo = false ;
boolean video = false ;
boolean document = false ;
for ( int a = 0 ; a < preset . mask . length ; a + + ) {
if ( ( preset . mask [ a ] & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 ) {
photo = true ;
}
if ( ( preset . mask [ a ] & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 ) {
video = true ;
}
if ( ( preset . mask [ a ] & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 ) {
document = true ;
}
if ( photo & & video & & document ) {
break ;
}
}
2022-06-21 04:51:00 +02:00
req . settings . photo_size_max = photo ? ( int ) preset . sizes [ PRESET_SIZE_NUM_PHOTO ] : 0 ;
2019-03-03 21:40:48 +01:00
req . settings . video_size_max = video ? preset . sizes [ PRESET_SIZE_NUM_VIDEO ] : 0 ;
req . settings . file_size_max = document ? preset . sizes [ PRESET_SIZE_NUM_DOCUMENT ] : 0 ;
2019-07-18 15:01:39 +02:00
getConnectionsManager ( ) . sendRequest ( req , ( response , error ) - > {
2019-03-03 21:40:48 +01:00
} ) ;
}
2020-07-26 10:03:38 +02:00
protected void cancelDownloading ( ArrayList < Pair < Long , Integer > > arrayList ) {
for ( int a = 0 , N = arrayList . size ( ) ; a < N ; a + + ) {
Pair < Long , Integer > pair = arrayList . get ( a ) ;
DownloadObject downloadObject = downloadQueuePairs . get ( pair ) ;
if ( downloadObject = = null ) {
continue ;
}
if ( downloadObject . object instanceof TLRPC . Document ) {
TLRPC . Document document = ( TLRPC . Document ) downloadObject . object ;
2020-10-30 11:26:29 +01:00
getFileLoader ( ) . cancelLoadFile ( document , true ) ;
2020-07-26 10:03:38 +02:00
} else if ( downloadObject . object instanceof TLRPC . Photo ) {
TLRPC . Photo photo = ( TLRPC . Photo ) downloadObject . object ;
TLRPC . PhotoSize photoSize = FileLoader . getClosestPhotoSizeWithSize ( photo . sizes , AndroidUtilities . getPhotoSize ( ) ) ;
if ( photoSize ! = null ) {
2020-10-30 11:26:29 +01:00
getFileLoader ( ) . cancelLoadFile ( photoSize , true ) ;
2020-07-26 10:03:38 +02:00
}
}
}
}
2018-07-30 04:07:02 +02:00
protected void processDownloadObjects ( int type , ArrayList < DownloadObject > objects ) {
if ( objects . isEmpty ( ) ) {
return ;
}
2020-10-30 11:26:29 +01:00
ArrayList < DownloadObject > queue ;
2019-03-03 21:40:48 +01:00
if ( type = = AUTODOWNLOAD_TYPE_PHOTO ) {
2018-07-30 04:07:02 +02:00
queue = photoDownloadQueue ;
2019-03-03 21:40:48 +01:00
} else if ( type = = AUTODOWNLOAD_TYPE_AUDIO ) {
2018-07-30 04:07:02 +02:00
queue = audioDownloadQueue ;
2019-03-03 21:40:48 +01:00
} else if ( type = = AUTODOWNLOAD_TYPE_VIDEO ) {
2018-07-30 04:07:02 +02:00
queue = videoDownloadQueue ;
2020-10-30 11:26:29 +01:00
} else {
2018-07-30 04:07:02 +02:00
queue = documentDownloadQueue ;
}
for ( int a = 0 ; a < objects . size ( ) ; a + + ) {
DownloadObject downloadObject = objects . get ( a ) ;
String path ;
2019-05-14 14:08:05 +02:00
TLRPC . PhotoSize photoSize = null ;
2018-07-30 04:07:02 +02:00
if ( downloadObject . object instanceof TLRPC . Document ) {
TLRPC . Document document = ( TLRPC . Document ) downloadObject . object ;
path = FileLoader . getAttachFileName ( document ) ;
2019-05-14 14:08:05 +02:00
} else if ( downloadObject . object instanceof TLRPC . Photo ) {
TLRPC . Photo photo = ( TLRPC . Photo ) downloadObject . object ;
photoSize = FileLoader . getClosestPhotoSizeWithSize ( photo . sizes , AndroidUtilities . getPhotoSize ( ) ) ;
2019-12-31 14:08:08 +01:00
path = FileLoader . getAttachFileName ( photoSize ) ;
2019-05-14 14:08:05 +02:00
} else {
path = null ;
2018-07-30 04:07:02 +02:00
}
2019-05-14 14:08:05 +02:00
if ( path = = null | | downloadQueueKeys . containsKey ( path ) ) {
2018-07-30 04:07:02 +02:00
continue ;
}
boolean added = true ;
2019-05-14 14:08:05 +02:00
if ( photoSize ! = null ) {
TLRPC . Photo photo = ( TLRPC . Photo ) downloadObject . object ;
int cacheType ;
if ( downloadObject . secret ) {
cacheType = 2 ;
} else if ( downloadObject . forceCache ) {
cacheType = 1 ;
} else {
cacheType = 0 ;
}
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . loadFile ( ImageLocation . getForPhoto ( photoSize , photo ) , downloadObject . parent , null , 0 , cacheType ) ;
2018-07-30 04:07:02 +02:00
} else if ( downloadObject . object instanceof TLRPC . Document ) {
TLRPC . Document document = ( TLRPC . Document ) downloadObject . object ;
2019-07-18 15:01:39 +02:00
getFileLoader ( ) . loadFile ( document , downloadObject . parent , 0 , downloadObject . secret ? 2 : 0 ) ;
2018-07-30 04:07:02 +02:00
} else {
added = false ;
}
if ( added ) {
queue . add ( downloadObject ) ;
downloadQueueKeys . put ( path , downloadObject ) ;
2020-07-26 10:03:38 +02:00
downloadQueuePairs . put ( new Pair < > ( downloadObject . id , downloadObject . type ) , downloadObject ) ;
2018-07-30 04:07:02 +02:00
}
}
}
protected void newDownloadObjectsAvailable ( int downloadMask ) {
int mask = getCurrentDownloadMask ( ) ;
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 & & ( downloadMask & AUTODOWNLOAD_TYPE_PHOTO ) ! = 0 & & photoDownloadQueue . isEmpty ( ) ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . getDownloadQueue ( AUTODOWNLOAD_TYPE_PHOTO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 & & ( downloadMask & AUTODOWNLOAD_TYPE_AUDIO ) ! = 0 & & audioDownloadQueue . isEmpty ( ) ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . getDownloadQueue ( AUTODOWNLOAD_TYPE_AUDIO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 & & ( downloadMask & AUTODOWNLOAD_TYPE_VIDEO ) ! = 0 & & videoDownloadQueue . isEmpty ( ) ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . getDownloadQueue ( AUTODOWNLOAD_TYPE_VIDEO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( ( mask & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 & & ( downloadMask & AUTODOWNLOAD_TYPE_DOCUMENT ) ! = 0 & & documentDownloadQueue . isEmpty ( ) ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . getDownloadQueue ( AUTODOWNLOAD_TYPE_DOCUMENT ) ;
2018-07-30 04:07:02 +02:00
}
}
private void checkDownloadFinished ( String fileName , int state ) {
DownloadObject downloadObject = downloadQueueKeys . get ( fileName ) ;
if ( downloadObject ! = null ) {
downloadQueueKeys . remove ( fileName ) ;
2020-07-26 10:03:38 +02:00
downloadQueuePairs . remove ( new Pair < > ( downloadObject . id , downloadObject . type ) ) ;
2018-07-30 04:07:02 +02:00
if ( state = = 0 | | state = = 2 ) {
2019-07-18 15:01:39 +02:00
getMessagesStorage ( ) . removeFromDownloadQueue ( downloadObject . id , downloadObject . type , false /*state != 0*/ ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
if ( downloadObject . type = = AUTODOWNLOAD_TYPE_PHOTO ) {
2018-07-30 04:07:02 +02:00
photoDownloadQueue . remove ( downloadObject ) ;
if ( photoDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_PHOTO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
} else if ( downloadObject . type = = AUTODOWNLOAD_TYPE_AUDIO ) {
2018-07-30 04:07:02 +02:00
audioDownloadQueue . remove ( downloadObject ) ;
if ( audioDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_AUDIO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
} else if ( downloadObject . type = = AUTODOWNLOAD_TYPE_VIDEO ) {
2018-07-30 04:07:02 +02:00
videoDownloadQueue . remove ( downloadObject ) ;
if ( videoDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_VIDEO ) ;
2018-07-30 04:07:02 +02:00
}
2019-03-03 21:40:48 +01:00
} else if ( downloadObject . type = = AUTODOWNLOAD_TYPE_DOCUMENT ) {
2018-07-30 04:07:02 +02:00
documentDownloadQueue . remove ( downloadObject ) ;
if ( documentDownloadQueue . isEmpty ( ) ) {
2019-03-03 21:40:48 +01:00
newDownloadObjectsAvailable ( AUTODOWNLOAD_TYPE_DOCUMENT ) ;
2018-07-30 04:07:02 +02:00
}
}
}
}
public int generateObserverTag ( ) {
return lastTag + + ;
}
public void addLoadingFileObserver ( String fileName , FileDownloadProgressListener observer ) {
addLoadingFileObserver ( fileName , null , observer ) ;
}
public void addLoadingFileObserver ( String fileName , MessageObject messageObject , FileDownloadProgressListener observer ) {
if ( listenerInProgress ) {
addLaterArray . put ( fileName , observer ) ;
return ;
}
removeLoadingFileObserver ( observer ) ;
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList = = null ) {
arrayList = new ArrayList < > ( ) ;
loadingFileObservers . put ( fileName , arrayList ) ;
}
arrayList . add ( new WeakReference < > ( observer ) ) ;
if ( messageObject ! = null ) {
ArrayList < MessageObject > messageObjects = loadingFileMessagesObservers . get ( fileName ) ;
if ( messageObjects = = null ) {
messageObjects = new ArrayList < > ( ) ;
loadingFileMessagesObservers . put ( fileName , messageObjects ) ;
}
messageObjects . add ( messageObject ) ;
}
observersByTag . put ( observer . getObserverTag ( ) , fileName ) ;
}
public void removeLoadingFileObserver ( FileDownloadProgressListener observer ) {
if ( listenerInProgress ) {
deleteLaterArray . add ( observer ) ;
return ;
}
String fileName = observersByTag . get ( observer . getObserverTag ( ) ) ;
if ( fileName ! = null ) {
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList ! = null ) {
for ( int a = 0 ; a < arrayList . size ( ) ; a + + ) {
WeakReference < FileDownloadProgressListener > reference = arrayList . get ( a ) ;
if ( reference . get ( ) = = null | | reference . get ( ) = = observer ) {
arrayList . remove ( a ) ;
a - - ;
}
}
if ( arrayList . isEmpty ( ) ) {
loadingFileObservers . remove ( fileName ) ;
}
}
observersByTag . remove ( observer . getObserverTag ( ) ) ;
}
}
private void processLaterArrays ( ) {
for ( HashMap . Entry < String , FileDownloadProgressListener > listener : addLaterArray . entrySet ( ) ) {
addLoadingFileObserver ( listener . getKey ( ) , listener . getValue ( ) ) ;
}
addLaterArray . clear ( ) ;
for ( FileDownloadProgressListener listener : deleteLaterArray ) {
removeLoadingFileObserver ( listener ) ;
}
deleteLaterArray . clear ( ) ;
}
@Override
public void didReceivedNotification ( int id , int account , Object . . . args ) {
2021-06-25 02:43:10 +02:00
if ( id = = NotificationCenter . fileLoadFailed | | id = = NotificationCenter . httpFileDidFailedLoad ) {
2018-07-30 04:07:02 +02:00
String fileName = ( String ) args [ 0 ] ;
2019-01-23 18:03:33 +01:00
Integer canceled = ( Integer ) args [ 1 ] ;
listenerInProgress = true ;
2018-07-30 04:07:02 +02:00
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList ! = null ) {
for ( int a = 0 , size = arrayList . size ( ) ; a < size ; a + + ) {
WeakReference < FileDownloadProgressListener > reference = arrayList . get ( a ) ;
if ( reference . get ( ) ! = null ) {
2019-01-23 18:03:33 +01:00
reference . get ( ) . onFailedDownload ( fileName , canceled = = 1 ) ;
if ( canceled ! = 1 ) {
observersByTag . remove ( reference . get ( ) . getObserverTag ( ) ) ;
}
2018-07-30 04:07:02 +02:00
}
}
2019-01-23 18:03:33 +01:00
if ( canceled ! = 1 ) {
loadingFileObservers . remove ( fileName ) ;
}
2018-07-30 04:07:02 +02:00
}
listenerInProgress = false ;
processLaterArrays ( ) ;
2019-01-23 18:03:33 +01:00
checkDownloadFinished ( fileName , canceled ) ;
2021-06-25 02:43:10 +02:00
} else if ( id = = NotificationCenter . fileLoaded | | id = = NotificationCenter . httpFileDidLoad ) {
2018-07-30 04:07:02 +02:00
listenerInProgress = true ;
String fileName = ( String ) args [ 0 ] ;
ArrayList < MessageObject > messageObjects = loadingFileMessagesObservers . get ( fileName ) ;
if ( messageObjects ! = null ) {
for ( int a = 0 , size = messageObjects . size ( ) ; a < size ; a + + ) {
MessageObject messageObject = messageObjects . get ( a ) ;
messageObject . mediaExists = true ;
}
loadingFileMessagesObservers . remove ( fileName ) ;
}
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList ! = null ) {
for ( int a = 0 , size = arrayList . size ( ) ; a < size ; a + + ) {
WeakReference < FileDownloadProgressListener > reference = arrayList . get ( a ) ;
if ( reference . get ( ) ! = null ) {
reference . get ( ) . onSuccessDownload ( fileName ) ;
observersByTag . remove ( reference . get ( ) . getObserverTag ( ) ) ;
}
}
loadingFileObservers . remove ( fileName ) ;
}
listenerInProgress = false ;
processLaterArrays ( ) ;
checkDownloadFinished ( fileName , 0 ) ;
2021-06-25 02:43:10 +02:00
} else if ( id = = NotificationCenter . fileLoadProgressChanged ) {
2018-07-30 04:07:02 +02:00
listenerInProgress = true ;
String fileName = ( String ) args [ 0 ] ;
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList ! = null ) {
2020-01-23 07:15:40 +01:00
Long loadedSize = ( Long ) args [ 1 ] ;
Long totalSize = ( Long ) args [ 2 ] ;
2018-07-30 04:07:02 +02:00
for ( int a = 0 , size = arrayList . size ( ) ; a < size ; a + + ) {
WeakReference < FileDownloadProgressListener > reference = arrayList . get ( a ) ;
if ( reference . get ( ) ! = null ) {
2020-01-23 07:15:40 +01:00
reference . get ( ) . onProgressDownload ( fileName , loadedSize , totalSize ) ;
2018-07-30 04:07:02 +02:00
}
}
}
listenerInProgress = false ;
processLaterArrays ( ) ;
2021-06-25 02:43:10 +02:00
} else if ( id = = NotificationCenter . fileUploadProgressChanged ) {
2018-07-30 04:07:02 +02:00
listenerInProgress = true ;
String fileName = ( String ) args [ 0 ] ;
ArrayList < WeakReference < FileDownloadProgressListener > > arrayList = loadingFileObservers . get ( fileName ) ;
if ( arrayList ! = null ) {
2020-01-23 07:15:40 +01:00
Long loadedSize = ( Long ) args [ 1 ] ;
Long totalSize = ( Long ) args [ 2 ] ;
Boolean enc = ( Boolean ) args [ 3 ] ;
2018-07-30 04:07:02 +02:00
for ( int a = 0 , size = arrayList . size ( ) ; a < size ; a + + ) {
WeakReference < FileDownloadProgressListener > reference = arrayList . get ( a ) ;
if ( reference . get ( ) ! = null ) {
2020-01-23 07:15:40 +01:00
reference . get ( ) . onProgressUpload ( fileName , loadedSize , totalSize , enc ) ;
2018-07-30 04:07:02 +02:00
}
}
}
listenerInProgress = false ;
processLaterArrays ( ) ;
try {
2019-07-18 15:01:39 +02:00
ArrayList < SendMessagesHelper . DelayedMessage > delayedMessages = getSendMessagesHelper ( ) . getDelayedMessages ( fileName ) ;
2018-07-30 04:07:02 +02:00
if ( delayedMessages ! = null ) {
for ( int a = 0 ; a < delayedMessages . size ( ) ; a + + ) {
SendMessagesHelper . DelayedMessage delayedMessage = delayedMessages . get ( a ) ;
if ( delayedMessage . encryptedChat = = null ) {
2020-09-30 15:48:47 +02:00
long dialogId = delayedMessage . peer ;
int topMessageId = delayedMessage . topMessageId ;
Long lastTime = typingTimes . get ( dialogId ) ;
2018-07-30 04:07:02 +02:00
if ( delayedMessage . type = = 4 ) {
if ( lastTime = = null | | lastTime + 4000 < System . currentTimeMillis ( ) ) {
MessageObject messageObject = ( MessageObject ) delayedMessage . extraHashMap . get ( fileName + " _i " ) ;
if ( messageObject ! = null & & messageObject . isVideo ( ) ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 5 , 0 ) ;
2020-10-31 22:13:37 +01:00
} else if ( messageObject ! = null & & messageObject . getDocument ( ) ! = null ) {
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 3 , 0 ) ;
2018-07-30 04:07:02 +02:00
} else {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 4 , 0 ) ;
2018-07-30 04:07:02 +02:00
}
2020-09-30 15:48:47 +02:00
typingTimes . put ( dialogId , System . currentTimeMillis ( ) ) ;
2018-07-30 04:07:02 +02:00
}
} else {
TLRPC . Document document = delayedMessage . obj . getDocument ( ) ;
if ( lastTime = = null | | lastTime + 4000 < System . currentTimeMillis ( ) ) {
if ( delayedMessage . obj . isRoundVideo ( ) ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 8 , 0 ) ;
2018-07-30 04:07:02 +02:00
} else if ( delayedMessage . obj . isVideo ( ) ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 5 , 0 ) ;
2018-07-30 04:07:02 +02:00
} else if ( delayedMessage . obj . isVoice ( ) ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 9 , 0 ) ;
2018-07-30 04:07:02 +02:00
} else if ( delayedMessage . obj . getDocument ( ) ! = null ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 3 , 0 ) ;
2019-05-14 14:08:05 +02:00
} else if ( delayedMessage . photoSize ! = null ) {
2020-09-30 15:48:47 +02:00
getMessagesController ( ) . sendTyping ( dialogId , topMessageId , 4 , 0 ) ;
2018-07-30 04:07:02 +02:00
}
2020-09-30 15:48:47 +02:00
typingTimes . put ( dialogId , System . currentTimeMillis ( ) ) ;
2018-07-30 04:07:02 +02:00
}
}
}
}
}
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
}
}
2020-01-23 07:15:40 +01:00
public static float getProgress ( long [ ] progressSizes ) {
if ( progressSizes = = null | | progressSizes . length < 2 | | progressSizes [ 1 ] = = 0 ) {
return 0f ;
}
return Math . min ( 1f , progressSizes [ 0 ] / ( float ) progressSizes [ 1 ] ) ;
}
2022-03-20 21:22:37 +01:00
public void startDownloadFile ( TLRPC . Document document , MessageObject parentObject ) {
2022-04-16 16:43:17 +02:00
if ( parentObject . getDocument ( ) = = null ) {
return ;
}
2022-03-20 21:22:37 +01:00
AndroidUtilities . runOnUIThread ( ( ) - > {
boolean contains = false ;
for ( int i = 0 ; i < recentDownloadingFiles . size ( ) ; i + + ) {
2022-06-21 04:51:00 +02:00
if ( recentDownloadingFiles . get ( i ) . getDocument ( ) ! = null & & recentDownloadingFiles . get ( i ) . getDocument ( ) . id = = parentObject . getDocument ( ) . id ) {
2022-04-16 16:43:17 +02:00
contains = true ;
2022-03-20 21:22:37 +01:00
break ;
}
}
if ( ! contains ) {
for ( int i = 0 ; i < downloadingFiles . size ( ) ; i + + ) {
2022-06-21 04:51:00 +02:00
if ( downloadingFiles . get ( i ) . getDocument ( ) ! = null & & downloadingFiles . get ( i ) . getDocument ( ) . id = = parentObject . getDocument ( ) . id ) {
2022-03-20 21:22:37 +01:00
contains = true ;
break ;
}
}
}
if ( ! contains ) {
downloadingFiles . add ( parentObject ) ;
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
try {
NativeByteBuffer data = new NativeByteBuffer ( parentObject . messageOwner . getObjectSize ( ) ) ;
parentObject . messageOwner . serializeToStream ( data ) ;
SQLitePreparedStatement state = getMessagesStorage ( ) . getDatabase ( ) . executeFast ( " REPLACE INTO downloading_documents VALUES(?, ?, ?, ?, ?) " ) ;
state . bindByteBuffer ( 1 , data ) ;
state . bindInteger ( 2 , parentObject . getDocument ( ) . dc_id ) ;
state . bindLong ( 3 , parentObject . getDocument ( ) . id ) ;
state . bindLong ( 4 , System . currentTimeMillis ( ) ) ;
state . bindInteger ( 4 , 0 ) ;
state . step ( ) ;
state . dispose ( ) ;
data . reuse ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
} ) ;
}
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
} ) ;
}
public void onDownloadComplete ( MessageObject parentObject ) {
if ( parentObject = = null ) {
return ;
}
AndroidUtilities . runOnUIThread ( ( ) - > {
boolean removed = false ;
for ( int i = 0 ; i < downloadingFiles . size ( ) ; i + + ) {
if ( downloadingFiles . get ( i ) . getDocument ( ) . id = = parentObject . getDocument ( ) . id ) {
downloadingFiles . remove ( i ) ;
removed = true ;
break ;
}
}
if ( removed ) {
boolean contains = false ;
for ( int i = 0 ; i < recentDownloadingFiles . size ( ) ; i + + ) {
if ( recentDownloadingFiles . get ( i ) . getDocument ( ) . id = = parentObject . getDocument ( ) . id ) {
contains = true ;
break ;
}
}
if ( ! contains ) {
recentDownloadingFiles . add ( 0 , parentObject ) ;
putToUnviewedDownloads ( parentObject ) ;
}
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
2022-04-16 16:43:17 +02:00
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
try {
String req = String . format ( Locale . ENGLISH , " UPDATE downloading_documents SET state = 1, date = %d WHERE hash = %d AND id = %d " , System . currentTimeMillis ( ) , parentObject . getDocument ( ) . dc_id , parentObject . getDocument ( ) . id ) ;
getMessagesStorage ( ) . getDatabase ( ) . executeFast ( req ) . stepThis ( ) . dispose ( ) ;
SQLiteCursor cursor = getMessagesStorage ( ) . getDatabase ( ) . queryFinalized ( " SELECT COUNT(*) FROM downloading_documents WHERE state = 1 " ) ;
int count = 0 ;
if ( cursor . next ( ) ) {
count = cursor . intValue ( 0 ) ;
}
cursor . dispose ( ) ;
2022-03-20 21:22:37 +01:00
2022-04-16 16:43:17 +02:00
cursor = getMessagesStorage ( ) . getDatabase ( ) . queryFinalized ( " SELECT state FROM downloading_documents WHERE state = 1 " ) ;
if ( cursor . next ( ) ) {
int state = cursor . intValue ( 0 ) ;
}
cursor . dispose ( ) ;
int limitDownloadsDocuments = 100 ;
if ( count > limitDownloadsDocuments ) {
cursor = getMessagesStorage ( ) . getDatabase ( ) . queryFinalized ( " SELECT hash, id FROM downloading_documents WHERE state = 1 ORDER BY date ASC LIMIT " + ( limitDownloadsDocuments - count ) ) ;
ArrayList < DownloadingDocumentEntry > entriesToRemove = new ArrayList < > ( ) ;
while ( cursor . next ( ) ) {
DownloadingDocumentEntry entry = new DownloadingDocumentEntry ( ) ;
entry . hash = cursor . intValue ( 0 ) ;
entry . id = cursor . longValue ( 1 ) ;
entriesToRemove . add ( entry ) ;
}
cursor . dispose ( ) ;
SQLitePreparedStatement state = getMessagesStorage ( ) . getDatabase ( ) . executeFast ( " DELETE FROM downloading_documents WHERE hash = ? AND id = ? " ) ;
for ( int i = 0 ; i < entriesToRemove . size ( ) ; i + + ) {
state . requery ( ) ;
state . bindInteger ( 1 , entriesToRemove . get ( i ) . hash ) ;
state . bindLong ( 2 , entriesToRemove . get ( i ) . id ) ;
state . step ( ) ;
}
state . dispose ( ) ;
}
} catch ( Exception e ) {
FileLog . e ( e ) ;
2022-03-20 21:22:37 +01:00
}
2022-04-16 16:43:17 +02:00
} ) ;
2022-03-20 21:22:37 +01:00
}
} ) ;
2022-04-16 16:43:17 +02:00
2022-03-20 21:22:37 +01:00
}
public void onDownloadFail ( MessageObject parentObject , int reason ) {
if ( parentObject = = null ) {
return ;
}
AndroidUtilities . runOnUIThread ( ( ) - > {
boolean removed = false ;
for ( int i = 0 ; i < downloadingFiles . size ( ) ; i + + ) {
if ( downloadingFiles . get ( i ) . getDocument ( ) . id = = parentObject . getDocument ( ) . id ) {
downloadingFiles . remove ( i ) ;
removed = true ;
break ;
}
}
if ( removed ) {
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
if ( reason = = 0 ) {
NotificationCenter . getGlobalInstance ( ) . postNotificationName ( NotificationCenter . showBulletin , Bulletin . TYPE_ERROR , LocaleController . formatString ( " MessageNotFound " , R . string . MessageNotFound ) ) ;
}
}
} ) ;
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
try {
SQLitePreparedStatement state = getMessagesStorage ( ) . getDatabase ( ) . executeFast ( " DELETE FROM downloading_documents WHERE hash = ? AND id = ? " ) ;
state . bindInteger ( 1 , parentObject . getDocument ( ) . dc_id ) ;
state . bindLong ( 2 , parentObject . getDocument ( ) . id ) ;
state . step ( ) ;
state . dispose ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
} ) ;
}
2022-04-16 16:43:17 +02:00
Runnable clearUnviewedDownloadsRunnale = new Runnable ( ) {
2022-03-20 21:22:37 +01:00
@Override
public void run ( ) {
clearUnviewedDownloads ( ) ;
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
}
} ;
private void putToUnviewedDownloads ( MessageObject parentObject ) {
unviewedDownloads . put ( parentObject . getId ( ) , parentObject ) ;
2022-04-16 16:43:17 +02:00
AndroidUtilities . cancelRunOnUIThread ( clearUnviewedDownloadsRunnale ) ;
AndroidUtilities . runOnUIThread ( clearUnviewedDownloadsRunnale , 60000 ) ;
2022-03-20 21:22:37 +01:00
}
public void clearUnviewedDownloads ( ) {
unviewedDownloads . clear ( ) ;
}
public void checkUnviewedDownloads ( int messageId , long dialogId ) {
MessageObject messageObject = unviewedDownloads . get ( messageId ) ;
if ( messageObject ! = null & & messageObject . getDialogId ( ) = = dialogId ) {
unviewedDownloads . remove ( messageId ) ;
if ( unviewedDownloads . size ( ) = = 0 ) {
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
}
}
}
public boolean hasUnviewedDownloads ( ) {
return unviewedDownloads . size ( ) > 0 ;
}
private class DownloadingDocumentEntry {
long id ;
int hash ;
}
public void loadDownloadingFiles ( ) {
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
ArrayList < MessageObject > downloadingMessages = new ArrayList < > ( ) ;
ArrayList < MessageObject > recentlyDownloadedMessages = new ArrayList < > ( ) ;
2022-06-21 04:51:00 +02:00
ArrayList < MessageObject > newMessages = new ArrayList < > ( ) ;
2022-03-20 21:22:37 +01:00
try {
SQLiteCursor cursor2 = getMessagesStorage ( ) . getDatabase ( ) . queryFinalized ( " SELECT data, state FROM downloading_documents ORDER BY date DESC " ) ;
while ( cursor2 . next ( ) ) {
NativeByteBuffer data = cursor2 . byteBufferValue ( 0 ) ;
int state = cursor2 . intValue ( 1 ) ;
if ( data ! = null ) {
TLRPC . Message message = TLRPC . Message . TLdeserialize ( data , data . readInt32 ( false ) , false ) ;
if ( message ! = null ) {
message . readAttachPath ( data , UserConfig . getInstance ( currentAccount ) . clientUserId ) ;
2022-06-21 04:51:00 +02:00
MessageObject messageObject = new MessageObject ( currentAccount , message , false , false ) ;
newMessages . add ( messageObject ) ;
2022-03-20 21:22:37 +01:00
if ( state = = 0 ) {
downloadingMessages . add ( messageObject ) ;
2022-06-21 04:51:00 +02:00
} else {
2022-03-20 21:22:37 +01:00
recentlyDownloadedMessages . add ( messageObject ) ;
}
}
data . reuse ( ) ;
}
}
cursor2 . dispose ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
2022-06-21 04:51:00 +02:00
getFileLoader ( ) . checkMediaExistance ( downloadingMessages ) ;
getFileLoader ( ) . checkMediaExistance ( recentlyDownloadedMessages ) ;
2022-03-20 21:22:37 +01:00
AndroidUtilities . runOnUIThread ( ( ) - > {
downloadingFiles . clear ( ) ;
downloadingFiles . addAll ( downloadingMessages ) ;
recentDownloadingFiles . clear ( ) ;
recentDownloadingFiles . addAll ( recentlyDownloadedMessages ) ;
} ) ;
} ) ;
}
public void clearRecentDownloadedFiles ( ) {
recentDownloadingFiles . clear ( ) ;
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
try {
getMessagesStorage ( ) . getDatabase ( ) . executeFast ( " DELETE FROM downloading_documents WHERE state = 1 " ) . stepThis ( ) . dispose ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
} ) ;
}
public void deleteRecentFiles ( ArrayList < MessageObject > messageObjects ) {
for ( int i = 0 ; i < messageObjects . size ( ) ; i + + ) {
boolean found = false ;
for ( int j = 0 ; j < recentDownloadingFiles . size ( ) ; j + + ) {
2022-04-21 20:03:20 +02:00
if ( messageObjects . get ( i ) . getId ( ) = = recentDownloadingFiles . get ( j ) . getId ( ) & & recentDownloadingFiles . get ( j ) . getDialogId ( ) = = messageObjects . get ( i ) . getDialogId ( ) ) {
2022-03-20 21:22:37 +01:00
recentDownloadingFiles . remove ( j ) ;
found = true ;
break ;
}
}
if ( ! found ) {
for ( int j = 0 ; j < downloadingFiles . size ( ) ; j + + ) {
2022-04-21 20:03:20 +02:00
if ( messageObjects . get ( i ) . getId ( ) = = downloadingFiles . get ( j ) . getId ( ) & & downloadingFiles . get ( j ) . getDialogId ( ) = = messageObjects . get ( i ) . getDialogId ( ) ) {
2022-03-20 21:22:37 +01:00
downloadingFiles . remove ( j ) ;
found = true ;
break ;
}
}
}
messageObjects . get ( i ) . putInDownloadsStore = false ;
FileLoader . getInstance ( currentAccount ) . loadFile ( messageObjects . get ( i ) . getDocument ( ) , messageObjects . get ( i ) , 0 , 0 ) ;
FileLoader . getInstance ( currentAccount ) . cancelLoadFile ( messageObjects . get ( i ) . getDocument ( ) , true ) ;
}
getNotificationCenter ( ) . postNotificationName ( NotificationCenter . onDownloadingFilesChanged ) ;
getMessagesStorage ( ) . getStorageQueue ( ) . postRunnable ( ( ) - > {
try {
SQLitePreparedStatement state = getMessagesStorage ( ) . getDatabase ( ) . executeFast ( " DELETE FROM downloading_documents WHERE hash = ? AND id = ? " ) ;
for ( int i = 0 ; i < messageObjects . size ( ) ; i + + ) {
state . requery ( ) ;
state . bindInteger ( 1 , messageObjects . get ( i ) . getDocument ( ) . dc_id ) ;
state . bindLong ( 2 , messageObjects . get ( i ) . getDocument ( ) . id ) ;
state . step ( ) ;
try {
2022-06-21 04:51:00 +02:00
File file = FileLoader . getInstance ( currentAccount ) . getPathToMessage ( messageObjects . get ( i ) . messageOwner ) ;
2022-03-20 21:22:37 +01:00
file . delete ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
}
state . dispose ( ) ;
} catch ( Exception e ) {
FileLog . e ( e ) ;
}
} ) ;
}
2018-07-30 04:07:02 +02:00
}