Skip to content

Commit 47bf65b

Browse files
committed
Merge branch 'develop'
# Conflicts: # MobileWeather/app/build.gradle
2 parents df736fc + f520342 commit 47bf65b

File tree

6 files changed

+101
-27
lines changed

6 files changed

+101
-27
lines changed

MobileWeather/app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 29
4+
compileSdkVersion 31
55
defaultConfig {
66
applicationId "com.sdl.mobileweather"
77
minSdkVersion 16
8-
targetSdkVersion 29
8+
targetSdkVersion 31
99
versionCode 27
1010
versionName "1.7.15"
1111
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
@@ -37,7 +37,7 @@ dependencies {
3737
testImplementation 'junit:junit:4.12'
3838
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3939
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
40-
implementation 'com.smartdevicelink:sdl_android:5.+'
40+
implementation 'com.smartdevicelink:sdl_android:5.4.0'
4141
implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
4242
implementation 'com.google.android.gms:play-services-location:16.0.0'
4343
}

MobileWeather/app/src/main/AndroidManifest.xml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<!-- Required for AppLink connection -->
88
<uses-permission android:name="android.permission.BLUETOOTH" />
9+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
10+
tools:targetApi="31"/>
911
<!-- Required for weather data -->
1012
<uses-permission android:name="android.permission.INTERNET" />
1113
<!-- Required to check if WiFi is enabled -->
@@ -23,6 +25,8 @@
2325

2426
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2527

28+
<uses-feature android:name="android.hardware.usb.accessory"/>
29+
2630
<application
2731
android:name="com.sdl.mobileweather.smartdevicelink.SdlApplication"
2832
android:allowBackup="true"
@@ -35,7 +39,8 @@
3539
android:name="com.sdl.mobileweather.activity.MainActivity"
3640
android:configChanges="orientation"
3741
android:label="@string/title_activity_main"
38-
android:screenOrientation="portrait">
42+
android:screenOrientation="portrait"
43+
android:exported="true">
3944
<intent-filter>
4045
<action android:name="android.intent.action.MAIN" />
4146

@@ -48,9 +53,26 @@
4853
<activity android:name="com.smartdevicelink.managers.lockscreen.SDLLockScreenActivity"
4954
android:launchMode="singleTop"/>
5055

51-
<service android:name="com.sdl.mobileweather.smartdevicelink.SdlService"/>
5256
<service android:name="com.sdl.mobileweather.wunderground.WUndergroundService"/>
5357
<service android:name="com.sdl.mobileweather.forecastio.ForecastIoService"/>
58+
<activity android:name="com.smartdevicelink.transport.USBAccessoryAttachmentActivity"
59+
android:launchMode="singleTop"
60+
android:exported="true">
61+
<intent-filter>
62+
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
63+
</intent-filter>
64+
65+
<meta-data
66+
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
67+
android:resource="@xml/accessory_filter" />
68+
</activity>
69+
70+
<service
71+
android:name="com.sdl.mobileweather.smartdevicelink.SdlService"
72+
android:enabled="true"
73+
android:foregroundServiceType="connectedDevice"
74+
/>
75+
5476

5577
<service
5678
android:name="com.sdl.mobileweather.smartdevicelink.SdlRouterService"
@@ -64,7 +86,8 @@
6486
android:value="@integer/sdl_router_service_version_value" />
6587
</service>
6688

67-
<receiver android:name="com.sdl.mobileweather.smartdevicelink.SdlReceiver">
89+
<receiver android:name="com.sdl.mobileweather.smartdevicelink.SdlReceiver"
90+
android:exported="true">
6891
<intent-filter>
6992
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
7093
<action android:name="sdl.router.startservice" />

MobileWeather/app/src/main/java/com/sdl/mobileweather/activity/MainActivity.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sdl.mobileweather.activity;
22

3+
34
import android.Manifest;
45
import android.app.ActionBar;
56
import android.app.ActionBar.Tab;
@@ -11,6 +12,7 @@
1112
import android.content.IntentFilter;
1213
import android.content.pm.PackageManager;
1314
import android.content.res.Configuration;
15+
import android.os.Build;
1416
import android.os.Bundle;
1517
import androidx.legacy.app.ActionBarDrawerToggle;
1618
import androidx.core.app.ActivityCompat;
@@ -27,18 +29,20 @@
2729
import android.widget.ListView;
2830
import android.widget.Toast;
2931

32+
import com.sdl.mobileweather.BuildConfig;
3033
import com.sdl.mobileweather.R;
3134
import com.sdl.mobileweather.fragments.ConditionsFragment;
3235
import com.sdl.mobileweather.fragments.ForecastFragment;
3336
import com.sdl.mobileweather.smartdevicelink.SdlActivity;
3437
import com.sdl.mobileweather.smartdevicelink.SdlApplication;
38+
import com.sdl.mobileweather.smartdevicelink.SdlReceiver;
3539

3640

3741
public class MainActivity extends SdlActivity implements ActionBar.TabListener {
3842

3943
private static final String SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
4044
private static final String APP_ID = "bf2c3a7bad6b0c79152f50cc42ba1ace";
41-
private static final int LOCATION_PERMISSION_REQUEST_CODE = 100;
45+
private static final int PERMISSIONS_REQUEST_CODE = 100;
4246

4347
private Fragment mCurrentFragment;
4448
private DrawerLayout mDrawerLayout;
@@ -139,14 +143,41 @@ else if ((getResources().getString(R.string.drawer_item_about)).equals(item)){
139143
private void checkForCrashes() {}
140144

141145
private void checkForUpdates() {}
142-
146+
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;
160+
}
161+
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);
173+
}
174+
143175
@Override
144176
protected void onCreate(Bundle savedInstanceState) {
145177
Log.v(SdlApplication.TAG, "onCreate main");
146178
super.onCreate(savedInstanceState);
147179
setContentView(R.layout.activity_main);
148-
149-
180+
150181
// Create tabs
151182
ActionBar bar = getActionBar();
152183
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
@@ -207,8 +238,8 @@ protected void onStart() {
207238
lbManager.registerReceiver(mHourlyForecastReceiver, new IntentFilter("com.sdl.mobileweather.HourlyForecast"));
208239

209240
// Ask for permissions
210-
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
211-
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
241+
if (!checkPermissions()) {
242+
requestPermissions();
212243
} else {
213244
startServices();
214245
}
@@ -367,10 +398,10 @@ public void onSaveInstanceState(Bundle outState) {
367398
}
368399

369400
@Override
370-
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
371-
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
372-
if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED){
373-
Toast.makeText(this, "The app cannot run without this permission!", Toast.LENGTH_SHORT).show();
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();
374405
finish();
375406
} else {
376407
startServices();

MobileWeather/app/src/main/java/com/sdl/mobileweather/smartdevicelink/SdlReceiver.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sdl.mobileweather.smartdevicelink;
22

3+
import android.app.PendingIntent;
34
import android.bluetooth.BluetoothDevice;
45
import android.content.Context;
56
import android.content.Intent;
@@ -8,6 +9,7 @@
89

910
import com.smartdevicelink.transport.SdlBroadcastReceiver;
1011
import com.smartdevicelink.transport.SdlRouterService;
12+
import com.smartdevicelink.transport.TransportConstants;
1113

1214
public class SdlReceiver extends SdlBroadcastReceiver {
1315

@@ -44,14 +46,25 @@ public void onSdlEnabled(Context context, Intent intent) {
4446
//Use the provided intent but set the class to the SdlService
4547
intent.setClass(context, SdlService.class);
4648

47-
48-
// SdlService needs to be foregrounded in Android O and above
49-
// This will prevent apps in the background from crashing when they try to start SdlService
50-
// Because Android O doesn't allow background apps to start background services
51-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
52-
context.startForegroundService(intent);
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
50+
if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) {
51+
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);
52+
try {
53+
//Here we are allowing the RouterService that is in the Foreground to start the SdlService on our behalf
54+
pendingIntent.send(context, 0, intent);
55+
} catch (PendingIntent.CanceledException e) {
56+
e.printStackTrace();
57+
}
58+
}
5359
} else {
54-
context.startService(intent);
60+
// SdlService needs to be foregrounded in Android O and above
61+
// This will prevent apps in the background from crashing when they try to start SdlService
62+
// Because Android O doesn't allow background apps to start background services
63+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
64+
context.startForegroundService(intent);
65+
} else {
66+
context.startService(intent);
67+
}
5568
}
5669
}
5770

MobileWeather/app/src/main/java/com/sdl/mobileweather/smartdevicelink/SdlService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.smartdevicelink.util.DebugTool;
7777
import com.smartdevicelink.util.SystemInfo;
7878

79+
import com.smartdevicelink.util.SystemInfo;
7980
import java.net.URL;
8081
import java.text.SimpleDateFormat;
8182
import java.util.ArrayList;

MobileWeather/app/src/main/java/com/sdl/mobileweather/weather/WeatherAlarmManager.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sdl.mobileweather.weather;
22

3+
import android.os.Build;
34
import java.util.Calendar;
45

56
import android.app.AlarmManager;
@@ -148,10 +149,15 @@ private void restartAlarm(Context context) {
148149
if (mAlarmManager != null && mAlarmIntent != null) {
149150
mAlarmManager.cancel(mAlarmIntent);
150151
}
151-
152-
mAlarmIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_ID, mUpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
153-
154-
Calendar cal = Calendar.getInstance();
152+
153+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
154+
mAlarmIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_ID, mUpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
155+
}
156+
else {
157+
mAlarmIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_ID, mUpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
158+
}
159+
160+
Calendar cal = Calendar.getInstance();
155161
cal.setTimeInMillis(System.currentTimeMillis());
156162
cal.add(Calendar.MINUTE, mUpdateInterval);
157163
Log.d(SdlApplication.TAG, "restartAlarm mUpdateInterval = " + mUpdateInterval);

0 commit comments

Comments
 (0)