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

151 lines
6.5 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
* This is the source code of Telegram for Android v. 1.2.3.
* 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.
*/
package org.telegram.messenger;
2013-11-04 13:31:01 +01:00
import android.content.Context;
import android.content.SharedPreferences;
2013-10-25 17:19:00 +02:00
import org.telegram.TL.TLClassStore;
import org.telegram.TL.TLRPC;
2013-11-04 13:31:01 +01:00
import org.telegram.ui.ApplicationLoader;
2013-10-25 17:19:00 +02:00
import java.io.File;
import java.io.FileOutputStream;
public class UserConfig {
public static TLRPC.User currentUser;
public static int clientUserId = 0;
public static boolean clientActivated = false;
public static boolean registeredForPush = false;
public static String pushString = "";
public static int lastSendMessageId = -1;
public static int lastLocalId = -1;
public static String contactsHash = "";
public static String importHash = "";
private final static Integer sync = 1;
public static boolean saveIncomingPhotos = false;
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-10-25 17:19:00 +02:00
synchronized (sync) {
SerializedData data = new SerializedData();
if (currentUser != null) {
2013-11-04 13:31:01 +01:00
data.writeInt32(2);
2013-10-25 17:19:00 +02:00
currentUser.serializeToStream(data);
clientUserId = currentUser.id;
clientActivated = true;
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.commit();
2013-10-25 17:19:00 +02:00
} else {
data.writeInt32(0);
}
2013-11-04 13:31:01 +01:00
if (withFile) {
try {
File configFile = new File(ApplicationLoader.applicationContext.getFilesDir(), "user.dat");
if (!configFile.exists()) {
configFile.createNewFile();
}
FileOutputStream stream = new FileOutputStream(configFile);
stream.write(data.toByteArray());
stream.close();
} catch (Exception e) {
e.printStackTrace();
2013-10-25 17:19:00 +02:00
}
}
}
}
public static void loadConfig() {
synchronized (sync) {
2013-11-04 13:31:01 +01:00
File configFile = new File(ApplicationLoader.applicationContext.getFilesDir(), "user.dat");
2013-10-25 17:19:00 +02:00
if (configFile.exists()) {
try {
SerializedData data = new SerializedData(configFile);
2013-11-04 13:31:01 +01:00
int ver = data.readInt32();
if (ver == 1) {
2013-10-25 17:19:00 +02:00
int constructor = data.readInt32();
currentUser = (TLRPC.TL_userSelf)TLClassStore.Instance().TLdeserialize(data, constructor);
clientUserId = currentUser.id;
clientActivated = true;
2013-11-04 13:31:01 +01:00
MessagesStorage.lastDateValue = data.readInt32();
MessagesStorage.lastPtsValue = data.readInt32();
MessagesStorage.lastSeqValue = data.readInt32();
2013-10-25 17:19:00 +02:00
registeredForPush = data.readBool();
pushString = data.readString();
lastSendMessageId = data.readInt32();
lastLocalId = data.readInt32();
contactsHash = data.readString();
importHash = data.readString();
saveIncomingPhotos = data.readBool();
if (currentUser.status != null) {
if (currentUser.status.expires != 0) {
currentUser.status.was_online = currentUser.status.expires;
} else {
currentUser.status.expires = currentUser.status.was_online;
}
}
2013-11-04 13:31:01 +01:00
MessagesStorage.lastQtsValue = data.readInt32();
MessagesStorage.lastSecretVersion = data.readInt32();
2013-10-25 17:19:00 +02:00
int val = data.readInt32();
if (val == 1) {
2013-11-04 13:31:01 +01:00
MessagesStorage.secretPBytes = data.readByteArray();
2013-10-25 17:19:00 +02:00
}
2013-11-04 13:31:01 +01:00
MessagesStorage.secretG = data.readInt32();
} else if (ver == 2) {
int constructor = data.readInt32();
currentUser = (TLRPC.TL_userSelf)TLClassStore.Instance().TLdeserialize(data, constructor);
clientUserId = currentUser.id;
clientActivated = true;
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE);
registeredForPush = preferences.getBoolean("registeredForPush", false);
pushString = preferences.getString("pushString", "");
lastSendMessageId = preferences.getInt("lastSendMessageId", -1);
lastLocalId = preferences.getInt("lastLocalId", -1);
contactsHash = preferences.getString("contactsHash", "");
importHash = preferences.getString("importHash", "");
saveIncomingPhotos = preferences.getBoolean("saveIncomingPhotos", false);
2013-10-25 17:19:00 +02:00
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void clearConfig() {
clientUserId = 0;
clientActivated = false;
currentUser = null;
registeredForPush = false;
contactsHash = "";
lastLocalId = -1;
importHash = "";
lastSendMessageId = -1;
saveIncomingPhotos = false;
2013-11-04 13:31:01 +01:00
saveConfig(true);
2013-10-25 17:19:00 +02:00
}
}