Android 6+ Permissions #2
This commit is contained in:
parent
f25715d87c
commit
dd02fcaa85
1 changed files with 31 additions and 39 deletions
|
@ -10,6 +10,7 @@ import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
@ -25,6 +26,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import info.nightscout.androidaps.events.EventAppExit;
|
import info.nightscout.androidaps.events.EventAppExit;
|
||||||
|
import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.events.EventRefreshGui;
|
import info.nightscout.androidaps.events.EventRefreshGui;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
|
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
|
||||||
|
@ -39,14 +41,19 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static KeepAliveReceiver keepAliveReceiver;
|
private static KeepAliveReceiver keepAliveReceiver;
|
||||||
|
|
||||||
|
static final Integer STORAGE = 0x1;
|
||||||
|
static final Integer SMS = 0x2;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
checkPermission();
|
|
||||||
Iconify.with(new FontAwesomeModule());
|
Iconify.with(new FontAwesomeModule());
|
||||||
LocaleHelper.onCreate(this, "en");
|
LocaleHelper.onCreate(this, "en");
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
checkEula();
|
checkEula();
|
||||||
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
|
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE);
|
||||||
|
}
|
||||||
if (Config.logFunctionCalls)
|
if (Config.logFunctionCalls)
|
||||||
log.debug("onCreate");
|
log.debug("onCreate");
|
||||||
|
|
||||||
|
@ -184,52 +191,37 @@ public class MainActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkPermission() {
|
@Subscribe
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
public void onStatusEvent(final EventPreferenceChange ev) {
|
||||||
if (!checkIfAlreadyhavePermission()) {requestForSpecificPermission();}
|
SharedPreferences smssettings = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
if (smssettings.getBoolean("smscommunicator_remotecommandsallowed", false)) {
|
||||||
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
|
askForPermission(Manifest.permission.RECEIVE_SMS, SMS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkIfAlreadyhavePermission() {
|
private void askForPermission(String permission, Integer requestCode) {
|
||||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||||
return true;
|
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
|
||||||
}
|
}
|
||||||
else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
|
|
||||||
alert.setTitle("Restart");
|
|
||||||
alert.setMessage("Please Restart Android APS \notherwise Android APS will not work proper!");
|
|
||||||
alert.setPositiveButton("OK",null);
|
|
||||||
alert.show();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void requestForSpecificPermission(){
|
|
||||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
switch (requestCode) {
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
case 101:
|
if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED){
|
||||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
switch (requestCode) {
|
||||||
//granted
|
case 1:
|
||||||
}
|
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
||||||
else {
|
alert.setTitle("Restart");
|
||||||
//not granted
|
alert.setMessage("Please Restart Android APS \notherwise Android APS will not work proper!");
|
||||||
}
|
alert.setPositiveButton("OK",null);
|
||||||
break;
|
alert.show();
|
||||||
default:
|
break;
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
case 2:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue