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,6 +246,7 @@ class LocalFileTransferFragment :
}
private fun checkExternalStorageWritePermission(): Boolean { // To access and store the zims
if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
return permissionIsGranted(WRITE_EXTERNAL_STORAGE).also { permissionGranted ->
if (!permissionGranted) {
if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) {
@ -256,6 +260,8 @@ class LocalFileTransferFragment :
}
}
}
return true
}
private fun shouldShowRationale(writeExternalStorage: String) =
ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), writeExternalStorage)

View File

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

View File

@ -350,6 +350,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}
private fun checkExternalStorageWritePermission(): Boolean {
if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
return hasPermission(WRITE_EXTERNAL_STORAGE).also { permissionGranted ->
if (!permissionGranted) {
if (shouldShowRationale(WRITE_EXTERNAL_STORAGE)) {
@ -366,6 +367,8 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}
}
}
return true
}
private fun openAppSettings() {
val uri: Uri = Uri.fromParts("package", requireActivity().packageName, null)
@ -397,6 +400,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE
) {
if (grantResults[0] != PERMISSION_GRANTED) {
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,11 +944,13 @@ public abstract class CoreReaderFragment extends BaseFragment
}
private boolean requestExternalStorageWritePermissionForNotes() {
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;
isPermissionGranted = true;
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
/* shouldShowRequestPermissionRationale() returns false when:
@ -964,10 +966,13 @@ public abstract class CoreReaderFragment extends BaseFragment
REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE);
}
} else { // For Android versions below Marshmallow 6.0 (API 23)
return true; // As already requested at install time
isPermissionGranted = 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;
}

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 {