NekoX/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java

472 lines
18 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2013-10-25 17:19:00 +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.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.messenger;
2013-10-25 17:19:00 +02:00
2017-03-31 01:58:05 +02:00
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
2013-10-25 17:19:00 +02:00
import android.app.Application;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
2013-10-25 17:19:00 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
2013-10-25 17:19:00 +02:00
import android.content.SharedPreferences;
2015-10-29 18:10:07 +01:00
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
2019-01-23 18:03:33 +01:00
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
2014-04-03 12:23:39 +02:00
import android.os.PowerManager;
2020-12-23 08:48:30 +01:00
import android.os.SystemClock;
2019-01-23 18:03:33 +01:00
import android.telephony.TelephonyManager;
2018-07-30 04:07:02 +02:00
import android.text.TextUtils;
2013-10-25 17:19:00 +02:00
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
2021-06-25 02:43:10 +02:00
import com.google.firebase.messaging.FirebaseMessaging;
2013-10-25 17:19:00 +02:00
2021-06-25 02:43:10 +02:00
import org.telegram.messenger.voip.VideoCapturerDevice;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
2018-07-30 04:07:02 +02:00
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.Components.ForegroundDetector;
2013-10-25 17:19:00 +02:00
import java.io.File;
2013-10-25 17:19:00 +02:00
2020-04-24 11:21:58 +02:00
import androidx.multidex.MultiDex;
2013-10-25 17:19:00 +02:00
public class ApplicationLoader extends Application {
2017-03-31 01:58:05 +02:00
@SuppressLint("StaticFieldLeak")
public static volatile Context applicationContext;
2019-01-23 18:03:33 +01:00
public static volatile NetworkInfo currentNetworkInfo;
public static volatile Handler applicationHandler;
2019-01-23 18:03:33 +01:00
private static ConnectivityManager connectivityManager;
private static volatile boolean applicationInited = false;
2020-12-23 08:48:30 +01:00
public static long startTime;
2014-04-03 12:23:39 +02:00
public static volatile boolean isScreenOn = false;
public static volatile boolean mainInterfacePaused = true;
2020-12-23 08:48:30 +01:00
public static volatile boolean mainInterfaceStopped = true;
2018-07-30 04:07:02 +02:00
public static volatile boolean externalInterfacePaused = true;
2017-03-31 01:58:05 +02:00
public static volatile boolean mainInterfacePausedStageQueue = true;
2020-12-23 08:48:30 +01:00
public static boolean canDrawOverlays;
2017-03-31 01:58:05 +02:00
public static volatile long mainInterfacePausedStageQueueTime;
2019-08-22 01:53:26 +02:00
public static boolean hasPlayServices;
2020-04-24 11:21:58 +02:00
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
2015-10-29 18:10:07 +01:00
public static File getFilesDirFixed() {
for (int a = 0; a < 10; a++) {
File path = ApplicationLoader.applicationContext.getFilesDir();
if (path != null) {
return path;
}
}
try {
ApplicationInfo info = applicationContext.getApplicationInfo();
File path = new File(info.dataDir, "files");
path.mkdirs();
return path;
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
return new File("/data/data/org.telegram.messenger/files");
}
public static void postInitApplication() {
if (applicationInited) {
return;
}
applicationInited = true;
2013-10-25 17:19:00 +02:00
try {
2020-12-23 08:48:30 +01:00
LocaleController.getInstance(); //TODO improve
} catch (Exception e) {
e.printStackTrace();
}
2019-01-23 18:03:33 +01:00
try {
connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2019-03-03 21:40:48 +01:00
try {
currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
} catch (Throwable ignore) {
}
2019-01-23 18:03:33 +01:00
boolean isSlow = isConnectionSlow();
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
ConnectionsManager.getInstance(a).checkConnection();
FileLoader.getInstance(a).onNetworkChanged(isSlow);
}
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
ApplicationLoader.applicationContext.registerReceiver(networkStateReceiver, filter);
} catch (Exception e) {
e.printStackTrace();
}
try {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
final BroadcastReceiver mReceiver = new ScreenReceiver();
applicationContext.registerReceiver(mReceiver, filter);
} catch (Exception e) {
e.printStackTrace();
}
2014-04-03 12:23:39 +02:00
try {
2019-09-10 12:56:11 +02:00
PowerManager pm = (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
2014-04-03 12:23:39 +02:00
isScreenOn = pm.isScreenOn();
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("screen state = " + isScreenOn);
}
2014-04-03 12:23:39 +02:00
} catch (Exception e) {
2021-07-31 01:39:13 +02:00
e.printStackTrace();
2014-04-03 12:23:39 +02:00
}
2018-07-30 04:07:02 +02:00
SharedConfig.loadConfig();
2020-12-23 08:48:30 +01:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
2018-07-30 04:07:02 +02:00
UserConfig.getInstance(a).loadConfig();
MessagesController.getInstance(a);
2019-07-18 15:01:39 +02:00
if (a == 0) {
SharedConfig.pushStringStatus = "__FIREBASE_GENERATING_SINCE_" + ConnectionsManager.getInstance(a).getCurrentTime() + "__";
} else {
ConnectionsManager.getInstance(a);
}
2018-07-30 04:07:02 +02:00
TLRPC.User user = UserConfig.getInstance(a).getCurrentUser();
if (user != null) {
MessagesController.getInstance(a).putUser(user, true);
SendMessagesHelper.getInstance(a).checkUnsentMessages();
}
2013-10-25 17:19:00 +02:00
}
2019-09-10 12:56:11 +02:00
ApplicationLoader app = (ApplicationLoader) ApplicationLoader.applicationContext;
app.initPlayServices();
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("app initied");
}
2014-07-20 01:31:49 +02:00
MediaController.getInstance();
2020-12-23 08:48:30 +01:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
2018-07-30 04:07:02 +02:00
ContactsController.getInstance(a).checkAppAccount();
DownloadController.getInstance(a);
}
WearDataLayerListenerService.updateWatchConnectionState();
}
2019-03-03 21:40:48 +01:00
public ApplicationLoader() {
super();
}
@Override
public void onCreate() {
2019-03-03 21:40:48 +01:00
try {
applicationContext = getApplicationContext();
} catch (Throwable ignore) {
}
super.onCreate();
2014-10-30 22:27:41 +01:00
2020-12-23 08:48:30 +01:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime()));
}
2019-03-03 21:40:48 +01:00
if (applicationContext == null) {
applicationContext = getApplicationContext();
}
NativeLoader.initNativeLibs(ApplicationLoader.applicationContext);
2018-07-30 04:07:02 +02:00
ConnectionsManager.native_setJava(false);
new ForegroundDetector(this) {
@Override
public void onActivityStarted(Activity activity) {
boolean wasInBackground = isBackground();
super.onActivityStarted(activity);
if (wasInBackground) {
ensureCurrentNetworkGet(true);
}
}
};
2020-12-23 08:48:30 +01:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("load libs time = " + (SystemClock.elapsedRealtime() - startTime));
}
applicationHandler = new Handler(applicationContext.getMainLooper());
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(ApplicationLoader::startPushService);
2018-07-30 04:07:02 +02:00
}
public static void startPushService() {
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = MessagesController.getGlobalNotificationsSettings();
2019-12-31 14:08:08 +01:00
boolean enabled;
if (preferences.contains("pushService")) {
enabled = preferences.getBoolean("pushService", true);
} else {
enabled = MessagesController.getMainSettings(UserConfig.selectedAccount).getBoolean("keepAliveService", false);
}
if (enabled) {
2018-07-30 04:07:02 +02:00
try {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
2018-08-27 10:33:11 +02:00
} catch (Throwable ignore) {
2018-07-30 04:07:02 +02:00
}
} else {
2019-12-31 14:08:08 +01:00
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));
2019-12-31 14:08:08 +01:00
PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
AlarmManager alarm = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
}
2013-12-20 20:25:49 +01:00
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
2014-03-25 11:58:47 +01:00
try {
LocaleController.getInstance().onDeviceConfigurationChange(newConfig);
2016-10-11 13:57:01 +02:00
AndroidUtilities.checkDisplaySize(applicationContext, newConfig);
2021-06-25 02:43:10 +02:00
VideoCapturerDevice.checkScreenCapturerSize();
2014-03-25 11:58:47 +01:00
} catch (Exception e) {
e.printStackTrace();
}
}
private void initPlayServices() {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-08-22 01:53:26 +02:00
if (hasPlayServices = checkPlayServices()) {
2018-08-27 10:33:11 +02:00
final String currentPushString = SharedConfig.pushString;
if (!TextUtils.isEmpty(currentPushString)) {
2019-07-18 15:01:39 +02:00
if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) {
2018-08-27 10:33:11 +02:00
FileLog.d("GCM regId = " + currentPushString);
}
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2018-08-27 10:33:11 +02:00
FileLog.d("GCM Registration not found.");
}
}
Utilities.globalQueue.postRunnable(() -> {
try {
2021-08-31 21:06:39 +02:00
SharedConfig.pushStringGetTimeStart = SystemClock.elapsedRealtime();
2021-06-25 02:43:10 +02:00
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
2021-08-31 21:06:39 +02:00
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();
2021-06-25 02:43:10 +02:00
if (!task.isSuccessful()) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("Failed to get regid");
}
SharedConfig.pushStringStatus = "__FIREBASE_FAILED__";
GcmPushListenerService.sendRegistrationToServer(null);
return;
}
String token = task.getResult();
if (!TextUtils.isEmpty(token)) {
GcmPushListenerService.sendRegistrationToServer(token);
}
});
2018-08-27 10:33:11 +02:00
} catch (Throwable e) {
FileLog.e(e);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
});
} else {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("No valid Google Play Services APK found.");
}
2019-07-18 15:01:39 +02:00
SharedConfig.pushStringStatus = "__NO_GOOGLE_PLAY_SERVICES__";
GcmPushListenerService.sendRegistrationToServer(null);
}
2018-07-30 04:07:02 +02:00
}, 1000);
}
2013-10-25 17:19:00 +02:00
private boolean checkPlayServices() {
2016-05-25 23:49:47 +02:00
try {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
return resultCode == ConnectionResult.SUCCESS;
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2016-05-25 23:49:47 +02:00
}
return true;
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
private static void ensureCurrentNetworkGet(boolean force) {
if (force || currentNetworkInfo == null) {
2019-01-23 18:03:33 +01:00
try {
if (connectivityManager == null) {
connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
}
currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
} catch (Throwable ignore) {
2019-01-23 18:03:33 +01:00
}
}
}
public static boolean isRoaming() {
try {
ensureCurrentNetworkGet(false);
2019-01-23 18:03:33 +01:00
return currentNetworkInfo != null && currentNetworkInfo.isRoaming();
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedOrConnectingToWiFi() {
try {
ensureCurrentNetworkGet(false);
2020-06-05 04:40:01 +02:00
if (currentNetworkInfo != null && (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET)) {
2019-01-23 18:03:33 +01:00
NetworkInfo.State state = currentNetworkInfo.getState();
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING || state == NetworkInfo.State.SUSPENDED) {
return true;
}
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedToWiFi() {
try {
ensureCurrentNetworkGet(false);
2020-06-05 04:40:01 +02:00
if (currentNetworkInfo != null && (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) && currentNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
2019-01-23 18:03:33 +01:00
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectionSlow() {
try {
ensureCurrentNetworkGet(false);
2019-01-23 18:03:33 +01:00
if (currentNetworkInfo != null && currentNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
switch (currentNetworkInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_IDEN:
return true;
}
}
} catch (Throwable ignore) {
}
return false;
}
2019-12-31 14:08:08 +01:00
public static int getAutodownloadNetworkType() {
try {
2020-06-05 04:40:01 +02:00
ensureCurrentNetworkGet(false);
if (currentNetworkInfo == null) {
return StatsController.TYPE_MOBILE;
}
if (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
if (connectivityManager.isActiveNetworkMetered()) {
return StatsController.TYPE_MOBILE;
} else {
return StatsController.TYPE_WIFI;
2019-12-31 14:08:08 +01:00
}
}
2020-06-05 04:40:01 +02:00
if (currentNetworkInfo.isRoaming()) {
2019-12-31 14:08:08 +01:00
return StatsController.TYPE_ROAMING;
}
} catch (Exception e) {
FileLog.e(e);
}
return StatsController.TYPE_MOBILE;
}
2019-01-23 18:03:33 +01:00
public static int getCurrentNetworkType() {
if (isConnectedOrConnectingToWiFi()) {
return StatsController.TYPE_WIFI;
} else if (isRoaming()) {
return StatsController.TYPE_ROAMING;
} else {
return StatsController.TYPE_MOBILE;
}
}
public static boolean isNetworkOnlineFast() {
2019-01-23 18:03:33 +01:00
try {
ensureCurrentNetworkGet(false);
if (currentNetworkInfo == null) {
return true;
}
if (currentNetworkInfo.isConnectedOrConnecting() || currentNetworkInfo.isAvailable()) {
return true;
2019-01-23 18:03:33 +01:00
}
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
}
} catch (Exception e) {
FileLog.e(e);
return true;
2019-01-23 18:03:33 +01:00
}
return false;
}
public static boolean isNetworkOnlineRealtime() {
2019-01-23 18:03:33 +01:00
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && (netInfo.isConnectedOrConnecting() || netInfo.isAvailable())) {
return true;
}
netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
}
} catch (Exception e) {
FileLog.e(e);
return true;
}
return false;
}
public static boolean isNetworkOnline() {
boolean result = isNetworkOnlineRealtime();
if (BuildVars.DEBUG_PRIVATE_VERSION) {
boolean result2 = isNetworkOnlineFast();
if (result != result2) {
FileLog.d("network online mismatch");
}
}
return result;
}
2013-10-25 17:19:00 +02:00
}