Skip to content

Commit 3fddfdf

Browse files
authored
Merge pull request #31 from SmartDeviceLink-Examples/feature/update_5.6
Feature/update 5.6
2 parents 09ae072 + c2a7257 commit 3fddfdf

File tree

5 files changed

+75
-41
lines changed

5 files changed

+75
-41
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 31
4+
compileSdkVersion 33
55
defaultConfig {
66
applicationId "com.sdl.mobileweather"
77
minSdkVersion 16
8-
targetSdkVersion 31
8+
targetSdkVersion 33
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.5.0'
40+
implementation 'com.smartdevicelink:sdl_android:5.6.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<uses-permission android:name="android.permission.BLUETOOTH" />
99
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
1010
tools:targetApi="31"/>
11+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
12+
tools:targetApi="33"/>
1113
<!-- Required for weather data -->
1214
<uses-permission android:name="android.permission.INTERNET" />
1315
<!-- Required to check if WiFi is enabled -->

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

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

3-
43
import android.Manifest;
54
import android.app.ActionBar;
65
import android.app.ActionBar.Tab;
@@ -14,6 +13,8 @@
1413
import android.content.res.Configuration;
1514
import android.os.Build;
1615
import android.os.Bundle;
16+
17+
import androidx.annotation.NonNull;
1718
import androidx.legacy.app.ActionBarDrawerToggle;
1819
import androidx.core.app.ActivityCompat;
1920
import androidx.core.content.ContextCompat;
@@ -35,14 +36,15 @@
3536
import com.sdl.mobileweather.fragments.ForecastFragment;
3637
import com.sdl.mobileweather.smartdevicelink.SdlActivity;
3738
import com.sdl.mobileweather.smartdevicelink.SdlApplication;
38-
import com.sdl.mobileweather.smartdevicelink.SdlReceiver;
39+
40+
import java.util.ArrayList;
3941

4042

4143
public 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
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import com.smartdevicelink.util.DebugTool;
8181
import com.smartdevicelink.util.SystemInfo;
8282

83-
8483
import com.smartdevicelink.util.SystemInfo;
8584
import java.net.URL;
8685
import java.text.SimpleDateFormat;
@@ -1029,18 +1028,7 @@ private void showWeatherConditions(boolean includeSpeak) {
10291028
getResources().getString(R.string.weather_conditions_speak),
10301029
title, temperature, humidity, windSpeed, speedUnitsFull);
10311030
}
1032-
chunk.setText(speakString);
1033-
chunk.setType(SpeechCapabilities.TEXT);
1034-
chunks.add(chunk);
1035-
final Speak speakRequest = new Speak();
1036-
speakRequest.setTtsChunks(chunks);
1037-
speakRequest.setCorrelationID(autoIncCorrId++);
1038-
sdlManager.getScreenManager().commit(new CompletionListener() {
1039-
@Override
1040-
public void onComplete(boolean b) {
1041-
sdlManager.sendRPC(speakRequest);
1042-
}
1043-
});
1031+
speak(speakString);
10441032
}
10451033
} else {
10461034
showNoConditionsAvail();

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
This tutorial is designed to show you how to include the SmartDeviceLink SDK in your app and to help you get your app’s features onto the vehicle’s infotainment head unit.
44

55
Please visit the [wiki](https://github.com/smartdevicelink/sdl_mobileweather_tutorial_android/wiki) of this repository to get the full tutorial.
6-
7-
### Weather API Key
8-
9-
1. Sign up and get your own [OpenWeather API Key](https://home.openweathermap.org/api_keys).
10-
2. Set the value of `API_KEY`in `OpenWeatherMapService.java` to your API key
6+
7+
### Weather API Key
8+
9+
1. Sign up and get your own [OpenWeather API Key](https://home.openweathermap.org/api_keys).
1110
2. Set the value of `API_KEY`in `OpenWeatherMapService.java` to your API key

0 commit comments

Comments
 (0)