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

644 lines
24 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;
2022-02-12 06:22:45 +01:00
import android.net.Network;
import android.net.NetworkCapabilities;
2019-01-23 18:03:33 +01:00
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;
2020-12-23 08:48:30 +01:00
import android.os.SystemClock;
2019-01-23 18:03:33 +01:00
import android.telephony.TelephonyManager;
2020-06-25 17:28:24 +02:00
import android.util.Log;
2022-02-12 06:22:45 +01:00
import androidx.annotation.NonNull;
import androidx.multidex.MultiDex;
2020-06-25 17:28:24 +02:00
import androidx.multidex.MultiDex;
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;
2021-03-12 13:37:39 +01:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.ForegroundDetector;
2013-10-25 17:19:00 +02:00
import java.io.File;
2020-06-25 17:28:24 +02:00
import java.lang.reflect.Method;
2021-03-12 13:37:39 +01:00
import java.util.HashSet;
import java.util.LinkedList;
2013-10-25 17:19:00 +02:00
2020-06-25 17:28:24 +02:00
import tw.nekomimi.nekogram.ExternalGcm;
import tw.nekomimi.nekogram.NekoXConfig;
2021-04-05 21:14:02 +02:00
import tw.nekomimi.nekogram.parts.SignturesKt;
2020-06-25 17:28:24 +02:00
import tw.nekomimi.nekogram.utils.FileUtil;
import static android.os.Build.VERSION.SDK_INT;
2020-02-25 10:04:26 +01:00
2013-10-25 17:19:00 +02:00
public class ApplicationLoader extends Application {
private static PendingIntent pendingIntent;
2017-03-31 01:58:05 +02:00
@SuppressLint("StaticFieldLeak")
public static volatile Context applicationContext;
2020-06-25 17:28:24 +02:00
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;
2022-03-11 17:49:54 +01:00
private static volatile ConnectivityManager.NetworkCallback networkCallback;
2022-02-12 06:22:45 +01:00
private static long lastNetworkCheckTypeTime;
private static int lastKnownNetworkType = -1;
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);
2021-04-25 17:06:51 +02:00
MultiDex.install(this);
2021-03-12 14:33:53 +01:00
try {
applicationContext = getApplicationContext();
} catch (Throwable ignore) {
}
2021-04-25 17:06:51 +02:00
if (SDK_INT >= Build.VERSION_CODES.P) {
Reflection.unseal(base);
2020-06-25 17:28:24 +02:00
}
2020-12-24 17:33:17 +01:00
Thread.currentThread().setUncaughtExceptionHandler((thread, error) -> {
Log.e("nekox", "from " + thread.toString(), error);
});
2020-04-24 11:21:58 +02:00
}
2020-06-25 17:28:24 +02:00
/**
* @author weishu
* @date 2018/6/7.
*/
public static class Reflection {
private static final String TAG = "Reflection";
private static Object sVmRuntime;
private static Method setHiddenApiExemptions;
static {
if (SDK_INT >= Build.VERSION_CODES.P) {
try {
Method forName = Class.class.getDeclaredMethod("forName", String.class);
Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
Class<?> vmRuntimeClass = (Class<?>) forName.invoke(null, "dalvik.system.VMRuntime");
Method getRuntime = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null);
setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
sVmRuntime = getRuntime.invoke(null);
} catch (Throwable e) {
FileLog.e("reflect bootstrap failed:", e);
}
}
}
private static int UNKNOWN = -9999;
private static final int ERROR_SET_APPLICATION_FAILED = -20;
private static final int ERROR_EXEMPT_FAILED = -21;
private static int unsealed = UNKNOWN;
public static int unseal(Context context) {
if (SDK_INT < 28) {
// Below Android P, ignore
return 0;
}
// try exempt API first.
if (exemptAll()) {
return 0;
} else {
return ERROR_EXEMPT_FAILED;
}
}
/**
* make the method exempted from hidden API check.
*
* @param method the method signature prefix.
* @return true if success.
*/
public static boolean exempt(String method) {
return exempt(new String[]{method});
}
/**
* make specific methods exempted from hidden API check.
*
* @param methods the method signature prefix, such as "Ldalvik/system", "Landroid" or even "L"
* @return true if success
*/
public static boolean exempt(String... methods) {
if (sVmRuntime == null || setHiddenApiExemptions == null) {
return false;
}
try {
setHiddenApiExemptions.invoke(sVmRuntime, new Object[]{methods});
return true;
} catch (Throwable e) {
return false;
}
}
/**
* Make all hidden API exempted.
*
* @return true if success.
*/
public static boolean exemptAll() {
return exempt(new String[]{"L"});
}
}
@SuppressLint("SdCardPath")
public static File getDataDirFixed() {
try {
File path = applicationContext.getFilesDir();
2015-10-29 18:10:07 +01:00
if (path != null) {
2020-06-25 17:28:24 +02:00
return path.getParentFile();
2015-10-29 18:10:07 +01:00
}
2020-06-25 17:28:24 +02:00
} catch (Exception ignored) {
2015-10-29 18:10:07 +01:00
}
try {
ApplicationInfo info = applicationContext.getApplicationInfo();
2020-06-25 17:28:24 +02:00
return new File(info.dataDir);
} catch (Exception ignored) {
2015-10-29 18:10:07 +01:00
}
2020-06-25 17:28:24 +02:00
return new File("/data/data/" + BuildConfig.APPLICATION_ID + "/");
}
public static File getFilesDirFixed() {
File filesDir = new File(getDataDirFixed(), "files");
FileUtil.initDir(filesDir);
return filesDir;
}
public static File getCacheDirFixed() {
File filesDir = new File(getDataDirFixed(), "cache");
FileUtil.initDir(filesDir);
return filesDir;
2015-10-29 18:10:07 +01:00
}
public static void postInitApplication() {
2022-03-20 21:22:37 +01:00
if (applicationInited || applicationContext == null) {
return;
}
applicationInited = true;
2013-10-25 17:19:00 +02:00
2021-03-12 13:37:39 +01:00
SharedConfig.loadConfig();
UserConfig.getInstance(0).loadConfig();
2020-06-25 17:28:24 +02:00
2021-03-12 13:37:39 +01:00
LinkedList<Runnable> postRun = new LinkedList<>();
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();
2021-03-12 13:37:39 +01:00
for (int a : SharedConfig.activeAccounts) {
2019-01-23 18:03:33 +01:00
ConnectionsManager.getInstance(a).checkConnection();
FileLoader.getInstance(a).onNetworkChanged(isSlow);
}
2021-03-12 13:37:39 +01:00
if (SharedConfig.loginingAccount != -1) {
ConnectionsManager.getInstance(SharedConfig.loginingAccount).checkConnection();
FileLoader.getInstance(SharedConfig.loginingAccount).onNetworkChanged(isSlow);
}
2019-01-23 18:03:33 +01:00
}
};
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
}
2021-03-12 13:37:39 +01:00
for (int a : SharedConfig.activeAccounts) {
2020-06-25 17:28:24 +02:00
final int finalA = a;
2021-03-12 13:37:39 +01:00
Runnable initRunnable = () -> loadAccount(finalA);
2020-06-25 17:28:24 +02:00
if (finalA == UserConfig.selectedAccount) initRunnable.run();
2021-03-12 13:37:39 +01:00
else postRun.add(initRunnable);
2020-06-25 17:28:24 +02:00
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("app initied");
}
2021-03-12 13:37:39 +01:00
for (Runnable runnable : postRun) {
Utilities.stageQueue.postRunnable(runnable);
}
Utilities.stageQueue.postRunnable(ExternalGcm::initPlayServices);
}
2014-07-20 01:31:49 +02:00
2021-03-12 13:37:39 +01:00
private static final HashSet<Integer> loadedAccounts = new HashSet<>();
public static void loadAccount(int account) {
if (!loadedAccounts.add(account)) return;
2021-03-12 14:33:53 +01:00
UserConfig inst = UserConfig.getInstance(account);
inst.loadConfig();
if (!inst.isClientActivated()) {
if (SharedConfig.activeAccounts.remove(account)) {
SharedConfig.saveAccounts();
}
}
2021-03-12 13:37:39 +01:00
MessagesController.getInstance(account);
if (account == 0) {
SharedConfig.pushStringStatus = "__FIREBASE_GENERATING_SINCE_" + ConnectionsManager.getInstance(account).getCurrentTime() + "__";
} else {
ConnectionsManager.getInstance(account);
}
TLRPC.User user = UserConfig.getInstance(account).getCurrentUser();
if (user != null) {
MessagesController.getInstance(account).putUser(user, true);
2018-07-30 04:07:02 +02:00
}
2021-03-12 13:37:39 +01:00
Utilities.stageQueue.postRunnable(() -> {
SendMessagesHelper.getInstance(account).checkUnsentMessages();
ContactsController.getInstance(account).checkAppAccount();
DownloadController.getInstance(account);
});
2021-09-20 11:14:20 +02:00
2021-09-20 00:10:42 +02:00
ChatThemeController.init();
}
2019-03-03 21:40:48 +01:00
public ApplicationLoader() {
super();
}
@Override
public void onCreate() {
2020-06-25 17:28:24 +02:00
2019-03-03 21:40:48 +01:00
try {
applicationContext = getApplicationContext();
} catch (Throwable ignore) {
}
2021-03-20 07:21:12 +01:00
if (BuildVars.isPlay && ExternalGcm.checkSplit(this)) {
2021-03-18 10:44:39 +01:00
return; // Skip app initialization.
}
super.onCreate();
2020-02-18 10:21:51 +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();
}
// Since static init is thread-safe, no lock is needed there.
Utilities.stageQueue.postRunnable(() -> {
2021-05-06 14:51:10 +02:00
SignturesKt.checkMT(this);
});
2020-12-17 06:45:19 +01:00
try {
Class.forName("org.robolectric.android.internal.AndroidTestEnvironment");
return;
2021-04-05 21:14:02 +02:00
} catch (ClassNotFoundException ignored) {
2020-12-17 06:45:19 +01:00
}
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());
2020-06-25 17:28:24 +02:00
org.osmdroid.config.Configuration.getInstance().setUserAgentValue("Telegram-FOSS ( NekoX ) " + BuildConfig.VERSION_NAME);
org.osmdroid.config.Configuration.getInstance().setOsmdroidBasePath(new File(ApplicationLoader.applicationContext.getCacheDir(), "osmdroid"));
2020-06-25 17:28:24 +02:00
startPushService();
2018-07-30 04:07:02 +02:00
}
public static void startPushService() {
2021-03-12 08:26:52 +01:00
Utilities.stageQueue.postRunnable(ApplicationLoader::startPushServiceInternal);
2020-12-23 11:52:32 +01:00
}
private static void startPushServiceInternal() {
if (ExternalGcm.checkPlayServices()) {
2020-06-25 17:28:24 +02:00
return;
}
SharedPreferences preferences = MessagesController.getNotificationsSettings(UserConfig.selectedAccount);
2019-12-31 14:08:08 +01:00
boolean enabled;
if (preferences.contains("pushService")) {
enabled = preferences.getBoolean("pushService", false);
2019-12-31 14:08:08 +01:00
} else {
enabled = MessagesController.getMainSettings(UserConfig.selectedAccount).getBoolean("keepAliveService", false);
2020-06-25 17:28:24 +02:00
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("pushService", enabled);
editor.putBoolean("pushConnection", enabled);
editor.apply();
ConnectionsManager.getInstance(UserConfig.selectedAccount).setPushConnectionEnabled(enabled);
2019-12-31 14:08:08 +01:00
}
if (enabled) {
AndroidUtilities.runOnUIThread(() -> {
try {
Log.d("TFOSS", "Trying to start push service every 10 minutes");
// Telegram-FOSS: unconditionally enable push service
AlarmManager am = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(applicationContext, NotificationsService.class);
pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, i, 0);
am.cancel(pendingIntent);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent);
2020-12-23 11:52:32 +01:00
Log.d("TFOSS", "Starting push service...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
applicationContext.startForegroundService(new Intent(applicationContext, NotificationsService.class));
} else {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
}
} catch (Throwable e) {
Log.d("TFOSS", "Failed to start push service");
}
});
} else AndroidUtilities.runOnUIThread(() -> {
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);
2020-06-25 17:28:24 +02:00
AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
2019-12-31 14:08:08 +01:00
alarm.cancel(pintent);
if (pendingIntent != null) {
alarm.cancel(pendingIntent);
}
2020-12-23 11:52:32 +01:00
});
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 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();
2022-02-12 06:22:45 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
2022-03-11 17:49:54 +01:00
if (networkCallback == null) {
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
lastKnownNetworkType = -1;
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
lastKnownNetworkType = -1;
}
};
connectivityManager.registerDefaultNetworkCallback(networkCallback);
}
2022-02-12 06:22:45 +01:00
}
2019-01-23 18:03:33 +01:00
} 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) {
2022-02-12 06:22:45 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (lastKnownNetworkType == StatsController.TYPE_MOBILE || lastKnownNetworkType == StatsController.TYPE_WIFI) && System.currentTimeMillis() - lastNetworkCheckTypeTime < 5000) {
return lastKnownNetworkType;
}
2020-06-05 04:40:01 +02:00
if (connectivityManager.isActiveNetworkMetered()) {
2022-02-12 06:22:45 +01:00
lastKnownNetworkType = StatsController.TYPE_MOBILE;
2020-06-05 04:40:01 +02:00
} else {
2022-02-12 06:22:45 +01:00
lastKnownNetworkType = StatsController.TYPE_WIFI;
2019-12-31 14:08:08 +01:00
}
2022-02-12 06:22:45 +01:00
lastNetworkCheckTypeTime = System.currentTimeMillis();
return lastKnownNetworkType;
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
}