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

294 lines
13 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).
*
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-11-04 13:31:01 +01:00
import android.content.Context;
import android.content.SharedPreferences;
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
2014-06-13 12:42:21 +02:00
private static TLRPC.User currentUser;
2013-10-25 17:19:00 +02:00
public static boolean registeredForPush = false;
public static String pushString = "";
2013-12-20 20:25:49 +01:00
public static int lastSendMessageId = -210000;
public static int lastLocalId = -210000;
public static int lastBroadcastId = -1;
2013-10-25 17:19:00 +02:00
public static String contactsHash = "";
public static String importHash = "";
public static boolean blockedUsersLoaded = false;
private final static Object sync = new Object();
2013-10-25 17:19:00 +02:00
public static boolean saveIncomingPhotos = false;
public static int contactsVersion = 1;
public static String passcodeHash = "";
2015-05-21 23:27:27 +02:00
public static byte[] passcodeSalt = new byte[0];
public static boolean appLocked = false;
public static int passcodeType = 0;
public static int autoLockIn = 60 * 60;
public static int lastPauseTime = 0;
public static boolean isWaitingForPasscodeEnter = false;
2015-10-29 18:10:07 +01:00
public static boolean useFingerprint = true;
public static int lastUpdateVersion;
2015-08-13 11:23:31 +02:00
public static int lastContactsSyncTime;
2015-09-24 22:52:02 +02:00
public static boolean channelsLoaded = false;
2013-10-25 17:19:00 +02:00
public static int getNewMessageId() {
int id;
synchronized (sync) {
id = lastSendMessageId;
lastSendMessageId--;
}
return id;
}
2013-11-04 13:31:01 +01:00
public static void saveConfig(boolean withFile) {
2013-12-20 20:25:49 +01:00
saveConfig(withFile, null);
}
public static 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 {
2013-11-04 13:31:01 +01:00
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("registeredForPush", registeredForPush);
editor.putString("pushString", pushString);
editor.putInt("lastSendMessageId", lastSendMessageId);
editor.putInt("lastLocalId", lastLocalId);
editor.putString("contactsHash", contactsHash);
editor.putString("importHash", importHash);
editor.putBoolean("saveIncomingPhotos", saveIncomingPhotos);
editor.putInt("contactsVersion", contactsVersion);
editor.putInt("lastBroadcastId", lastBroadcastId);
editor.putBoolean("blockedUsersLoaded", blockedUsersLoaded);
editor.putString("passcodeHash1", passcodeHash);
2015-05-21 23:27:27 +02:00
editor.putString("passcodeSalt", passcodeSalt.length > 0 ? Base64.encodeToString(passcodeSalt, Base64.DEFAULT) : "");
editor.putBoolean("appLocked", appLocked);
editor.putInt("passcodeType", passcodeType);
editor.putInt("autoLockIn", autoLockIn);
editor.putInt("lastPauseTime", lastPauseTime);
editor.putInt("lastUpdateVersion", lastUpdateVersion);
2015-08-13 11:23:31 +02:00
editor.putInt("lastContactsSyncTime", lastContactsSyncTime);
2015-09-24 22:52:02 +02:00
editor.putBoolean("channelsLoaded", channelsLoaded);
2015-10-29 18:10:07 +01:00
editor.putBoolean("useFingerprint", useFingerprint);
2013-12-20 20:25:49 +01:00
if (currentUser != null) {
if (withFile) {
SerializedData data = new SerializedData();
currentUser.serializeToStream(data);
String userString = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
editor.putString("user", userString);
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) {
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
}
}
2014-06-13 12:42:21 +02:00
public static boolean isClientActivated() {
synchronized (sync) {
return currentUser != null;
}
}
public static int getClientUserId() {
synchronized (sync) {
return currentUser != null ? currentUser.id : 0;
}
}
public static TLRPC.User getCurrentUser() {
synchronized (sync) {
return currentUser;
}
}
public static void setCurrentUser(TLRPC.User user) {
synchronized (sync) {
currentUser = user;
}
}
2013-10-25 17:19:00 +02:00
public static void loadConfig() {
synchronized (sync) {
2015-10-29 18:10:07 +01:00
final File configFile = new File(ApplicationLoader.getFilesDirFixed(), "user.dat");
2013-10-25 17:19:00 +02:00
if (configFile.exists()) {
try {
SerializedData data = new SerializedData(configFile);
int ver = data.readInt32(false);
2013-11-04 13:31:01 +01:00
if (ver == 1) {
int constructor = data.readInt32(false);
2015-06-29 19:12:11 +02:00
currentUser = TLRPC.User.TLdeserialize(data, constructor, false);
MessagesStorage.lastDateValue = data.readInt32(false);
MessagesStorage.lastPtsValue = data.readInt32(false);
MessagesStorage.lastSeqValue = data.readInt32(false);
registeredForPush = data.readBool(false);
pushString = data.readString(false);
lastSendMessageId = data.readInt32(false);
lastLocalId = data.readInt32(false);
contactsHash = data.readString(false);
importHash = data.readString(false);
saveIncomingPhotos = data.readBool(false);
contactsVersion = 0;
MessagesStorage.lastQtsValue = data.readInt32(false);
MessagesStorage.lastSecretVersion = data.readInt32(false);
int val = data.readInt32(false);
2013-10-25 17:19:00 +02:00
if (val == 1) {
MessagesStorage.secretPBytes = data.readByteArray(false);
2013-10-25 17:19:00 +02:00
}
MessagesStorage.secretG = data.readInt32(false);
2013-12-20 20:25:49 +01:00
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
saveConfig(true, configFile);
}
});
2013-11-04 13:31:01 +01:00
} else if (ver == 2) {
int constructor = data.readInt32(false);
2015-06-29 19:12:11 +02:00
currentUser = TLRPC.User.TLdeserialize(data, constructor, false);
2013-11-04 13:31:01 +01:00
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
registeredForPush = preferences.getBoolean("registeredForPush", false);
pushString = preferences.getString("pushString", "");
2013-12-20 20:25:49 +01:00
lastSendMessageId = preferences.getInt("lastSendMessageId", -210000);
lastLocalId = preferences.getInt("lastLocalId", -210000);
2013-11-04 13:31:01 +01:00
contactsHash = preferences.getString("contactsHash", "");
importHash = preferences.getString("importHash", "");
2013-11-04 13:31:01 +01:00
saveIncomingPhotos = preferences.getBoolean("saveIncomingPhotos", false);
contactsVersion = preferences.getInt("contactsVersion", 0);
2013-10-25 17:19:00 +02:00
}
2013-12-20 20:25:49 +01:00
if (lastLocalId > -210000) {
lastLocalId = -210000;
}
if (lastSendMessageId > -210000) {
lastSendMessageId = -210000;
}
2015-02-27 20:57:58 +01:00
data.cleanup();
2013-12-20 20:25:49 +01:00
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
saveConfig(true, configFile);
}
});
2013-10-25 17:19:00 +02:00
} catch (Exception e) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
}
} else {
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
registeredForPush = preferences.getBoolean("registeredForPush", false);
pushString = preferences.getString("pushString", "");
lastSendMessageId = preferences.getInt("lastSendMessageId", -210000);
lastLocalId = preferences.getInt("lastLocalId", -210000);
contactsHash = preferences.getString("contactsHash", "");
importHash = preferences.getString("importHash", "");
2013-12-20 20:25:49 +01:00
saveIncomingPhotos = preferences.getBoolean("saveIncomingPhotos", false);
contactsVersion = preferences.getInt("contactsVersion", 0);
lastBroadcastId = preferences.getInt("lastBroadcastId", -1);
blockedUsersLoaded = preferences.getBoolean("blockedUsersLoaded", false);
passcodeHash = preferences.getString("passcodeHash1", "");
appLocked = preferences.getBoolean("appLocked", false);
passcodeType = preferences.getInt("passcodeType", 0);
autoLockIn = preferences.getInt("autoLockIn", 60 * 60);
lastPauseTime = preferences.getInt("lastPauseTime", 0);
2015-10-29 18:10:07 +01:00
useFingerprint = preferences.getBoolean("useFingerprint", true);
lastUpdateVersion = preferences.getInt("lastUpdateVersion", 511);
2015-08-13 11:23:31 +02:00
lastContactsSyncTime = preferences.getInt("lastContactsSyncTime", (int) (System.currentTimeMillis() / 1000) - 23 * 60 * 60);
2015-09-24 22:52:02 +02:00
channelsLoaded = preferences.getBoolean("channelsLoaded", false);
2013-12-20 20:25:49 +01:00
String user = preferences.getString("user", null);
if (user != null) {
byte[] userBytes = Base64.decode(user, Base64.DEFAULT);
if (userBytes != null) {
SerializedData data = new SerializedData(userBytes);
2015-06-29 19:12:11 +02:00
currentUser = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
2015-02-27 20:57:58 +01:00
data.cleanup();
2013-12-20 20:25:49 +01:00
}
}
2015-05-21 23:27:27 +02:00
String passcodeSaltString = preferences.getString("passcodeSalt", "");
if (passcodeSaltString.length() > 0) {
passcodeSalt = Base64.decode(passcodeSaltString, Base64.DEFAULT);
} else {
passcodeSalt = new byte[0];
}
}
}
}
public static boolean checkPasscode(String passcode) {
if (passcodeSalt.length == 0) {
boolean result = Utilities.MD5(passcode).equals(passcodeHash);
if (result) {
try {
passcodeSalt = new byte[16];
Utilities.random.nextBytes(passcodeSalt);
byte[] passcodeBytes = passcode.getBytes("UTF-8");
byte[] bytes = new byte[32 + passcodeBytes.length];
System.arraycopy(passcodeSalt, 0, bytes, 0, 16);
System.arraycopy(passcodeBytes, 0, bytes, 16, passcodeBytes.length);
System.arraycopy(passcodeSalt, 0, bytes, passcodeBytes.length + 16, 16);
passcodeHash = Utilities.bytesToHex(Utilities.computeSHA256(bytes, 0, bytes.length));
saveConfig(false);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
return result;
} else {
try {
byte[] passcodeBytes = passcode.getBytes("UTF-8");
byte[] bytes = new byte[32 + passcodeBytes.length];
System.arraycopy(passcodeSalt, 0, bytes, 0, 16);
System.arraycopy(passcodeBytes, 0, bytes, 16, passcodeBytes.length);
System.arraycopy(passcodeSalt, 0, bytes, passcodeBytes.length + 16, 16);
String hash = Utilities.bytesToHex(Utilities.computeSHA256(bytes, 0, bytes.length));
return passcodeHash.equals(hash);
} catch (Exception e) {
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
}
2015-05-21 23:27:27 +02:00
return false;
2013-10-25 17:19:00 +02:00
}
public static void clearConfig() {
currentUser = null;
registeredForPush = false;
contactsHash = "";
importHash = "";
2013-12-20 20:25:49 +01:00
lastSendMessageId = -210000;
contactsVersion = 1;
lastBroadcastId = -1;
2013-10-25 17:19:00 +02:00
saveIncomingPhotos = false;
blockedUsersLoaded = false;
2015-09-24 22:52:02 +02:00
channelsLoaded = false;
appLocked = false;
passcodeType = 0;
passcodeHash = "";
2015-05-21 23:27:27 +02:00
passcodeSalt = new byte[0];
autoLockIn = 60 * 60;
lastPauseTime = 0;
2015-10-29 18:10:07 +01:00
useFingerprint = true;
isWaitingForPasscodeEnter = false;
lastUpdateVersion = BuildVars.BUILD_VERSION;
2015-08-13 11:23:31 +02:00
lastContactsSyncTime = (int) (System.currentTimeMillis() / 1000) - 23 * 60 * 60;
2013-11-04 13:31:01 +01:00
saveConfig(true);
2013-10-25 17:19:00 +02:00
}
}