Skip to content

Commit

Permalink
feat: cloud settings
Browse files Browse the repository at this point in the history
Co-authored-by: tehcneko <[email protected]>
  • Loading branch information
omg-xtao and tehcneko committed Sep 16, 2023
1 parent 351c90a commit 4f8529e
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import java.util.concurrent.ConcurrentHashMap;

import tw.nekomimi.nekogram.helpers.CloudStorageHelper;
import tw.nekomimi.nekogram.helpers.UserHelper;
import tw.nekomimi.nekogram.ui.MessageHelper;

public class AccountInstance {

private int currentAccount;
Expand Down Expand Up @@ -97,6 +101,18 @@ public SharedPreferences getNotificationsSettings() {
return MessagesController.getNotificationsSettings(currentAccount);
}

public MessageHelper getMessageHelper() {
return MessageHelper.getInstance(currentAccount);
}

public UserHelper getUserHelper() {
return UserHelper.getInstance(currentAccount);
}

public CloudStorageHelper getCloudStorageHelper() {
return CloudStorageHelper.getInstance(currentAccount);
}

public MemberRequestsController getMemberRequestsController() {
return MemberRequestsController.getInstance(currentAccount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import java.util.ArrayList;

import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.helpers.CloudStorageHelper;
import tw.nekomimi.nekogram.helpers.UserHelper;
import tw.nekomimi.nekogram.utils.VibrateUtil;
import tw.nekomimi.nekogram.ui.MessageHelper;

Expand Down Expand Up @@ -792,7 +794,15 @@ public void setFragmentPanTranslationOffset(int offset) {
}

public MessageHelper getMessageHelper() {
return MessageHelper.getInstance(currentAccount);
return getAccountInstance().getMessageHelper();
}

public UserHelper getUserHelper() {
return getAccountInstance().getUserHelper();
}

public CloudStorageHelper getCloudStorageHelper() {
return getAccountInstance().getCloudStorageHelper();
}

public void saveKeyboardPositionBeforeTransition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;

import tw.nekomimi.nekogram.config.ConfigItem;
import tw.nekomimi.nekogram.helpers.CloudSettingsHelper;

import static tw.nekomimi.nekogram.config.ConfigItem.*;

Expand Down Expand Up @@ -258,6 +259,8 @@ public static void loadConfig(boolean force) {
}
}
}
if (!configLoaded)
preferences.registerOnSharedPreferenceChangeListener(CloudSettingsHelper.listener);
for (int a = 1; a <= 5; a++) {
datacenterInfos.add(new DatacenterInfo(a));
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package tw.nekomimi.nekogram.helpers;

import android.util.SparseArray;

import com.google.gson.Gson;

import org.telegram.messenger.AccountInstance;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.Utilities;
import org.telegram.tgnet.TLRPC;

import java.util.HashMap;

public class CloudStorageHelper extends AccountInstance {

private static final SparseArray<CloudStorageHelper> Instance = new SparseArray<>();
private static final long WEBVIEW_BOT_ID = 1433866570L;
private static final String WEBVIEW_BOT = "NextAloneBot";

private final Gson gson = new Gson();

public CloudStorageHelper(int num) {
super(num);
}

public static CloudStorageHelper getInstance(int num) {
CloudStorageHelper localInstance = Instance.get(num);
if (localInstance == null) {
synchronized (CloudStorageHelper.class) {
localInstance = Instance.get(num);
if (localInstance == null) {
Instance.put(num, localInstance = new CloudStorageHelper(num));
}
}
}
return localInstance;
}

private void invokeWebViewCustomMethod(String method, String data, Utilities.Callback2<String, String> callback) {
invokeWebViewCustomMethod(method, data, true, callback);
}

private void invokeWebViewCustomMethod(String method, String data, boolean searchUser, Utilities.Callback2<String, String> callback) {
TLRPC.User user = getMessagesController().getUser(WEBVIEW_BOT_ID);
if (user == null) {
if (searchUser) {
getUserHelper().resolveUser(WEBVIEW_BOT, WEBVIEW_BOT_ID, arg -> invokeWebViewCustomMethod(method, data, false, callback));
} else {
callback.run(null, "USER_NOT_FOUND");
}
return;
}
TLRPC.TL_bots_invokeWebViewCustomMethod req = new TLRPC.TL_bots_invokeWebViewCustomMethod();
req.bot = getMessagesController().getInputUser(user);
req.custom_method = method;
req.params = new TLRPC.TL_dataJSON();
req.params.data = data;
getConnectionsManager().sendRequest(req, (res, error) -> AndroidUtilities.runOnUIThread(() -> {
if (callback != null) {
if (error != null) {
callback.run(null, error.text);
} else if (res instanceof TLRPC.TL_dataJSON) {
callback.run(((TLRPC.TL_dataJSON) res).data, null);
} else {
callback.run(null, null);
}
}
}));
}

public void setItem(String key, String value, Utilities.Callback2<String, String> callback) {
HashMap<String, String> map = new HashMap<>();
map.put("key", key);
map.put("value", value);
invokeWebViewCustomMethod("saveStorageValue", gson.toJson(map), callback);
}

public void getItem(String key, Utilities.Callback2<String, String> callback) {
getItems(new String[]{key}, (res, error) -> {
if (error == null) {
callback.run(res.get(key), null);
} else {
callback.run(null, error);
}
});
}

public void getItems(String[] keys, Utilities.Callback2<HashMap<String, String>, String> callback) {
HashMap<String, String[]> map = new HashMap<>();
map.put("keys", keys);
invokeWebViewCustomMethod("getStorageValues", gson.toJson(map), (res, error) -> {
if (error == null) {
//noinspection unchecked
callback.run(gson.fromJson(res, HashMap.class), null);
} else {
callback.run(null, error);
}
});
}

public void removeItem(String key, Utilities.Callback2<String, String> callback) {
removeItems(new String[]{key}, callback);
}

public void removeItems(String[] keys, Utilities.Callback2<String, String> callback) {
HashMap<String, String[]> map = new HashMap<>();
map.put("keys", keys);
invokeWebViewCustomMethod("deleteStorageValues", gson.toJson(map), callback);
}

public void getKeys(Utilities.Callback2<String[], String> callback) {
invokeWebViewCustomMethod("getStorageKeys", "{}", (res, error) -> {
if (error == null) {
String[] keys = gson.fromJson(res, String[].class);
callback.run(keys, null);
} else {
callback.run(null, error);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tw.nekomimi.nekogram.helpers;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;

import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.BaseController;
import org.telegram.messenger.ChatObject;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.Utilities;
import org.telegram.messenger.browser.Browser;
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.RequestDelegate;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.AlertDialog;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.ChatActivity;
import org.telegram.ui.ProfileActivity;
import org.telegram.ui.TopicsFragment;

import java.util.HashMap;
import java.util.Locale;

public class UserHelper extends BaseController {

private static final SparseArray<UserHelper> Instance = new SparseArray<>();

public UserHelper(int num) {
super(num);
}

public static UserHelper getInstance(int num) {
UserHelper localInstance = Instance.get(num);
if (localInstance == null) {
synchronized (UserHelper.class) {
localInstance = Instance.get(num);
if (localInstance == null) {
Instance.put(num, localInstance = new UserHelper(num));
}
}
}
return localInstance;
}

void resolveUser(String userName, long userId, Utilities.Callback<TLRPC.User> callback) {
resolvePeer(userName, peer -> {
if (peer instanceof TLRPC.TL_peerUser) {
callback.run(peer.user_id == userId ? getMessagesController().getUser(userId) : null);
} else {
callback.run(null);
}
});
}

private void resolvePeer(String userName, Utilities.Callback<TLRPC.Peer> callback) {
TLRPC.TL_contacts_resolveUsername req = new TLRPC.TL_contacts_resolveUsername();
req.username = userName;
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (response != null) {
TLRPC.TL_contacts_resolvedPeer res = (TLRPC.TL_contacts_resolvedPeer) response;
getMessagesController().putUsers(res.users, false);
getMessagesController().putChats(res.chats, false);
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, true);
callback.run(res.peer);
} else {
callback.run(null);
}
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import kotlin.text.StringsKt;

import tw.nekomimi.nekogram.DatacenterActivity;
import tw.nekomimi.nekogram.helpers.CloudSettingsHelper;
import tw.nekomimi.nekogram.helpers.PasscodeHelper;
import tw.nekomimi.nekogram.utils.AlertUtil;
import tw.nekomimi.nekogram.utils.EnvUtil;
Expand Down Expand Up @@ -103,6 +104,7 @@ public boolean onFragmentCreate() {

private static final int backup_settings = 1;
private static final int import_settings = 2;
private static final int sync_settings = 3;

@SuppressLint("NewApi")
@Override
Expand All @@ -114,6 +116,7 @@ public View createView(Context context) {
ActionBarMenuItem otherMenu = menu.addItem(0, R.drawable.ic_ab_other);
otherMenu.addSubItem(backup_settings, LocaleController.getString("BackupSettings", R.string.BackupSettings));
otherMenu.addSubItem(import_settings, LocaleController.getString("ImportSettings", R.string.ImportSettings));
menu.addItem(sync_settings, R.drawable.cloud_sync);

if (AndroidUtilities.isTablet()) {
actionBar.setOccupyStatusBar(false);
Expand Down Expand Up @@ -152,6 +155,8 @@ public void startDocumentSelectActivity() {
}
});
presentFragment(fragment);
} else if (id == sync_settings) {
CloudSettingsHelper.getInstance().showDialog(NekoSettingsActivity.this);
}
}
});
Expand Down Expand Up @@ -208,7 +213,7 @@ private void backupSettings() {

}

private String backupSettingsJson() throws JSONException {
public static String backupSettingsJson() throws JSONException {

JSONObject configJson = new JSONObject();

Expand Down
9 changes: 9 additions & 0 deletions TMessagesProj/src/main/res/drawable/cloud.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector android:height="72dp"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="72dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@android:color/white"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM19,18H6c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4h0.71C7.37,7.69 9.48,6 12,6c3.04,0 5.5,2.46 5.5,5.5v0.5H19c1.66,0 3,1.34 3,3s-1.34,3 -3,3z" />
</vector>
9 changes: 9 additions & 0 deletions TMessagesProj/src/main/res/drawable/cloud_sync.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M24,17.48c0,1.38 -1.12,2.5 -2.5,2.5L15,20c-1.66,0 -3,-1.34 -3,-3c0,-1.6 1.26,-2.9 2.84,-2.98C15.4,12.83 16.6,12 18,12c1.76,0 3.2,1.3 3.45,2.99c0.02,0 0.03,-0.01 0.05,-0.01C22.88,14.98 24,16.1 24,17.48zM10,15c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v1.44c-1.22,-1.1 -2,-2.67 -2,-4.44c0,-2.38 1.39,-4.43 3.4,-5.4C9.77,6.42 10,6.04 10,5.63c0,-0.71 -0.73,-1.18 -1.37,-0.88C5.89,6.03 4,8.79 4,12c0,2.4 1.06,4.54 2.73,6H5c-0.55,0 -1,0.45 -1,1s0.45,1 1,1h4c0.55,0 1,-0.45 1,-1V15zM19,6c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1h-4c-0.55,0 -1,0.45 -1,1v4c0,0.55 0.45,1 1,1s1,-0.45 1,-1V7.56c0.98,0.89 1.68,2.08 1.92,3.44l2.02,0c-0.25,-1.99 -1.23,-3.74 -2.66,-5H19z" />
</vector>
13 changes: 13 additions & 0 deletions TMessagesProj/src/main/res/values/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,17 @@
<string name="RecentChats">Recent chats</string>
<string name="ClearRecentChats">Clear recent chats</string>
<string name="ClearRecentChatAlert">Do you want to clear your recent chats?</string>
<string name="CloudConfig">Cloud Settings</string>
<string name="CloudConfigDesc">Save your Neko Settings to Telegram cloud.</string>
<string name="CloudConfigSyncDate">Last synced: %s, cloud: %s</string>
<string name="CloudConfigSyncDateNever">never</string>
<string name="CloudConfigSync">Sync to cloud</string>
<string name="CloudConfigSyncShort">Sync</string>
<string name="CloudConfigRestore">Restore from cloud</string>
<string name="CloudConfigRestoreShort">Restore</string>
<string name="CloudConfigSyncing">Syncing...</string>
<string name="CloudConfigAutoSync">Auto sync</string>
<string name="CloudConfigAutoSyncDesc">Sync to cloud automatically after making changes to Neko Settings.</string>
<string name="CloudConfigSyncFailed">Failed to sync settings to cloud.</string>
<string name="CloudConfigRestoreFailed">Failed to restore settings from cloud.</string>
</resources>

0 comments on commit 4f8529e

Please sign in to comment.