Added option to show image title or alt as caption

This commit is contained in:
José Padarian 2020-04-12 19:46:12 +10:00
parent 2fd2f21e6f
commit f70a0551b4
6 changed files with 94 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package com.readrops.app.activities;
import android.Manifest;
import android.app.DialogFragment;
import android.app.DownloadManager;
import android.content.Intent;
import android.content.pm.PackageManager;
@ -39,6 +40,7 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.readrops.app.R;
import com.readrops.app.fragments.ImageCaptionFragment;
import com.readrops.readropsdb.entities.Item;
import com.readrops.readropsdb.pojo.ItemWithFeed;
import com.readrops.app.utils.DateUtils;
@ -49,6 +51,9 @@ import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.utils.Utils;
import com.readrops.app.viewmodels.ItemViewModel;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.readrops.app.utils.ReadropsKeys.ACTION_BAR_COLOR;
import static com.readrops.app.utils.ReadropsKeys.IMAGE_URL;
import static com.readrops.app.utils.ReadropsKeys.ITEM_ID;
@ -79,6 +84,7 @@ public class ItemActivity extends AppCompatActivity {
private CoordinatorLayout rootLayout;
private String urlToDownload;
private String imageTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -278,23 +284,47 @@ public class ItemActivity extends AppCompatActivity {
if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
new MaterialDialog.Builder(this)
.title(R.string.image_options)
.items(R.array.image_options)
.itemsCallback((dialog, itemView, position, text) -> {
if (position == 0)
shareImage(hitTestResult.getExtra());
MaterialDialog.Builder builder = new MaterialDialog.Builder(this);
builder.title(R.string.image_options);
builder.items(R.array.image_options);
builder.itemsCallback((dialog, itemView, position, text) -> {
switch (position) {
case 0:
shareImage(hitTestResult.getExtra());
break;
case 1:
if (PermissionManager.isPermissionGranted(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
downloadImage(hitTestResult.getExtra());
else {
if (PermissionManager.isPermissionGranted(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
downloadImage(hitTestResult.getExtra());
else {
urlToDownload = hitTestResult.getExtra();
PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_REQUEST, Manifest.permission.WRITE_EXTERNAL_STORAGE);
urlToDownload = hitTestResult.getExtra();
PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_REQUEST, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
break;
case 2:
urlToDownload = hitTestResult.getExtra();
String content = webView.getItemContent();
Pattern p = Pattern.compile("(<img.*src=\"" + urlToDownload + "\".*>)");
Matcher m = p.matcher(content);
if (m.matches()) {
Pattern p2 = Pattern.compile("<img.*(title|alt)=\"(.*?)\".*>");
Matcher m2 = p2.matcher(content);
if (m2.matches()) {
imageTitle = m2.group(2);
} else {
imageTitle = "";
}
}
})
.show();
DialogFragment newFragment = ImageCaptionFragment.newInstance(urlToDownload, imageTitle);
newFragment.show(getFragmentManager(), "dialog");
break;
default:
throw new IllegalStateException("Unexpected value: " + position);
}
});
builder.show();
}
}

View File

@ -0,0 +1,41 @@
package com.readrops.app.fragments;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import com.readrops.app.R;
public class ImageCaptionFragment extends DialogFragment {
public static ImageCaptionFragment newInstance(CharSequence title, CharSequence message) {
ImageCaptionFragment f = new ImageCaptionFragment();
Bundle args = new Bundle();
args.putCharSequence("title", title);
args.putCharSequence("message", message);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
CharSequence title = getArguments().getCharSequence("title");
CharSequence message = getArguments().getCharSequence("message");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setMessage(message);
builder.setNegativeButton(R.string.back, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
return builder.create();
}
}

View File

@ -48,6 +48,11 @@ public class ReadropsWebView extends WebView {
loadData(base64Content, "text/html; charset=utf-8", "base64");
}
public String getItemContent() {
String content = itemWithFeed.getItem().getContent();
return content;
}
private void getColors(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ReadropsWebView);
textColor = typedArray.getColor(R.styleable.ReadropsWebView_textColor, 0);

View File

@ -129,5 +129,7 @@
<string name="auto_synchro_disabled">La synchronisation automatique est désactivée</string>
<string name="enable_auto_synchro_text">Les notifications nécessitent l\'activation de la synchronisation automatique pour fonctionner.\nVoulez-vous ouvrir les paramètres ?</string>
<string name="open">Ouvrir</string>
<string name="back">Retour</string>
<string name="show_caption">Afficher la légende</string>
</resources>

View File

@ -13,6 +13,7 @@
<string-array name="image_options">
<item>@string/share_image</item>
<item>@string/download_image</item>
<item>@string/show_caption</item>
</string-array>
<string-array name="items_per_feed_numbers_values">

View File

@ -135,4 +135,6 @@
<string name="auto_synchro_disabled">Automatic synchronization is disabled</string>
<string name="enable_auto_synchro_text">To be displayed, notifications need auto synchronization to be activated.\nDo you want to open settings ?</string>
<string name="open">Open</string>
<string name="back">Back</string>
<string name="show_caption">Show caption</string>
</resources>