Skip to content

Commit 3ba6ed6

Browse files
committed
Request storage permission if it's not enabled
1 parent 2a553dc commit 3ba6ed6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ buildscript {
1515
allprojects {
1616
repositories {
1717
jcenter()
18+
maven {
19+
url 'https://maven.google.com'
20+
}
1821
}
1922
}
2023

android/project/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ dependencies {
5353
exclude group: 'com.android.support', module: 'support-annotations'
5454
})
5555
testCompile 'junit:junit:4.12'
56+
compile "com.android.support:support-compat:26.1.0"
5657
}

android/project/src/main/java/org/libsdl/app/SDLActivity.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import android.media.*;
3030
import android.hardware.*;
3131
import android.content.pm.ActivityInfo;
32+
import android.content.pm.PackageManager;
33+
import android.Manifest;
34+
import android.support.v4.content.ContextCompat;
35+
import android.support.v4.app.ActivityCompat;
3236

3337
/**
3438
SDL Activity
@@ -161,6 +165,36 @@ public void onClick(DialogInterface dialog,int id) {
161165
return;
162166
}
163167

168+
if (!haveStoragePermissions()) {
169+
String[] requiredPermissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
170+
ActivityCompat.requestPermissions(this, requiredPermissions, 1);
171+
synchronized(this) {
172+
try {
173+
wait();
174+
} catch (InterruptedException ex) {
175+
ex.printStackTrace();
176+
}
177+
}
178+
179+
if (!haveStoragePermissions()) {
180+
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
181+
dlgAlert.setMessage("Freeciv cannot start without access to storage.");
182+
dlgAlert.setTitle("Storage access required");
183+
dlgAlert.setPositiveButton("Exit",
184+
new DialogInterface.OnClickListener() {
185+
@Override
186+
public void onClick(DialogInterface dialog,int id) {
187+
// if this button is clicked, close current activity
188+
SDLActivity.mSingleton.finish();
189+
}
190+
});
191+
dlgAlert.setCancelable(false);
192+
dlgAlert.create().show();
193+
194+
return;
195+
}
196+
}
197+
164198
// Set up the surface
165199
mSurface = new SDLSurface(getApplication());
166200

@@ -188,6 +222,17 @@ public void onClick(DialogInterface dialog,int id) {
188222
}
189223
}
190224

225+
@Override
226+
public synchronized void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
227+
notify();
228+
}
229+
230+
private boolean haveStoragePermissions() {
231+
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
232+
&& ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
233+
}
234+
235+
191236
// Events
192237
@Override
193238
protected void onPause() {

0 commit comments

Comments
 (0)