823 lines
30 KiB
Java
823 lines
30 KiB
Java
/*
|
|
* This is the source code of Telegram for Android v. 5.x.x.
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
*
|
|
* Copyright Nikolai Kudashov, 2013-2018.
|
|
*/
|
|
|
|
package org.telegram.messenger;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.app.Activity;
|
|
import android.app.Application;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.res.Configuration;
|
|
import android.net.ConnectivityManager;
|
|
import android.net.Network;
|
|
import android.net.NetworkCapabilities;
|
|
import android.net.NetworkInfo;
|
|
import android.os.Build;
|
|
import android.os.Handler;
|
|
import android.os.PowerManager;
|
|
import android.os.SystemClock;
|
|
import android.telephony.TelephonyManager;
|
|
import android.view.ViewGroup;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import com.google.android.gms.common.ConnectionResult;
|
|
import com.google.android.gms.common.GooglePlayServicesUtil;
|
|
|
|
import org.json.JSONObject;
|
|
import org.telegram.messenger.voip.VideoCapturerDevice;
|
|
import org.telegram.tgnet.ConnectionsManager;
|
|
import org.telegram.tgnet.TLRPC;
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
|
import org.telegram.ui.Components.ForegroundDetector;
|
|
import org.telegram.ui.Components.ItemOptions;
|
|
import org.telegram.ui.IUpdateLayout;
|
|
import org.telegram.ui.LauncherIconController;
|
|
|
|
import java.io.File;
|
|
import java.util.Locale;
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
import tw.nekomimi.nekogram.FirebaseFix;
|
|
import tw.nekomimi.nekogram.NekoConfig;
|
|
import tw.nekomimi.nekogram.helpers.AnalyticsHelper;
|
|
import tw.nekomimi.nekogram.helpers.ComponentsHelper;
|
|
|
|
public class ApplicationLoader extends Application {
|
|
|
|
public static ApplicationLoader applicationLoaderInstance;
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
public static volatile Context applicationContext;
|
|
public static volatile NetworkInfo currentNetworkInfo;
|
|
public static volatile Handler applicationHandler;
|
|
public static final CountDownLatch countDownLatch = new CountDownLatch(1);
|
|
|
|
private static ConnectivityManager connectivityManager;
|
|
private static volatile boolean applicationInited = false;
|
|
private static volatile ConnectivityManager.NetworkCallback networkCallback;
|
|
private static long lastNetworkCheckTypeTime;
|
|
private static int lastKnownNetworkType = -1;
|
|
|
|
public static long startTime;
|
|
|
|
public static volatile boolean isScreenOn = false;
|
|
public static volatile boolean mainInterfacePaused = true;
|
|
public static volatile boolean mainInterfaceStopped = true;
|
|
public static volatile boolean externalInterfacePaused = true;
|
|
public static volatile boolean mainInterfacePausedStageQueue = true;
|
|
public static boolean canDrawOverlays;
|
|
public static volatile long mainInterfacePausedStageQueueTime;
|
|
|
|
private static PushListenerController.IPushListenerServiceProvider pushProvider;
|
|
private static IMapsProvider mapsProvider;
|
|
private static ILocationServiceProvider locationServiceProvider;
|
|
|
|
@Override
|
|
protected void attachBaseContext(Context base) {
|
|
super.attachBaseContext(base);
|
|
FirebaseFix.check(base);
|
|
}
|
|
|
|
public static ILocationServiceProvider getLocationServiceProvider() {
|
|
if (locationServiceProvider == null) {
|
|
locationServiceProvider = applicationLoaderInstance.onCreateLocationServiceProvider();
|
|
locationServiceProvider.init(applicationContext);
|
|
}
|
|
return locationServiceProvider;
|
|
}
|
|
|
|
protected ILocationServiceProvider onCreateLocationServiceProvider() {
|
|
return new GoogleLocationProvider();
|
|
}
|
|
|
|
public static IMapsProvider getMapsProvider() {
|
|
if (mapsProvider == null) {
|
|
mapsProvider = applicationLoaderInstance.onCreateMapsProvider();
|
|
}
|
|
return mapsProvider;
|
|
}
|
|
|
|
protected IMapsProvider onCreateMapsProvider() {
|
|
return new GoogleMapsProvider();
|
|
}
|
|
|
|
public static PushListenerController.IPushListenerServiceProvider getPushProvider() {
|
|
if (pushProvider == null) {
|
|
pushProvider = applicationLoaderInstance.onCreatePushProvider();
|
|
}
|
|
return pushProvider;
|
|
}
|
|
|
|
protected PushListenerController.IPushListenerServiceProvider onCreatePushProvider() {
|
|
return PushListenerController.GooglePushListenerServiceProvider.INSTANCE;
|
|
}
|
|
|
|
public static String getApplicationId() {
|
|
return applicationLoaderInstance.onGetApplicationId();
|
|
}
|
|
|
|
protected String onGetApplicationId() {
|
|
return null;
|
|
}
|
|
|
|
public static boolean isHuaweiStoreBuild() {
|
|
return applicationLoaderInstance.isHuaweiBuild();
|
|
}
|
|
|
|
public static boolean isStandaloneBuild() {
|
|
return applicationLoaderInstance.isStandalone();
|
|
}
|
|
|
|
public static boolean isBetaBuild() {
|
|
return applicationLoaderInstance.isBeta();
|
|
}
|
|
|
|
public static boolean isAndroidTestEnvironment() {
|
|
return applicationLoaderInstance.isAndroidTestEnv();
|
|
}
|
|
|
|
protected boolean isHuaweiBuild() {
|
|
return false;
|
|
}
|
|
|
|
protected boolean isStandalone() {
|
|
return false;
|
|
}
|
|
|
|
protected boolean isBeta() {
|
|
return false;
|
|
}
|
|
|
|
protected boolean isAndroidTestEnv() {
|
|
return false;
|
|
}
|
|
|
|
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) {
|
|
FileLog.e(e);
|
|
}
|
|
return new File("/data/data/org.telegram.messenger/files");
|
|
}
|
|
|
|
public static File getFilesDirFixed(String child) {
|
|
try {
|
|
File path = getFilesDirFixed();
|
|
File dir = new File(path, child);
|
|
dir.mkdirs();
|
|
|
|
return dir;
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static void postInitApplication() {
|
|
if (applicationInited || applicationContext == null) {
|
|
return;
|
|
}
|
|
applicationInited = true;
|
|
NativeLoader.initNativeLibs(ApplicationLoader.applicationContext);
|
|
XrayController.init(ApplicationLoader.applicationContext);
|
|
|
|
try {
|
|
LocaleController.getInstance(); //TODO improve
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
try {
|
|
connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
try {
|
|
currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
|
} catch (Throwable ignore) {
|
|
|
|
}
|
|
|
|
boolean isSlow = isConnectionSlow();
|
|
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
|
|
if (!UserConfig.getInstance(a).isClientActivated()) continue;
|
|
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();
|
|
}
|
|
|
|
try {
|
|
PowerManager pm = (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
|
|
isScreenOn = pm.isScreenOn();
|
|
if (BuildVars.LOGS_ENABLED) {
|
|
FileLog.d("screen state = " + isScreenOn);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
SharedConfig.loadConfig();
|
|
tw.nekomimi.nekogram.helpers.FontHelper.init();
|
|
SharedPrefsHelper.init(applicationContext);
|
|
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
|
|
UserConfig.getInstance(a).loadConfig();
|
|
if (!UserConfig.getInstance(a).isClientActivated()) continue;
|
|
MessagesController.getInstance(a);
|
|
if (a == 0) {
|
|
SharedConfig.pushStringStatus = "__FIREBASE_GENERATING_SINCE_" + ConnectionsManager.getInstance(a).getCurrentTime() + "__";
|
|
} else {
|
|
ConnectionsManager.getInstance(a);
|
|
}
|
|
TLRPC.User user = UserConfig.getInstance(a).getCurrentUser();
|
|
if (user != null) {
|
|
MessagesController.getInstance(a).putUser(user, true);
|
|
SendMessagesHelper.getInstance(a).checkUnsentMessages();
|
|
}
|
|
}
|
|
|
|
ApplicationLoader app = (ApplicationLoader) ApplicationLoader.applicationContext;
|
|
app.initPushServices();
|
|
if (BuildVars.LOGS_ENABLED) {
|
|
FileLog.d("app initied");
|
|
}
|
|
|
|
// Restore Xray proxy if it was active before restart
|
|
restoreXrayIfNeeded();
|
|
|
|
MediaController.getInstance();
|
|
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
|
|
if (!UserConfig.getInstance(a).isClientActivated()) continue;
|
|
ContactsController.getInstance(a).checkAppAccount();
|
|
DownloadController.getInstance(a);
|
|
}
|
|
BillingController.getInstance().startConnection();
|
|
}
|
|
|
|
public ApplicationLoader() {
|
|
super();
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
applicationLoaderInstance = this;
|
|
try {
|
|
applicationContext = getApplicationContext();
|
|
} catch (Throwable ignore) {
|
|
|
|
}
|
|
|
|
super.onCreate();
|
|
|
|
AnalyticsHelper.start(this);
|
|
ComponentsHelper.fixComponents(this);
|
|
|
|
if (BuildVars.LOGS_ENABLED) {
|
|
FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime()));
|
|
try {
|
|
final PackageInfo info = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
|
|
final String abi;
|
|
switch (info.versionCode % 10) {
|
|
case 1:
|
|
case 2:
|
|
abi = "store bundled " + Build.CPU_ABI + " " + Build.CPU_ABI2;
|
|
break;
|
|
default:
|
|
case 9:
|
|
if (ApplicationLoader.isStandaloneBuild()) {
|
|
abi = "direct " + Build.CPU_ABI + " " + Build.CPU_ABI2;
|
|
} else {
|
|
abi = "universal " + Build.CPU_ABI + " " + Build.CPU_ABI2;
|
|
}
|
|
break;
|
|
}
|
|
FileLog.d("buildVersion = " + String.format(Locale.US, "v%s (%d[%d]) %s", info.versionName, info.versionCode / 10, info.versionCode % 10, abi));
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
FileLog.d("device = manufacturer=" + Build.MANUFACTURER + ", device=" + Build.DEVICE + ", model=" + Build.MODEL + ", product=" + Build.PRODUCT);
|
|
}
|
|
if (applicationContext == null) {
|
|
applicationContext = getApplicationContext();
|
|
}
|
|
|
|
NativeLoader.initNativeLibs(ApplicationLoader.applicationContext);
|
|
try {
|
|
ConnectionsManager.native_setJava(false);
|
|
} catch (UnsatisfiedLinkError error) {
|
|
throw new RuntimeException("can't load native libraries " + Build.CPU_ABI + " lookup folder " + NativeLoader.getAbiFolder());
|
|
}
|
|
new ForegroundDetector(this) {
|
|
@Override
|
|
public void onActivityStarted(Activity activity) {
|
|
boolean wasInBackground = isBackground();
|
|
super.onActivityStarted(activity);
|
|
if (wasInBackground) {
|
|
ensureCurrentNetworkGet(true);
|
|
}
|
|
}
|
|
};
|
|
if (BuildVars.LOGS_ENABLED) {
|
|
FileLog.d("load libs time = " + (SystemClock.elapsedRealtime() - startTime));
|
|
}
|
|
|
|
applicationHandler = new Handler(applicationContext.getMainLooper());
|
|
|
|
AndroidUtilities.runOnUIThread(ApplicationLoader::startPushService);
|
|
countDownLatch.countDown();
|
|
|
|
// Delay icon fix to avoid process restart on Android 14 during startup
|
|
AndroidUtilities.runOnUIThread(LauncherIconController::tryFixLauncherIconIfNeeded, 3000);
|
|
ProxyRotationController.init();
|
|
}
|
|
|
|
public static void startPushService() {
|
|
SharedPreferences preferences = MessagesController.getGlobalNotificationsSettings();
|
|
boolean enabled;
|
|
if (preferences.contains("pushService")) {
|
|
enabled = preferences.getBoolean("pushService", true);
|
|
} else {
|
|
enabled = MessagesController.getMainSettings(UserConfig.selectedAccount).getBoolean("keepAliveService", false);
|
|
}
|
|
if (enabled) {
|
|
try {
|
|
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
|
|
} catch (Throwable ignore) {
|
|
|
|
}
|
|
} else {
|
|
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
super.onConfigurationChanged(newConfig);
|
|
try {
|
|
LocaleController.getInstance().onDeviceConfigurationChange(newConfig);
|
|
AndroidUtilities.checkDisplaySize(applicationContext, newConfig);
|
|
VideoCapturerDevice.checkScreenCapturerSize();
|
|
AndroidUtilities.resetTabletFlag();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static void restoreXrayIfNeeded() {
|
|
android.content.SharedPreferences prefs =
|
|
applicationContext.getSharedPreferences("mainconfig", android.app.Activity.MODE_PRIVATE);
|
|
boolean proxyEnabled = prefs.getBoolean("proxy_enabled", false);
|
|
String proxyIp = prefs.getString("proxy_ip", "");
|
|
int savedPort = prefs.getInt("proxy_port", 0);
|
|
|
|
// First launch: the user has never configured a proxy yet. Auto-enable a
|
|
// bundled VLESS proxy so the app can connect even if plain Telegram is
|
|
// blocked. We only do this once (guarded by "builtin_proxy_autoenabled")
|
|
// and never override a choice an existing user has already made.
|
|
if (!prefs.getBoolean("builtin_proxy_autoenabled", false)) {
|
|
if (proxyEnabled || prefs.contains("proxy_ip")) {
|
|
// Existing user (already has proxy settings): don't touch their
|
|
// choice, just remember that we've handled the one-time check.
|
|
prefs.edit().putBoolean("builtin_proxy_autoenabled", true).apply();
|
|
} else {
|
|
autoEnableBuiltinProxy(prefs);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!proxyEnabled || !"127.0.0.1".equals(proxyIp)) return;
|
|
if (XrayController.isRunning()) return;
|
|
|
|
// Pick the right builtin proxy by saved local port
|
|
SharedConfig.loadProxyList();
|
|
SharedConfig.ProxyInfo proxy = SharedConfig.currentProxy;
|
|
if (proxy == null || !proxy.isVless()) {
|
|
proxy = XrayController.createBuiltinProxyByPort(savedPort);
|
|
if (proxy == null) {
|
|
proxy = XrayController.createBuiltinProxy();
|
|
}
|
|
}
|
|
// If the saved built-in proxy is tagged for the other network (e.g. a
|
|
// "WiFi" server while we're on mobile data now), switch to a matching one.
|
|
if (proxy != null && proxy.builtin && !XrayController.matchesCurrentNetwork(proxy)) {
|
|
SharedConfig.ProxyInfo match = XrayController.createBuiltinProxyForCurrentNetwork();
|
|
if (match != null && XrayController.matchesCurrentNetwork(match)) {
|
|
proxy = match;
|
|
int localPort = match.vlessLocalPort > 0 ? match.vlessLocalPort : 10808;
|
|
prefs.edit().putInt("proxy_port", localPort).apply();
|
|
}
|
|
}
|
|
final SharedConfig.ProxyInfo proxyToStart = proxy;
|
|
if (proxyToStart == null) return; // no builtin servers configured
|
|
SharedConfig.currentProxy = proxyToStart;
|
|
|
|
new Thread(() -> {
|
|
boolean ok = XrayController.start(proxyToStart.toVlessConfig());
|
|
if (ok) {
|
|
ConnectionsManager.setProxySettings(
|
|
true, "127.0.0.1", proxyToStart.vlessLocalPort, "", "", "");
|
|
} else {
|
|
FileLog.e("XrayController: failed to restore proxy on restart");
|
|
}
|
|
}, "xray-restore").start();
|
|
}
|
|
|
|
private static void autoEnableBuiltinProxy(android.content.SharedPreferences prefs) {
|
|
SharedConfig.loadProxyList();
|
|
SharedConfig.ProxyInfo proxy = XrayController.createBuiltinProxyForCurrentNetwork();
|
|
if (proxy == null) {
|
|
// No bundled servers configured in this build — mark as handled so we
|
|
// don't keep probing on every launch.
|
|
prefs.edit().putBoolean("builtin_proxy_autoenabled", true).apply();
|
|
return;
|
|
}
|
|
|
|
// Persist the same settings ProxyListActivity writes when a builtin
|
|
// proxy is selected, so the next launch restores it normally.
|
|
int localPort = proxy.vlessLocalPort > 0 ? proxy.vlessLocalPort : 10808;
|
|
prefs.edit()
|
|
.putString("proxy_ip", "127.0.0.1")
|
|
.putString("proxy_pass", "")
|
|
.putString("proxy_user", "")
|
|
.putInt("proxy_port", localPort)
|
|
.putString("proxy_secret", "")
|
|
.putBoolean("proxy_enabled", true)
|
|
.putBoolean("proxy_enabled_calls", false)
|
|
.putBoolean("builtin_proxy_autoenabled", true)
|
|
.apply();
|
|
SharedConfig.currentProxy = proxy;
|
|
|
|
final SharedConfig.ProxyInfo proxyToStart = proxy;
|
|
new Thread(() -> {
|
|
boolean ok = XrayController.start(proxyToStart.toVlessConfig());
|
|
if (ok) {
|
|
ConnectionsManager.setProxySettings(
|
|
true, "127.0.0.1", proxyToStart.vlessLocalPort, "", "", "");
|
|
} else {
|
|
FileLog.e("XrayController: failed to auto-enable builtin proxy on first launch");
|
|
}
|
|
}, "xray-autoenable").start();
|
|
}
|
|
|
|
private void initPushServices() {
|
|
AndroidUtilities.runOnUIThread(() -> {
|
|
if (getPushProvider().hasServices()) {
|
|
getPushProvider().onRequestPushToken();
|
|
} else {
|
|
if (BuildVars.LOGS_ENABLED) {
|
|
FileLog.d("No valid " + getPushProvider().getLogTitle() + " APK found.");
|
|
}
|
|
SharedConfig.pushStringStatus = "__NO_GOOGLE_PLAY_SERVICES__";
|
|
PushListenerController.sendRegistrationToServer(getPushProvider().getPushType(), null);
|
|
}
|
|
}, 1000);
|
|
}
|
|
|
|
private boolean checkPlayServices() {
|
|
try {
|
|
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
|
|
return resultCode == ConnectionResult.SUCCESS;
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static long lastNetworkCheck = -1;
|
|
private static void ensureCurrentNetworkGet() {
|
|
final long now = System.currentTimeMillis();
|
|
ensureCurrentNetworkGet(now - lastNetworkCheck > 5000);
|
|
lastNetworkCheck = now;
|
|
}
|
|
|
|
private static void ensureCurrentNetworkGet(boolean force) {
|
|
if (force || currentNetworkInfo == null) {
|
|
try {
|
|
if (connectivityManager == null) {
|
|
connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
}
|
|
currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
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);
|
|
}
|
|
}
|
|
} catch (Throwable ignore) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isRoaming() {
|
|
try {
|
|
ensureCurrentNetworkGet(false);
|
|
return currentNetworkInfo != null && currentNetworkInfo.isRoaming();
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean isConnectedOrConnectingToWiFi() {
|
|
try {
|
|
ensureCurrentNetworkGet(false);
|
|
if (currentNetworkInfo != null && (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET)) {
|
|
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);
|
|
if (currentNetworkInfo != null && (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) && currentNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
|
|
return true;
|
|
}
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean isConnectionSlow() {
|
|
try {
|
|
ensureCurrentNetworkGet(false);
|
|
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 int getAutodownloadNetworkType() {
|
|
try {
|
|
ensureCurrentNetworkGet(false);
|
|
if (currentNetworkInfo == null) {
|
|
return StatsController.TYPE_MOBILE;
|
|
}
|
|
if (currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI || currentNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (lastKnownNetworkType == StatsController.TYPE_MOBILE || lastKnownNetworkType == StatsController.TYPE_WIFI) && System.currentTimeMillis() - lastNetworkCheckTypeTime < 5000) {
|
|
return lastKnownNetworkType;
|
|
}
|
|
if (connectivityManager.isActiveNetworkMetered()) {
|
|
lastKnownNetworkType = StatsController.TYPE_MOBILE;
|
|
} else {
|
|
lastKnownNetworkType = StatsController.TYPE_WIFI;
|
|
}
|
|
lastNetworkCheckTypeTime = System.currentTimeMillis();
|
|
return lastKnownNetworkType;
|
|
}
|
|
if (currentNetworkInfo.isRoaming()) {
|
|
return StatsController.TYPE_ROAMING;
|
|
}
|
|
} catch (Exception e) {
|
|
FileLog.e(e);
|
|
}
|
|
return StatsController.TYPE_MOBILE;
|
|
}
|
|
|
|
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() {
|
|
try {
|
|
ensureCurrentNetworkGet(false);
|
|
if (currentNetworkInfo == null) {
|
|
return true;
|
|
}
|
|
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 isNetworkOnlineRealtime() {
|
|
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;
|
|
}
|
|
|
|
public static void startAppCenter(Activity context) {
|
|
applicationLoaderInstance.startAppCenterInternal(context);
|
|
}
|
|
|
|
public static void checkForUpdates() {
|
|
applicationLoaderInstance.checkForUpdatesInternal();
|
|
}
|
|
|
|
public static void appCenterLog(Throwable e) {
|
|
applicationLoaderInstance.appCenterLogInternal(e);
|
|
}
|
|
|
|
protected void appCenterLogInternal(Throwable e) {
|
|
|
|
}
|
|
|
|
protected void checkForUpdatesInternal() {
|
|
|
|
}
|
|
|
|
protected void startAppCenterInternal(Activity context) {
|
|
|
|
}
|
|
|
|
public static void logDualCamera(boolean success, boolean vendor) {
|
|
applicationLoaderInstance.logDualCameraInternal(success, vendor);
|
|
}
|
|
|
|
protected void logDualCameraInternal(boolean success, boolean vendor) {
|
|
|
|
}
|
|
|
|
public boolean checkApkInstallPermissions(final Context context) {
|
|
return false;
|
|
}
|
|
|
|
public boolean openApkInstall(Activity activity, TLRPC.Document document) {
|
|
return false;
|
|
}
|
|
|
|
public boolean showUpdateAppPopup(Context context, TLRPC.TL_help_appUpdate update, int account) {
|
|
return false;
|
|
}
|
|
|
|
public boolean showCustomUpdateAppPopup(Context context, BetaUpdate update, int account) {
|
|
return false;
|
|
}
|
|
|
|
public IUpdateLayout takeUpdateLayout(Activity activity, ViewGroup sideMenuContainer) {
|
|
return null;
|
|
}
|
|
|
|
public TLRPC.Update parseTLUpdate(int constructor) {
|
|
return null;
|
|
}
|
|
|
|
public void processUpdate(int currentAccount, TLRPC.Update update) {
|
|
|
|
}
|
|
|
|
public boolean onSuggestionFill(String suggestion, CharSequence[] output, boolean[] closeable) {
|
|
return false;
|
|
}
|
|
|
|
public boolean onSuggestionClick(String suggestion) {
|
|
return false;
|
|
}
|
|
|
|
public void addItemOptions(ItemOptions itemOptions) {
|
|
|
|
}
|
|
|
|
public boolean checkRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
return false;
|
|
}
|
|
|
|
public boolean consumePush(int account, JSONObject json) {
|
|
return false;
|
|
}
|
|
|
|
public void onResume() {
|
|
|
|
}
|
|
|
|
public boolean onPause() {
|
|
return false;
|
|
}
|
|
|
|
public BaseFragment openSettings(int n) {
|
|
return null;
|
|
}
|
|
|
|
public boolean isCustomUpdate() {
|
|
return false;
|
|
}
|
|
public void downloadUpdate() {}
|
|
public void cancelDownloadingUpdate() {}
|
|
public boolean isDownloadingUpdate() {
|
|
return false;
|
|
}
|
|
public float getDownloadingUpdateProgress() {
|
|
return 0.0f;
|
|
}
|
|
public void checkUpdate(boolean force, Runnable whenDone) {}
|
|
public BetaUpdate getUpdate() {
|
|
return null;
|
|
}
|
|
public File getDownloadedUpdateFile() {
|
|
return null;
|
|
}
|
|
}
|