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

View File

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

View File

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

View File

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

View File

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

View File

@ -229,7 +229,8 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
if (CoreApp.getInstance().isExternalStorageWritable()) {
if (ContextCompat.checkSelfPermission(getActivity(),
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)
.show();
return;

View File

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

View File

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