Skip to content

Commit

Permalink
Fix to external storage permissions request
Browse files Browse the repository at this point in the history
  • Loading branch information
GarethAyres committed Nov 7, 2018
1 parent 7fd915e commit 1312624
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.ac.swansea.eduroamcat"
android:versionCode="50"
android:versionName="1.2.5">
android:versionCode="51"
android:versionName="1.2.6">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Expand Down
3 changes: 2 additions & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<string name="authMethod_text_innereapmethod">Inner EAP Method Type:</string>
<string name="authMethod_text_outter">Outer Identity:</string>
<string name="authMethod_text_server">Authentication Server:</string>
<string name="authMethod_text_certcn">Certificate CN:</string>
<string name="authMethod_text_certcn">CA Certificate CN:</string>
<string name="authMethod_text_error1">Authentication Methods</string>
<string name="authMethod_text_error2">ERROR - No Auth Methods found.</string>
<string name="summary_text_title">Configuration File Summary</string>
Expand Down Expand Up @@ -157,4 +157,5 @@
<string name="authMethod_text_clientcertexp">Credential Expiry:</string>
<string name="pinDialog">Enter import PIN:</string>
<string name="pinFailed">PIN incorrect, unable to import!</string>
<string name="storagePermission">No External Storage Permission</string>
</resources>
2 changes: 0 additions & 2 deletions src/uk/ac/swansea/eduroamcat/ConfigureFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
X509Certificate acert;
acert=aAuthMethod.getClientCert();
if (acert != null) {
eduroamCAT.debug("key cert=" + acert.toString());
eduroamCAT.debug("subjectdn="+acert.getSubjectDN().getName());
String expiry = acert.getNotAfter().toString();
String issuer = acert.getSubjectDN().getName();
int start,finish=0;
Expand Down
8 changes: 7 additions & 1 deletion src/uk/ac/swansea/eduroamcat/DownloadEAPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ protected Integer doInBackground(String... aurl) {
URLConnection connection = url.openConnection();
eduroamCAT.debug("starting download...");
eduroamCAT.downloaded=false;
//test storage
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
eduroamCAT.debug("Storage state ok ="+state.toString());
}
else eduroamCAT.debug("Storage state BAD ="+state.toString());
InputStream input = new BufferedInputStream(url.openStream());
String path = Environment.getExternalStorageDirectory().getPath() + "/EAPConfig/";
File file = new File(path);
Expand All @@ -43,7 +49,7 @@ protected Integer doInBackground(String... aurl) {
code=1;

} catch (Exception e) {
eduroamCAT.debug("Error writing file to external storage");
eduroamCAT.debug("Error writing file to external storage:"+e.getMessage() );
code=0;
}
if (code>0) eduroamCAT.downloaded=true;
Expand Down
32 changes: 32 additions & 0 deletions src/uk/ac/swansea/eduroamcat/EAPMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -56,6 +60,7 @@ public class EAPMetadata extends Activity {
String keyPass=""; //default to nothing to start (optional)
//global clietn cert value for retry
static NodeList clientCert;
private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;

public boolean testExternalStorage()
{
Expand All @@ -65,6 +70,21 @@ public boolean testExternalStorage()
}
else return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
this.recreate();
} else {
Toast.makeText(this, this.getString(R.string.storagePermission), Toast.LENGTH_LONG).show();
}
return;
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -82,6 +102,18 @@ protected void onCreate(Bundle savedInstanceState) {
boolean configFileError = false;
String pathToDownload ="";
eduroamCAT.debug("Got eap-config:"+configIntent.getDataString());

//check real-time permissions for storage
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
eduroamCAT.debug("No External Storage permissions");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
configFileError=true;
Toast.makeText(this, this.getString(R.string.storagePermission), Toast.LENGTH_LONG).show();
}
else configFileError=false;

if (configIntent.getDataString().contains("http://") || configIntent.getDataString().contains("https://"))
{
//download file to sdcard
Expand Down

0 comments on commit 1312624

Please sign in to comment.