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

466 lines
20 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.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).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.messenger;
2013-11-04 13:31:01 +01:00
import android.content.Context;
import android.content.SharedPreferences;
2018-07-30 04:07:02 +02:00
import android.content.pm.PackageInfo;
import android.os.SystemClock;
2013-12-20 20:25:49 +01:00
import android.util.Base64;
2013-11-04 13:31:01 +01:00
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.SerializedData;
import org.telegram.tgnet.TLRPC;
2013-10-25 17:19:00 +02:00
import java.io.File;
public class UserConfig {
2015-05-21 23:27:27 +02:00
2018-07-30 04:07:02 +02:00
public static int selectedAccount;
public final static int MAX_ACCOUNT_COUNT = 3;
private final Object sync = new Object();
private boolean configLoaded;
private TLRPC.User currentUser;
public boolean registeredForPush;
public int lastSendMessageId = -210000;
public int lastBroadcastId = -1;
public int contactsSavedCount;
public int clientUserId;
public boolean blockedUsersLoaded;
public int lastContactsSyncTime;
public int lastHintsSyncTime;
public boolean draftsLoaded;
public boolean pinnedDialogsLoaded = true;
public boolean unreadDialogsLoaded = true;
public TLRPC.TL_account_tmpPassword tmpPassword;
public int ratingLoadTime;
public int botRatingLoadTime;
public boolean contactsReimported;
public int migrateOffsetId = -1;
public int migrateOffsetDate = -1;
public int migrateOffsetUserId = -1;
public int migrateOffsetChatId = -1;
public int migrateOffsetChannelId = -1;
public long migrateOffsetAccess = -1;
public int totalDialogsLoadCount = 0;
public int dialogsLoadOffsetId = 0;
public int dialogsLoadOffsetDate = 0;
public int dialogsLoadOffsetUserId = 0;
public int dialogsLoadOffsetChatId = 0;
public int dialogsLoadOffsetChannelId = 0;
public long dialogsLoadOffsetAccess = 0;
public boolean notificationsSettingsLoaded;
public boolean syncContacts = true;
public boolean suggestContacts = true;
public boolean hasSecureData;
public int loginTime;
public TLRPC.TL_help_termsOfService unacceptedTermsOfService;
public TLRPC.TL_help_appUpdate pendingAppUpdate;
public int pendingAppUpdateBuildVersion;
public long pendingAppUpdateInstallTime;
public long lastUpdateCheckTime;
public volatile byte[] savedPasswordHash;
public volatile byte[] savedSaltedPassword;
public volatile long savedPasswordTime;
private int currentAccount;
private static volatile UserConfig[] Instance = new UserConfig[UserConfig.MAX_ACCOUNT_COUNT];
public static UserConfig getInstance(int num) {
UserConfig localInstance = Instance[num];
if (localInstance == null) {
synchronized (UserConfig.class) {
localInstance = Instance[num];
if (localInstance == null) {
Instance[num] = localInstance = new UserConfig(num);
}
}
}
return localInstance;
}
public static int getActivatedAccountsCount() {
int count = 0;
for (int a = 0; a < MAX_ACCOUNT_COUNT; a++) {
if (getInstance(a).isClientActivated()) {
count++;
}
}
return count;
}
public UserConfig(int instance) {
currentAccount = instance;
}
public int getNewMessageId() {
2013-10-25 17:19:00 +02:00
int id;
synchronized (sync) {
id = lastSendMessageId;
lastSendMessageId--;
}
return id;
}
2018-07-30 04:07:02 +02:00
public void saveConfig(boolean withFile) {
2013-12-20 20:25:49 +01:00
saveConfig(withFile, null);
}
2018-07-30 04:07:02 +02:00
public void saveConfig(boolean withFile, File oldFile) {
2013-10-25 17:19:00 +02:00
synchronized (sync) {
2013-12-20 20:25:49 +01:00
try {
2018-07-30 04:07:02 +02:00
SharedPreferences preferences;
if (currentAccount == 0) {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
} else {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfig" + currentAccount, Context.MODE_PRIVATE);
}
2013-11-04 13:31:01 +01:00
SharedPreferences.Editor editor = preferences.edit();
2018-07-30 04:07:02 +02:00
if (currentAccount == 0) {
editor.putInt("selectedAccount", selectedAccount);
}
editor.putBoolean("registeredForPush", registeredForPush);
editor.putInt("lastSendMessageId", lastSendMessageId);
2017-12-08 18:35:59 +01:00
editor.putInt("contactsSavedCount", contactsSavedCount);
editor.putInt("lastBroadcastId", lastBroadcastId);
editor.putBoolean("blockedUsersLoaded", blockedUsersLoaded);
2015-08-13 11:23:31 +02:00
editor.putInt("lastContactsSyncTime", lastContactsSyncTime);
2016-05-25 23:49:47 +02:00
editor.putInt("lastHintsSyncTime", lastHintsSyncTime);
editor.putBoolean("draftsLoaded", draftsLoaded);
2017-03-31 01:58:05 +02:00
editor.putBoolean("pinnedDialogsLoaded", pinnedDialogsLoaded);
2018-07-30 04:07:02 +02:00
editor.putBoolean("unreadDialogsLoaded", unreadDialogsLoaded);
2017-12-08 18:35:59 +01:00
editor.putInt("ratingLoadTime", ratingLoadTime);
editor.putInt("botRatingLoadTime", botRatingLoadTime);
editor.putBoolean("contactsReimported", contactsReimported);
2018-07-30 04:07:02 +02:00
editor.putInt("loginTime", loginTime);
editor.putBoolean("syncContacts", syncContacts);
editor.putBoolean("suggestContacts", suggestContacts);
editor.putBoolean("hasSecureData", hasSecureData);
editor.putBoolean("notificationsSettingsLoaded", notificationsSettingsLoaded);
2017-07-08 18:32:04 +02:00
editor.putInt("3migrateOffsetId", migrateOffsetId);
2015-11-26 22:04:02 +01:00
if (migrateOffsetId != -1) {
2017-07-08 18:32:04 +02:00
editor.putInt("3migrateOffsetDate", migrateOffsetDate);
editor.putInt("3migrateOffsetUserId", migrateOffsetUserId);
editor.putInt("3migrateOffsetChatId", migrateOffsetChatId);
editor.putInt("3migrateOffsetChannelId", migrateOffsetChannelId);
editor.putLong("3migrateOffsetAccess", migrateOffsetAccess);
2015-11-26 22:04:02 +01:00
}
2017-07-08 18:32:04 +02:00
2018-07-30 04:07:02 +02:00
if (unacceptedTermsOfService != null) {
try {
SerializedData data = new SerializedData(unacceptedTermsOfService.getObjectSize());
unacceptedTermsOfService.serializeToStream(data);
String str = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
editor.putString("terms", str);
data.cleanup();
} catch (Exception ignore) {
}
} else {
editor.remove("terms");
}
if (currentAccount == 0) {
if (pendingAppUpdate != null) {
try {
SerializedData data = new SerializedData(pendingAppUpdate.getObjectSize());
pendingAppUpdate.serializeToStream(data);
String str = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
editor.putString("appUpdate", str);
editor.putInt("appUpdateBuild", pendingAppUpdateBuildVersion);
editor.putLong("appUpdateTime", pendingAppUpdateInstallTime);
editor.putLong("appUpdateCheckTime", lastUpdateCheckTime);
data.cleanup();
} catch (Exception ignore) {
}
} else {
editor.remove("appUpdate");
}
}
2017-07-08 18:32:04 +02:00
editor.putInt("2totalDialogsLoadCount", totalDialogsLoadCount);
editor.putInt("2dialogsLoadOffsetId", dialogsLoadOffsetId);
editor.putInt("2dialogsLoadOffsetDate", dialogsLoadOffsetDate);
editor.putInt("2dialogsLoadOffsetUserId", dialogsLoadOffsetUserId);
editor.putInt("2dialogsLoadOffsetChatId", dialogsLoadOffsetChatId);
editor.putInt("2dialogsLoadOffsetChannelId", dialogsLoadOffsetChannelId);
editor.putLong("2dialogsLoadOffsetAccess", dialogsLoadOffsetAccess);
2018-07-30 04:07:02 +02:00
SharedConfig.saveConfig();
2017-03-31 01:58:05 +02:00
if (tmpPassword != null) {
SerializedData data = new SerializedData();
tmpPassword.serializeToStream(data);
String string = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
editor.putString("tmpPassword", string);
data.cleanup();
} else {
editor.remove("tmpPassword");
}
2015-11-26 22:04:02 +01:00
2013-12-20 20:25:49 +01:00
if (currentUser != null) {
if (withFile) {
SerializedData data = new SerializedData();
currentUser.serializeToStream(data);
2017-03-31 01:58:05 +02:00
String string = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
editor.putString("user", string);
2015-02-27 20:57:58 +01:00
data.cleanup();
2013-11-04 13:31:01 +01:00
}
2013-12-20 20:25:49 +01:00
} else {
editor.remove("user");
}
2015-05-21 23:27:27 +02:00
2013-12-20 20:25:49 +01:00
editor.commit();
if (oldFile != null) {
oldFile.delete();
2013-10-25 17:19:00 +02:00
}
2013-12-20 20:25:49 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2013-10-25 17:19:00 +02:00
}
}
}
2018-07-30 04:07:02 +02:00
public boolean isClientActivated() {
2014-06-13 12:42:21 +02:00
synchronized (sync) {
return currentUser != null;
}
}
2018-07-30 04:07:02 +02:00
public int getClientUserId() {
2014-06-13 12:42:21 +02:00
synchronized (sync) {
return currentUser != null ? currentUser.id : 0;
}
}
2018-07-30 04:07:02 +02:00
public String getClientPhone() {
synchronized (sync) {
return currentUser != null && currentUser.phone != null ? currentUser.phone : "";
}
}
public TLRPC.User getCurrentUser() {
2014-06-13 12:42:21 +02:00
synchronized (sync) {
return currentUser;
}
}
2018-07-30 04:07:02 +02:00
public void setCurrentUser(TLRPC.User user) {
2014-06-13 12:42:21 +02:00
synchronized (sync) {
currentUser = user;
2018-07-30 04:07:02 +02:00
clientUserId = user.id;
2014-06-13 12:42:21 +02:00
}
}
2018-07-30 04:07:02 +02:00
public void loadConfig() {
2013-10-25 17:19:00 +02:00
synchronized (sync) {
2017-12-08 18:35:59 +01:00
if (configLoaded) {
return;
}
2018-07-30 04:07:02 +02:00
SharedPreferences preferences;
if (currentAccount == 0) {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
selectedAccount = preferences.getInt("selectedAccount", 0);
} else {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfig" + currentAccount, Context.MODE_PRIVATE);
}
registeredForPush = preferences.getBoolean("registeredForPush", false);
lastSendMessageId = preferences.getInt("lastSendMessageId", -210000);
contactsSavedCount = preferences.getInt("contactsSavedCount", 0);
lastBroadcastId = preferences.getInt("lastBroadcastId", -1);
blockedUsersLoaded = preferences.getBoolean("blockedUsersLoaded", false);
lastContactsSyncTime = preferences.getInt("lastContactsSyncTime", (int) (System.currentTimeMillis() / 1000) - 23 * 60 * 60);
lastHintsSyncTime = preferences.getInt("lastHintsSyncTime", (int) (System.currentTimeMillis() / 1000) - 25 * 60 * 60);
draftsLoaded = preferences.getBoolean("draftsLoaded", false);
pinnedDialogsLoaded = preferences.getBoolean("pinnedDialogsLoaded", false);
unreadDialogsLoaded = preferences.getBoolean("unreadDialogsLoaded", false);
contactsReimported = preferences.getBoolean("contactsReimported", false);
ratingLoadTime = preferences.getInt("ratingLoadTime", 0);
botRatingLoadTime = preferences.getInt("botRatingLoadTime", 0);
loginTime = preferences.getInt("loginTime", currentAccount);
syncContacts = preferences.getBoolean("syncContacts", true);
suggestContacts = preferences.getBoolean("suggestContacts", true);
hasSecureData = preferences.getBoolean("hasSecureData", false);
notificationsSettingsLoaded = preferences.getBoolean("notificationsSettingsLoaded", false);
try {
String terms = preferences.getString("terms", null);
if (terms != null) {
byte[] arr = Base64.decode(terms, Base64.DEFAULT);
if (arr != null) {
SerializedData data = new SerializedData(arr);
unacceptedTermsOfService = TLRPC.TL_help_termsOfService.TLdeserialize(data, data.readInt32(false), false);
data.cleanup();
}
}
} catch (Exception e) {
FileLog.e(e);
}
if (currentAccount == 0) {
lastUpdateCheckTime = preferences.getLong("appUpdateCheckTime", System.currentTimeMillis());
2013-10-25 17:19:00 +02:00
try {
2018-07-30 04:07:02 +02:00
String update = preferences.getString("appUpdate", null);
if (update != null) {
pendingAppUpdateBuildVersion = preferences.getInt("appUpdateBuild", BuildVars.BUILD_VERSION);
pendingAppUpdateInstallTime = preferences.getLong("appUpdateTime", System.currentTimeMillis());
byte[] arr = Base64.decode(update, Base64.DEFAULT);
if (arr != null) {
SerializedData data = new SerializedData(arr);
pendingAppUpdate = (TLRPC.TL_help_appUpdate) TLRPC.help_AppUpdate.TLdeserialize(data, data.readInt32(false), false);
data.cleanup();
2013-10-25 17:19:00 +02:00
}
}
2018-07-30 04:07:02 +02:00
if (pendingAppUpdate != null) {
long updateTime = 0;
try {
PackageInfo packageInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
updateTime = Math.max(packageInfo.lastUpdateTime, packageInfo.firstInstallTime);
} catch (Exception e) {
FileLog.e(e);
}
if (pendingAppUpdateBuildVersion != BuildVars.BUILD_VERSION || pendingAppUpdateInstallTime < updateTime) {
pendingAppUpdate = null;
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
saveConfig(false);
}
});
2013-12-20 20:25:49 +01:00
}
2018-07-30 04:07:02 +02:00
}
2013-10-25 17:19:00 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2013-12-20 20:25:49 +01:00
}
2018-07-30 04:07:02 +02:00
}
2017-07-08 18:32:04 +02:00
2018-07-30 04:07:02 +02:00
migrateOffsetId = preferences.getInt("3migrateOffsetId", 0);
if (migrateOffsetId != -1) {
migrateOffsetDate = preferences.getInt("3migrateOffsetDate", 0);
migrateOffsetUserId = preferences.getInt("3migrateOffsetUserId", 0);
migrateOffsetChatId = preferences.getInt("3migrateOffsetChatId", 0);
migrateOffsetChannelId = preferences.getInt("3migrateOffsetChannelId", 0);
migrateOffsetAccess = preferences.getLong("3migrateOffsetAccess", 0);
}
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
dialogsLoadOffsetId = preferences.getInt("2dialogsLoadOffsetId", -1);
totalDialogsLoadCount = preferences.getInt("2totalDialogsLoadCount", 0);
dialogsLoadOffsetDate = preferences.getInt("2dialogsLoadOffsetDate", -1);
dialogsLoadOffsetUserId = preferences.getInt("2dialogsLoadOffsetUserId", -1);
dialogsLoadOffsetChatId = preferences.getInt("2dialogsLoadOffsetChatId", -1);
dialogsLoadOffsetChannelId = preferences.getInt("2dialogsLoadOffsetChannelId", -1);
dialogsLoadOffsetAccess = preferences.getLong("2dialogsLoadOffsetAccess", -1);
String string = preferences.getString("tmpPassword", null);
if (string != null) {
byte[] bytes = Base64.decode(string, Base64.DEFAULT);
if (bytes != null) {
SerializedData data = new SerializedData(bytes);
tmpPassword = TLRPC.TL_account_tmpPassword.TLdeserialize(data, data.readInt32(false), false);
data.cleanup();
2015-05-21 23:27:27 +02:00
}
2018-07-30 04:07:02 +02:00
}
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
string = preferences.getString("user", null);
if (string != null) {
byte[] bytes = Base64.decode(string, Base64.DEFAULT);
if (bytes != null) {
SerializedData data = new SerializedData(bytes);
currentUser = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.cleanup();
2017-03-31 01:58:05 +02:00
}
2015-05-21 23:27:27 +02:00
}
2018-07-30 04:07:02 +02:00
if (currentUser != null) {
clientUserId = currentUser.id;
}
2017-12-08 18:35:59 +01:00
configLoaded = true;
2015-05-21 23:27:27 +02:00
}
}
2018-07-30 04:07:02 +02:00
public void savePassword(byte[] hash, byte[] salted) {
savedPasswordTime = SystemClock.elapsedRealtime();
savedPasswordHash = hash;
savedSaltedPassword = salted;
}
public void checkSavedPassword() {
if (savedSaltedPassword == null && savedPasswordHash == null || Math.abs(SystemClock.elapsedRealtime() - savedPasswordTime) < 30 * 60 * 1000) {
return;
}
resetSavedPassword();
}
public void resetSavedPassword() {
savedPasswordTime = 0;
if (savedPasswordHash != null) {
for (int a = 0; a < savedPasswordHash.length; a++) {
savedPasswordHash[a] = 0;
2015-05-21 23:27:27 +02:00
}
2018-07-30 04:07:02 +02:00
savedPasswordHash = null;
}
if (savedSaltedPassword != null) {
for (int a = 0; a < savedSaltedPassword.length; a++) {
savedSaltedPassword[a] = 0;
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
savedSaltedPassword = null;
2013-10-25 17:19:00 +02:00
}
}
2018-07-30 04:07:02 +02:00
public void clearConfig() {
2013-10-25 17:19:00 +02:00
currentUser = null;
2018-07-30 04:07:02 +02:00
clientUserId = 0;
2013-10-25 17:19:00 +02:00
registeredForPush = false;
2017-12-08 18:35:59 +01:00
contactsSavedCount = 0;
2013-12-20 20:25:49 +01:00
lastSendMessageId = -210000;
lastBroadcastId = -1;
blockedUsersLoaded = false;
2018-07-30 04:07:02 +02:00
notificationsSettingsLoaded = false;
2015-11-26 22:04:02 +01:00
migrateOffsetId = -1;
migrateOffsetDate = -1;
migrateOffsetUserId = -1;
migrateOffsetChatId = -1;
migrateOffsetChannelId = -1;
migrateOffsetAccess = -1;
2017-07-08 18:32:04 +02:00
dialogsLoadOffsetId = 0;
totalDialogsLoadCount = 0;
dialogsLoadOffsetDate = 0;
dialogsLoadOffsetUserId = 0;
dialogsLoadOffsetChatId = 0;
dialogsLoadOffsetChannelId = 0;
dialogsLoadOffsetAccess = 0;
2017-12-08 18:35:59 +01:00
ratingLoadTime = 0;
botRatingLoadTime = 0;
draftsLoaded = true;
2017-12-08 18:35:59 +01:00
contactsReimported = true;
2018-07-30 04:07:02 +02:00
syncContacts = true;
suggestContacts = true;
2017-03-31 01:58:05 +02:00
pinnedDialogsLoaded = false;
2018-07-30 04:07:02 +02:00
unreadDialogsLoaded = true;
unacceptedTermsOfService = null;
pendingAppUpdate = null;
hasSecureData = false;
loginTime = (int) (System.currentTimeMillis() / 1000);
2015-08-13 11:23:31 +02:00
lastContactsSyncTime = (int) (System.currentTimeMillis() / 1000) - 23 * 60 * 60;
2016-05-25 23:49:47 +02:00
lastHintsSyncTime = (int) (System.currentTimeMillis() / 1000) - 25 * 60 * 60;
2018-07-30 04:07:02 +02:00
resetSavedPassword();
boolean hasActivated = false;
for (int a = 0; a < MAX_ACCOUNT_COUNT; a++) {
if (UserConfig.getInstance(a).isClientActivated()) {
hasActivated = true;
break;
}
}
if (!hasActivated) {
SharedConfig.clearConfig();
}
2013-11-04 13:31:01 +01:00
saveConfig(true);
2013-10-25 17:19:00 +02:00
}
}