Skip to content

Commit

Permalink
Merge pull request #31 from SmartDeviceLink-Examples/feature/update_5.6
Browse files Browse the repository at this point in the history
Feature/update 5.6
  • Loading branch information
JulianKast authored Oct 26, 2022
2 parents 09ae072 + c2a7257 commit 3fddfdf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 41 deletions.
6 changes: 3 additions & 3 deletions MobileWeather/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
compileSdkVersion 33
defaultConfig {
applicationId "com.sdl.mobileweather"
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode 27
versionName "1.7.15"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand Down Expand Up @@ -37,7 +37,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.smartdevicelink:sdl_android:5.5.0'
implementation 'com.smartdevicelink:sdl_android:5.6.0'
implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
}
2 changes: 2 additions & 0 deletions MobileWeather/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
tools:targetApi="31"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
tools:targetApi="33"/>
<!-- Required for weather data -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required to check if WiFi is enabled -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sdl.mobileweather.activity;


import android.Manifest;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
Expand All @@ -14,6 +13,8 @@
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.legacy.app.ActionBarDrawerToggle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
Expand All @@ -35,14 +36,15 @@
import com.sdl.mobileweather.fragments.ForecastFragment;
import com.sdl.mobileweather.smartdevicelink.SdlActivity;
import com.sdl.mobileweather.smartdevicelink.SdlApplication;
import com.sdl.mobileweather.smartdevicelink.SdlReceiver;

import java.util.ArrayList;


public class MainActivity extends SdlActivity implements ActionBar.TabListener {

private static final String SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
private static final String APP_ID = "bf2c3a7bad6b0c79152f50cc42ba1ace";
private static final int PERMISSIONS_REQUEST_CODE = 100;
private static final int PERMISSIONS_REQUEST_CODE = 200;

private Fragment mCurrentFragment;
private DrawerLayout mDrawerLayout;
Expand Down Expand Up @@ -137,14 +139,12 @@ else if ((getResources().getString(R.string.drawer_item_about)).equals(item)){
mDrawerList.setItemChecked(position, false);
mDrawerLayout.closeDrawer(mDrawerList);
}




private void checkForCrashes() {}

private void checkForUpdates() {}

private boolean checkPermissions() {
private boolean hasPermissions() {
boolean permissionsGranted;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand All @@ -159,17 +159,22 @@ private boolean checkPermissions() {
return permissionsGranted;
}

private void requestPermissions() {
String[] permissions;
private void requestPermission(String[] permissions, int REQUEST_CODE) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permissions = new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.ACCESS_FINE_LOCATION};
private @NonNull String[] permissionsNeeded() {
ArrayList<String> result = new ArrayList<>();
if (!hasBTPermission()) {
result.add(Manifest.permission.BLUETOOTH_CONNECT);
}
else {
permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
if (!hasPNPermission()) {
result.add(Manifest.permission.POST_NOTIFICATIONS);
}

ActivityCompat.requestPermissions(this, permissions, PERMISSIONS_REQUEST_CODE);
if (!hasLocation()) {
result.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
return (result.toArray(new String[result.size()]));
}

@Override
Expand Down Expand Up @@ -238,13 +243,41 @@ protected void onStart() {
lbManager.registerReceiver(mHourlyForecastReceiver, new IntentFilter("com.sdl.mobileweather.HourlyForecast"));

// Ask for permissions
if (!checkPermissions()) {
requestPermissions();
} else {
startServices();
String[] permissionsNeeded = permissionsNeeded();
if (permissionsNeeded.length > 0) {
requestPermission(permissionsNeeded, PERMISSIONS_REQUEST_CODE);
for (String permission : permissionsNeeded) {
if (Manifest.permission.BLUETOOTH_CONNECT.equals(permission)) {
// We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
return;
}
}
}
startServices();
}

/**
* Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
* @return false if we need to request BLUETOOTH_CONNECT permission
*/
private boolean hasBTPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? checkPermission(Manifest.permission.BLUETOOTH_CONNECT) : true;
}

/**
* Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
* @return false if we need to request POST_NOTIFICATIONS permission
*/
private boolean hasPNPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ? checkPermission(Manifest.permission.POST_NOTIFICATIONS) : true;
}

private boolean hasLocation() {
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
}

private boolean checkPermission(String permission) {
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), permission);
}

private void startServices() {
Expand Down Expand Up @@ -400,11 +433,23 @@ public void onSaveInstanceState(Bundle outState) {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_CODE) {
if (!checkPermissions()) {
if (!hasPermissions()) {
Toast.makeText(this, "The app cannot run without these permissions!", Toast.LENGTH_SHORT).show();
finish();
} else {
startServices();
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (permissions[i].equals(Manifest.permission.POST_NOTIFICATIONS)) {
boolean postNotificationGranted =
grantResults[i] == PackageManager.PERMISSION_GRANTED;
if (!postNotificationGranted) {
// User denied permission, Notifications for SDL will not appear
// on Android 13 devices.
}
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
import com.smartdevicelink.util.DebugTool;
import com.smartdevicelink.util.SystemInfo;


import com.smartdevicelink.util.SystemInfo;
import java.net.URL;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -1029,18 +1028,7 @@ private void showWeatherConditions(boolean includeSpeak) {
getResources().getString(R.string.weather_conditions_speak),
title, temperature, humidity, windSpeed, speedUnitsFull);
}
chunk.setText(speakString);
chunk.setType(SpeechCapabilities.TEXT);
chunks.add(chunk);
final Speak speakRequest = new Speak();
speakRequest.setTtsChunks(chunks);
speakRequest.setCorrelationID(autoIncCorrId++);
sdlManager.getScreenManager().commit(new CompletionListener() {
@Override
public void onComplete(boolean b) {
sdlManager.sendRPC(speakRequest);
}
});
speak(speakString);
}
} else {
showNoConditionsAvail();
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
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.

Please visit the [wiki](https://github.com/smartdevicelink/sdl_mobileweather_tutorial_android/wiki) of this repository to get the full tutorial.

### Weather API Key

1. Sign up and get your own [OpenWeather API Key](https://home.openweathermap.org/api_keys).
2. Set the value of `API_KEY`in `OpenWeatherMapService.java` to your API key

### Weather API Key

1. Sign up and get your own [OpenWeather API Key](https://home.openweathermap.org/api_keys).
2. Set the value of `API_KEY`in `OpenWeatherMapService.java` to your API key

0 comments on commit 3fddfdf

Please sign in to comment.