11package com .sdl .mobileweather .activity ;
22
3- import static android .Manifest .permission .BLUETOOTH_CONNECT ;
43
54import android .Manifest ;
65import android .app .ActionBar ;
3029import android .widget .ListView ;
3130import android .widget .Toast ;
3231
32+ import com .sdl .mobileweather .BuildConfig ;
3333import com .sdl .mobileweather .R ;
3434import com .sdl .mobileweather .fragments .ConditionsFragment ;
3535import com .sdl .mobileweather .fragments .ForecastFragment ;
@@ -42,8 +42,7 @@ public class MainActivity extends SdlActivity implements ActionBar.TabListener {
4242
4343 private static final String SELECTED_NAVIGATION_ITEM = "selected_navigation_item" ;
4444 private static final String APP_ID = "bf2c3a7bad6b0c79152f50cc42ba1ace" ;
45- private static final int LOCATION_PERMISSION_REQUEST_CODE = 100 ;
46- private static final int BLUETOOTH_REQUEST_CODE = 200 ;
45+ private static final int PERMISSIONS_REQUEST_CODE = 100 ;
4746
4847 private Fragment mCurrentFragment ;
4948 private DrawerLayout mDrawerLayout ;
@@ -145,12 +144,32 @@ private void checkForCrashes() {}
145144
146145 private void checkForUpdates () {}
147146
148- private boolean checkPermission () {
149- return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), BLUETOOTH_CONNECT );
147+ private boolean checkPermissions () {
148+ boolean permissionsGranted ;
149+
150+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
151+ boolean bluetoothGranted = PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), Manifest .permission .BLUETOOTH_CONNECT );
152+ boolean locationGranted = PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), Manifest .permission .ACCESS_FINE_LOCATION );
153+ permissionsGranted = (BuildConfig .TRANSPORT .equals ("TCP" ) || bluetoothGranted ) && locationGranted ;
154+ }
155+ else {
156+ permissionsGranted = PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), Manifest .permission .ACCESS_FINE_LOCATION );
157+ }
158+
159+ return permissionsGranted ;
150160 }
151161
152- private void requestPermission () {
153- ActivityCompat .requestPermissions (this , new String []{BLUETOOTH_CONNECT }, BLUETOOTH_REQUEST_CODE );
162+ private void requestPermissions () {
163+ String [] permissions ;
164+
165+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
166+ permissions = new String []{Manifest .permission .BLUETOOTH_CONNECT , Manifest .permission .ACCESS_FINE_LOCATION };
167+ }
168+ else {
169+ permissions = new String []{Manifest .permission .ACCESS_FINE_LOCATION };
170+ }
171+
172+ ActivityCompat .requestPermissions (this , permissions , PERMISSIONS_REQUEST_CODE );
154173 }
155174
156175 @ Override
@@ -159,13 +178,6 @@ protected void onCreate(Bundle savedInstanceState) {
159178 super .onCreate (savedInstanceState );
160179 setContentView (R .layout .activity_main );
161180
162- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && !checkPermission ()) {
163- requestPermission ();
164- return ;
165- }
166-
167- SdlReceiver .queryForConnectedService (this );
168-
169181 // Create tabs
170182 ActionBar bar = getActionBar ();
171183 bar .setNavigationMode (ActionBar .NAVIGATION_MODE_TABS );
@@ -226,8 +238,8 @@ protected void onStart() {
226238 lbManager .registerReceiver (mHourlyForecastReceiver , new IntentFilter ("com.sdl.mobileweather.HourlyForecast" ));
227239
228240 // Ask for permissions
229- if (ContextCompat . checkSelfPermission ( this , Manifest . permission . ACCESS_FINE_LOCATION ) != PackageManager . PERMISSION_GRANTED ) {
230- ActivityCompat . requestPermissions (this , new String []{ Manifest . permission . ACCESS_FINE_LOCATION }, LOCATION_PERMISSION_REQUEST_CODE );
241+ if (! checkPermissions () ) {
242+ requestPermissions ();
231243 } else {
232244 startServices ();
233245 }
@@ -386,27 +398,14 @@ public void onSaveInstanceState(Bundle outState) {
386398 }
387399
388400 @ Override
389- public void onRequestPermissionsResult (int requestCode , String permissions [], int [] grantResults ) {
390- switch (requestCode ) {
391- case LOCATION_PERMISSION_REQUEST_CODE :
392- if (grantResults .length <= 0 || grantResults [0 ] != PackageManager .PERMISSION_GRANTED ) {
393- Toast .makeText (this , "The app cannot run without this permission!" ,
394- Toast .LENGTH_SHORT ).show ();
395- finish ();
396- } else {
397- startServices ();
398- }
399- break ;
400- case BLUETOOTH_REQUEST_CODE :
401- if (grantResults .length > 0 ) {
402- boolean btConnectGranted = grantResults [0 ] == PackageManager .PERMISSION_GRANTED ;
403-
404- if (btConnectGranted ) {
405- //Bluetooth permissions have been granted by the user so we can try to start out SdlService.
406- SdlReceiver .queryForConnectedService (this );
407- }
408- }
409- break ;
401+ public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
402+ if (requestCode == PERMISSIONS_REQUEST_CODE ) {
403+ if (!checkPermissions ()) {
404+ Toast .makeText (this , "The app cannot run without these permissions!" , Toast .LENGTH_SHORT ).show ();
405+ finish ();
406+ } else {
407+ startServices ();
408+ }
410409 }
411410 }
412411}
0 commit comments