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

426 lines
17 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-05-21 23:27:27 +02:00
* This is the source code of Telegram for Android v. 2.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).
*
2015-05-21 23:27:27 +02:00
* Copyright Nikolai Kudashov, 2013-2015.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.messenger;
2013-10-25 17:19:00 +02:00
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-09-24 22:52:02 +02:00
import android.content.pm.PackageInfo;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
2013-10-25 17:19:00 +02:00
import android.os.AsyncTask;
2014-10-30 22:27:41 +01:00
import android.os.Build;
import android.os.Handler;
2014-04-03 12:23:39 +02:00
import android.os.PowerManager;
2015-09-24 22:52:02 +02:00
import android.util.Base64;
2013-10-25 17:19:00 +02:00
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.SerializedData;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.Components.ForegroundDetector;
2013-10-25 17:19:00 +02:00
import java.io.File;
2015-09-24 22:52:02 +02:00
import java.io.RandomAccessFile;
2013-10-25 17:19:00 +02:00
import java.util.concurrent.atomic.AtomicInteger;
public class ApplicationLoader extends Application {
2013-10-25 17:19:00 +02:00
private GoogleCloudMessaging gcm;
private AtomicInteger msgId = new AtomicInteger();
private String regid;
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static Drawable cachedWallpaper;
private static int selectedColor;
private static boolean isCustomTheme;
private static final Object sync = new Object();
2013-10-25 17:19:00 +02:00
public static volatile Context applicationContext;
public static volatile Handler applicationHandler;
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;
2013-10-25 17:19:00 +02:00
public static boolean isCustomTheme() {
return isCustomTheme;
}
public static int getSelectedColor() {
return selectedColor;
}
public static void reloadWallpaper() {
cachedWallpaper = null;
loadWallpaper();
}
public static void loadWallpaper() {
if (cachedWallpaper != null) {
return;
}
Utilities.searchQueue.postRunnable(new Runnable() {
@Override
public void run() {
synchronized (sync) {
int selectedColor = 0;
try {
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
int selectedBackground = preferences.getInt("selectedBackground", 1000001);
selectedColor = preferences.getInt("selectedColor", 0);
if (selectedColor == 0) {
if (selectedBackground == 1000001) {
cachedWallpaper = applicationContext.getResources().getDrawable(R.drawable.background_hd);
isCustomTheme = false;
} else {
File toFile = new File(ApplicationLoader.applicationContext.getFilesDir(), "wallpaper.jpg");
if (toFile.exists()) {
cachedWallpaper = Drawable.createFromPath(toFile.getAbsolutePath());
isCustomTheme = true;
} else {
cachedWallpaper = applicationContext.getResources().getDrawable(R.drawable.background_hd);
isCustomTheme = false;
}
}
}
} catch (Throwable throwable) {
//ignore
}
if (cachedWallpaper == null) {
if (selectedColor == 0) {
selectedColor = -2693905;
}
cachedWallpaper = new ColorDrawable(selectedColor);
}
}
}
});
}
public static Drawable getCachedWallpaper() {
synchronized (sync) {
return cachedWallpaper;
}
}
2015-09-24 22:52:02 +02:00
private static void convertConfig() {
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("dataconfig", Context.MODE_PRIVATE);
if (preferences.contains("currentDatacenterId")) {
SerializedData buffer = new SerializedData(32 * 1024);
buffer.writeInt32(2);
buffer.writeBool(preferences.getInt("datacenterSetId", 0) != 0);
buffer.writeBool(true);
buffer.writeInt32(preferences.getInt("currentDatacenterId", 0));
buffer.writeInt32(preferences.getInt("timeDifference", 0));
buffer.writeInt32(preferences.getInt("lastDcUpdateTime", 0));
buffer.writeInt64(preferences.getLong("pushSessionId", 0));
buffer.writeBool(false);
buffer.writeInt32(0);
try {
String datacentersString = preferences.getString("datacenters", null);
if (datacentersString != null) {
byte[] datacentersBytes = Base64.decode(datacentersString, Base64.DEFAULT);
if (datacentersBytes != null) {
SerializedData data = new SerializedData(datacentersBytes);
buffer.writeInt32(data.readInt32(false));
buffer.writeBytes(datacentersBytes, 4, datacentersBytes.length - 4);
data.cleanup();
}
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
try {
File file = new File(ApplicationLoader.applicationContext.getFilesDir(), "tgnet.dat");
RandomAccessFile fileOutputStream = new RandomAccessFile(file, "rws");
byte[] bytes = buffer.toByteArray();
fileOutputStream.writeInt(Integer.reverseBytes(bytes.length));
fileOutputStream.write(bytes);
fileOutputStream.close();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
buffer.cleanup();
preferences.edit().clear().commit();
}
}
public static void postInitApplication() {
if (applicationInited) {
return;
}
2014-04-03 12:23:39 +02:00
applicationInited = true;
2015-09-24 22:52:02 +02:00
convertConfig();
2013-10-25 17:19:00 +02:00
try {
LocaleController.getInstance();
} 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 {
PowerManager pm = (PowerManager)ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
isScreenOn = pm.isScreenOn();
FileLog.e("tmessages", "screen state = " + isScreenOn);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2013-10-25 17:19:00 +02:00
UserConfig.loadConfig();
2015-09-24 22:52:02 +02:00
String deviceModel;
String langCode;
String appVersion;
String systemVersion;
String configPath = ApplicationLoader.applicationContext.getFilesDir().toString();
try {
langCode = LocaleController.getLocaleString(LocaleController.getInstance().getSystemDefaultLocale());
deviceModel = Build.MANUFACTURER + Build.MODEL;
PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
appVersion = pInfo.versionName + " (" + pInfo.versionCode + ")";
systemVersion = "SDK " + Build.VERSION.SDK_INT;
} catch (Exception e) {
langCode = "en";
deviceModel = "Android unknown";
appVersion = "App version unknown";
systemVersion = "SDK " + Build.VERSION.SDK_INT;
}
if (langCode.length() == 0) {
langCode = "en";
}
if (deviceModel.length() == 0) {
deviceModel = "Android unknown";
}
if (appVersion.length() == 0) {
appVersion = "App version unknown";
}
if (systemVersion.length() == 0) {
systemVersion = "SDK Unknown";
}
2015-05-21 23:27:27 +02:00
MessagesController.getInstance();
2015-09-24 22:52:02 +02:00
ConnectionsManager.getInstance().init(BuildVars.BUILD_VERSION, TLRPC.LAYER, BuildVars.APP_ID, deviceModel, systemVersion, appVersion, langCode, configPath, UserConfig.getClientUserId());
2014-06-13 12:42:21 +02:00
if (UserConfig.getCurrentUser() != null) {
MessagesController.getInstance().putUser(UserConfig.getCurrentUser(), true);
2014-06-13 12:42:21 +02:00
ConnectionsManager.getInstance().applyCountryPortNumber(UserConfig.getCurrentUser().phone);
MessagesController.getInstance().getBlockedUsers(true);
SendMessagesHelper.getInstance().checkUnsentMessages();
2013-10-25 17:19:00 +02:00
}
ApplicationLoader app = (ApplicationLoader)ApplicationLoader.applicationContext;
app.initPlayServices();
FileLog.e("tmessages", "app initied");
2014-07-20 01:31:49 +02:00
ContactsController.getInstance().checkAppAccount();
MediaController.getInstance();
}
@Override
public void onCreate() {
super.onCreate();
2014-10-30 22:27:41 +01:00
if (Build.VERSION.SDK_INT < 11) {
java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
}
applicationContext = getApplicationContext();
NativeLoader.initNativeLibs(ApplicationLoader.applicationContext);
2015-09-24 22:52:02 +02:00
ConnectionsManager.native_setJava(Build.VERSION.SDK_INT == 14 || Build.VERSION.SDK_INT == 15);
2014-03-25 01:25:32 +01:00
if (Build.VERSION.SDK_INT >= 14) {
new ForegroundDetector(this);
}
applicationHandler = new Handler(applicationContext.getMainLooper());
startPushService();
}
public static void startPushService() {
SharedPreferences preferences = applicationContext.getSharedPreferences("Notifications", MODE_PRIVATE);
if (preferences.getBoolean("pushService", true)) {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
if (android.os.Build.VERSION.SDK_INT >= 19) {
2014-06-20 21:34:16 +02:00
// Calendar cal = Calendar.getInstance();
// PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
// AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
// alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30000, pintent);
PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
2014-06-20 21:34:16 +02:00
AlarmManager alarm = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
}
} else {
stopPushService();
}
}
public static void stopPushService() {
applicationContext.stopService(new Intent(applicationContext, NotificationsService.class));
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);
AndroidUtilities.checkDisplaySize();
2014-03-25 11:58:47 +01:00
} catch (Exception e) {
e.printStackTrace();
}
}
private void initPlayServices() {
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId();
if (regid.length() == 0) {
registerInBackground();
} else {
sendRegistrationIdToBackend(false);
}
} else {
FileLog.d("tmessages", "No valid Google Play Services APK found.");
}
2013-10-25 17:19:00 +02:00
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
return resultCode == ConnectionResult.SUCCESS;
/*if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i("tmessages", "This device is not supported.");
}
return false;
}
return true;*/
}
private String getRegistrationId() {
final SharedPreferences prefs = getGCMPreferences(applicationContext);
2013-10-25 17:19:00 +02:00
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.length() == 0) {
2013-12-20 20:25:49 +01:00
FileLog.d("tmessages", "Registration not found.");
2013-10-25 17:19:00 +02:00
return "";
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
if (registeredVersion != BuildVars.BUILD_VERSION) {
2013-12-20 20:25:49 +01:00
FileLog.d("tmessages", "App version changed.");
2013-10-25 17:19:00 +02:00
return "";
}
return registrationId;
}
private SharedPreferences getGCMPreferences(Context context) {
return getSharedPreferences(ApplicationLoader.class.getSimpleName(), Context.MODE_PRIVATE);
}
private void registerInBackground() {
AsyncTask<String, String, Boolean> task = new AsyncTask<String, String, Boolean>() {
@Override
protected Boolean doInBackground(String... objects) {
2013-12-20 20:25:49 +01:00
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(applicationContext);
}
int count = 0;
while (count < 1000) {
try {
count++;
regid = gcm.register(BuildVars.GCM_SENDER_ID);
2013-12-20 20:25:49 +01:00
sendRegistrationIdToBackend(true);
storeRegistrationId(applicationContext, regid);
return true;
} catch (Exception e) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
}
try {
if (count % 20 == 0) {
Thread.sleep(60000 * 30);
} else {
Thread.sleep(5000);
}
} catch (InterruptedException e) {
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
}
return false;
}
};
if (android.os.Build.VERSION.SDK_INT >= 11) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
} else {
task.execute(null, null, null);
}
2013-10-25 17:19:00 +02:00
}
private void sendRegistrationIdToBackend(final boolean isNew) {
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
UserConfig.pushString = regid;
UserConfig.registeredForPush = !isNew;
2013-11-04 13:31:01 +01:00
UserConfig.saveConfig(false);
if (UserConfig.getClientUserId() != 0) {
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
MessagesController.getInstance().registerForPush(regid);
}
});
}
2013-10-25 17:19:00 +02:00
}
});
}
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getGCMPreferences(context);
int appVersion = BuildVars.BUILD_VERSION;
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", "Saving regId on app version " + appVersion);
2013-10-25 17:19:00 +02:00
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
}