diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-10-02 16:03:00 -0400 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-10-02 16:03:00 -0400 |
commit | 616145a0393b2b96336b253068d03a1e40128ce8 (patch) | |
tree | ad517b87dfb51457964ab2982cfa7bcf3e9aa4b9 | |
parent | 3f91db4adcfc285b8c06cab0d1f81887fa1d25a4 (diff) | |
download | sdl_android-616145a0393b2b96336b253068d03a1e40128ce8.tar.gz |
Reformat hello sdl android code
9 files changed, 467 insertions, 464 deletions
diff --git a/android/hello_sdl_android/build.gradle b/android/hello_sdl_android/build.gradle index f990b38e6..6270e028d 100755 --- a/android/hello_sdl_android/build.gradle +++ b/android/hello_sdl_android/build.gradle @@ -17,7 +17,7 @@ android { } } flavorDimensions "default" - productFlavors{ + productFlavors { multi_sec_high { buildConfigField 'String', 'TRANSPORT', '"MULTI"' buildConfigField 'String', 'SECURITY', '"HIGH"' diff --git a/android/hello_sdl_android/src/main/AndroidManifest.xml b/android/hello_sdl_android/src/main/AndroidManifest.xml index 214a04355..369ec58b0 100755 --- a/android/hello_sdl_android/src/main/AndroidManifest.xml +++ b/android/hello_sdl_android/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools" - package="com.sdl.hellosdlandroid"> + xmlns:tools="http://schemas.android.com/tools" + package="com.sdl.hellosdlandroid"> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.INTERNET" /> @@ -20,7 +20,7 @@ tools:ignore="DeepLinks"> <activity android:name=".MainActivity" - android:label="@string/app_name" > + android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> @@ -28,8 +28,9 @@ </intent-filter> </activity> - <activity android:name="com.smartdevicelink.transport.USBAccessoryAttachmentActivity" - android:launchMode="singleTop"> + <activity + android:name="com.smartdevicelink.transport.USBAccessoryAttachmentActivity" + android:launchMode="singleTop"> <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> </intent-filter> @@ -41,31 +42,34 @@ <service android:name="com.sdl.hellosdlandroid.SdlService" - android:foregroundServiceType="connectedDevice"> - </service> + android:foregroundServiceType="connectedDevice"></service> <service android:name=".SdlRouterService" android:exported="true" android:process="com.smartdevicelink.router" android:foregroundServiceType="connectedDevice"> <intent-filter> - <action android:name="com.smartdevicelink.router.service"/> + <action android:name="com.smartdevicelink.router.service" /> </intent-filter> - <meta-data android:name="@string/sdl_router_service_version_name" android:value="@integer/sdl_router_service_version_value" /> + <meta-data + android:name="@string/sdl_router_service_version_name" + android:value="@integer/sdl_router_service_version_value" /> </service> + <receiver android:name=".SdlReceiver" android:enabled="true" android:exported="true" tools:ignore="ExportedReceiver"> <intent-filter> - <action android:name="com.smartdevicelink.USB_ACCESSORY_ATTACHED"/> <!--For AOA --> + <action android:name="com.smartdevicelink.USB_ACCESSORY_ATTACHED" /> <!--For AOA --> <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> <action android:name="sdl.router.startservice" /> </intent-filter> </receiver> - <activity android:name="com.smartdevicelink.managers.lockscreen.SDLLockScreenActivity" - android:launchMode="singleTop"/> + <activity + android:name="com.smartdevicelink.managers.lockscreen.SDLLockScreenActivity" + android:launchMode="singleTop" /> </application> </manifest>
\ No newline at end of file diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java index 69a86129a..43ad79ac7 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java @@ -9,35 +9,35 @@ import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - //If we are connected to a module we want to start our SdlService - if(BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) { - SdlReceiver.queryForConnectedService(this); - }else if(BuildConfig.TRANSPORT.equals("TCP")) { - Intent proxyIntent = new Intent(this, SdlService.class); - startService(proxyIntent); - } - } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + //If we are connected to a module we want to start our SdlService + if (BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) { + SdlReceiver.queryForConnectedService(this); + } else if (BuildConfig.TRANSPORT.equals("TCP")) { + Intent proxyIntent = new Intent(this, SdlService.class); + startService(proxyIntent); + } + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.main, menu); - return true; - } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.main, menu); + return true; + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - if (id == R.id.action_settings) { - return true; - } - return super.onOptionsItemSelected(item); - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + if (id == R.id.action_settings) { + return true; + } + return super.onOptionsItemSelected(item); + } } diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java index a63258a49..32c0ab1d5 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java @@ -8,31 +8,31 @@ import com.smartdevicelink.transport.SdlBroadcastReceiver; import com.smartdevicelink.transport.SdlRouterService; import com.smartdevicelink.util.DebugTool; -public class SdlReceiver extends SdlBroadcastReceiver { - private static final String TAG = "SdlBroadcastReceiver"; +public class SdlReceiver extends SdlBroadcastReceiver { + private static final String TAG = "SdlBroadcastReceiver"; - @Override - public void onSdlEnabled(Context context, Intent intent) { - DebugTool.logInfo(TAG, "SDL Enabled"); - intent.setClass(context, SdlService.class); + @Override + public void onSdlEnabled(Context context, Intent intent) { + DebugTool.logInfo(TAG, "SDL Enabled"); + intent.setClass(context, SdlService.class); - // SdlService needs to be foregrounded in Android O and above - // This will prevent apps in the background from crashing when they try to start SdlService - // Because Android O doesn't allow background apps to start background services - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent); - } else { - context.startService(intent); - } - } + // SdlService needs to be foregrounded in Android O and above + // This will prevent apps in the background from crashing when they try to start SdlService + // Because Android O doesn't allow background apps to start background services + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(intent); + } else { + context.startService(intent); + } + } - @Override - public Class<? extends SdlRouterService> defineLocalSdlRouterClass() { - return com.sdl.hellosdlandroid.SdlRouterService.class; - } + @Override + public Class<? extends SdlRouterService> defineLocalSdlRouterClass() { + return com.sdl.hellosdlandroid.SdlRouterService.class; + } - @Override - public void onReceive(Context context, Intent intent) { - super.onReceive(context, intent); // Required if overriding this method - } + @Override + public void onReceive(Context context, Intent intent) { + super.onReceive(context, intent); // Required if overriding this method + } }
\ No newline at end of file diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlRouterService.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlRouterService.java index b4bec8cde..b04939034 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlRouterService.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlRouterService.java @@ -1,8 +1,7 @@ package com.sdl.hellosdlandroid; -public class SdlRouterService extends com.smartdevicelink.transport.SdlRouterService { - +public class SdlRouterService extends com.smartdevicelink.transport.SdlRouterService { } diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java index 9a7ef2eeb..0b83e0842 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java @@ -56,397 +56,397 @@ import java.util.Vector; public class SdlService extends Service { - private static final String TAG = "SDL Service"; - - private static final String APP_NAME = "Hello Sdl"; - private static final String APP_NAME_ES = "Hola Sdl"; - private static final String APP_NAME_FR = "Bonjour Sdl"; - private static final String APP_ID = "8678309"; - - private static final String ICON_FILENAME = "hello_sdl_icon.png"; - private static final String SDL_IMAGE_FILENAME = "sdl_full_image.png"; - - private static final String WELCOME_SHOW = "Welcome to HelloSDL"; - private static final String WELCOME_SPEAK = "Welcome to Hello S D L"; - - private static final String TEST_COMMAND_NAME = "Test Command"; - - private static final int FOREGROUND_SERVICE_ID = 111; - - // TCP/IP transport config - // The default port is 12345 - // The IP is of the machine that is running SDL Core - private static final int TCP_PORT = 12247; - private static final String DEV_MACHINE_IP_ADDRESS = "m.sdl.tools"; - - // variable to create and call functions of the SyncProxy - private SdlManager sdlManager = null; - private List<ChoiceCell> choiceCellList; - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public void onCreate() { - Log.d(TAG, "onCreate"); - super.onCreate(); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - enterForeground(); - } - } - - // Helper method to let the service enter foreground mode - @SuppressLint("NewApi") - public void enterForeground() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel(APP_ID, "SdlService", NotificationManager.IMPORTANCE_DEFAULT); - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if (notificationManager != null) { - notificationManager.createNotificationChannel(channel); - Notification serviceNotification = new Notification.Builder(this, channel.getId()) - .setContentTitle("Connected through SDL") - .setSmallIcon(R.drawable.ic_sdl) - .build(); - startForeground(FOREGROUND_SERVICE_ID, serviceNotification); - } - } - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - startProxy(); - return START_STICKY; - } - - @Override - public void onDestroy() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - stopForeground(true); - } - - if (sdlManager != null) { - sdlManager.dispose(); - } - - super.onDestroy(); - } - - private void startProxy() { - // This logic is to select the correct transport and security levels defined in the selected build flavor - // Build flavors are selected by the "build variants" tab typically located in the bottom left of Android Studio - // Typically in your app, you will only set one of these. - if (sdlManager == null) { - Log.i(TAG, "Starting SDL Proxy"); - // Enable DebugTool for debug build type - if (BuildConfig.DEBUG){ - DebugTool.enableDebugTool(); - } - BaseTransportConfig transport = null; - if (BuildConfig.TRANSPORT.equals("MULTI")) { - int securityLevel; - if (BuildConfig.SECURITY.equals("HIGH")) { - securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH; - } else if (BuildConfig.SECURITY.equals("MED")) { - securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_MED; - } else if (BuildConfig.SECURITY.equals("LOW")) { - securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_LOW; - } else { - securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF; - } - transport = new MultiplexTransportConfig(this, APP_ID, securityLevel); - } else if (BuildConfig.TRANSPORT.equals("TCP")) { - transport = new TCPTransportConfig(TCP_PORT, DEV_MACHINE_IP_ADDRESS, true); - } else if (BuildConfig.TRANSPORT.equals("MULTI_HB")) { - MultiplexTransportConfig mtc = new MultiplexTransportConfig(this, APP_ID, MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF); - mtc.setRequiresHighBandwidth(true); - transport = mtc; - } - - // The app type to be used - Vector<AppHMIType> appType = new Vector<>(); - appType.add(AppHMIType.DEFAULT); - - // The manager listener helps you know when certain events that pertain to the SDL Manager happen - // Here we will listen for ON_HMI_STATUS and ON_COMMAND notifications - SdlManagerListener listener = new SdlManagerListener() { - @Override - public void onStart() { - // HMI Status Listener - sdlManager.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, new OnRPCNotificationListener() { - @Override - public void onNotified(RPCNotification notification) { - OnHMIStatus onHMIStatus = (OnHMIStatus)notification; - if (onHMIStatus.getWindowID() != null && onHMIStatus.getWindowID() != PredefinedWindows.DEFAULT_WINDOW.getValue()) { - return; - } - if (onHMIStatus.getHmiLevel() == HMILevel.HMI_FULL && onHMIStatus.getFirstRun()) { - setVoiceCommands(); - sendMenus(); - performWelcomeSpeak(); - performWelcomeShow(); - preloadChoices(); - subscribeToButtons(); - } - } - }); - } - - @Override - public void onDestroy() { - SdlService.this.stopSelf(); - } - - @Override - public void onError(String info, Exception e) { - } - - @Override - public LifecycleConfigurationUpdate managerShouldUpdateLifecycle(Language language, Language hmiLanguage) { - boolean isNeedUpdate = false; - String appName = APP_NAME; - String ttsName = APP_NAME; - switch (language) { - case ES_MX: - isNeedUpdate = true; - ttsName = APP_NAME_ES; - break; - case FR_CA: - isNeedUpdate = true; - ttsName = APP_NAME_FR; - break; - default: - break; - } - switch (hmiLanguage) { - case ES_MX: - isNeedUpdate = true; - appName = APP_NAME_ES; - break; - case FR_CA: - isNeedUpdate = true; - appName = APP_NAME_FR; - break; - default: - break; - } - if (isNeedUpdate) { - Vector<TTSChunk> chunks = new Vector<>(Collections.singletonList(new TTSChunk(ttsName, SpeechCapabilities.TEXT))); - return new LifecycleConfigurationUpdate(appName, null, chunks, null); - } else { - return null; - } - } - }; - - // Create App Icon, this is set in the SdlManager builder - SdlArtwork appIcon = new SdlArtwork(ICON_FILENAME, FileType.GRAPHIC_PNG, R.mipmap.ic_launcher, true); - - // The manager builder sets options for your session - SdlManager.Builder builder = new SdlManager.Builder(this, APP_ID, APP_NAME, listener); - builder.setAppTypes(appType); - builder.setTransportType(transport); - builder.setAppIcon(appIcon); - sdlManager = builder.build(); - sdlManager.start(); - } - } - - /** - * Send some voice commands - */ - private void setVoiceCommands(){ - - List<String> list1 = Collections.singletonList("Command One"); - List<String> list2 = Collections.singletonList("Command two"); - - VoiceCommand voiceCommand1 = new VoiceCommand(list1, new VoiceCommandSelectionListener() { - @Override - public void onVoiceCommandSelected() { - Log.i(TAG, "Voice Command 1 triggered"); - } - }); - - VoiceCommand voiceCommand2 = new VoiceCommand(list2, new VoiceCommandSelectionListener() { - @Override - public void onVoiceCommandSelected() { - Log.i(TAG, "Voice Command 2 triggered"); - } - }); - - sdlManager.getScreenManager().setVoiceCommands(Arrays.asList(voiceCommand1,voiceCommand2)); - } - - /** - * Add menus for the app on SDL. - */ - private void sendMenus(){ - - // some arts - SdlArtwork livio = new SdlArtwork("livio", FileType.GRAPHIC_PNG, R.drawable.sdl, false); - - // some voice commands - List<String> voice2 = Collections.singletonList("Cell two"); - - MenuCell mainCell1 = new MenuCell("Test Cell 1 (speak)", livio, null, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - Log.i(TAG, "Test cell 1 triggered. Source: "+ trigger.toString()); - showTest(); - } - }); - - MenuCell mainCell2 = new MenuCell("Test Cell 2", null, voice2, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - Log.i(TAG, "Test cell 2 triggered. Source: "+ trigger.toString()); - } - }); - - // SUB MENU - - MenuCell subCell1 = new MenuCell("SubCell 1",null, null, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - Log.i(TAG, "Sub cell 1 triggered. Source: "+ trigger.toString()); - } - }); - - MenuCell subCell2 = new MenuCell("SubCell 2",null, null, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - Log.i(TAG, "Sub cell 2 triggered. Source: "+ trigger.toString()); - } - }); - - // sub menu parent cell - MenuCell mainCell3 = new MenuCell("Test Cell 3 (sub menu)", MenuLayout.LIST, null, Arrays.asList(subCell1,subCell2)); - - MenuCell mainCell4 = new MenuCell("Show Perform Interaction", null, null, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - showPerformInteraction(); - } - }); - - MenuCell mainCell5 = new MenuCell("Clear the menu",null, null, new MenuSelectionListener() { - @Override - public void onTriggered(TriggerSource trigger) { - Log.i(TAG, "Clearing Menu. Source: "+ trigger.toString()); - // Clear this thing - sdlManager.getScreenManager().setMenu(Collections.<MenuCell>emptyList()); - showAlert("Menu Cleared"); - } - }); - - // Send the entire menu off to be created - sdlManager.getScreenManager().setMenu(Arrays.asList(mainCell1, mainCell2, mainCell3, mainCell4, mainCell5)); - } - - /** - * Will speak a sample welcome message - */ - private void performWelcomeSpeak(){ - List<TTSChunk> chunks = Collections.singletonList(new TTSChunk(WELCOME_SPEAK, SpeechCapabilities.TEXT)); - sdlManager.sendRPC(new Speak(chunks)); - } - - /** - * Use the Screen Manager to set the initial screen text and set the image. - * Because we are setting multiple items, we will call beginTransaction() first, - * and finish with commit() when we are done. - */ - private void performWelcomeShow() { - sdlManager.getScreenManager().beginTransaction(); - sdlManager.getScreenManager().setTextField1(APP_NAME); - sdlManager.getScreenManager().setTextField2(WELCOME_SHOW); - sdlManager.getScreenManager().setPrimaryGraphic(new SdlArtwork(SDL_IMAGE_FILENAME, FileType.GRAPHIC_PNG, R.drawable.sdl, true)); - sdlManager.getScreenManager().commit(new CompletionListener() { - @Override - public void onComplete(boolean success) { - if (success){ - Log.i(TAG, "welcome show successful"); - } - } - }); - } - - /** - * Attempts to Subscribe to all preset buttons - */ - private void subscribeToButtons() { - ButtonName[] buttonNames = {ButtonName.PLAY_PAUSE, ButtonName.SEEKLEFT, ButtonName.SEEKRIGHT, ButtonName.AC_MAX, ButtonName.AC, ButtonName.RECIRCULATE, - ButtonName.FAN_UP, ButtonName.FAN_DOWN, ButtonName.TEMP_UP, ButtonName.TEMP_DOWN, ButtonName.FAN_DOWN, ButtonName.DEFROST_MAX, ButtonName.DEFROST_REAR, ButtonName.DEFROST, - ButtonName.UPPER_VENT, ButtonName.LOWER_VENT, ButtonName.VOLUME_UP, ButtonName.VOLUME_DOWN, ButtonName.EJECT, ButtonName.SOURCE, ButtonName.SHUFFLE, ButtonName.REPEAT}; - - OnButtonListener onButtonListener = new OnButtonListener() { - @Override - public void onPress(ButtonName buttonName, OnButtonPress buttonPress) { - sdlManager.getScreenManager().setTextField1(buttonName + " pressed"); - } - - @Override - public void onEvent(ButtonName buttonName, OnButtonEvent buttonEvent) { - sdlManager.getScreenManager().setTextField2(buttonName + " " + buttonEvent.getButtonEventMode()); - } - - @Override - public void onError(String info) { - Log.i(TAG, "onError: " + info); - } - }; - - for (ButtonName buttonName : buttonNames) { - sdlManager.getScreenManager().addButtonListener(buttonName, onButtonListener); - } - } - - /** - * Will show a sample test message on screen as well as speak a sample test message - */ - private void showTest(){ - sdlManager.getScreenManager().beginTransaction(); - sdlManager.getScreenManager().setTextField1("Test Cell 1 has been selected"); - sdlManager.getScreenManager().setTextField2(""); - sdlManager.getScreenManager().commit(null); - - List<TTSChunk> chunks = Collections.singletonList(new TTSChunk(TEST_COMMAND_NAME, SpeechCapabilities.TEXT)); - sdlManager.sendRPC(new Speak(chunks)); - } - - private void showAlert(String text){ - Alert alert = new Alert(); - alert.setAlertText1(text); - alert.setDuration(5000); - sdlManager.sendRPC(alert); - } - - // Choice Set - - private void preloadChoices(){ - ChoiceCell cell1 = new ChoiceCell("Item 1"); - ChoiceCell cell2 = new ChoiceCell("Item 2"); - ChoiceCell cell3 = new ChoiceCell("Item 3"); - choiceCellList = new ArrayList<>(Arrays.asList(cell1,cell2,cell3)); - sdlManager.getScreenManager().preloadChoices(choiceCellList, null); - } - - private void showPerformInteraction(){ - if (choiceCellList != null) { - ChoiceSet choiceSet = new ChoiceSet("Choose an Item from the list", choiceCellList, new ChoiceSetSelectionListener() { - @Override - public void onChoiceSelected(ChoiceCell choiceCell, TriggerSource triggerSource, int rowIndex) { - showAlert(choiceCell.getText() + " was selected"); - } - - @Override - public void onError(String error) { - Log.e(TAG, "There was an error showing the perform interaction: "+ error); - } - }); - sdlManager.getScreenManager().presentChoiceSet(choiceSet, InteractionMode.MANUAL_ONLY); - } - } + private static final String TAG = "SDL Service"; + + private static final String APP_NAME = "Hello Sdl"; + private static final String APP_NAME_ES = "Hola Sdl"; + private static final String APP_NAME_FR = "Bonjour Sdl"; + private static final String APP_ID = "8678309"; + + private static final String ICON_FILENAME = "hello_sdl_icon.png"; + private static final String SDL_IMAGE_FILENAME = "sdl_full_image.png"; + + private static final String WELCOME_SHOW = "Welcome to HelloSDL"; + private static final String WELCOME_SPEAK = "Welcome to Hello S D L"; + + private static final String TEST_COMMAND_NAME = "Test Command"; + + private static final int FOREGROUND_SERVICE_ID = 111; + + // TCP/IP transport config + // The default port is 12345 + // The IP is of the machine that is running SDL Core + private static final int TCP_PORT = 12247; + private static final String DEV_MACHINE_IP_ADDRESS = "m.sdl.tools"; + + // variable to create and call functions of the SyncProxy + private SdlManager sdlManager = null; + private List<ChoiceCell> choiceCellList; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + Log.d(TAG, "onCreate"); + super.onCreate(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + enterForeground(); + } + } + + // Helper method to let the service enter foreground mode + @SuppressLint("NewApi") + public void enterForeground() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(APP_ID, "SdlService", NotificationManager.IMPORTANCE_DEFAULT); + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) { + notificationManager.createNotificationChannel(channel); + Notification serviceNotification = new Notification.Builder(this, channel.getId()) + .setContentTitle("Connected through SDL") + .setSmallIcon(R.drawable.ic_sdl) + .build(); + startForeground(FOREGROUND_SERVICE_ID, serviceNotification); + } + } + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + startProxy(); + return START_STICKY; + } + + @Override + public void onDestroy() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + stopForeground(true); + } + + if (sdlManager != null) { + sdlManager.dispose(); + } + + super.onDestroy(); + } + + private void startProxy() { + // This logic is to select the correct transport and security levels defined in the selected build flavor + // Build flavors are selected by the "build variants" tab typically located in the bottom left of Android Studio + // Typically in your app, you will only set one of these. + if (sdlManager == null) { + Log.i(TAG, "Starting SDL Proxy"); + // Enable DebugTool for debug build type + if (BuildConfig.DEBUG) { + DebugTool.enableDebugTool(); + } + BaseTransportConfig transport = null; + if (BuildConfig.TRANSPORT.equals("MULTI")) { + int securityLevel; + if (BuildConfig.SECURITY.equals("HIGH")) { + securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH; + } else if (BuildConfig.SECURITY.equals("MED")) { + securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_MED; + } else if (BuildConfig.SECURITY.equals("LOW")) { + securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_LOW; + } else { + securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF; + } + transport = new MultiplexTransportConfig(this, APP_ID, securityLevel); + } else if (BuildConfig.TRANSPORT.equals("TCP")) { + transport = new TCPTransportConfig(TCP_PORT, DEV_MACHINE_IP_ADDRESS, true); + } else if (BuildConfig.TRANSPORT.equals("MULTI_HB")) { + MultiplexTransportConfig mtc = new MultiplexTransportConfig(this, APP_ID, MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF); + mtc.setRequiresHighBandwidth(true); + transport = mtc; + } + + // The app type to be used + Vector<AppHMIType> appType = new Vector<>(); + appType.add(AppHMIType.DEFAULT); + + // The manager listener helps you know when certain events that pertain to the SDL Manager happen + // Here we will listen for ON_HMI_STATUS and ON_COMMAND notifications + SdlManagerListener listener = new SdlManagerListener() { + @Override + public void onStart() { + // HMI Status Listener + sdlManager.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, new OnRPCNotificationListener() { + @Override + public void onNotified(RPCNotification notification) { + OnHMIStatus onHMIStatus = (OnHMIStatus) notification; + if (onHMIStatus.getWindowID() != null && onHMIStatus.getWindowID() != PredefinedWindows.DEFAULT_WINDOW.getValue()) { + return; + } + if (onHMIStatus.getHmiLevel() == HMILevel.HMI_FULL && onHMIStatus.getFirstRun()) { + setVoiceCommands(); + sendMenus(); + performWelcomeSpeak(); + performWelcomeShow(); + preloadChoices(); + subscribeToButtons(); + } + } + }); + } + + @Override + public void onDestroy() { + SdlService.this.stopSelf(); + } + + @Override + public void onError(String info, Exception e) { + } + + @Override + public LifecycleConfigurationUpdate managerShouldUpdateLifecycle(Language language, Language hmiLanguage) { + boolean isNeedUpdate = false; + String appName = APP_NAME; + String ttsName = APP_NAME; + switch (language) { + case ES_MX: + isNeedUpdate = true; + ttsName = APP_NAME_ES; + break; + case FR_CA: + isNeedUpdate = true; + ttsName = APP_NAME_FR; + break; + default: + break; + } + switch (hmiLanguage) { + case ES_MX: + isNeedUpdate = true; + appName = APP_NAME_ES; + break; + case FR_CA: + isNeedUpdate = true; + appName = APP_NAME_FR; + break; + default: + break; + } + if (isNeedUpdate) { + Vector<TTSChunk> chunks = new Vector<>(Collections.singletonList(new TTSChunk(ttsName, SpeechCapabilities.TEXT))); + return new LifecycleConfigurationUpdate(appName, null, chunks, null); + } else { + return null; + } + } + }; + + // Create App Icon, this is set in the SdlManager builder + SdlArtwork appIcon = new SdlArtwork(ICON_FILENAME, FileType.GRAPHIC_PNG, R.mipmap.ic_launcher, true); + + // The manager builder sets options for your session + SdlManager.Builder builder = new SdlManager.Builder(this, APP_ID, APP_NAME, listener); + builder.setAppTypes(appType); + builder.setTransportType(transport); + builder.setAppIcon(appIcon); + sdlManager = builder.build(); + sdlManager.start(); + } + } + + /** + * Send some voice commands + */ + private void setVoiceCommands() { + + List<String> list1 = Collections.singletonList("Command One"); + List<String> list2 = Collections.singletonList("Command two"); + + VoiceCommand voiceCommand1 = new VoiceCommand(list1, new VoiceCommandSelectionListener() { + @Override + public void onVoiceCommandSelected() { + Log.i(TAG, "Voice Command 1 triggered"); + } + }); + + VoiceCommand voiceCommand2 = new VoiceCommand(list2, new VoiceCommandSelectionListener() { + @Override + public void onVoiceCommandSelected() { + Log.i(TAG, "Voice Command 2 triggered"); + } + }); + + sdlManager.getScreenManager().setVoiceCommands(Arrays.asList(voiceCommand1, voiceCommand2)); + } + + /** + * Add menus for the app on SDL. + */ + private void sendMenus() { + + // some arts + SdlArtwork livio = new SdlArtwork("livio", FileType.GRAPHIC_PNG, R.drawable.sdl, false); + + // some voice commands + List<String> voice2 = Collections.singletonList("Cell two"); + + MenuCell mainCell1 = new MenuCell("Test Cell 1 (speak)", livio, null, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + Log.i(TAG, "Test cell 1 triggered. Source: " + trigger.toString()); + showTest(); + } + }); + + MenuCell mainCell2 = new MenuCell("Test Cell 2", null, voice2, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + Log.i(TAG, "Test cell 2 triggered. Source: " + trigger.toString()); + } + }); + + // SUB MENU + + MenuCell subCell1 = new MenuCell("SubCell 1", null, null, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + Log.i(TAG, "Sub cell 1 triggered. Source: " + trigger.toString()); + } + }); + + MenuCell subCell2 = new MenuCell("SubCell 2", null, null, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + Log.i(TAG, "Sub cell 2 triggered. Source: " + trigger.toString()); + } + }); + + // sub menu parent cell + MenuCell mainCell3 = new MenuCell("Test Cell 3 (sub menu)", MenuLayout.LIST, null, Arrays.asList(subCell1, subCell2)); + + MenuCell mainCell4 = new MenuCell("Show Perform Interaction", null, null, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + showPerformInteraction(); + } + }); + + MenuCell mainCell5 = new MenuCell("Clear the menu", null, null, new MenuSelectionListener() { + @Override + public void onTriggered(TriggerSource trigger) { + Log.i(TAG, "Clearing Menu. Source: " + trigger.toString()); + // Clear this thing + sdlManager.getScreenManager().setMenu(Collections.<MenuCell>emptyList()); + showAlert("Menu Cleared"); + } + }); + + // Send the entire menu off to be created + sdlManager.getScreenManager().setMenu(Arrays.asList(mainCell1, mainCell2, mainCell3, mainCell4, mainCell5)); + } + + /** + * Will speak a sample welcome message + */ + private void performWelcomeSpeak() { + List<TTSChunk> chunks = Collections.singletonList(new TTSChunk(WELCOME_SPEAK, SpeechCapabilities.TEXT)); + sdlManager.sendRPC(new Speak(chunks)); + } + + /** + * Use the Screen Manager to set the initial screen text and set the image. + * Because we are setting multiple items, we will call beginTransaction() first, + * and finish with commit() when we are done. + */ + private void performWelcomeShow() { + sdlManager.getScreenManager().beginTransaction(); + sdlManager.getScreenManager().setTextField1(APP_NAME); + sdlManager.getScreenManager().setTextField2(WELCOME_SHOW); + sdlManager.getScreenManager().setPrimaryGraphic(new SdlArtwork(SDL_IMAGE_FILENAME, FileType.GRAPHIC_PNG, R.drawable.sdl, true)); + sdlManager.getScreenManager().commit(new CompletionListener() { + @Override + public void onComplete(boolean success) { + if (success) { + Log.i(TAG, "welcome show successful"); + } + } + }); + } + + /** + * Attempts to Subscribe to all preset buttons + */ + private void subscribeToButtons() { + ButtonName[] buttonNames = {ButtonName.PLAY_PAUSE, ButtonName.SEEKLEFT, ButtonName.SEEKRIGHT, ButtonName.AC_MAX, ButtonName.AC, ButtonName.RECIRCULATE, + ButtonName.FAN_UP, ButtonName.FAN_DOWN, ButtonName.TEMP_UP, ButtonName.TEMP_DOWN, ButtonName.FAN_DOWN, ButtonName.DEFROST_MAX, ButtonName.DEFROST_REAR, ButtonName.DEFROST, + ButtonName.UPPER_VENT, ButtonName.LOWER_VENT, ButtonName.VOLUME_UP, ButtonName.VOLUME_DOWN, ButtonName.EJECT, ButtonName.SOURCE, ButtonName.SHUFFLE, ButtonName.REPEAT}; + + OnButtonListener onButtonListener = new OnButtonListener() { + @Override + public void onPress(ButtonName buttonName, OnButtonPress buttonPress) { + sdlManager.getScreenManager().setTextField1(buttonName + " pressed"); + } + + @Override + public void onEvent(ButtonName buttonName, OnButtonEvent buttonEvent) { + sdlManager.getScreenManager().setTextField2(buttonName + " " + buttonEvent.getButtonEventMode()); + } + + @Override + public void onError(String info) { + Log.i(TAG, "onError: " + info); + } + }; + + for (ButtonName buttonName : buttonNames) { + sdlManager.getScreenManager().addButtonListener(buttonName, onButtonListener); + } + } + + /** + * Will show a sample test message on screen as well as speak a sample test message + */ + private void showTest() { + sdlManager.getScreenManager().beginTransaction(); + sdlManager.getScreenManager().setTextField1("Test Cell 1 has been selected"); + sdlManager.getScreenManager().setTextField2(""); + sdlManager.getScreenManager().commit(null); + + List<TTSChunk> chunks = Collections.singletonList(new TTSChunk(TEST_COMMAND_NAME, SpeechCapabilities.TEXT)); + sdlManager.sendRPC(new Speak(chunks)); + } + + private void showAlert(String text) { + Alert alert = new Alert(); + alert.setAlertText1(text); + alert.setDuration(5000); + sdlManager.sendRPC(alert); + } + + // Choice Set + + private void preloadChoices() { + ChoiceCell cell1 = new ChoiceCell("Item 1"); + ChoiceCell cell2 = new ChoiceCell("Item 2"); + ChoiceCell cell3 = new ChoiceCell("Item 3"); + choiceCellList = new ArrayList<>(Arrays.asList(cell1, cell2, cell3)); + sdlManager.getScreenManager().preloadChoices(choiceCellList, null); + } + + private void showPerformInteraction() { + if (choiceCellList != null) { + ChoiceSet choiceSet = new ChoiceSet("Choose an Item from the list", choiceCellList, new ChoiceSetSelectionListener() { + @Override + public void onChoiceSelected(ChoiceCell choiceCell, TriggerSource triggerSource, int rowIndex) { + showAlert(choiceCell.getText() + " was selected"); + } + + @Override + public void onError(String error) { + Log.e(TAG, "There was an error showing the perform interaction: " + error); + } + }); + sdlManager.getScreenManager().presentChoiceSet(choiceSet, InteractionMode.MANUAL_ONLY); + } + } } diff --git a/android/hello_sdl_android/src/main/res/layout/activity_main.xml b/android/hello_sdl_android/src/main/res/layout/activity_main.xml index 8da613491..b4ed2fb3d 100755 --- a/android/hello_sdl_android/src/main/res/layout/activity_main.xml +++ b/android/hello_sdl_android/src/main/res/layout/activity_main.xml @@ -6,7 +6,7 @@ android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" - tools:context="com.sdl.hellosdlandroid.MainActivity" > + tools:context="com.sdl.hellosdlandroid.MainActivity"> <TextView android:layout_width="wrap_content" diff --git a/android/hello_sdl_android/src/main/res/menu/main.xml b/android/hello_sdl_android/src/main/res/menu/main.xml index d74718db3..898fd7765 100755 --- a/android/hello_sdl_android/src/main/res/menu/main.xml +++ b/android/hello_sdl_android/src/main/res/menu/main.xml @@ -1,12 +1,12 @@ <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" - tools:context="com.sdl.hellosdlandroid.MainActivity" > + tools:context="com.sdl.hellosdlandroid.MainActivity"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" - app:showAsAction="never"/> + app:showAsAction="never" /> </menu> diff --git a/android/hello_sdl_android/src/main/res/xml/accessory_filter.xml b/android/hello_sdl_android/src/main/res/xml/accessory_filter.xml index 4b0ab7c83..6a6e1ea97 100755 --- a/android/hello_sdl_android/src/main/res/xml/accessory_filter.xml +++ b/android/hello_sdl_android/src/main/res/xml/accessory_filter.xml @@ -3,5 +3,5 @@ <usb-accessory manufacturer="SDL" model="Core" - version="1.0"/> + version="1.0" /> </resource>
\ No newline at end of file |