From f25715d87c7850a07591f238660b1b2797cec723 Mon Sep 17 00:00:00 2001 From: LadyViktoria Date: Thu, 24 Nov 2016 18:46:16 +0100 Subject: [PATCH] Android 6+ Permissions --- .../nightscout/androidaps/MainActivity.java | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index aead928e52..d8356dec9b 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -1,11 +1,14 @@ package info.nightscout.androidaps; +import android.Manifest; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; @@ -39,6 +42,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + checkPermission(); Iconify.with(new FontAwesomeModule()); LocaleHelper.onCreate(this, "en"); setContentView(R.layout.activity_main); @@ -162,7 +166,6 @@ public class MainActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } - private void registerBus() { try { MainApp.bus().unregister(this); @@ -181,4 +184,52 @@ public class MainActivity extends AppCompatActivity { } } -} + private void checkPermission() { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { + if (!checkIfAlreadyhavePermission()) {requestForSpecificPermission();} + } + } + + private boolean checkIfAlreadyhavePermission() { + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + return true; + } + 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 + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + switch (requestCode) { + case 101: + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + //granted + } + else { + //not granted + } + break; + default: + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + } +} \ No newline at end of file