update multi account

This commit is contained in:
Vologhat
2022-04-11 20:57:52 +03:00
parent 9d05d077b5
commit 8febcfdfb7
4 changed files with 112 additions and 113 deletions
@@ -4,12 +4,16 @@ import static android.content.Context.MODE_PRIVATE;
import static ru.vtosters.lite.utils.Helper.GetContext;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
@@ -17,36 +21,33 @@ import com.vk.audio.AudioMessageUtils;
import com.vk.auth.api.VKAccount;
import com.vk.core.f.FileUtils;
import com.vk.core.util.AppContextHolder;
import com.vk.core.util.ToastUtils;
import com.vk.imageloader.VKImageLoader;
import com.vk.imageloader.view.VKCircleImageView;
import com.vk.pushes.PushSubscriber;
import com.vtosters.lite.auth.VKAccountManager;
import com.vtosters.lite.im.ImEngineProvider;
import com.vtosters.lite.ui.refreshlayout.CircleImageView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import ru.vtosters.lite.utils.Helper;
import ru.vtosters.lite.utils.Themes;
import com.vtosters.lite.R;
public class MultiAccountAdapter extends RecyclerView.a<MultiAccountAdapter.MultiAccountViewHolder> {
private final List<MultiAccountItem> mAccounts = new ArrayList<>();
public MultiAccountAdapter() {
CharSequence[] getAccAmountNames = MultiAccountManager.getAccAmountNames();
CharSequence[] getAccAmount = MultiAccountManager.getAccAmount();
CharSequence[] getAvatars = MultiAccountManager.getAvatars();
for (int i = 0; i < getAccAmount.length; i++) {
mAccounts.add(new MultiAccountItem((String) getAccAmountNames[i], (String) getAvatars[i], i));
}
}
private final List<MultiAccountItem> mAccounts = MultiAccountManager.buildList();
@Override
public MultiAccountViewHolder b(ViewGroup parent, int viewType) {
RelativeLayout layout = new RelativeLayout(parent.getContext());
layout.setPadding(
Helper.convertDpToPixel(6),
Helper.convertDpToPixel(12),
Helper.convertDpToPixel(6),
Helper.convertDpToPixel(6),
Helper.convertDpToPixel(6)
@@ -73,6 +74,7 @@ public class MultiAccountAdapter extends RecyclerView.a<MultiAccountAdapter.Mult
nickname.setEllipsize(TextUtils.TruncateAt.END);
nickname.setSingleLine(true);
nickname.setTextSize(15.0f);
nickname.setTextColor(Themes.getTextAttr());
nickname.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(-2, -2);
params1.addRule(RelativeLayout.CENTER_VERTICAL);
@@ -82,8 +84,8 @@ public class MultiAccountAdapter extends RecyclerView.a<MultiAccountAdapter.Mult
ImageButton action = new ImageButton(parent.getContext());
action.setId(3);
action.setBackgroundResource(com.vtosters.lite.R.drawable.rounded_list_selector);
action.setImageResource(com.vtosters.lite.R.drawable.ic_list_remove);
action.setVisibility(View.INVISIBLE);
Drawable drawable = Themes.recolorDrawable(Helper.getResources().getDrawable(R.drawable.ic_list_remove));
action.setImageDrawable(drawable);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
Helper.convertDpToPixel(48),
Helper.convertDpToPixel(48)
@@ -97,7 +99,7 @@ public class MultiAccountAdapter extends RecyclerView.a<MultiAccountAdapter.Mult
@Override
public void a(MultiAccountViewHolder holder, int pos) {
holder.bind(mAccounts.get(pos == 0 ? null : pos - 1), pos);
holder.bind(pos == 0 ? null : mAccounts.get(pos-1), pos);
}
@Override
@@ -116,52 +118,66 @@ public class MultiAccountAdapter extends RecyclerView.a<MultiAccountAdapter.Mult
super(view);
mContainer = (RelativeLayout) view;
mAvatar = view.findViewById(1);
mNickname = view.findViewById(2);
mAction = view.findViewById(3);
mAvatar = (VKCircleImageView) view.findViewById(1);
mNickname = (TextView) view.findViewById(2);
mAction = (ImageButton) view.findViewById(3);
}
public void bind(MultiAccountItem item, int pos) {
if (item == null) {
mAvatar.setImageResource(com.vtosters.lite.R.drawable.ic_list_add);
if (pos == 0) {
mNickname.setText("Добавить новый аккаунт");
mContainer.setOnClickListener(v -> {
SharedPreferences sharedPrefs = GetContext().getSharedPreferences("com.vtosters.lite_preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("account", Integer.toString(MultiAccountManager.getAvalibleAcc()));
editor.commit();
MultiAccountManager.addAccount();
VKImageLoader.b();
ImEngineProvider.a().h();
AudioMessageUtils.j();
FileUtils.l();
VKAccount b = VKAccountManager.b();
GetContext().getSharedPreferences("push_subscriber" + pos, MODE_PRIVATE).edit().clear().commit();
PushSubscriber.a.a(b.b(), b.c());
AppContextHolder.a.getSharedPreferences("gcm", 0).edit().clear().apply();
ToastUtils.a(Helper.getString("restartapp"));
Helper.restarting();
});
Drawable drawable = Themes.recolorDrawable(Helper.getResources().getDrawable(R.drawable.ic_list_add));
mAction.setImageDrawable(drawable);
mAction.setClickable(false);
} else {
mContainer.setOnClickListener(v -> {
// здесь выбор аккаунта
MultiAccountManager.switchAccount(item.index);
VKImageLoader.b();
ImEngineProvider.a().h();
AudioMessageUtils.j();
FileUtils.l();
VKAccount b = VKAccountManager.b();
GetContext().getSharedPreferences("push_subscriber" + pos, MODE_PRIVATE).edit().clear().commit();
PushSubscriber.a.a(b.b(), b.c());
AppContextHolder.a.getSharedPreferences("gcm", 0).edit().clear().apply();
ToastUtils.a(Helper.getString("restartapp"));
Helper.restarting();
});
mAvatar.a(item.imageUrl);
if (Helper.isNetworkConnected())
mAvatar.a(item.imageUrl);
else
mAvatar.a(R.drawable.vkim_ic_placeholder_user_square_64dp);
mNickname.setText(item.nickname);
mAction.setOnClickListener(v -> {
// здесь удаление аккаунта из префов
GetContext().getSharedPreferences("pref_account_manager" + pos, MODE_PRIVATE).edit().clear().commit();
MultiAccountManager.deleteAccount(item.index);
VKImageLoader.b();
ImEngineProvider.a().h();
AudioMessageUtils.j();
FileUtils.l();
VKAccount b = VKAccountManager.b();
PushSubscriber.a.a(b.b(), b.c());
AppContextHolder.a.getSharedPreferences("gcm", 0).edit().clear().apply();
ToastUtils.a(Helper.getString("restartapp"));
Helper.restarting();
});
mAction.setVisibility(View.VISIBLE);
}
}
}
@@ -1,116 +1,92 @@
package ru.vtosters.lite.ui.fragments.multiaccount;
import static android.content.Context.MODE_PRIVATE;
import static ru.vtosters.lite.utils.Helper.GetContext;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ru.vtosters.lite.utils.Helper;
public class MultiAccountManager {
public static SharedPreferences getCurrentAccount() {
String Value = ru.vtosters.lite.utils.Helper.GetPreferences().getString("account", "");
return Helper.GetContext().getSharedPreferences("pref_account_manager" + Value, MODE_PRIVATE);
}
public static void MigrateToNewType() {
SharedPreferences OldPrefs = Helper.GetContext().getSharedPreferences("pref_account_manager", MODE_PRIVATE);
public static void migrate() {
SharedPreferences OldPrefs = Helper.GetContext().getSharedPreferences("pref_account_manager", Context.MODE_PRIVATE);
String OldPrefsValue = OldPrefs.getString("key_vk_account", "");
SharedPreferences NewPrefs = GetContext().getSharedPreferences("pref_account_manager0", MODE_PRIVATE);
SharedPreferences NewPrefs = GetContext().getSharedPreferences("pref_account_manager0", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = NewPrefs.edit();
editor.putString("key_vk_account", OldPrefsValue);
editor.commit();
editor.apply();
}
public static String getCurrentAccountName() {
String Value = ru.vtosters.lite.utils.Helper.GetPreferences().getString("account", "");
return getName(Value);
public static SharedPreferences getCurrentAccount() {
int account = Helper.GetPreferences().getInt("account", 0);
return Helper.GetContext().getSharedPreferences("pref_account_manager" + (account != 0 ? account : ""), Context.MODE_PRIVATE);
}
public static SharedPreferences getNeededAccount(String str) {
return Helper.GetContext().getSharedPreferences("pref_account_manager" + str, MODE_PRIVATE);
private static int getAccountPrefsCount() {
File[] prefs = new File(Helper.GetContext().getFilesDir().getParent(), "shared_prefs")
.listFiles((dir, name) -> {
return name.matches("pref_account_manager\\d+\\.xml");
});
return prefs != null ? prefs.length : 0;
}
public static String getName(String str) {
Pattern pattern = Pattern.compile(".*\"name\":\\{.*?:\"(.*?)\"\\}.*");
Matcher matcher = pattern.matcher(getNeededAccount(str).getString("key_vk_account", ""));
private static String withRegex(String base, String regex, String def) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(base);
if (matcher.find()) {
String group = matcher.group(1);
return group;
return matcher.group(1);
}
return "err";
return def;
}
public static String getAvatar(String str) {
Pattern pattern = Pattern.compile(".*\"photo\":\\{.*?:\"(.*?)\"\\}.*");
Matcher matcher = pattern.matcher(getNeededAccount(str).getString("key_vk_account", ""));
if (matcher.find()) {
String group = matcher.group(1);
String replaced = group.replace("\\/", "/");
return replaced;
}
return "https://vk.com/images/camera_200.png";
}
public static int getAvalibleAcc() {
int intg = 0;
while (true) {
if (getName(Integer.toString(intg)) != "err") {
intg++;
} else {
break;
public static List<MultiAccountItem> buildList() {
List<MultiAccountItem> list = new ArrayList<>();
for (int i = 0; i <= getAccountPrefsCount(); i++) {
SharedPreferences prefs = Helper.GetContext().getSharedPreferences("pref_account_manager" + i, Context.MODE_PRIVATE);
String keyVKAccount = prefs.getString("key_vk_account", "");
if (!keyVKAccount.isEmpty()) {
String name = withRegex(keyVKAccount, ".*\"name\":\\{.*?:\"(.*?)\"\\}.*", "");
String avatarUrl = withRegex(keyVKAccount, ".*\"photo\":\\{.*?:\"(.*?)\"\\}.*", "https://vk.com/images/camera_200.png")
.replace("\\/", "/");
Log.d("MultiAcc", String.format("Name: %s, Avatar: %s, Index: %d", name, avatarUrl, i));
list.add(new MultiAccountItem(
name, avatarUrl, i
));
}
}
;
return intg;
return list;
}
public static CharSequence[] getAccAmountNames() {
int intg = 0;
ArrayList<String> list = new ArrayList<String>();
while (true) {
if (getName(Integer.toString(intg)) != "err") {
list.add(getName(Integer.toString(intg)));
intg++;
} else {
break;
}
};
final CharSequence[] charSequenceItems = list.toArray(new CharSequence[list.size()]);
return charSequenceItems;
public static boolean switchAccount(int i) {
int account = Helper.GetPreferences().getInt("account", 0);
if (account == i) return false;
Helper.GetPreferences().edit().putInt("account", i).commit();
return true;
}
public static CharSequence[] getAvatars() {
int intg = 0;
ArrayList<String> list = new ArrayList<String>();
while (true) {
if (getAvatar(Integer.toString(intg)) != "err") {
list.add(getAvatar(Integer.toString(intg)));
intg++;
} else {
break;
}
};
final CharSequence[] charSequenceItems = list.toArray(new CharSequence[list.size()]);
return charSequenceItems;
public static void addAccount() {
Helper.GetPreferences().edit().putInt("account", getAccountPrefsCount() + 1).commit();
/*SharedPreferences prefs = Helper.GetContext().getSharedPreferences("pref_account_manager" + newAccount, Context.MODE_PRIVATE);
File file = new File(Helper.GetContext().getDir("shared_prefs", Context.MODE_PRIVATE), "pref_account_manager" + newAccount);
if (file.exists()) file.delete();*/
}
public static CharSequence[] getAccAmount() {
int intg = 0;
ArrayList<String> list = new ArrayList<String>();
while (true) {
if (getName(Integer.toString(intg)) != "err") {
list.add(Integer.toString(intg));
intg++;
} else {
break;
}
};
final CharSequence[] charSequenceItems = list.toArray(new CharSequence[list.size()]);
return charSequenceItems;
public static void deleteAccount(int i) {
int account = Helper.GetPreferences().getInt("account", 0);
SharedPreferences prefs = Helper.GetContext().getSharedPreferences("pref_account_manager" + i, Context.MODE_PRIVATE);
prefs.edit().clear().commit();
File file = new File(new File(Helper.GetContext().getFilesDir().getParent(), "shared_prefs"), "pref_account_manager" + i);
if (file.exists()) file.delete();
if (account == i && getAccountPrefsCount() > 0)
switchAccount(buildList().get(0).index);
}
}
@@ -5,6 +5,9 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
@@ -135,4 +138,10 @@ public class Helper {
public static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
public static boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) GetContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null ? activeNetwork.isConnectedOrConnecting() :false;
}
}
@@ -13,7 +13,6 @@ import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import com.vk.about.Items;
@@ -56,7 +55,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import kotlin.jvm.internal.Intrinsics;
import ru.vtosters.lite.ui.fragments.dockbar.DockBarManager;
import ru.vtosters.lite.ui.fragments.multiaccount.MultiAccountManager;
@@ -711,7 +709,7 @@ public class Prefs {
setupFilters();
fixGapps();
setProxy();
MultiAccountManager.MigrateToNewType();
MultiAccountManager.migrate();
registerActivities(application);
}