refactoring some code

This commit is contained in:
gdlbo
2023-05-26 21:03:23 +03:00
parent adcd2472d5
commit fa99df93c4
6 changed files with 80 additions and 101 deletions
@@ -86,25 +86,22 @@ public class PhotoViewer {
}
//endregion
private static void copyImage(AttachmentWithMedia attachment)
{
private static void copyImage(AttachmentWithMedia attachment) {
var req = new Request.a()
.b(getImageUrlFromAttachment(attachment))
.a();
client.a(req).a(new Callback()
{
.b(getImageUrlFromAttachment(attachment))
.a();
client.a(req).a(new Callback() {
@Override
public void a(Call call, IOException e)
{ e.printStackTrace(); }
public void a(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void a(Call call, Response response)
throws IOException
{
throws IOException {
var tmpImage = new File(AndroidUtils.getGlobalContext().getExternalCacheDir(), attachment.getId() + ".jpg");
try(var resp = client.a(req).execute();
var sink = Okio.a(Okio.b(tmpImage)))
{
try (var resp = client.a(req).execute();
var sink = Okio.a(Okio.b(tmpImage))) {
sink.a(resp.a().f());
var manager = (ClipboardManager) AndroidUtils.getGlobalContext().getSystemService(Context.CLIPBOARD_SERVICE);
var uri = FileProvider.getUriForFile(
@@ -121,23 +118,23 @@ public class PhotoViewer {
});
}
private static void searchImageWithUrl(AttachmentWithMedia attachment)
{
private static void searchImageWithUrl(AttachmentWithMedia attachment) {
String url = getImageUrlFromAttachment(attachment);
if(TextUtils.isEmpty(url)) return;
if (TextUtils.isEmpty(url)) return;
int selectedEngine = SearchEngine.getDefaultSearchEngine();
if (selectedEngine < 0) {
var items = new String[SearchEngine.values().length];
for(int i = 0;i < SearchEngine.values().length; ++i)
for (int i = 0; i < SearchEngine.values().length; ++i)
items[i] = SearchEngine.values()[i].mTitle;
new VkAlertDialog.Builder(LifecycleUtils.getCurrentActivity())
.setItems(items, (di, i) -> openUrl(SearchEngine.values()[i].buildSearchUrl(url)))
.show();
} else openUrl(SearchEngine.values()[selectedEngine].buildSearchUrl(url));
} else {
openUrl(SearchEngine.values()[selectedEngine].buildSearchUrl(url));
}
}
private static void copyImageUrl(AttachmentWithMedia attachment)
{
private static void copyImageUrl(AttachmentWithMedia attachment) {
String url = getImageUrlFromAttachment(attachment);
if (TextUtils.isEmpty(url)) return;
ClipboardManager manager = (ClipboardManager) AndroidUtils.getGlobalContext().getSystemService(Context.CLIPBOARD_SERVICE);
@@ -145,8 +142,7 @@ public class PhotoViewer {
Toast.makeText(AndroidUtils.getGlobalContext(), R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
private static void openUrl(String url)
{
private static void openUrl(String url) {
if (TextUtils.isEmpty(url)) {
Log.d("PhotoViewer", "url is null or empty");
return;
@@ -156,25 +152,24 @@ public class PhotoViewer {
AndroidUtils.getGlobalContext().startActivity(intent);
}
private static String getImageUrlFromAttachment(AttachmentWithMedia attachment)
{
List< ImageSize > imageSizes;
if(attachment instanceof PhotoAttachment)
private static String getImageUrlFromAttachment(AttachmentWithMedia attachment) {
List<ImageSize> imageSizes;
if (attachment instanceof PhotoAttachment) {
imageSizes = ((PhotoAttachment) attachment).D.Q.t1();
else if(attachment instanceof DocumentAttachment
&& ((DocumentAttachment) attachment).J != null)
} else if (attachment instanceof DocumentAttachment && ((DocumentAttachment) attachment).J != null) {
imageSizes = ((DocumentAttachment) attachment).J.t1();
else
{
} else {
ToastUtils.a(R.string.photo_get_error);
return "";
}
if(imageSizes.isEmpty()) return "";
if (imageSizes.isEmpty()) return "";
var max = imageSizes.get(0);
for(int i = 1; i < imageSizes.size(); i++)
{
for (int i = 1; i < imageSizes.size(); i++) {
var tmp = imageSizes.get(i);
if(max.t1() < tmp.t1()) max = tmp;
if (max.t1() < tmp.t1()) max = tmp;
}
return max.url;
}
@@ -8,6 +8,7 @@ import com.vk.im.engine.utils.ImDialogsUtils;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import ru.vtosters.hooks.other.ThemesUtils;
import ru.vtosters.lite.concurrent.VTExecutors;
import ru.vtosters.lite.net.Request;
import ru.vtosters.lite.proxy.ProxyUtils;
import ru.vtosters.lite.utils.AccountManagerUtils;
@@ -43,7 +44,7 @@ public class Requests {
}
public static void pinnedMsg(int dialogid, boolean needToBePinned) {
Thread thread = new Thread(() -> {
VTExecutors.getIoExecutor().execute(() -> {
try {
var request = new okhttp3.Request.a()
.b("https://" + ProxyUtils.getApi() + "/method/" + (needToBePinned ? "messages.pinConversation" : "messages.unpinConversation") + "?peer_id=" + dialogid + "&access_token=" + AccountManagerUtils.getUserToken() + "&v=5.119")
@@ -56,8 +57,6 @@ public class Requests {
}
});
thread.start();
AndroidUtils.sendToast(AndroidUtils.getString(com.vtosters.lite.R.string.pin_dialog) + " " + AndroidUtils.getString(needToBePinned ? com.vtosters.lite.R.string.dialog_pinned : com.vtosters.lite.R.string.dialog_unpinned));
}
}
@@ -17,9 +17,11 @@ import com.vtosters.lite.R;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.json.JSONException;
import org.json.JSONObject;
import ru.vtosters.hooks.other.Preferences;
import ru.vtosters.hooks.other.ThemesUtils;
import ru.vtosters.lite.concurrent.VTExecutors;
import ru.vtosters.lite.downloaders.AudioDownloader;
import ru.vtosters.lite.downloaders.VideoDownloader;
import ru.vtosters.lite.music.LastFMScrobbler;
@@ -138,18 +140,17 @@ public class MediaFragment extends TrackedMaterialPreferenceToolbarFragment {
items[0] = new ImagineArrayAdapter.ImagineArrayAdapterItem(
ThemesUtils.recolorDrawable(AndroidUtils.getResources().getDrawable(R.drawable.link_outline_28)),
AndroidUtils.getString("by_choice"));
for(int i = 0; i < SearchEngine.values().length; ++i)
{
for (int i = 0; i < SearchEngine.values().length; ++i) {
var engine = SearchEngine.values()[i];
items[i + 1] = engine.mIconRes != R.drawable.ic_picture_outline_28
? new ImagineArrayAdapter.ImagineArrayAdapterItem(engine.mIconRes, engine.mTitle)
: new ImagineArrayAdapter.ImagineArrayAdapterItem(
ThemesUtils.recolorDrawable(AndroidUtils.getResources().getDrawable(R.drawable.ic_picture_outline_28)),
engine.mTitle);
ThemesUtils.recolorDrawable(AndroidUtils.getResources().getDrawable(R.drawable.ic_picture_outline_28)),
engine.mTitle);
}
var adapter = new ImagineArrayAdapter(requireContext(), Arrays.asList(items));
adapter.setSelected(Preferences.getPreferences().getInt("search_engine", 0));
adapter.setSelected(SearchEngine.getDefaultSearchEngine());
new VkAlertDialog.Builder(getActivity())
.setAdapter(adapter, (di, i) -> {
@@ -241,32 +242,26 @@ public class MediaFragment extends TrackedMaterialPreferenceToolbarFragment {
}
public void deleteVideoHistory() {
Thread thread = new Thread(() -> {
VTExecutors.getIoExecutor().execute(() -> {
var request = new Request.a()
.b("https://" + ProxyUtils.getApi() + "/method/" + "video.clearViewingHistoryRecords" + "?https=1" + "&access_token=" + AccountManagerUtils.getUserToken() + "&v=5.119")
.a(Headers.a("User-Agent", Network.l.c().a(), "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"))
.a();
try {
var request = new Request.a()
.b("https://" + ProxyUtils.getApi() + "/method/" + "video.clearViewingHistoryRecords" + "?https=1" + "&access_token=" + AccountManagerUtils.getUserToken() + "&v=5.119")
.a(Headers.a("User-Agent", Network.l.c().a(), "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"))
.a();
var response = new JSONObject(new OkHttpClient().a(request).execute().a().g());
try {
var response = new JSONObject(new OkHttpClient().a(request).execute().a().g());
if (response.optInt("response") == 1) {
requireActivity().runOnUiThread(() -> AndroidUtils.sendToast(requireContext().getString(R.string.video_history_cleaned)));
} else {
requireActivity().runOnUiThread(() -> AndroidUtils.sendToast(requireContext().getString(R.string.delete_video_history_error)));
}
Log.d("VideoHistory", response.toString());
} catch (IOException e) {
e.printStackTrace();
if (response.optInt("response") == 1) {
requireActivity().runOnUiThread(() -> AndroidUtils.sendToast(requireContext().getString(R.string.video_history_cleaned)));
} else {
requireActivity().runOnUiThread(() -> AndroidUtils.sendToast(requireContext().getString(R.string.delete_video_history_error)));
}
Log.d("VideoHistory", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
});
thread.start();
});;
}
private void deleteVideoHistoryDialog(Context context) {
@@ -27,19 +27,18 @@ import static ru.vtosters.hooks.other.ThemesUtils.getTextAttr;
import static ru.vtosters.lite.utils.AndroidUtils.*;
public class RenameTool {
private static final int DB_VERSION = 2;
private static final String COLUMN_NAME = "name";
public static final String COLUMN_FIRSTNAME = "first_name";
public static final String COLUMN_LASTNAME = "last_name";
public static final SparseArray<Pair<String, String>> renamedUsers = new SparseArray<>();
public static final SparseArray<String> renamedGroups = new SparseArray<>();
private static final int DB_VERSION = 2;
private static final String COLUMN_NAME = "name";
private static final String COLUMN_VKID = "vk_id";
private static final String DB_NAME = "vt_rename.db";
private static final String TABLE_NAME = "renames";
private static final String TABLE_NAME_GROUP = "renames_group";
public static final SparseArray<Pair<String, String>> renamedUsers = new SparseArray<>();
public static final SparseArray<String> renamedGroups = new SparseArray<>();
private static RenameTool.DbHelper helperInstance;
public static boolean updateRequested = true;
private static RenameTool.DbHelper helperInstance;
protected static RenameTool.DbHelper getHelper() {
if (helperInstance == null) {
@@ -1,56 +1,51 @@
package ru.vtosters.lite.utils;
import androidx.annotation.DrawableRes;
import com.vtosters.lite.R;
import ru.vtosters.hooks.other.Preferences;
import java.net.URLEncoder;
import com.vtosters.lite.R;
public enum SearchEngine
{
YANDEX(
R.drawable.yandex,
public enum SearchEngine {
YANDEX(R.drawable.yandex,
"Yandex",
"https://yandex.com/images/search?rpt=imageview&url="),
GOOGLE(
R.drawable.google,
GOOGLE(R.drawable.google,
"Google",
"https://www.google.com/searchbyimage?client=app&image_url="),
BING(
R.drawable.microsoft,
BING(R.drawable.microsoft,
"Bing",
"https://www.bing.com/images/search?view=detailv2&iss=SBI&form=SBIIDP&sbisrc=UrlPaste&q=imgurl:"),
TRACEMOE(
R.drawable.ic_picture_outline_28,
TRACEMOE(R.drawable.ic_picture_outline_28,
"trace.moe",
"https://trace.moe/?url="),
ASCII2D(
R.drawable.ic_picture_outline_28,
ASCII2D(R.drawable.ic_picture_outline_28,
"Ascii2d",
"https://ascii2d.net/search/url/"),
SAUCENAO(
R.drawable.ic_picture_outline_28,
SAUCENAO(R.drawable.ic_picture_outline_28,
"SauceNAO",
"https://saucenao.com/search.php?url="),;
"https://saucenao.com/search.php?url=");
@DrawableRes
public int mIconRes;
public String mTitle;
private String mBaseUrl;
public final int mIconRes;
public final String mTitle;
private final String mBaseUrl;
SearchEngine(@DrawableRes int iconRes, String title, String baseUrl)
{
SearchEngine(@DrawableRes int iconRes, String title, String baseUrl) {
this.mIconRes = iconRes;
this.mTitle = title;
this.mBaseUrl = baseUrl; }
this.mBaseUrl = baseUrl;
}
public String buildSearchUrl(String targetUrl)
{ return this.mBaseUrl + URLEncoder.encode(targetUrl); }
public static int getDefaultSearchEngine() {
return Preferences.getPreferences().getInt("search_engine", -1);
}
public static int getDefaultSearchEngine()
{ return Preferences.getPreferences().getInt("search_engine", -1); }
public static void setDefaultSearchEngine(int i) {
Preferences.getPreferences().edit().putInt("search_engine", i).apply();
}
public static void setDefaultSearchEngine(int i)
{ Preferences.getPreferences().edit().putInt("search_engine", i).apply(); }
public String buildSearchUrl(String targetUrl) {
return this.mBaseUrl + URLEncoder.encode(targetUrl);
}
}
@@ -3,10 +3,6 @@ package ru.vtosters.lite.utils;
import android.webkit.WebView;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import static ru.vtosters.hooks.other.ThemesUtils.*;
import static ru.vtosters.lite.utils.AndroidUtils.getGlobalContext;