Skip to content

Commit c270c45

Browse files
mathiascodeCl1608Ho
authored andcommitted
Simplify permission check (#39)
* Simplify permission check * Check preferences before write * Destroy on pause
1 parent 567cf8d commit c270c45

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

app/src/main/java/org/cuberite/android/MainActivity.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,31 @@ public static void showSnackBar(Activity activity, String message) {
108108
}
109109

110110
private void showPermissionPopup() {
111-
AlertDialog.Builder permissionPopupBuilder = new AlertDialog.Builder(this);
112-
permissionPopupBuilder.setTitle(getString(R.string.status_permissions_needed));
113-
permissionPopupBuilder.setMessage(R.string.message_externalstorage_permission);
114-
permissionPopupBuilder.setCancelable(false);
115-
permissionPopupBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
116-
public void onClick(DialogInterface dialog, int id) {
117-
Log.d(LOG, "Requesting permissions for external storage");
118-
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION);
119-
}
120-
});
111+
permissionPopup = new AlertDialog.Builder(this)
112+
.setTitle(getString(R.string.status_permissions_needed))
113+
.setMessage(R.string.message_externalstorage_permission)
114+
.setCancelable(false)
115+
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
116+
public void onClick(DialogInterface dialog, int id) {
117+
Log.d(LOG, "Requesting permissions for external storage");
118+
permissionPopup = null;
119+
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION);
120+
}
121+
})
122+
.create();
121123

122-
permissionPopup = permissionPopupBuilder.create();
123124
permissionPopup.show();
124125
}
125126

126127
private void checkPermissions() {
127-
if (preferences.getString("cuberiteLocation", null) == null) {
128-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
129-
// Always use public dir in Lollipop and earlier, since permissions are granted when the app is installed
130-
preferences.edit().putString("cuberiteLocation", PUBLIC_DIR + "/cuberite-server").apply();
131-
} else {
128+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
129+
// User is running Android 6 or above, show permission popup on first run
130+
// or if user granted permission and later denied it
131+
132+
if (!preferences.getString("cuberiteLocation", "").startsWith(PRIVATE_DIR)) {
132133
showPermissionPopup();
133134
}
134-
} else if (preferences.getString("cuberiteLocation", "").startsWith(PUBLIC_DIR) &&
135-
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
136-
showPermissionPopup();
137-
} else if (preferences.getString("cuberiteLocation", "").startsWith(PRIVATE_DIR) &&
138-
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
135+
} else if (!preferences.getString("cuberiteLocation", "").startsWith(PUBLIC_DIR)) {
139136
preferences.edit().putString("cuberiteLocation", PUBLIC_DIR + "/cuberite-server").apply();
140137
}
141138
}
@@ -144,7 +141,8 @@ private void checkPermissions() {
144141
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
145142
if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) {
146143
// If request is cancelled, the result arrays are empty.
147-
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
144+
if (grantResults.length > 0
145+
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
148146
Log.i(LOG, "Got permissions, using public directory");
149147
preferences.edit().putString("cuberiteLocation", PUBLIC_DIR + "/cuberite-server").apply();
150148
} else {
@@ -168,4 +166,4 @@ public void onResume() {
168166
super.onResume();
169167
checkPermissions();
170168
}
171-
}
169+
}

0 commit comments

Comments
 (0)