From 223e6e077211bef6c3ac23ea1ea2e6a8f1393184 Mon Sep 17 00:00:00 2001 From: Sergey Eremin Date: Thu, 16 Nov 2017 19:21:23 +0300 Subject: [PATCH] Issue #308 log denied permission instead of crashing --- .../yeriomin/yalpstore/DetailsActivity.java | 1 + .../yeriomin/yalpstore/UpdatableAppsActivity.java | 2 ++ .../yeriomin/yalpstore/YalpStoreActivity.java | 2 ++ .../fragment/details/ButtonDownload.java | 15 ++++++++++----- .../playstore/BackgroundUpdatableAppsTask.java | 11 +++++++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/DetailsActivity.java b/app/src/main/java/com/github/yeriomin/yalpstore/DetailsActivity.java index 73facf0f..24b85200 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/DetailsActivity.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/DetailsActivity.java @@ -94,6 +94,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { if (isGranted(requestCode, permissions, grantResults)) { + Log.i(getClass().getName(), "User granted the write permission"); if (null == downloadOrInstallFragment && null != app) { downloadOrInstallFragment = new DownloadOrInstall(this, app); redrawButtons(); diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/UpdatableAppsActivity.java b/app/src/main/java/com/github/yeriomin/yalpstore/UpdatableAppsActivity.java index 0ccc2714..bc0aa97a 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/UpdatableAppsActivity.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/UpdatableAppsActivity.java @@ -2,6 +2,7 @@ import android.content.Intent; import android.os.Bundle; +import android.util.Log; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.Button; @@ -56,6 +57,7 @@ public void loadApps() { @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { if (isGranted(requestCode, permissions, grantResults)) { + Log.i(getClass().getName(), "User granted the write permission"); launchUpdateAll(); } } diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/YalpStoreActivity.java b/app/src/main/java/com/github/yeriomin/yalpstore/YalpStoreActivity.java index aa5329fd..a20604ef 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/YalpStoreActivity.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/YalpStoreActivity.java @@ -118,6 +118,7 @@ protected boolean isGranted(int requestCode, String permissions[], int[] grantRe public boolean checkPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Log.i(getClass().getName(), "Checking if write permission is granted"); return checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } return true; @@ -125,6 +126,7 @@ public boolean checkPermission() { public void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Log.i(getClass().getName(), "Requesting the write permission"); requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_CODE diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/fragment/details/ButtonDownload.java b/app/src/main/java/com/github/yeriomin/yalpstore/fragment/details/ButtonDownload.java index 89e054e9..9d4aaa04 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/fragment/details/ButtonDownload.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/fragment/details/ButtonDownload.java @@ -55,6 +55,7 @@ public void checkAndDownload() { if (app.getVersionCode() == 0 && !(activity instanceof ManualDownloadActivity)) { activity.startActivity(new Intent(activity, ManualDownloadActivity.class)); } else if (activity.checkPermission()) { + Log.i(getClass().getName(), "Write permission granted"); download(); View buttonCancel = activity.findViewById(R.id.cancel); if (null != buttonCancel) { @@ -82,12 +83,16 @@ public void download() { AndroidAppDeliveryData.newBuilder().setDownloadUrl(UpdaterFactory.get(activity).getUrlString(app.getVersionCode())).build(), getDownloadProgressListener() ); - } else if (prepareDownloadsDir()) { - getPurchaseTask().execute(); } else { - File dir = Paths.getYalpPath(activity); - Log.i(getClass().getName(), dir.getAbsolutePath() + " exists=" + dir.exists() + ", isDirectory=" + dir.isDirectory() + ", writable=" + dir.canWrite()); - ContextUtil.toast(this.activity.getApplicationContext(), R.string.error_downloads_directory_not_writable); + boolean writePermission = activity.checkPermission(); + Log.i(getClass().getName(), "Write permission granted - " + writePermission); + if (writePermission && prepareDownloadsDir()) { + getPurchaseTask().execute(); + } else { + File dir = Paths.getYalpPath(activity); + Log.i(getClass().getName(), dir.getAbsolutePath() + " exists=" + dir.exists() + ", isDirectory=" + dir.isDirectory() + ", writable=" + dir.canWrite()); + ContextUtil.toast(this.activity.getApplicationContext(), R.string.error_downloads_directory_not_writable); + } } } diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/task/playstore/BackgroundUpdatableAppsTask.java b/app/src/main/java/com/github/yeriomin/yalpstore/task/playstore/BackgroundUpdatableAppsTask.java index dd1b8614..2ef64988 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/task/playstore/BackgroundUpdatableAppsTask.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/task/playstore/BackgroundUpdatableAppsTask.java @@ -1,8 +1,11 @@ package com.github.yeriomin.yalpstore.task.playstore; +import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; import android.util.Log; import com.github.yeriomin.yalpstore.DownloadManagerAdapter; @@ -56,6 +59,14 @@ protected void onPostExecute(List apps) { } private boolean canUpdate() { + boolean writePermission = true; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + writePermission = context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + } + if (!writePermission) { + Log.i(getClass().getName(), "Write permission not granted"); + return false; + } return forceUpdate || (PreferenceActivity.getBoolean(context, PreferenceActivity.PREFERENCE_BACKGROUND_UPDATE_DOWNLOAD) && (DownloadManagerFactory.get(context) instanceof DownloadManagerAdapter