added some features to photo viewer #411

-Added image searching in the internet with feature of selecting search engine
-Added copying image file url to the clipboard
-Added opening image file url in the browsers
This commit is contained in:
Vologhat
2022-07-17 21:15:23 +03:00
parent 95636ad4f4
commit 918967f502
12 changed files with 446 additions and 0 deletions
@@ -0,0 +1,167 @@
package ru.vtosters.lite.hooks;
import static ru.vtosters.lite.utils.Globals.getIdentifier;
import static ru.vtosters.lite.utils.Globals.getPreferences;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.vk.core.dialogs.actionspopup.ActionsPopup;
import com.vk.core.drawable.RecoloredDrawable;
import com.vk.dto.common.AttachmentWithMedia;
import com.vtosters.lite.R;
import com.vtosters.lite.attachments.PhotoAttachment;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URLEncoder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import ru.vtosters.lite.utils.Globals;
import ru.vtosters.lite.utils.Themes;
public class PhotoViewer {
static OkHttpClient client = new OkHttpClient();
public static boolean interceptClick(AttachmentWithMedia attachment, MenuItem item, View view) {
if (item.getItemId() == Globals.getIdentifier("search_photo", "id")) {
searchPhoto(getUrlFromAttachment((PhotoAttachment) attachment));
return true;
} else if (item.getItemId() == Globals.getIdentifier("copy_photo_url", "id")) {
copyPhotoUrl(getUrlFromAttachment((PhotoAttachment) attachment));
return true;
} else if (item.getItemId() == Globals.getIdentifier("open_original_photo", "id")) {
openUrl(getUrlFromAttachment((PhotoAttachment) attachment));
return true;
}
return true;
}
public static void addMenuItems(AttachmentWithMedia attachment, ActionsPopup.b actionPopup, int i, boolean z) {
actionPopup.a(getIdentifier("search_photo_content", "string"),
Themes.isDarkTheme()
? Globals.getContext().getDrawable(R.drawable.ic_menu_search_outline_28)
: new RecoloredDrawable(Globals.getContext().getDrawable(R.drawable.ic_menu_search_outline_28), Themes.getAccentColor()),
false,
() -> {
searchPhoto(getUrlFromAttachment((PhotoAttachment) attachment));
return null;
}
);
actionPopup.a(getIdentifier("copy_photo_url_content", "string"),
!Themes.isDarkTheme()
? new RecoloredDrawable(Globals.getContext().getDrawable(R.drawable.ic_copy_outline_28), Themes.getAccentColor())
: new RecoloredDrawable(Globals.getContext().getDrawable(R.drawable.ic_copy_outline_28), Color.WHITE),
false,
() -> {
copyPhotoUrl(getUrlFromAttachment((PhotoAttachment) attachment));
return null;
}
);
actionPopup.a(getIdentifier("open_original_photo_content", "string"),
Themes.isDarkTheme()
? Globals.getContext().getDrawable(R.drawable.ic_link_outline_28)
: new RecoloredDrawable(Globals.getContext().getDrawable(R.drawable.ic_link_outline_28),
Themes.getAccentColor()),
false,
() -> {
openUrl(getUrlFromAttachment((PhotoAttachment) attachment));
return null;
}
);
}
private static String getUrlFromAttachment(PhotoAttachment attachment) {
var imageSizes = attachment.D.Q.t1();
if (imageSizes.isEmpty())
return null;
var max = imageSizes.get(0);
for (int i = 1; i < imageSizes.size(); ++i) {
var size = imageSizes.get(i);
if (max != null && max.t1() < size.t1()) {
max = size;
}
}
return max.url;
}
private static void searchPhoto(String url) {
if (url == null || url.isEmpty()) return;
switch (getPreferences().getInt("search_engine", 0)) {
case 0:
searchWithYandex(url);
break;
case 1:
searchWithGoogle(url);
break;
case 2:
searchWithBing(url);
break;
}
}
private static void searchWithYandex(String url) {
var request = new Request.a()
.b(String.format("https://yandex.ru/images-apphost/image-download?url=%s&images_avatars_size=preview&images_avatars_namespace=images-cbir",
URLEncoder.encode(url)))
.a();
client.a(request).a(new Callback() {
@Override
public void a(Call call, IOException e) {
Log.d("PhotoViewer", e+"");
}
@Override
public void a(Call call, Response response) throws IOException {
try {
JSONObject payload = new JSONObject(response.a().g());
var cbirId = payload.optInt("image_shard") + "/" + payload.optString("image_id");
var imageUrl = String.format("https://avatars.mds.yandex.net/get-images-cbir/%s/orig", cbirId);
var searchUrl = String.format("https://yandex.ru/images/search?rpt=imageview&url=%s&cbir_id=%s", imageUrl, cbirId);
openUrl(searchUrl);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
private static void searchWithGoogle(String url) {
openUrl("https://www.google.ru/searchbyimage?image_url=" +
URLEncoder.encode(url));
}
private static void searchWithBing(String url) {
openUrl("https://www.bing.com/images/search?view=detailv2&iss=SBI&form=SBIIDP&sbisrc=UrlPaste&q=imgurl:"
+ URLEncoder.encode(url));
}
private static void copyPhotoUrl(String url) {
if (url == null || url.isEmpty()) return;
ClipboardManager manager = (ClipboardManager) Globals.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
manager.setPrimaryClip(ClipData.newPlainText("vk_photo_url", url));
Toast.makeText(Globals.getContext(), Globals.getIdentifier("copied_to_clipboard", "string"), Toast.LENGTH_SHORT).show();
}
private static void openUrl(String url) {
var uri = Uri.parse(url);
var intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Globals.getContext().startActivity(intent);
}
}
@@ -0,0 +1,173 @@
package ru.vtosters.lite.ui.components;
import static ru.vtosters.lite.utils.Themes.getTextAttr;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import ru.vtosters.lite.utils.Globals;
import ru.vtosters.lite.utils.LayoutHelper;
public class ImagineArrayAdapter extends ArrayAdapter<ImagineArrayAdapter.ImagineArrayAdapterItem> {
private List<ImagineArrayAdapterItem> mItems = new ArrayList<>();
private OnClickListener mListener;
private int mSelectedIndex = -1;
public ImagineArrayAdapter(@NonNull Context context, OnClickListener listener) {
super(context, 0);
this.mListener = listener;
}
public ImagineArrayAdapter(@NonNull Context context, List<ImagineArrayAdapterItem> items, OnClickListener listener) {
super(context, 0, items);
mItems = items;
this.mListener = listener;
}
public void setSelected(int i) {
this.mSelectedIndex = i;
}
@Override
public int getCount() {
return mItems.size();
}
public void add(ImagineArrayAdapterItem item) {
mItems.add(item);
super.add(item);
}
public void add(Drawable icon, String text) {
var item = new ImagineArrayAdapterItem(icon, text);
mItems.add(item);
super.add(item);
}
public void add(int iconId, String text) {
var icon = Globals.getResources().getDrawable(iconId);
var item = new ImagineArrayAdapterItem(icon, text);
mItems.add(item);
super.add(item);
}
public void add(Drawable icon, int textId) {
var text = Globals.getResources().getString(textId);
var item = new ImagineArrayAdapterItem(icon, text);
mItems.add(item);
super.add(item);
}
public void add(int iconId, int textId) {
var icon = Globals.getResources().getDrawable(iconId);
var text = Globals.getResources().getString(textId);
var item = new ImagineArrayAdapterItem(icon, text);
mItems.add(item);
super.add(item);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
var container = new LinearLayout(getContext());
container.setOrientation(LinearLayout.HORIZONTAL);
container.setGravity(Gravity.CENTER_VERTICAL);
container.setPadding(0, Globals.convertDpToPixel(5.0f), 0, Globals.convertDpToPixel(5.0f));
container.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
container.setOnClickListener(v -> {
if (position != mSelectedIndex) {
mListener.onClick(position);
}
});
// if make radiobutton without layout inflation it can't be unclickable
var checkedTextView = (CheckedTextView) LayoutInflater.from(getContext())
.inflate(Globals.getIdentifier("select_dialog_singlechoice_material", "layout"), parent, false);
checkedTextView.setChecked(position == mSelectedIndex);
container.addView(checkedTextView, LayoutHelper.createLinear(Globals.convertDpToPixel(50.0f), -2));
var item = mItems.get(position);
var imageView = new ImageView(getContext());
imageView.setImageDrawable(item.getIcon());
imageView.setPadding(
Globals.convertDpToPixel(13.0f),
0,
0,
0
);
var imageViewParams = LayoutHelper.createLinear(
Globals.convertDpToPixel(50.0f),
Globals.convertDpToPixel(50.0f)
);
container.addView(imageView, imageViewParams);
var textView = new CheckedTextView(getContext());
textView.setText(item.getText());
textView.setTextColor(getTextAttr());
textView.setPadding(
Globals.convertDpToPixel(13.0f),
Globals.convertDpToPixel(5.0f),
Globals.convertDpToPixel(5.0f),
Globals.convertDpToPixel(5.0f));
textView.setTextSize(18.0f);
textView.setGravity(Gravity.CENTER_VERTICAL);
var textViewParams = LayoutHelper.createLinear(-2, -1);
container.addView(textView, textViewParams);
return container;
}
public static class ImagineArrayAdapterItem {
private Drawable mIcon;
private String mText;
public ImagineArrayAdapterItem(Drawable icon, String text) {
this.mIcon = icon;
this.mText = text;
}
public ImagineArrayAdapterItem(int iconId, String text) {
this.mIcon = Globals.getResources().getDrawable(iconId);
this.mText = text;
}
public ImagineArrayAdapterItem(Drawable icon, int textId) {
this.mIcon = icon;
this.mText = Globals.getResources().getString(textId);
}
public ImagineArrayAdapterItem(int iconId, int textId) {
this.mIcon = Globals.getResources().getDrawable(iconId);
this.mText = Globals.getResources().getString(textId);
}
public Drawable getIcon() {
return mIcon;
}
public String getText() {
return mText;
}
}
public interface OnClickListener {
public void onClick(int i);
}
}
@@ -2,6 +2,7 @@ package ru.vtosters.lite.ui.fragments;
import static ru.vtosters.lite.utils.Globals.edit;
import static ru.vtosters.lite.utils.Globals.getIdentifier;
import static ru.vtosters.lite.utils.Globals.getPreferences;
import static ru.vtosters.lite.utils.Globals.restartApplicationWithTimer;
import static ru.vtosters.lite.utils.Themes.getAccentColor;
import static ru.vtosters.lite.utils.Themes.getAlertStyle;
@@ -11,15 +12,21 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import com.vk.core.dialogs.alert.VkAlertDialog;
import com.vtosters.lite.general.fragments.MaterialPreferenceToolbarFragment;
import java.util.Arrays;
import ru.vtosters.lite.downloaders.VideoDownloader;
import ru.vtosters.lite.ui.components.ImagineArrayAdapter;
public class MediaFragment extends MaterialPreferenceToolbarFragment{
public static void download(Context ctx){
@@ -58,6 +65,28 @@ public class MediaFragment extends MaterialPreferenceToolbarFragment{
private void prefs(){
findPreference("download_video").setOnPreferenceClickListener(new MediaFragment.download());
findPreference("dateformat").setOnPreferenceChangeListener(new MediaFragment.restart());
findPreference("select_photo_search_engine").setOnPreferenceClickListener(preference -> {
var items = Arrays.asList(
new ImagineArrayAdapter.ImagineArrayAdapterItem(getIdentifier("yandex", "drawable"), "Yandex"),
new ImagineArrayAdapter.ImagineArrayAdapterItem(getIdentifier("google", "drawable"), "Google"),
new ImagineArrayAdapter.ImagineArrayAdapterItem(getIdentifier("microsoft", "drawable"), "Bing")
);
var alert = new VkAlertDialog.Builder(getActivity())
.create();
var listView = (ListView) LayoutInflater.from(getContext()).inflate(com.vtosters.lite.R.layout.abc_select_dialog_material, null, false);
var adapter = new ImagineArrayAdapter(getContext(), items, i -> {
getPreferences().edit().putInt("search_engine", i).apply();
alert.dismiss();
});
adapter.setSelected(getPreferences().getInt("search_engine", 0));
listView.setAdapter(adapter);
alert.setView(listView);
alert.show();
return true;
});
}
public class download implements Preference.OnPreferenceClickListener{
+18
View File
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M23.745,12.27C23.745,11.48 23.675,10.73 23.555,10L12.255,10L12.255,14.51L18.725,14.51C18.435,15.99 17.585,17.24 16.325,18.09L16.325,21.09L20.185,21.09C22.445,19 23.745,15.92 23.745,12.27Z"
android:fillColor="#4285F4"/>
<path
android:pathData="M12.255,24C15.495,24 18.205,22.92 20.185,21.09L16.325,18.09C15.245,18.81 13.875,19.25 12.255,19.25C9.125,19.25 6.475,17.14 5.525,14.29L1.545,14.29L1.545,17.38C3.515,21.3 7.565,24 12.255,24Z"
android:fillColor="#34A853"/>
<path
android:pathData="M5.525,14.29C5.275,13.57 5.145,12.8 5.145,12C5.145,11.2 5.285,10.43 5.525,9.71L5.525,6.62L1.545,6.62C0.725,8.24 0.255,10.06 0.255,12C0.255,13.94 0.725,15.76 1.545,17.38L5.525,14.29Z"
android:fillColor="#FBBC05"/>
<path
android:pathData="M12.255,4.75C14.025,4.75 15.605,5.36 16.855,6.55L20.275,3.13C18.205,1.19 15.495,0 12.255,0C7.565,0 3.515,2.7 1.545,6.62L5.525,9.71C6.475,6.86 9.125,4.75 12.255,4.75Z"
android:fillColor="#EA4335"/>
</vector>
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

+12
View File
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M2.04,12c0,-5.523 4.476,-10 10,-10 5.522,0 10,4.477 10,10s-4.478,10 -10,10c-5.524,0 -10,-4.477 -10,-10z"
android:fillColor="#FC3F1D"/>
<path
android:pathData="M13.32,7.666h-0.924c-1.694,0 -2.585,0.858 -2.585,2.123 0,1.43 0.616,2.1 1.881,2.959l1.045,0.704 -3.003,4.487H7.49l2.695,-4.014c-1.55,-1.111 -2.42,-2.19 -2.42,-4.015 0,-2.288 1.595,-3.85 4.62,-3.85h3.003v11.868H13.32V7.666z"
android:fillColor="#fff"/>
</vector>
+12
View File
@@ -40,4 +40,16 @@
android:id="@id/report"
android:showAsAction="never"
android:title="@string/report_content" />
<item
android:id="@id/search_photo"
android:showAsAction="never"
android:title="@string/search_photo_content" />
<item
android:id="@id/copy_photo_url"
android:showAsAction="never"
android:title="@string/copy_photo_url_content" />
<item
android:id="@id/open_original_photo"
android:showAsAction="never"
android:title="@string/open_original_photo_content" />
</menu>
+4
View File
@@ -2645,6 +2645,10 @@
<string name="photo_upload_ok_multiple">Фотографии загружены</string>
<string name="photo_upload_ok_multiple_long">Фотографии успешно загружены</string>
<string name="photo_viewer_save_photo">Сохранить</string>
<string name="search_photo_content">Найти фото</string>
<string name="copy_photo_url_content">Скопировать ссылку на фото</string>
<string name="open_original_photo_content">Оригинал</string>
<string name="copied_to_clipboard">Скопировано в буфер обмена</string>
<string name="photo_viewer_share">Поделиться</string>
<string name="photos">Фотографии</string>
<string name="photos_like_block_title">Фотографии</string>
+3
View File
@@ -2769,6 +2769,9 @@
<item name="replies_count" type="id" />
<item name="reply_to_name" type="id" />
<item name="report" type="id" />
<item name="search_photo" type="id" />
<item name="copy_photo_url" type="id" />
<item name="open_original_photo" type="id" />
<item name="request_check_icon" type="id" />
<item name="request_money_view" type="id" />
<item name="required" type="id" />
+4
View File
@@ -3277,6 +3277,10 @@ the VK camera"</string>
<string name="reply_from_group">Reply as community</string>
<string name="reply_to">Reply</string>
<string name="report_content">Report</string>
<string name="search_photo_content">Search photo</string>
<string name="copy_photo_url_content">Copy photo url</string>
<string name="open_original_photo_content">Original</string>
<string name="copied_to_clipboard">Copied to clipboard</string>
<string name="report_msgs_confirm">Are you sure you want to mark as spam and delete %s?</string>
<string name="report_msgs_delete">Spam</string>
<string name="report_msgs_title">Mark as spam</string>
+5
View File
@@ -63,6 +63,11 @@
android:summary="@string/interfacevideorecsum"
android:title="@string/interfacevideorec" />
</PreferenceCategory>
<PreferenceCategory android:title="Фото">
<Preference
android:key="select_photo_search_engine"
android:title="Выбор движка для поиска фото" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/vkx_title">
<Preference
android:enabled="false"
@@ -722,6 +722,16 @@
.line 62
:cond_c
move-object/from16 v0, p1
move-object/from16 v1, p2
move/from16 v2, p3
move/from16 v3, p4
invoke-static/range {v0 .. v3}, Lru/vtosters/lite/hooks/PhotoViewer;->addMenuItems(Lcom/vk/dto/common/AttachmentWithMedia;Lcom/vk/core/dialogs/actionspopup/ActionsPopup$b;IZ)V
invoke-virtual/range {p2 .. p2}, Lcom/vk/core/dialogs/actionspopup/ActionsPopup$b;->c()Lcom/vk/core/dialogs/actionspopup/ActionsPopup;
move-result-object v0
@@ -2747,6 +2757,15 @@
.method public final a(Lcom/vk/dto/common/AttachmentWithMedia;Landroid/view/MenuItem;Landroid/view/View;)Z
.locals 2
invoke-static {p1, p2, p3}, Lru/vtosters/lite/hooks/PhotoViewer;->interceptClick(Lcom/vk/dto/common/AttachmentWithMedia;Landroid/view/MenuItem;Landroid/view/View;)Z
move-result v0
if-nez v0, :cond_custom
return v0
:cond_custom
.line 20
instance-of v0, p1, Lcom/vtosters/lite/attachments/PhotoAttachment;