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

499 lines
19 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.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;
2020-02-25 10:04:26 +01:00
import android.os.Build;
import android.os.Handler;
2014-04-03 12:23:39 +02:00
import android.os.PowerManager;
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;
2018-07-30 04:07:02 +02:00
import com.google.firebase.iid.FirebaseInstanceId;
2013-10-25 17:19:00 +02:00
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-02-25 10:04:26 +01:00
import tw.nekomimi.nekogram.NekoConfig;
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 boolean unableGetCurrentNetwork;
public static volatile Handler applicationHandler;
2019-01-23 18:03:33 +01:00
private static ConnectivityManager connectivityManager;
private static volatile boolean applicationInited = false;
2014-04-03 12:23:39 +02:00
public static volatile boolean isScreenOn = false;
public static volatile boolean mainInterfacePaused = 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;
public static volatile long mainInterfacePausedStageQueueTime;
2019-08-22 01:53:26 +02:00
public static boolean hasPlayServices;
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;
}
2014-04-03 12:23:39 +02:00
applicationInited = true;
2013-10-25 17:19:00 +02:00
try {
LocaleController.getInstance();
} 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);
//Utilities.globalQueue.postRunnable(ApplicationLoader::ensureCurrentNetworkGet);
} 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) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2014-04-03 12:23:39 +02:00
}
2018-07-30 04:07:02 +02:00
SharedConfig.loadConfig();
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
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();
2018-07-30 04:07:02 +02:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
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();
2020-02-18 10:21:51 +01:00
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);
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 {
2020-02-25 10:04:26 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && NekoConfig.residentNotification) {
applicationContext.startForegroundService(new Intent(applicationContext, NotificationsService.class));
} else {
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);
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 {
2019-01-23 18:03:33 +01:00
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> {
String token = instanceIdResult.getToken();
if (!TextUtils.isEmpty(token)) {
GcmPushListenerService.sendRegistrationToServer(token);
}
2019-07-18 15:01:39 +02:00
}).addOnFailureListener(e -> {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("Failed to get regid");
}
SharedConfig.pushStringStatus = "__FIREBASE_FAILED__";
GcmPushListenerService.sendRegistrationToServer(null);
2019-01-23 18:03:33 +01:00
});
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() {
if (currentNetworkInfo == null) {
try {
if (connectivityManager == null) {
connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
}
currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
unableGetCurrentNetwork = false;
} catch (Throwable ignore) {
unableGetCurrentNetwork = true;
}
}
}
public static boolean isRoaming() {
try {
ensureCurrentNetworkGet();
return currentNetworkInfo != null && currentNetworkInfo.isRoaming();
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedOrConnectingToWiFi() {
try {
ensureCurrentNetworkGet();
if (currentNetworkInfo != null && currentNetworkInfo.getType() == ) {
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();
if (currentNetworkInfo != null && currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI && currentNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectionSlow() {
try {
ensureCurrentNetworkGet();
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;
}
public static boolean isNetworkOnline() {
try {
ensureCurrentNetworkGet();
if (!unableGetCurrentNetwork && currentNetworkInfo == null) {
return false;
}
if (currentNetworkInfo.isConnectedOrConnecting() || currentNetworkInfo.isAvailable()) {
return true;
}
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;
}
return false;
}
*/
public static boolean isRoaming() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null) {
return netInfo.isRoaming();
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
public static boolean isConnectedOrConnectingToWiFi() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo.State state = netInfo.getState();
if (netInfo != null && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING || state == NetworkInfo.State.SUSPENDED)) {
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
2019-12-31 14:08:08 +01:00
public static int getAutodownloadNetworkType() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null) {
if (netInfo.getState() == NetworkInfo.State.CONNECTED) {
if (connectivityManager.isActiveNetworkMetered()) {
return StatsController.TYPE_MOBILE;
} else {
return StatsController.TYPE_WIFI;
}
}
}
netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isRoaming()) {
return StatsController.TYPE_ROAMING;
}
} catch (Exception e) {
FileLog.e(e);
}
return StatsController.TYPE_MOBILE;
}
2019-01-23 18:03:33 +01:00
public static boolean isConnectedToWiFi() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
} catch (Exception e) {
FileLog.e(e);
}
return false;
}
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 isConnectionSlow() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
switch (netInfo.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;
}
public static boolean isNetworkOnline() {
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;
}
2013-10-25 17:19:00 +02:00
}