Remove READ/WRITE permission from playstore build variant

This commit is contained in:
MohitMaliFtechiz 2022-06-23 14:03:00 +05:30 committed by Kelson
parent 766e997df8
commit 07a73de55f
8 changed files with 76 additions and 51 deletions

View File

@ -57,12 +57,12 @@ import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack
import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.localFileTransfer.WifiDirectManager.Companion.getDeviceStatus import org.kiwix.kiwixmobile.localFileTransfer.WifiDirectManager.Companion.getDeviceStatus
import org.kiwix.kiwixmobile.localFileTransfer.adapter.WifiP2pDelegate import org.kiwix.kiwixmobile.localFileTransfer.adapter.WifiP2pDelegate
import org.kiwix.kiwixmobile.localFileTransfer.adapter.WifiPeerListAdapter import org.kiwix.kiwixmobile.localFileTransfer.adapter.WifiPeerListAdapter
import java.util.ArrayList
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -94,6 +94,9 @@ class LocalFileTransferFragment :
@Inject @Inject
lateinit var locationManager: LocationManager lateinit var locationManager: LocationManager
@Inject
lateinit var sharedPreferenceUtil: SharedPreferenceUtil
private var fileListAdapter: FileListAdapter? = null private var fileListAdapter: FileListAdapter? = null
private var wifiPeerListAdapter: WifiPeerListAdapter? = null private var wifiPeerListAdapter: WifiPeerListAdapter? = null
@ -243,18 +246,21 @@ class LocalFileTransferFragment :
} }
private fun checkExternalStorageWritePermission(): Boolean { // To access and store the zims private fun checkExternalStorageWritePermission(): Boolean { // To access and store the zims
return permissionIsGranted(WRITE_EXTERNAL_STORAGE).also { permissionGranted -> if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
if (!permissionGranted) { return permissionIsGranted(WRITE_EXTERNAL_STORAGE).also { permissionGranted ->
if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) { if (!permissionGranted) {
alertDialogShower.show( if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) {
KiwixDialog.StoragePermissionRationale, alertDialogShower.show(
::requestStoragePermissionPermission KiwixDialog.StoragePermissionRationale,
) ::requestStoragePermissionPermission
} else { )
requestStoragePermissionPermission() } else {
requestStoragePermissionPermission()
}
} }
} }
} }
return true
} }
private fun shouldShowRationale(writeExternalStorage: String) = private fun shouldShowRationale(writeExternalStorage: String) =

View File

@ -240,7 +240,8 @@ class LocalLibraryFragment : BaseFragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
checkPermissions() if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove())
checkPermissions()
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

@ -350,21 +350,24 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
} }
private fun checkExternalStorageWritePermission(): Boolean { private fun checkExternalStorageWritePermission(): Boolean {
return hasPermission(WRITE_EXTERNAL_STORAGE).also { permissionGranted -> if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
if (!permissionGranted) { return hasPermission(WRITE_EXTERNAL_STORAGE).also { permissionGranted ->
if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) { if (!permissionGranted) {
alertDialogShower.show( if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) {
KiwixDialog.WriteStoragePermissionRationale, alertDialogShower.show(
::requestExternalStoragePermission KiwixDialog.WriteStoragePermissionRationale,
) ::requestExternalStoragePermission
} else { )
alertDialogShower.show( } else {
KiwixDialog.WriteStoragePermissionRationale, alertDialogShower.show(
::openAppSettings KiwixDialog.WriteStoragePermissionRationale,
) ::openAppSettings
)
}
} }
} }
} }
return true
} }
private fun openAppSettings() { private fun openAppSettings() {
@ -397,7 +400,8 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE
) { ) {
if (grantResults[0] != PERMISSION_GRANTED) { if (grantResults[0] != PERMISSION_GRANTED) {
checkExternalStorageWritePermission() if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove())
checkExternalStorageWritePermission()
} }
} }
} }

View File

@ -272,7 +272,8 @@ class AddNoteDialog : DialogFragment() {
if (ContextCompat.checkSelfPermission( if (ContextCompat.checkSelfPermission(
requireContext(), requireContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED ) != PackageManager.PERMISSION_GRANTED &&
!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()
) { ) {
Log.d( Log.d(
TAG, TAG,

View File

@ -944,30 +944,35 @@ public abstract class CoreReaderFragment extends BaseFragment
} }
private boolean requestExternalStorageWritePermissionForNotes() { private boolean requestExternalStorageWritePermissionForNotes() {
if (Build.VERSION.SDK_INT >= 23) { // For Marshmallow & higher API levels boolean isPermissionGranted = false;
if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
if (Build.VERSION.SDK_INT >= 23) { // For Marshmallow & higher API levels
if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PERMISSION_GRANTED) { == PERMISSION_GRANTED) {
return true; isPermissionGranted = true;
} else { } else {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
/* shouldShowRequestPermissionRationale() returns false when: /* shouldShowRequestPermissionRationale() returns false when:
* 1) User has previously checked on "Don't ask me again", and/or * 1) User has previously checked on "Don't ask me again", and/or
* 2) Permission has been disabled on device * 2) Permission has been disabled on device
*/ */
ContextExtensionsKt.toast(getActivity(), ContextExtensionsKt.toast(getActivity(),
R.string.ext_storage_permission_rationale_add_note, R.string.ext_storage_permission_rationale_add_note,
Toast.LENGTH_LONG); Toast.LENGTH_LONG);
}
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE);
} }
} else { // For Android versions below Marshmallow 6.0 (API 23)
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, isPermissionGranted = true; // As already requested at install time
REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE);
} }
} else { // For Android versions below Marshmallow 6.0 (API 23) } else {
return true; // As already requested at install time isPermissionGranted = true;
} }
return false; return isPermissionGranted;
} }
@SuppressWarnings("SameReturnValue") @SuppressWarnings("SameReturnValue")
@ -1032,6 +1037,9 @@ public abstract class CoreReaderFragment extends BaseFragment
} }
private boolean hasPermission(String permission) { private boolean hasPermission(String permission) {
if (sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
return true;
}
return ContextCompat.checkSelfPermission(getActivity(), permission) == PERMISSION_GRANTED; return ContextCompat.checkSelfPermission(getActivity(), permission) == PERMISSION_GRANTED;
} }
@ -1060,10 +1068,10 @@ public abstract class CoreReaderFragment extends BaseFragment
openArticle(UNINITIALISER_ADDRESS); openArticle(UNINITIALISER_ADDRESS);
safeDispose(); safeDispose();
bookmarkingDisposable = Flowable.combineLatest( bookmarkingDisposable = Flowable.combineLatest(
newBookmarksDao.bookmarkUrlsForCurrentBook(zimFileReader), newBookmarksDao.bookmarkUrlsForCurrentBook(zimFileReader),
webUrlsProcessor, webUrlsProcessor,
(bookmarkUrls, currentUrl) -> bookmarkUrls.contains(currentUrl) (bookmarkUrls, currentUrl) -> bookmarkUrls.contains(currentUrl)
).observeOn(AndroidSchedulers.mainThread()) ).observeOn(AndroidSchedulers.mainThread())
.subscribe(isBookmarked -> { .subscribe(isBookmarked -> {
this.isBookmarked = isBookmarked; this.isBookmarked = isBookmarked;
bottomToolbarBookmark.setImageResource( bottomToolbarBookmark.setImageResource(
@ -1116,7 +1124,7 @@ public abstract class CoreReaderFragment extends BaseFragment
showAddNoteDialog(); showAddNoteDialog();
} else { } else {
Toast.makeText(getActivity().getApplicationContext(), Toast.makeText(getActivity().getApplicationContext(),
getString(R.string.ext_storage_write_permission_denied_add_note), Toast.LENGTH_LONG) getString(R.string.ext_storage_write_permission_denied_add_note), Toast.LENGTH_LONG)
.show(); .show();
} }

View File

@ -229,7 +229,8 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
if (CoreApp.getInstance().isExternalStorageWritable()) { if (CoreApp.getInstance().isExternalStorageWritable()) {
if (ContextCompat.checkSelfPermission(getActivity(), if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED &&
!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
Snackbar.make(getView(), R.string.ext_storage_permission_not_granted, Snackbar.LENGTH_SHORT) Snackbar.make(getView(), R.string.ext_storage_permission_not_granted, Snackbar.LENGTH_SHORT)
.show(); .show();
return; return;

View File

@ -190,6 +190,9 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) {
else else
path.substringBefore(context.getString(R.string.android_directory_seperator)) path.substringBefore(context.getString(R.string.android_directory_seperator))
fun isPlayStoreBuildWithAndroid11OrAbove(): Boolean =
isPlayStoreBuild && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
companion object { companion object {
// Prefs // Prefs
const val PREF_LANG = "pref_language_chooser" const val PREF_LANG = "pref_language_chooser"

View File

@ -157,7 +157,8 @@ class CustomReaderFragment : CoreReaderFragment() {
if (ContextCompat.checkSelfPermission( if (ContextCompat.checkSelfPermission(
requireActivity(), requireActivity(),
READ_EXTERNAL_STORAGE READ_EXTERNAL_STORAGE
) == PERMISSION_DENIED ) == PERMISSION_DENIED &&
!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()
) { ) {
requestPermissions(arrayOf(READ_EXTERNAL_STORAGE), REQUEST_READ_FOR_OBB) requestPermissions(arrayOf(READ_EXTERNAL_STORAGE), REQUEST_READ_FOR_OBB)
} else { } else {