11package com .sdl .mobileweather .activity ;
22
3-
43import android .Manifest ;
54import android .app .ActionBar ;
65import android .app .ActionBar .Tab ;
1413import android .content .res .Configuration ;
1514import android .os .Build ;
1615import android .os .Bundle ;
16+
17+ import androidx .annotation .NonNull ;
1718import androidx .legacy .app .ActionBarDrawerToggle ;
1819import androidx .core .app .ActivityCompat ;
1920import androidx .core .content .ContextCompat ;
3536import com .sdl .mobileweather .fragments .ForecastFragment ;
3637import com .sdl .mobileweather .smartdevicelink .SdlActivity ;
3738import com .sdl .mobileweather .smartdevicelink .SdlApplication ;
38- import com .sdl .mobileweather .smartdevicelink .SdlReceiver ;
39+
40+ import java .util .ArrayList ;
3941
4042
4143public class MainActivity extends SdlActivity implements ActionBar .TabListener {
4244
4345 private static final String SELECTED_NAVIGATION_ITEM = "selected_navigation_item" ;
4446 private static final String APP_ID = "bf2c3a7bad6b0c79152f50cc42ba1ace" ;
45- private static final int PERMISSIONS_REQUEST_CODE = 100 ;
47+ private static final int PERMISSIONS_REQUEST_CODE = 200 ;
4648
4749 private Fragment mCurrentFragment ;
4850 private DrawerLayout mDrawerLayout ;
@@ -137,14 +139,12 @@ else if ((getResources().getString(R.string.drawer_item_about)).equals(item)){
137139 mDrawerList .setItemChecked (position , false );
138140 mDrawerLayout .closeDrawer (mDrawerList );
139141 }
140-
141-
142-
142+
143143 private void checkForCrashes () {}
144144
145145 private void checkForUpdates () {}
146146
147- private boolean checkPermissions () {
147+ private boolean hasPermissions () {
148148 boolean permissionsGranted ;
149149
150150 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
@@ -159,17 +159,22 @@ private boolean checkPermissions() {
159159 return permissionsGranted ;
160160 }
161161
162- private void requestPermissions () {
163- String [] permissions ;
162+ private void requestPermission (String [] permissions , int REQUEST_CODE ) {
163+ ActivityCompat .requestPermissions (this , permissions , REQUEST_CODE );
164+ }
164165
165- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
166- permissions = new String []{Manifest .permission .BLUETOOTH_CONNECT , Manifest .permission .ACCESS_FINE_LOCATION };
166+ private @ NonNull String [] permissionsNeeded () {
167+ ArrayList <String > result = new ArrayList <>();
168+ if (!hasBTPermission ()) {
169+ result .add (Manifest .permission .BLUETOOTH_CONNECT );
167170 }
168- else {
169- permissions = new String []{ Manifest .permission .ACCESS_FINE_LOCATION } ;
171+ if (! hasPNPermission ()) {
172+ result . add ( Manifest .permission .POST_NOTIFICATIONS ) ;
170173 }
171-
172- ActivityCompat .requestPermissions (this , permissions , PERMISSIONS_REQUEST_CODE );
174+ if (!hasLocation ()) {
175+ result .add (Manifest .permission .ACCESS_FINE_LOCATION );
176+ }
177+ return (result .toArray (new String [result .size ()]));
173178 }
174179
175180 @ Override
@@ -238,13 +243,41 @@ protected void onStart() {
238243 lbManager .registerReceiver (mHourlyForecastReceiver , new IntentFilter ("com.sdl.mobileweather.HourlyForecast" ));
239244
240245 // Ask for permissions
241- if (!checkPermissions ()) {
242- requestPermissions ();
243- } else {
244- startServices ();
246+ String [] permissionsNeeded = permissionsNeeded ();
247+ if (permissionsNeeded .length > 0 ) {
248+ requestPermission (permissionsNeeded , PERMISSIONS_REQUEST_CODE );
249+ for (String permission : permissionsNeeded ) {
250+ if (Manifest .permission .BLUETOOTH_CONNECT .equals (permission )) {
251+ // We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
252+ return ;
253+ }
254+ }
245255 }
256+ startServices ();
257+ }
258+
259+ /**
260+ * Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
261+ * @return false if we need to request BLUETOOTH_CONNECT permission
262+ */
263+ private boolean hasBTPermission () {
264+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ? checkPermission (Manifest .permission .BLUETOOTH_CONNECT ) : true ;
265+ }
246266
267+ /**
268+ * Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
269+ * @return false if we need to request POST_NOTIFICATIONS permission
270+ */
271+ private boolean hasPNPermission () {
272+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ? checkPermission (Manifest .permission .POST_NOTIFICATIONS ) : true ;
273+ }
247274
275+ private boolean hasLocation () {
276+ return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), Manifest .permission .ACCESS_FINE_LOCATION );
277+ }
278+
279+ private boolean checkPermission (String permission ) {
280+ return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission (getApplicationContext (), permission );
248281 }
249282
250283 private void startServices () {
@@ -400,11 +433,23 @@ public void onSaveInstanceState(Bundle outState) {
400433 @ Override
401434 public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
402435 if (requestCode == PERMISSIONS_REQUEST_CODE ) {
403- if (!checkPermissions ()) {
436+ if (!hasPermissions ()) {
404437 Toast .makeText (this , "The app cannot run without these permissions!" , Toast .LENGTH_SHORT ).show ();
405438 finish ();
406439 } else {
407440 startServices ();
441+ if (grantResults .length > 0 ) {
442+ for (int i = 0 ; i < grantResults .length ; i ++) {
443+ if (permissions [i ].equals (Manifest .permission .POST_NOTIFICATIONS )) {
444+ boolean postNotificationGranted =
445+ grantResults [i ] == PackageManager .PERMISSION_GRANTED ;
446+ if (!postNotificationGranted ) {
447+ // User denied permission, Notifications for SDL will not appear
448+ // on Android 13 devices.
449+ }
450+ }
451+ }
452+ }
408453 }
409454 }
410455 }
0 commit comments