|
29 | 29 | import android.media.*; |
30 | 30 | import android.hardware.*; |
31 | 31 | 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; |
32 | 36 |
|
33 | 37 | /** |
34 | 38 | SDL Activity |
@@ -161,6 +165,36 @@ public void onClick(DialogInterface dialog,int id) { |
161 | 165 | return; |
162 | 166 | } |
163 | 167 |
|
| 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 | + |
164 | 198 | // Set up the surface |
165 | 199 | mSurface = new SDLSurface(getApplication()); |
166 | 200 |
|
@@ -188,6 +222,17 @@ public void onClick(DialogInterface dialog,int id) { |
188 | 222 | } |
189 | 223 | } |
190 | 224 |
|
| 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 | + |
191 | 236 | // Events |
192 | 237 | @Override |
193 | 238 | protected void onPause() { |
|
0 commit comments