permission handler implemented
This commit is contained in:
parent
313a87bd12
commit
180980f1f3
@ -2,6 +2,9 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.example.aped">
|
package="com.example.aped">
|
||||||
|
|
||||||
|
<!-- permission to read and write the XML file -->
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package com.example.aped;
|
package com.example.aped;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -9,10 +12,11 @@ import android.widget.Toast;
|
|||||||
import com.example.aped.communication.IIO;
|
import com.example.aped.communication.IIO;
|
||||||
import com.example.aped.utils.IXML;
|
import com.example.aped.utils.IXML;
|
||||||
import com.example.aped.utils.TestXML;
|
import com.example.aped.utils.TestXML;
|
||||||
import com.example.aped.utils.XMLHandler;
|
|
||||||
import com.google.android.material.navigation.NavigationView;
|
import com.google.android.material.navigation.NavigationView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.navigation.NavController;
|
import androidx.navigation.NavController;
|
||||||
import androidx.navigation.Navigation;
|
import androidx.navigation.Navigation;
|
||||||
import androidx.navigation.ui.AppBarConfiguration;
|
import androidx.navigation.ui.AppBarConfiguration;
|
||||||
@ -21,7 +25,11 @@ import androidx.drawerlayout.widget.DrawerLayout;
|
|||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
|
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
/** permission code for the storage permisson.*/
|
||||||
|
private final int STORAGE_PERMISSION_CODE = 42;
|
||||||
/** was soll angezeigt werden in Navigation.*/
|
/** was soll angezeigt werden in Navigation.*/
|
||||||
private AppBarConfiguration mAppBarConfiguration;
|
private AppBarConfiguration mAppBarConfiguration;
|
||||||
/** zur Verwendung von xml anstatt der direkten Einbindung.*/
|
/** zur Verwendung von xml anstatt der direkten Einbindung.*/
|
||||||
@ -32,6 +40,8 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceState) {
|
protected void onCreate(final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
//checks that the permission to read and write the xml is granted
|
||||||
|
ensurePermissions();
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
@ -73,26 +83,97 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
case R.id.action_settings:
|
case R.id.action_settings:
|
||||||
Toast.makeText(this, "Settings",
|
Toast.makeText(this, "Settings",
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
return true;
|
break;
|
||||||
|
|
||||||
case R.id.action_adjust_xml:
|
case R.id.action_adjust_xml:
|
||||||
Toast.makeText(this, "Adjust .xml",
|
Toast.makeText(this, "Adjust .xml",
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
return true;
|
break;
|
||||||
|
|
||||||
case R.id.action_download_xml:
|
case R.id.action_download_xml:
|
||||||
if (xml.download() == 0) {
|
if (xml.download() == 0) {
|
||||||
Toast.makeText(this, "Download .xml",
|
Toast.makeText(this, "Download .xml",
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
return true;
|
break;
|
||||||
case R.id.action_upload_xml:
|
case R.id.action_upload_xml:
|
||||||
if (xml.upload() == 0) {
|
if (xml.upload() == 0) {
|
||||||
Toast.makeText(this, "Upload .xml",
|
Toast.makeText(this, "Upload .xml",
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
return true;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Log.e("MainActivity", "unknown item: "
|
||||||
|
+ item.toString());
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ensures that all the permissions needed by the application are granted.
|
||||||
|
*/
|
||||||
|
private void ensurePermissions() {
|
||||||
|
if (ContextCompat.checkSelfPermission(
|
||||||
|
this,
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
== PERMISSION_GRANTED) {
|
||||||
|
Toast.makeText(
|
||||||
|
this,
|
||||||
|
"You have already granted this permission",
|
||||||
|
Toast.LENGTH_LONG)
|
||||||
|
.show();
|
||||||
|
} else {
|
||||||
|
if (ActivityCompat.shouldShowRequestPermissionRationale(
|
||||||
|
this,
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||||
|
|
||||||
|
new AlertDialog.Builder(this)
|
||||||
|
.setTitle("Permission needed")
|
||||||
|
.setMessage("This permission is needed to access the "
|
||||||
|
+ "XML configuration file")
|
||||||
|
.setPositiveButton("ok", (dialog, which) ->
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
MainActivity.this,
|
||||||
|
new String[]{
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||||
|
STORAGE_PERMISSION_CODE))
|
||||||
|
.setNegativeButton("cancel", (dialog, which) ->
|
||||||
|
System.exit(1))
|
||||||
|
.create()
|
||||||
|
.show();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this,
|
||||||
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||||
|
STORAGE_PERMISSION_CODE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles request permission results.
|
||||||
|
* @param requestCode the code of the requested
|
||||||
|
* @param permissions the permissions requested
|
||||||
|
* @param grantResults the result of the permission request
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(
|
||||||
|
final int requestCode,
|
||||||
|
@NonNull final String[] permissions,
|
||||||
|
@NonNull final int[] grantResults) {
|
||||||
|
if (requestCode == STORAGE_PERMISSION_CODE) {
|
||||||
|
if (grantResults.length > 0
|
||||||
|
&& grantResults[0] == PERMISSION_GRANTED) {
|
||||||
|
Toast.makeText(
|
||||||
|
this,
|
||||||
|
"Permission GRANTED",
|
||||||
|
Toast.LENGTH_LONG)
|
||||||
|
.show();
|
||||||
|
} else {
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user