1515 */
1616package org .opendatakit .sensors .ui .activity ;
1717
18+ import android .Manifest ;
1819import android .app .Activity ;
20+ import android .content .DialogInterface ;
1921import android .content .Intent ;
22+ import android .content .pm .PackageManager ;
2023import android .os .Bundle ;
24+ import android .support .annotation .NonNull ;
25+ import android .support .v4 .app .ActivityCompat ;
2126import android .util .Log ;
2227import android .view .View ;
28+ import android .widget .Toast ;
2329import org .opendatakit .sensors .Constants ;
2430import org .opendatakit .sensors .R ;
2531import org .opendatakit .sensors .SensorsSingleton ;
2632import org .opendatakit .sensors .ServiceConstants ;
33+ import org .opendatakit .utilities .RuntimePermissionUtils ;
2734
2835/**
2936@@ -33,6 +40,15 @@ public class AddSensorActivity extends Activity {
3340
3441 private static final String LOGTAG = AddSensorActivity .class .getSimpleName ();
3542
43+ protected static final String [] REQUIRED_PERMISSIONS = new String [] {
44+ Manifest .permission .BLUETOOTH_ADMIN ,
45+ Manifest .permission .BLUETOOTH ,
46+ Manifest .permission .WRITE_EXTERNAL_STORAGE ,
47+ Manifest .permission .ACCESS_COARSE_LOCATION
48+ };
49+
50+ protected static final int PERMISSION_REQ_CODE = 5 ;
51+
3652 private static final int RESULT_OK_BT = 1 ;
3753 private static final int RESULT_OK_USB = 2 ;
3854
@@ -41,6 +57,14 @@ public class AddSensorActivity extends Activity {
4157 @ Override public void onCreate (Bundle savedInstanceState ) {
4258 super .onCreate (savedInstanceState );
4359
60+ if (!RuntimePermissionUtils .checkSelfAllPermission (this , REQUIRED_PERMISSIONS )) {
61+ ActivityCompat .requestPermissions (
62+ this ,
63+ REQUIRED_PERMISSIONS ,
64+ PERMISSION_REQ_CODE
65+ );
66+ }
67+
4468 Intent intent = getIntent ();
4569 String tmpAppName = intent .getStringExtra (ServiceConstants .APP_NAME_KEY );
4670 if (tmpAppName == null ) {
@@ -92,4 +116,39 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
92116 }
93117 }
94118
119+ @ Override
120+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
121+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
122+
123+ if (requestCode != PERMISSION_REQ_CODE ) {
124+ return ;
125+ }
126+
127+ boolean granted = true ;
128+ if (grantResults .length > 0 ) {
129+ for (int i = 0 ; i < grantResults .length ; i ++) {
130+ if (grantResults [0 ] != PackageManager .PERMISSION_GRANTED ) {
131+ granted = false ;
132+ }
133+ }
134+
135+ if (granted )
136+ return ;
137+
138+ if (RuntimePermissionUtils .shouldShowAnyPermissionRationale (this , permissions )) {
139+ RuntimePermissionUtils .createPermissionRationaleDialog (this , requestCode , permissions ).setMessage (R .string .write_external_storage_rationale )
140+ .setNegativeButton (R .string .cancel , new DialogInterface .OnClickListener () {
141+ @ Override public void onClick (DialogInterface dialog , int which ) {
142+ dialog .cancel ();
143+ setResult (Activity .RESULT_CANCELED );
144+ finish ();
145+ }
146+ }).show ();
147+ } else {
148+ Toast .makeText (this , R .string .write_external_perm_denied , Toast .LENGTH_LONG ).show ();
149+ setResult (Activity .RESULT_CANCELED );
150+ finish ();
151+ }
152+ }
153+ }
95154}
0 commit comments