Part : 37 : Run Time Permission - Storage and Others

 Calling Method : 


if(!checkAndRequestPermissions()){
checkAndRequestPermissions();
return;
}


////////////////////////////////////////////////////////////////////////////////////


Method Code : 




public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;


private boolean checkAndRequestPermissions() {


int permissionReadExternalStorage;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
permissionReadExternalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_IMAGES);
else
permissionReadExternalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);


int permissionWriteExtarnalStorage;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
permissionWriteExtarnalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_AUDIO);
else
permissionWriteExtarnalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);


List<String> listPermissionsNeeded = new ArrayList<>();


if (permissionWriteExtarnalStorage != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_AUDIO);
else
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

}

if (permissionReadExternalStorage != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_IMAGES);
else
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

int permissionVideoStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_VIDEO);
if (permissionVideoStorage != PackageManager.PERMISSION_GRANTED) {

listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_VIDEO);

}

int notificationPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS);

if (notificationPermission != PackageManager.PERMISSION_GRANTED) {

listPermissionsNeeded.add(Manifest.permission.POST_NOTIFICATIONS);

}

}



if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[0]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}


@SuppressWarnings("ConstantConditions")
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {

Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
perms.put(Manifest.permission.READ_MEDIA_IMAGES, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_MEDIA_AUDIO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_MEDIA_VIDEO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.POST_NOTIFICATIONS, PackageManager.PERMISSION_GRANTED);


} else {
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
}


// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {


if (perms.get(Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_MEDIA_AUDIO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_MEDIA_VIDEO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED

) {
// All Permissions Are granted :
Toast.makeText(this, "Jajakumullah, For Granting Permission.", Toast.LENGTH_LONG).show();
makeFileListInListView();
//else any one or both the permissions are not granted
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_IMAGES)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_AUDIO)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_VIDEO)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.POST_NOTIFICATIONS)


) {
showDialogOK("Necessary Permissions required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
Toast.makeText(MainActivity.this, "Necessary Permissions required for this app", Toast.LENGTH_LONG).show();
// permissionSettingScreen ( );
// finish();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
permissionSettingScreen();

}
}


} else {


if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

) {
Toast.makeText(this, "Jajakumullah, For Granting Permission.", Toast.LENGTH_LONG).show();
//else any one or both the permissions are not granted
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)
) {
showDialogOK("Necessary Permissions required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
Toast.makeText(MainActivity.this, "Necessary Permissions required for this app", Toast.LENGTH_LONG).show();
// permissionSettingScreen ( );
// finish();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
permissionSettingScreen();

}
}


}


}

}
}

}

private void permissionSettingScreen() {
Toast.makeText(this, "Enable All permissions, Click On Permission", Toast.LENGTH_LONG)
.show();

Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
// finishAffinity();
finish();

}

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}


Manifest : 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />



<application
android:name="._MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.CreateDatabase"
tools:targetApi="31">
<activity
android:name=".InsertOrModifyFile"
android:exported="false" />
<activity
android:name=".DarsFileRead"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>



No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts