Add share option when a long press on an image in the item activity is detected

This commit is contained in:
Shinokuni 2019-11-15 12:08:24 +01:00
parent 5bd46ffacb
commit 39cc5260bd
7 changed files with 124 additions and 1 deletions

View File

@ -19,6 +19,17 @@
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">
<provider
android:authorities="${applicationId}"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity
android:name=".activities.WebViewActivity"
android:theme="@style/AppTheme.NoActionBar" />

View File

@ -3,20 +3,31 @@ package com.readrops.app.activities;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ShareCompat;
import androidx.lifecycle.ViewModelProvider;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
@ -37,6 +48,8 @@ import static com.readrops.app.utils.ReadropsKeys.WEB_URL;
public class ItemActivity extends AppCompatActivity {
private static final String TAG = ItemActivity.class.getSimpleName();
private ItemViewModel viewModel;
private TextView date;
private TextView title;
@ -84,6 +97,8 @@ public class ItemActivity extends AppCompatActivity {
readTimeLayout = findViewById(R.id.activity_item_readtime_layout);
dateLayout = findViewById(R.id.activity_item_date_layout);
registerForContextMenu(webView);
if (imageUrl == null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbarLayout.setTitleEnabled(false);
@ -246,4 +261,58 @@ public class ItemActivity extends AppCompatActivity {
shareIntent.putExtra(Intent.EXTRA_TEXT, itemWithFeed.getItem().getTitle() + " - " + itemWithFeed.getItem().getLink());
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_article)));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
WebView.HitTestResult hitTestResult = webView.getHitTestResult();
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());
else
downloadImage(hitTestResult.getExtra());
})
.show();
}
}
private void downloadImage(String url) {
}
private void shareImage(String url) {
GlideApp.with(this)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.load(url)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
try {
Uri uri = viewModel.saveImageInCache(resource);
Intent intent = ShareCompat.IntentBuilder.from(ItemActivity.this)
.setType("image/png")
.setStream(uri)
.setChooserTitle(R.string.share_image)
.createChooserIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
// not useful
}
});
}
}

View File

@ -1,14 +1,23 @@
package com.readrops.app.viewmodels;
import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.annotation.NonNull;
import com.readrops.app.database.Database;
import com.readrops.app.database.dao.ItemDao;
import com.readrops.app.database.pojo.ItemWithFeed;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ItemViewModel extends AndroidViewModel {
private ItemDao itemDao;
@ -23,4 +32,19 @@ public class ItemViewModel extends AndroidViewModel {
}
public Uri saveImageInCache(Bitmap bitmap) throws IOException {
File imagesFolder = new File(getApplication().getCacheDir().getAbsolutePath(), "images");
if (!imagesFolder.exists())
imagesFolder.mkdirs();
File image = new File(imagesFolder, "shared_image.png");
OutputStream stream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.flush();
stream.close();
return FileProvider.getUriForFile(getApplication(), getApplication().getPackageName(), image);
}
}

View File

@ -103,5 +103,8 @@
<string name="try_again">Réessayer</string>
<string name="permissions">Permissions</string>
<string name="or">Ou</string>
<string name="image_options">Options de l\'image</string>
<string name="download_image">Télécharger l\'image</string>
<string name="share_image">Partager l\'image</string>
</resources>

View File

@ -10,6 +10,11 @@
<item>@string/opml_export</item>
</string-array>
<string-array name="image_options">
<item>@string/share_image</item>
<item>@string/download_image</item>
</string-array>
<string-array name="items_per_feed_numbers_values">
<item>20</item>
<item>50</item>

View File

@ -112,4 +112,7 @@
<string name="try_again">Try again</string>
<string name="permissions">Permissions</string>
<string name="or">Or</string>
<string name="image_options">Image Options</string>
<string name="download_image">Download image</string>
<string name="share_image">Share image</string>
</resources>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<cache-path
name="shared_images"
path="images/" />
</paths>
</resources>