-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathApplicationLoader.java
696 lines (595 loc) · 24.4 KB
/
ApplicationLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
* 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.pm.PackageManager;
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.util.Pair;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.multidex.MultiDex;
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.Adapters.DrawerLayoutAdapter;
import org.telegram.ui.Components.ForegroundDetector;
import org.telegram.ui.Components.Premium.boosts.BoostRepository;
import org.telegram.ui.IUpdateButton;
import org.telegram.ui.IUpdateLayout;
import org.telegram.ui.LauncherIconController;
import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
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;
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);
MultiDex.install(this);
}
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();
}
protected boolean isHuaweiBuild() {
return false;
}
protected boolean isStandalone() {
return false;
}
protected boolean isBeta() {
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 void postInitApplication() {
if (applicationInited || applicationContext == null) {
return;
}
applicationInited = true;
NativeLoader.initNativeLibs(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++) {
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();
SharedPrefsHelper.init(applicationContext);
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
UserConfig.getInstance(a).loadConfig();
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");
}
MediaController.getInstance();
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { //TODO improve account
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();
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);
LauncherIconController.tryFixLauncherIconIfNeeded();
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 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 sideMenu, ViewGroup sideMenuContainer) {
return null;
}
public IUpdateButton takeUpdateButton(Context context) {
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 boolean extendDrawer(ArrayList<DrawerLayoutAdapter.Item> items) {
return false;
}
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;
}
}