load user avatar image asynchronously

This commit is contained in:
Vologhat
2022-12-09 21:03:50 +03:00
parent 1eaa3748c4
commit a88f59bac1
2 changed files with 44 additions and 96 deletions
@@ -1,48 +1,11 @@
package ru.vtosters.lite.ui.fragments;
import static ru.vtosters.lite.ui.PreferencesUtil.addListPreferenceIcon;
import static ru.vtosters.lite.ui.PreferencesUtil.addMaterialSwitchPreference;
import static ru.vtosters.lite.ui.PreferencesUtil.addPreference;
import static ru.vtosters.lite.ui.PreferencesUtil.addPreferenceCategory;
import static ru.vtosters.lite.ui.PreferencesUtil.addPreferenceDrawable;
import static ru.vtosters.lite.utils.About.getBuildNumber;
import static ru.vtosters.lite.utils.About.getCommitLink;
import static ru.vtosters.lite.utils.AccountManagerUtils.getUserPhoto;
import static ru.vtosters.lite.utils.AccountManagerUtils.getUsername;
import static ru.vtosters.lite.utils.AccountManagerUtils.isVKTester;
import static ru.vtosters.lite.utils.AndroidUtils.edit;
import static ru.vtosters.lite.utils.AndroidUtils.getPrefsValue;
import static ru.vtosters.lite.utils.AndroidUtils.isTablet;
import static ru.vtosters.lite.utils.GmsUtils.isGmsInstalled;
import static ru.vtosters.lite.utils.ImageUtils.getDrawableFromUrl;
import static ru.vtosters.lite.utils.Preferences.ads;
import static ru.vtosters.lite.utils.Preferences.autotranslate;
import static ru.vtosters.lite.utils.Preferences.dev;
import static ru.vtosters.lite.utils.Preferences.devmenu;
import static ru.vtosters.lite.utils.Preferences.disableSettingsSumms;
import static ru.vtosters.lite.utils.Preferences.hasSpecialVerif;
import static ru.vtosters.lite.utils.Preferences.isValidSignature;
import static ru.vtosters.lite.utils.Preferences.milkshake;
import static ru.vtosters.lite.utils.Preferences.navbar;
import static ru.vtosters.lite.utils.Preferences.offline;
import static ru.vtosters.lite.utils.Preferences.shortinfo;
import static ru.vtosters.lite.utils.Preferences.stories;
import static ru.vtosters.lite.utils.Preferences.superapp;
import static ru.vtosters.lite.utils.Preferences.vkme;
import static ru.vtosters.lite.utils.ThemesUtils.getDarkTheme;
import static ru.vtosters.lite.utils.ThemesUtils.getLightTheme;
import static ru.vtosters.lite.utils.ThemesUtils.isDarkTheme;
import static ru.vtosters.lite.utils.ThemesUtils.setTheme;
import static ru.vtosters.lite.utils.VTVerifications.vtverif;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.StringRes;
import com.aefyr.tsg.g2.TelegramStickersService;
import com.vk.about.AboutAppFragment;
import com.vk.balance.BalanceFragment;
@@ -60,16 +23,28 @@ import com.vtosters.lite.general.fragments.MaterialPreferenceToolbarFragment;
import com.vtosters.lite.general.fragments.SettingsAccountFragment;
import com.vtosters.lite.general.fragments.SettingsGeneralFragment;
import com.vtosters.lite.ui.MaterialSwitchPreference;
import ru.vtosters.lite.concurrent.VTExecutors;
import ru.vtosters.lite.hooks.ui.SystemThemeChangerHook;
import ru.vtosters.lite.ui.components.DockBarEditorManager;
import ru.vtosters.lite.ui.components.SuperAppEditorManager;
import ru.vtosters.lite.ui.dialogs.OTADialog;
import ru.vtosters.lite.ui.fragments.tgstickers.StickersFragment;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.LifecycleUtils;
import ru.vtosters.lite.utils.SSFSUtils;
import ru.vtosters.lite.utils.ThemesUtils;
import static ru.vtosters.lite.ui.PreferencesUtil.*;
import static ru.vtosters.lite.utils.About.getBuildNumber;
import static ru.vtosters.lite.utils.About.getCommitLink;
import static ru.vtosters.lite.utils.AccountManagerUtils.*;
import static ru.vtosters.lite.utils.AndroidUtils.*;
import static ru.vtosters.lite.utils.GmsUtils.isGmsInstalled;
import static ru.vtosters.lite.utils.ImageUtils.getDrawableFromUrl;
import static ru.vtosters.lite.utils.Preferences.*;
import static ru.vtosters.lite.utils.ThemesUtils.*;
import static ru.vtosters.lite.utils.VTVerifications.vtverif;
public class VTSettings extends MaterialPreferenceToolbarFragment {
public static String getValAsString(@StringRes int strRes, Boolean value) {
if (disableSettingsSumms()) return null;
@@ -165,21 +140,28 @@ public class VTSettings extends MaterialPreferenceToolbarFragment {
this.addPreferencesFromResource(R.xml.empty);
addPreferenceDrawable(this, "", getUsername(), requireContext().getString(R.string.vtllogout), getDrawableFromUrl(getUserPhoto(), R.drawable.ic_user_circle_outline_28, true, true), preference -> {
addPreferenceDrawable(this, "account_switcher", getUsername(), requireContext().getString(R.string.vtllogout), null, preference -> {
try {
VKAuth.a("logout", false);
} catch (Exception ignored) {
}
var intent = new Intent(requireContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
var intent = new Intent(requireContext(), MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
requireContext().startActivity(intent);
return false;
});
VTExecutors.getIoScheduler().a(() -> {
var icon = getDrawableFromUrl(getUserPhoto(), R.drawable.ic_user_circle_outline_28, true, true);
LifecycleUtils.getCurrentActivity().runOnUiThread(() -> {
findPreference("account_switcher").setIcon(icon);
});
});
addPreference(this, "", requireContext().getString(R.string.vtssfs), ssfs, R.drawable.ic_link_circle_outline_28, preference -> {
Context context = requireContext();
VKUIwrapper.setLink(SSFSUtils.getSSFSLink());
@@ -1,67 +1,33 @@
package ru.vtosters.lite.utils;
import static android.graphics.Bitmap.createBitmap;
import static android.graphics.Bitmap.createScaledBitmap;
import static androidx.core.content.ContextCompat.getDrawable;
import static ru.vtosters.lite.utils.Preferences.preferences;
import static ru.vtosters.lite.utils.ThemesUtils.getAccentColor;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.StrictMode;
import androidx.core.content.ContextCompat;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import ru.vtosters.lite.di.singleton.VtOkHttpClient;
import java.net.URL;
public class ImageUtils {
private static final OkHttpClient client = VtOkHttpClient.getInstance();
// must be called asynchronously
public static Drawable getDrawableFromUrl(String url, int placeholderRes, boolean rounded, boolean scaled) {
public static Bitmap bmp = null;
if (NetworkUtils.isNetworkConnected() && !NetworkUtils.isNetworkIsSlow()) {
try {
var bmp = BitmapFactory.decodeStream(new URL(url).openStream());
public static Drawable getDrawableFromUrl(String url, int defDrawableRes, Boolean rounded, Boolean scaled) {
if (!NetworkUtils.isNetworkConnected()) {
if (defDrawableRes == 0) return null;
return getDrawable(AndroidUtils.getGlobalContext(), defDrawableRes);
}
if (bmp != null) {
if (rounded) bmp = getBitmapClippedCircle(bmp);
if (scaled) bmp = Bitmap.createScaledBitmap(bmp, AndroidUtils.dp2px(256), AndroidUtils.dp2px(256), true);
if (NetworkUtils.isNetworkIsSlow()) {
if (defDrawableRes == 0) return null;
return getDrawable(AndroidUtils.getGlobalContext(), defDrawableRes);
}
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); // fix get pic
var request = new Request.a()
.b(url)
.a();
bmp = BitmapFactory.decodeStream(client.a(request).execute().a().a());
if (bmp == null) {
if (defDrawableRes == 0) return null;
return getDrawable(AndroidUtils.getGlobalContext(), defDrawableRes);
return new BitmapDrawable(AndroidUtils.getResources(), bmp);
}
} catch (Exception e) {
e.printStackTrace();
}
bmp = rounded ? getBitmapClippedCircle(bmp) : bmp; // rounding
bmp = scaled ? createScaledBitmap(bmp, AndroidUtils.dp2px(256), AndroidUtils.dp2px(256), true) : bmp; // scaling
return new BitmapDrawable(AndroidUtils.getResources(), bmp); // return our drawable
} catch (Exception e) {
if (defDrawableRes == 0) return null; // default pic can be null
return getDrawable(AndroidUtils.getGlobalContext(), defDrawableRes); // get default drawable via res
}
return placeholderRes > 0 ? ContextCompat.getDrawable(AndroidUtils.getGlobalContext(), placeholderRes) : null;
}
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
@@ -90,8 +56,8 @@ public class ImageUtils {
public static Bitmap getBitmapClippedCircle(Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final Bitmap outputBitmap = createBitmap(width, height, Bitmap.Config.ARGB_8888);
int pref = preferences.getInt("pic_rounding", 0);
final Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int pref = Preferences.preferences.getInt("pic_rounding", 0);
final Canvas canvas = new Canvas(outputBitmap);
final Path path = new Path();
@@ -112,7 +78,7 @@ public class ImageUtils {
}
public static Drawable setTint(Context ctx, Drawable d) {
d.setColorFilter(getAccentColor(), PorterDuff.Mode.SRC_ATOP);
d.setColorFilter(ThemesUtils.getAccentColor(), PorterDuff.Mode.SRC_ATOP);
return d;
}
}