diff --git a/app/src/main/java/com/readrops/app/activities/ItemActivity.java b/app/src/main/java/com/readrops/app/activities/ItemActivity.java index d1bf2567..ad9ba6fe 100644 --- a/app/src/main/java/com/readrops/app/activities/ItemActivity.java +++ b/app/src/main/java/com/readrops/app/activities/ItemActivity.java @@ -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)) diff --git a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java index 42737718..1233b1b4 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java @@ -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 diff --git a/app/src/main/java/com/readrops/app/utils/PermissionManager.kt b/app/src/main/java/com/readrops/app/utils/PermissionManager.kt new file mode 100644 index 00000000..55242ef9 --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/PermissionManager.kt @@ -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) + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_item.xml b/app/src/main/res/layout/activity_item.xml index 62db479e..c3d5cb07 100644 --- a/app/src/main/res/layout/activity_item.xml +++ b/app/src/main/res/layout/activity_item.xml @@ -2,6 +2,7 @@ Sombre Export des flux et dossiers Nouveau flux + Le téléchargement de l\'image nécessite l\'accès au stockage \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index be549d8a..3a8bb724 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -120,4 +120,5 @@ Dark Export feeds and folders New feed + To download the image, storage permission is needed