Merge branch 'master' into develop

This commit is contained in:
Shinokuni 2019-12-26 21:26:41 +01:00
commit 5aa07a186e
6 changed files with 82 additions and 14 deletions

View File

@ -1,7 +1,9 @@
package com.readrops.app.activities;
import android.Manifest;
import android.app.DownloadManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
@ -9,6 +11,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
@ -23,6 +26,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ShareCompat;
import androidx.lifecycle.ViewModelProviders;
@ -38,6 +43,7 @@ import com.readrops.app.database.entities.Item;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.utils.DateUtils;
import com.readrops.app.utils.GlideApp;
import com.readrops.app.utils.PermissionManager;
import com.readrops.app.utils.ReadropsWebView;
import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.utils.Utils;
@ -51,6 +57,7 @@ import static com.readrops.app.utils.ReadropsKeys.WEB_URL;
public class ItemActivity extends AppCompatActivity {
private static final String TAG = ItemActivity.class.getSimpleName();
private static final int WRITE_EXTERNAL_STORAGE_REQUEST = 1;
private ItemViewModel viewModel;
private TextView date;
@ -70,6 +77,9 @@ public class ItemActivity extends AppCompatActivity {
private boolean appBarCollapsed;
private CoordinatorLayout rootLayout;
private String urlToDownload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -98,6 +108,7 @@ public class ItemActivity extends AppCompatActivity {
readTime = findViewById(R.id.activity_item_readtime);
readTimeLayout = findViewById(R.id.activity_item_readtime_layout);
dateLayout = findViewById(R.id.activity_item_date_layout);
rootLayout = findViewById(R.id.item_root);
registerForContextMenu(webView);
@ -273,13 +284,47 @@ public class ItemActivity extends AppCompatActivity {
.itemsCallback((dialog, itemView, position, text) -> {
if (position == 0)
shareImage(hitTestResult.getExtra());
else
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);
}
}
})
.show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
downloadImage(urlToDownload);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
Utils.showSnackBarWithAction(rootLayout, getString(R.string.download_image_permission),
getString(R.string.try_again),
v -> PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_REQUEST,
Manifest.permission.WRITE_EXTERNAL_STORAGE));
} else {
Utils.showSnackBarWithAction(rootLayout, getString(R.string.download_image_permission),
getString(R.string.permissions), v -> {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(intent);
});
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void downloadImage(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
.setTitle(getString(R.string.download_image))

View File

@ -16,7 +16,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.preference.Preference;
@ -29,6 +28,7 @@ import com.readrops.app.activities.AddAccountActivity;
import com.readrops.app.activities.ManageFeedsFoldersActivity;
import com.readrops.app.database.entities.account.Account;
import com.readrops.app.database.entities.account.AccountType;
import com.readrops.app.utils.PermissionManager;
import com.readrops.app.utils.Utils;
import com.readrops.app.utils.matchers.OPMLMatcher;
import com.readrops.app.viewmodels.AccountViewModel;
@ -122,7 +122,7 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
if (position == 0) {
openOPMLFile();
} else {
if (isExternalStoragePermissionGranted())
if (PermissionManager.isPermissionGranted(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE))
exportAsOPMLFile();
else
requestExternalStoragePermission();
@ -266,14 +266,13 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
private void displayNotification(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(file.getAbsolutePath()));
intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "text/plain");
Notification notification = new NotificationCompat.Builder(getContext(), ReadropsApp.OPML_EXPORT_CHANNEL_ID)
.setContentTitle(getString(R.string.opml_export))
.setContentText(file.getName())
.setSmallIcon(R.drawable.ic_readrops)
.setSmallIcon(R.drawable.ic_notif)
.setContentIntent(PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setDeleteIntent(PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true)
.build();
@ -281,12 +280,9 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
manager.notify(2, notification);
}
private boolean isExternalStoragePermissionGranted() {
return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private void requestExternalStoragePermission() {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST);
PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_REQUEST,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
@Override
@ -310,8 +306,6 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
exportAsOPMLFile();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//endregion

View File

@ -0,0 +1,26 @@
package com.readrops.app.utils
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
class PermissionManager {
companion object {
@JvmStatic
fun isPermissionGranted(context: Context, permission: String): Boolean =
ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
@JvmStatic
fun requestPermissions(activity: Activity, requestCode: Int, vararg permissions: String) =
ActivityCompat.requestPermissions(activity, permissions, requestCode)
@JvmStatic
fun requestPermissions(fragment: Fragment, requestCode: Int, vararg permissions: String) =
fragment.requestPermissions(permissions, requestCode)
}
}

View File

@ -2,6 +2,7 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"

View File

@ -111,5 +111,6 @@
<string name="dark">Sombre</string>
<string name="opml_export_description">Export des flux et dossiers</string>
<string name="new_feed">Nouveau flux</string>
<string name="download_image_permission">Le téléchargement de l\'image nécessite l\'accès au stockage</string>
</resources>

View File

@ -120,4 +120,5 @@
<string name="dark">Dark</string>
<string name="opml_export_description">Export feeds and folders</string>
<string name="new_feed">New feed</string>
<string name="download_image_permission">To download the image, storage permission is needed</string>
</resources>