diff options
author | BrettyWhite <geekman3454@protonmail.com> | 2018-10-02 15:14:56 -0400 |
---|---|---|
committer | BrettyWhite <geekman3454@protonmail.com> | 2018-10-02 15:14:56 -0400 |
commit | 0402ccf362387e6810518aa1ed40ddf5fcc5c214 (patch) | |
tree | 9a6028c9299e571678d3bc0ae1a46bf5605596cc /hello_sdl_android | |
parent | c6083a1ae1b445f680e029e4be67ac50f0df8117 (diff) | |
download | sdl_android-0402ccf362387e6810518aa1ed40ddf5fcc5c214.tar.gz |
update to use managers
Diffstat (limited to 'hello_sdl_android')
4 files changed, 146 insertions, 895 deletions
diff --git a/hello_sdl_android/src/main/AndroidManifest.xml b/hello_sdl_android/src/main/AndroidManifest.xml index b455304dc..64fcfe76a 100755 --- a/hello_sdl_android/src/main/AndroidManifest.xml +++ b/hello_sdl_android/src/main/AndroidManifest.xml @@ -28,9 +28,6 @@ </intent-filter> </activity> - <activity - android:name=".LockScreenActivity"/> - <activity android:name="com.smartdevicelink.transport.USBAccessoryAttachmentActivity" android:launchMode="singleTop"> <intent-filter> @@ -64,6 +61,8 @@ <action android:name="sdl.router.startservice" /> </intent-filter> </receiver> + <activity android:name="com.smartdevicelink.managers.lockscreen.SDLLockScreenActivity" + android:launchMode="singleInstance"/> </application> </manifest>
\ No newline at end of file diff --git a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/LockScreenActivity.java b/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/LockScreenActivity.java deleted file mode 100755 index 16f35ffc0..000000000 --- a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/LockScreenActivity.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.sdl.hellosdlandroid; - -import android.app.Activity; -import android.app.Application; -import android.app.Application.ActivityLifecycleCallbacks; -import android.content.Intent; -import android.graphics.Bitmap; -import android.os.Build; -import android.os.Bundle; -import android.widget.ImageView; - -import com.smartdevicelink.proxy.rpc.enums.LockScreenStatus; - -public class LockScreenActivity extends Activity { - // This will be set to true if there is any activity running - // onResume will set this variable to true - // onPause will set this variable to false - // As a fallback to old API levels this will be set to true forever - private static boolean ACTIVITY_RUNNING; - // This will hold the activity instance of the lock screen if created - // onCreate will set this variable to the current lock screen instance - // onDestroy will set this variable to null - private static Activity LOCKSCREEN_INSTANCE; - // This will hold the current lock screen status - private static LockScreenStatus LOCKSCREEN_STATUS; - // This will ensure that the lifecycle is registered only once - private static boolean ACTIVITY_LIFECYCLE_REGISTERED; - // This will hold the lifecycle callback object - private static ActivityLifecycleCallbacks ACTIVITY_LIFECYCLE_CALLBACK; - // This will hold the instance of the application object - private static Application APPLICATION; - // This will hold the bitmap to update the lockscreen image - static Bitmap lockscreenIcon = null; - - - static { - ACTIVITY_RUNNING = false; - LOCKSCREEN_INSTANCE = null; - LOCKSCREEN_STATUS = LockScreenStatus.OFF; - - ACTIVITY_LIFECYCLE_REGISTERED = false; - } - - public static void registerActivityLifecycle(Application application) { - // register only once - if (ACTIVITY_LIFECYCLE_REGISTERED == false) { - ACTIVITY_LIFECYCLE_REGISTERED = true; - - // check if API level is >= 14 - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - // create the callback - ACTIVITY_LIFECYCLE_CALLBACK = new ActivityLifecycleCallbacks() { - @Override - public void onActivityResumed(Activity activity) { - ACTIVITY_RUNNING = true; - // recall this method so the lock screen comes up when necessary - updateLockScreenStatus(LOCKSCREEN_STATUS); - - ImageView lockscreenIV = (ImageView) activity.findViewById(R.id.lockscreen); - if(lockscreenIcon != null && lockscreenIV != null) { - lockscreenIV.setImageBitmap(lockscreenIcon); - lockscreenIcon = null; - } - } - - @Override - public void onActivityPaused(Activity activity) { - ACTIVITY_RUNNING = false; - } - - @Override - public void onActivityCreated(Activity activity, Bundle savedInstanceState) { - - } - - @Override - public void onActivityStarted(Activity activity) { - } - - @Override - public void onActivityStopped(Activity activity) { - } - - @Override - public void onActivitySaveInstanceState(Activity activity, Bundle outState) { - } - - @Override - public void onActivityDestroyed(Activity activity) { - } - }; - - APPLICATION = application; - - // do the activity registration - application.registerActivityLifecycleCallbacks(ACTIVITY_LIFECYCLE_CALLBACK); - } else { - // fallback and assume we always have an activity - ACTIVITY_RUNNING = true; - } - } - } - - public static void updateLockScreenStatus(LockScreenStatus status) { - LOCKSCREEN_STATUS = status; - - if (status.equals(LockScreenStatus.OFF)) { - // do we have a lock screen? if so we need to remove it - if (LOCKSCREEN_INSTANCE != null) { - LOCKSCREEN_INSTANCE.finish(); - } - } else { - // do we miss a lock screen and app is in foreground somehow? if so we need to lock it - if (LOCKSCREEN_INSTANCE == null && ACTIVITY_RUNNING == true) { - Intent intent = new Intent(APPLICATION, LockScreenActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); - intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION); - - APPLICATION.startActivity(intent); - } - } - } - - public static void updateLockScreenImage(Bitmap icon){ - lockscreenIcon = icon; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_lock_screen); - - LOCKSCREEN_INSTANCE = this; - - // redo the checkup - updateLockScreenStatus(LOCKSCREEN_STATUS); - } - - @Override - protected void onDestroy() { - LOCKSCREEN_INSTANCE = null; - - super.onDestroy(); - } - - @Override - public void onBackPressed() { - - } -} diff --git a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlApplication.java b/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlApplication.java index b5f39ec20..018009d58 100755 --- a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlApplication.java +++ b/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlApplication.java @@ -9,8 +9,6 @@ public class SdlApplication extends Application{ @Override public void onCreate() { super.onCreate(); - - LockScreenActivity.registerActivityLifecycle(this); } } diff --git a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java b/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java index 2d615b67b..9c4ef299f 100755 --- a/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java +++ b/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java @@ -4,8 +4,6 @@ import android.annotation.SuppressLint; import android.app.Notification; import android.app.Service; import android.content.Intent; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; import android.os.Build; @@ -13,127 +11,50 @@ import android.os.IBinder; import android.util.Log; import com.smartdevicelink.exception.SdlException; -import com.smartdevicelink.proxy.LockScreenManager; +import com.smartdevicelink.managers.CompletionListener; +import com.smartdevicelink.managers.SdlManager; +import com.smartdevicelink.managers.SdlManagerListener; +import com.smartdevicelink.managers.file.filetypes.SdlArtwork; +import com.smartdevicelink.protocol.enums.FunctionID; +import com.smartdevicelink.proxy.RPCNotification; import com.smartdevicelink.proxy.RPCRequest; -import com.smartdevicelink.proxy.RPCResponse; -import com.smartdevicelink.proxy.SdlProxyALM; -import com.smartdevicelink.proxy.callbacks.OnServiceEnded; -import com.smartdevicelink.proxy.callbacks.OnServiceNACKed; -import com.smartdevicelink.proxy.interfaces.IProxyListenerALM; +import com.smartdevicelink.proxy.TTSChunkFactory; import com.smartdevicelink.proxy.rpc.AddCommand; -import com.smartdevicelink.proxy.rpc.AddCommandResponse; -import com.smartdevicelink.proxy.rpc.AddSubMenuResponse; -import com.smartdevicelink.proxy.rpc.AlertManeuverResponse; -import com.smartdevicelink.proxy.rpc.AlertResponse; -import com.smartdevicelink.proxy.rpc.ButtonPressResponse; -import com.smartdevicelink.proxy.rpc.ChangeRegistrationResponse; -import com.smartdevicelink.proxy.rpc.CreateInteractionChoiceSetResponse; -import com.smartdevicelink.proxy.rpc.DeleteCommandResponse; -import com.smartdevicelink.proxy.rpc.DeleteFileResponse; -import com.smartdevicelink.proxy.rpc.DeleteInteractionChoiceSetResponse; -import com.smartdevicelink.proxy.rpc.DeleteSubMenuResponse; -import com.smartdevicelink.proxy.rpc.DiagnosticMessageResponse; -import com.smartdevicelink.proxy.rpc.DialNumberResponse; -import com.smartdevicelink.proxy.rpc.EndAudioPassThruResponse; -import com.smartdevicelink.proxy.rpc.GenericResponse; -import com.smartdevicelink.proxy.rpc.GetDTCsResponse; -import com.smartdevicelink.proxy.rpc.GetInteriorVehicleDataResponse; -import com.smartdevicelink.proxy.rpc.GetSystemCapabilityResponse; -import com.smartdevicelink.proxy.rpc.GetVehicleDataResponse; -import com.smartdevicelink.proxy.rpc.GetWayPointsResponse; -import com.smartdevicelink.proxy.rpc.Image; -import com.smartdevicelink.proxy.rpc.ListFiles; -import com.smartdevicelink.proxy.rpc.ListFilesResponse; import com.smartdevicelink.proxy.rpc.MenuParams; -import com.smartdevicelink.proxy.rpc.OnAudioPassThru; -import com.smartdevicelink.proxy.rpc.OnButtonEvent; -import com.smartdevicelink.proxy.rpc.OnButtonPress; import com.smartdevicelink.proxy.rpc.OnCommand; -import com.smartdevicelink.proxy.rpc.OnDriverDistraction; import com.smartdevicelink.proxy.rpc.OnHMIStatus; -import com.smartdevicelink.proxy.rpc.OnHashChange; -import com.smartdevicelink.proxy.rpc.OnInteriorVehicleData; -import com.smartdevicelink.proxy.rpc.OnKeyboardInput; -import com.smartdevicelink.proxy.rpc.OnLanguageChange; -import com.smartdevicelink.proxy.rpc.OnLockScreenStatus; -import com.smartdevicelink.proxy.rpc.OnPermissionsChange; -import com.smartdevicelink.proxy.rpc.OnRCStatus; -import com.smartdevicelink.proxy.rpc.OnStreamRPC; -import com.smartdevicelink.proxy.rpc.OnSystemRequest; -import com.smartdevicelink.proxy.rpc.OnTBTClientState; -import com.smartdevicelink.proxy.rpc.OnTouchEvent; -import com.smartdevicelink.proxy.rpc.OnVehicleData; -import com.smartdevicelink.proxy.rpc.OnWayPointChange; -import com.smartdevicelink.proxy.rpc.PerformAudioPassThruResponse; -import com.smartdevicelink.proxy.rpc.PerformInteractionResponse; -import com.smartdevicelink.proxy.rpc.PutFile; -import com.smartdevicelink.proxy.rpc.PutFileResponse; -import com.smartdevicelink.proxy.rpc.ReadDIDResponse; -import com.smartdevicelink.proxy.rpc.ResetGlobalPropertiesResponse; -import com.smartdevicelink.proxy.rpc.ScrollableMessageResponse; -import com.smartdevicelink.proxy.rpc.SendHapticDataResponse; -import com.smartdevicelink.proxy.rpc.SendLocationResponse; -import com.smartdevicelink.proxy.rpc.SetAppIconResponse; -import com.smartdevicelink.proxy.rpc.SetDisplayLayoutResponse; -import com.smartdevicelink.proxy.rpc.SetGlobalPropertiesResponse; -import com.smartdevicelink.proxy.rpc.SetInteriorVehicleDataResponse; -import com.smartdevicelink.proxy.rpc.SetMediaClockTimerResponse; -import com.smartdevicelink.proxy.rpc.ShowConstantTbtResponse; -import com.smartdevicelink.proxy.rpc.ShowResponse; -import com.smartdevicelink.proxy.rpc.SliderResponse; -import com.smartdevicelink.proxy.rpc.SpeakResponse; -import com.smartdevicelink.proxy.rpc.StreamRPCResponse; -import com.smartdevicelink.proxy.rpc.SubscribeButtonResponse; -import com.smartdevicelink.proxy.rpc.SubscribeVehicleDataResponse; -import com.smartdevicelink.proxy.rpc.SubscribeWayPointsResponse; -import com.smartdevicelink.proxy.rpc.SystemRequestResponse; -import com.smartdevicelink.proxy.rpc.UnsubscribeButtonResponse; -import com.smartdevicelink.proxy.rpc.UnsubscribeVehicleDataResponse; -import com.smartdevicelink.proxy.rpc.UnsubscribeWayPointsResponse; -import com.smartdevicelink.proxy.rpc.UpdateTurnListResponse; +import com.smartdevicelink.proxy.rpc.Speak; +import com.smartdevicelink.proxy.rpc.enums.AppHMIType; import com.smartdevicelink.proxy.rpc.enums.FileType; import com.smartdevicelink.proxy.rpc.enums.HMILevel; -import com.smartdevicelink.proxy.rpc.enums.ImageType; -import com.smartdevicelink.proxy.rpc.enums.LockScreenStatus; -import com.smartdevicelink.proxy.rpc.enums.RequestType; -import com.smartdevicelink.proxy.rpc.enums.SdlDisconnectedReason; -import com.smartdevicelink.proxy.rpc.enums.TextAlignment; -import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener; +import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener; import com.smartdevicelink.transport.BTTransportConfig; import com.smartdevicelink.transport.BaseTransportConfig; import com.smartdevicelink.transport.MultiplexTransportConfig; import com.smartdevicelink.transport.TCPTransportConfig; -import com.smartdevicelink.transport.TransportConstants; import com.smartdevicelink.transport.USBTransportConfig; -import com.smartdevicelink.util.CorrelationIdGenerator; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.Vector; -public class SdlService extends Service implements IProxyListenerALM{ +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_ID = "8675309"; - + private static final String APP_ID = "8677309"; + private static final String ICON_FILENAME = "hello_sdl_icon.png"; private static final String SDL_IMAGE_FILENAME = "sdl_full_image.png"; - private int iconCorrelationId; - private List<String> remoteFiles; - 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 TEST_COMMAND_ID = 1; private static final int FOREGROUND_SERVICE_ID = 111; + private boolean firstShow; // TCP/IP transport config // The default port is 12345 @@ -142,15 +63,11 @@ public class SdlService extends Service implements IProxyListenerALM{ private static final String DEV_MACHINE_IP_ADDRESS = "192.168.1.78"; // variable to create and call functions of the SyncProxy - private SdlProxyALM proxy = null; + private SdlManager sdlManager = null; - private boolean firstNonHmiNone = true; @SuppressWarnings("unused") private boolean isVehicleDataSubscribed = false; - private String lockScreenUrlFromCore = null; - private final LockScreenManager lockScreenManager = new LockScreenManager(); - @Override public IBinder onBind(Intent intent) { return null; @@ -158,9 +75,8 @@ public class SdlService extends Service implements IProxyListenerALM{ @Override public void onCreate() { - Log.d(TAG, "onCreate"); + Log.d(TAG, "onCreate"); super.onCreate(); - remoteFiles = new ArrayList<>(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { enterForeground(); @@ -180,100 +96,121 @@ public class SdlService extends Service implements IProxyListenerALM{ @Override public int onStartCommand(Intent intent, int flags, int startId) { - //Check if this was started with a flag to force a transport connect - boolean forced = intent !=null && intent.getBooleanExtra(TransportConstants.FORCE_TRANSPORT_CONNECTED, false); - startProxy(forced, intent); - + startProxy(intent); return START_STICKY; } @Override public void onDestroy() { - disposeSyncProxy(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { stopForeground(true); } super.onDestroy(); } - private void startProxy(boolean forceConnect, Intent intent) { - Log.i(TAG, "Trying to start proxy"); - if (proxy == null) { - try { - Log.i(TAG, "Starting SDL Proxy"); - BaseTransportConfig transport = null; - if(BuildConfig.TRANSPORT.equals("MBT")){ - 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("LBT")){ - transport = new BTTransportConfig(); - }else if(BuildConfig.TRANSPORT.equals("TCP")){ - transport = new TCPTransportConfig(TCP_PORT, DEV_MACHINE_IP_ADDRESS, true); - }else if(BuildConfig.TRANSPORT.equals("USB")) { - if (intent != null && intent.hasExtra(UsbManager.EXTRA_ACCESSORY)) { //If we want to support USB transport - if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.HONEYCOMB) { - Log.e(TAG, "Unable to start proxy. Android OS version is too low"); - return; - }else { - //We have a usb transport - transport = new USBTransportConfig(getBaseContext(), (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY)); - Log.d(TAG, "USB created."); - } - } - } - if(transport != null) { - proxy = new SdlProxyALM(this, APP_NAME, true, APP_ID, transport); + private void startProxy(Intent intent) { + // This logic is to select the correct transport and security levels defined in the selected build flavor + if (sdlManager == null) { + Log.i(TAG, "Starting SDL Proxy"); + BaseTransportConfig transport = null; + if (BuildConfig.TRANSPORT.equals("MBT")) { + 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; } - } catch (SdlException e) { - e.printStackTrace(); - // error creating proxy, returned proxy = null - if (proxy == null) { - stopSelf(); + transport = new MultiplexTransportConfig(this, APP_ID, securityLevel); + } else if (BuildConfig.TRANSPORT.equals("LBT")) { + transport = new BTTransportConfig(); + } else if (BuildConfig.TRANSPORT.equals("TCP")) { + transport = new TCPTransportConfig(TCP_PORT, DEV_MACHINE_IP_ADDRESS, true); + } else if (BuildConfig.TRANSPORT.equals("USB")) { + if (intent != null && intent.hasExtra(UsbManager.EXTRA_ACCESSORY)) { //If we want to support USB transport + if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.HONEYCOMB) { + Log.e(TAG, "Unable to start proxy. Android OS version is too low"); + return; + } else { + //We have a usb transport + transport = new USBTransportConfig(getBaseContext(), (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY)); + Log.d(TAG, "USB created."); + } } } - }else if(forceConnect){ - proxy.forceOnConnected(); - } - } - private void disposeSyncProxy() { - LockScreenActivity.updateLockScreenStatus(LockScreenStatus.OFF); + // The app type to be used + Vector<AppHMIType> appType = new Vector<>(); + appType.add(AppHMIType.MEDIA); + + // 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 status = (OnHMIStatus) notification; + if (status.getHmiLevel() == HMILevel.HMI_FULL) { + if (!firstShow) { + sendCommands(); + performWelcomeSpeak(); + performWelcomeShow(); + firstShow = true; + } + } + } + }); + + // Menu Selected Listener + sdlManager.addOnRPCNotificationListener(FunctionID.ON_COMMAND, new OnRPCNotificationListener() { + @Override + public void onNotified(RPCNotification notification) { + OnCommand command = (OnCommand) notification; + Integer id = command.getCmdID(); + if(id != null){ + switch(id){ + case TEST_COMMAND_ID: + showTest(); + break; + } + } + } + }); + } - if (proxy != null) { - try { - proxy.dispose(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - proxy = null; - } - } - this.firstNonHmiNone = true; - this.isVehicleDataSubscribed = false; - - } + @Override + public void onDestroy() { + SdlService.this.stopSelf(); + } - /** - * Will show a sample test message on screen as well as speak a sample test message - */ - private void showTest(){ - try { - proxy.show(TEST_COMMAND_NAME, "Command has been selected", TextAlignment.CENTERED, CorrelationIdGenerator.generateId()); - proxy.speak(TEST_COMMAND_NAME, CorrelationIdGenerator.generateId()); - } catch (SdlException e) { - e.printStackTrace(); + @Override + public void onError(String info, Exception e) { + } + }; + + // Create App Icon, this is set in the SdlManager builder + SdlArtwork appIcon = new SdlArtwork(); + appIcon.setType(FileType.GRAPHIC_PNG); + appIcon.setName(ICON_FILENAME); + appIcon.setResourceId(R.mipmap.ic_launcher); + appIcon.setPersistent(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(); } } - + /** * Add commands for the app on SDL. */ @@ -288,586 +225,54 @@ public class SdlService extends Service implements IProxyListenerALM{ } /** - * Sends an RPC Request to the connected head unit. Automatically adds a correlation id. - * @param request the rpc request that is to be sent to the module + * Will speak a sample welcome message */ - private void sendRpcRequest(RPCRequest request){ - try { - proxy.sendRPCRequest(request); - } catch (SdlException e) { - e.printStackTrace(); - } + private void performWelcomeSpeak(){ + sendRpcRequest(new Speak(TTSChunkFactory.createSimpleTTSChunks(WELCOME_SPEAK))); } - /** - * Sends the app icon through the uploadImage method with correct params - */ - private void sendIcon(){ - iconCorrelationId = CorrelationIdGenerator.generateId(); - uploadImage(R.mipmap.ic_launcher, ICON_FILENAME, iconCorrelationId, true); - } - - /** - * This method will help upload an image to the head unit - * @param resource the R.drawable.__ value of the image you wish to send - * @param imageName the filename that will be used to reference this image - * @param correlationId the correlation id to be used with this request. Helpful for monitoring putfileresponses - * @param isPersistent tell the system if the file should stay or be cleared out after connection. - */ - @SuppressWarnings("SameParameterValue") - private void uploadImage(int resource, String imageName, int correlationId, boolean isPersistent){ - PutFile putFile = new PutFile(); - putFile.setFileType(FileType.GRAPHIC_PNG); - putFile.setSdlFileName(imageName); - putFile.setCorrelationID(correlationId); - putFile.setPersistentFile(isPersistent); - putFile.setSystemFile(false); - putFile.setBulkData(contentsOfResource(resource)); - try { - proxy.sendRPCRequest(putFile); - } catch (SdlException e) { - e.printStackTrace(); - } - } - - /** - * Helper method to take resource files and turn them into byte arrays - * @param resource Resource file id. - * @return Resulting byte array. - */ - private byte[] contentsOfResource(int resource) { - InputStream is = null; - try { - is = getResources().openRawResource(resource); - ByteArrayOutputStream os = new ByteArrayOutputStream(is.available()); - final int bufferSize = 4096; - final byte[] buffer = new byte[bufferSize]; - int available; - while ((available = is.read(buffer)) >= 0) { - os.write(buffer, 0, available); - } - return os.toByteArray(); - } catch (IOException e) { - Log.w(TAG, "Can't read icon file", e); - return null; - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - @Override - public void onProxyClosed(String info, Exception e, SdlDisconnectedReason reason) { - stopSelf(); - if(reason.equals(SdlDisconnectedReason.LANGUAGE_CHANGE) && BuildConfig.TRANSPORT.equals("MBT")){ - Intent intent = new Intent(TransportConstants.START_ROUTER_SERVICE_ACTION); - intent.putExtra(SdlReceiver.RECONNECT_LANG_CHANGE, true); - sendBroadcast(intent); - } - } - - @Override - public void onOnHMIStatus(OnHMIStatus notification) { - if(notification.getHmiLevel().equals(HMILevel.HMI_FULL)){ - if (notification.getFirstRun()) { - // send welcome message if applicable - performWelcomeMessage(); - } - // Other HMI (Show, PerformInteraction, etc.) would go here - } - - if(!notification.getHmiLevel().equals(HMILevel.HMI_NONE) - && firstNonHmiNone){ - sendCommands(); - //uploadImages(); - firstNonHmiNone = false; - - // Other app setup (SubMenu, CreateChoiceSet, etc.) would go here - }else{ - //We have HMI_NONE - if(notification.getFirstRun()){ - uploadImages(); - } - } - } - - /** - * Listener for handling when a lockscreen image is downloaded. - */ - private class LockScreenDownloadedListener implements LockScreenManager.OnLockScreenIconDownloadedListener{ - - @Override - public void onLockScreenIconDownloaded(Bitmap icon) { - Log.i(TAG, "Lock screen icon downloaded successfully"); - LockScreenActivity.updateLockScreenImage(icon); - } - - @Override - public void onLockScreenIconDownloadError(Exception e) { - Log.e(TAG, "Couldn't download lock screen icon, resorting to default."); - LockScreenActivity.updateLockScreenImage(BitmapFactory.decodeResource(getResources(), - R.drawable.sdl)); - } - } - - /** - * Will show a sample welcome message on screen as well as speak a sample welcome message - */ - private void performWelcomeMessage(){ - try { - Image image = new Image(); - image.setValue(SDL_IMAGE_FILENAME); - image.setImageType(ImageType.DYNAMIC); - - //Set the welcome message on screen - proxy.show(APP_NAME, WELCOME_SHOW, null, null, null, null, null, image, null, null, TextAlignment.CENTERED, CorrelationIdGenerator.generateId()); - - //Say the welcome message - proxy.speak(WELCOME_SPEAK, CorrelationIdGenerator.generateId()); - - } catch (SdlException e) { - e.printStackTrace(); - } - - } - /** - * Requests list of images to SDL, and uploads images that are missing. + * 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 uploadImages(){ - ListFiles listFiles = new ListFiles(); - listFiles.setOnRPCResponseListener(new OnRPCResponseListener() { + 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 onResponse(int correlationId, RPCResponse response) { - if(response.getSuccess()){ - remoteFiles = ((ListFilesResponse) response).getFilenames(); - } - - // Check the mutable set for the AppIcon - // If not present, upload the image - if(remoteFiles== null || !remoteFiles.contains(SdlService.ICON_FILENAME)){ - sendIcon(); - }else{ - // If the file is already present, send the SetAppIcon request - try { - proxy.setappicon(ICON_FILENAME, CorrelationIdGenerator.generateId()); - } catch (SdlException e) { - e.printStackTrace(); - } - } - - // Check the mutable set for the SDL image - // If not present, upload the image - if(remoteFiles== null || !remoteFiles.contains(SdlService.SDL_IMAGE_FILENAME)){ - uploadImage(R.drawable.sdl, SDL_IMAGE_FILENAME, CorrelationIdGenerator.generateId(), true); + public void onComplete(boolean success) { + if (success){ + Log.i(TAG, "welcome show successful"); } } }); - this.sendRpcRequest(listFiles); - } - - @Override - public void onListFilesResponse(ListFilesResponse response) { - Log.i(TAG, "onListFilesResponse from SDL "); - } - - @Override - public void onPutFileResponse(PutFileResponse response) { - Log.i(TAG, "onPutFileResponse from SDL"); - if(response.getCorrelationID() == iconCorrelationId){ //If we have successfully uploaded our icon, we want to set it - try { - proxy.setappicon(ICON_FILENAME, CorrelationIdGenerator.generateId()); - } catch (SdlException e) { - e.printStackTrace(); - } - } - } - - @Override - public void onOnLockScreenNotification(OnLockScreenStatus notification) { - LockScreenActivity.updateLockScreenStatus(notification.getShowLockScreen()); - } - - @Override - public void onOnCommand(OnCommand notification){ - Integer id = notification.getCmdID(); - if(id != null){ - switch(id){ - case TEST_COMMAND_ID: - showTest(); - break; - } - } } /** - * Callback method that runs when the add command response is received from SDL. + * Will show a sample test message on screen as well as speak a sample test message */ - @Override - public void onAddCommandResponse(AddCommandResponse response) { - Log.i(TAG, "AddCommand response from SDL: " + response.getResultCode().name()); + private void showTest(){ + sdlManager.getScreenManager().beginTransaction(); + sdlManager.getScreenManager().setTextField1("Command has been selected"); + sdlManager.getScreenManager().setTextField2(""); + sdlManager.getScreenManager().commit(null); - } - - /* Vehicle Data */ - @Override - public void onOnPermissionsChange(OnPermissionsChange notification) { - Log.i(TAG, "Permision changed: " + notification); - - /* Uncomment to subscribe to vehicle data - List<PermissionItem> permissions = notification.getPermissionItem(); - for(PermissionItem permission:permissions){ - if(permission.getRpcName().equalsIgnoreCase(FunctionID.SUBSCRIBE_VEHICLE_DATA.name())){ - if(permission.getHMIPermissions().getAllowed()!=null && permission.getHMIPermissions().getAllowed().size()>0){ - if(!isVehicleDataSubscribed){ //If we haven't already subscribed we will subscribe now - //TODO: Add the vehicle data items you want to subscribe to - //proxy.subscribevehicledata(gps, speed, rpm, fuelLevel, fuelLevel_State, instantFuelConsumption, externalTemperature, prndl, tirePressure, odometer, beltStatus, bodyInformation, deviceStatus, driverBraking, correlationID); - proxy.subscribevehicledata(false, true, rpm, false, false, false, false, false, false, false, false, false, false, false, autoIncCorrId++); - } - } - } - } - */ + sendRpcRequest(new Speak(TTSChunkFactory.createSimpleTTSChunks(TEST_COMMAND_NAME))); } /** - * Rest of the SDL callbacks from the head unit + * Sends an RPC Request to the connected head unit. Automatically adds a correlation id. + * @param request the rpc request that is to be sent to the module */ - - @Override - public void onSubscribeVehicleDataResponse(SubscribeVehicleDataResponse response) { - if(response.getSuccess()){ - Log.i(TAG, "Subscribed to vehicle data"); - this.isVehicleDataSubscribed = true; - } - } - - @Override - public void onOnVehicleData(OnVehicleData notification) { - Log.i(TAG, "Vehicle data notification from SDL"); - //TODO Put your vehicle data code here - //ie, notification.getSpeed(). - } - - @Override - public void onAddSubMenuResponse(AddSubMenuResponse response) { - Log.i(TAG, "AddSubMenu response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onCreateInteractionChoiceSetResponse(CreateInteractionChoiceSetResponse response) { - Log.i(TAG, "CreateInteractionChoiceSet response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onAlertResponse(AlertResponse response) { - Log.i(TAG, "Alert response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onDeleteCommandResponse(DeleteCommandResponse response) { - Log.i(TAG, "DeleteCommand response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onDeleteInteractionChoiceSetResponse(DeleteInteractionChoiceSetResponse response) { - Log.i(TAG, "DeleteInteractionChoiceSet response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onDeleteSubMenuResponse(DeleteSubMenuResponse response) { - Log.i(TAG, "DeleteSubMenu response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onPerformInteractionResponse(PerformInteractionResponse response) { - Log.i(TAG, "PerformInteraction response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onResetGlobalPropertiesResponse( - ResetGlobalPropertiesResponse response) { - Log.i(TAG, "ResetGlobalProperties response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSetGlobalPropertiesResponse(SetGlobalPropertiesResponse response) { - Log.i(TAG, "SetGlobalProperties response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSetMediaClockTimerResponse(SetMediaClockTimerResponse response) { - Log.i(TAG, "SetMediaClockTimer response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onShowResponse(ShowResponse response) { - Log.i(TAG, "Show response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSpeakResponse(SpeakResponse response) { - Log.i(TAG, "SpeakCommand response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnButtonEvent(OnButtonEvent notification) { - Log.i(TAG, "OnButtonEvent notification from SDL: " + notification); - } - - @Override - public void onOnButtonPress(OnButtonPress notification) { - Log.i(TAG, "OnButtonPress notification from SDL: " + notification); - } - - @Override - public void onSubscribeButtonResponse(SubscribeButtonResponse response) { - Log.i(TAG, "SubscribeButton response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onUnsubscribeButtonResponse(UnsubscribeButtonResponse response) { - Log.i(TAG, "UnsubscribeButton response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnTBTClientState(OnTBTClientState notification) { - Log.i(TAG, "OnTBTClientState notification from SDL: " + notification); - } - - @Override - public void onUnsubscribeVehicleDataResponse( - UnsubscribeVehicleDataResponse response) { - Log.i(TAG, "UnsubscribeVehicleData response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onGetVehicleDataResponse(GetVehicleDataResponse response) { - Log.i(TAG, "GetVehicleData response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onReadDIDResponse(ReadDIDResponse response) { - Log.i(TAG, "ReadDID response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onGetDTCsResponse(GetDTCsResponse response) { - Log.i(TAG, "GetDTCs response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onPerformAudioPassThruResponse(PerformAudioPassThruResponse response) { - Log.i(TAG, "PerformAudioPassThru response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onEndAudioPassThruResponse(EndAudioPassThruResponse response) { - Log.i(TAG, "EndAudioPassThru response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnAudioPassThru(OnAudioPassThru notification) { - Log.i(TAG, "OnAudioPassThru notification from SDL: " + notification ); - } - - @Override - public void onDeleteFileResponse(DeleteFileResponse response) { - Log.i(TAG, "DeleteFile response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSetAppIconResponse(SetAppIconResponse response) { - Log.i(TAG, "SetAppIcon response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onScrollableMessageResponse(ScrollableMessageResponse response) { - Log.i(TAG, "ScrollableMessage response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onChangeRegistrationResponse(ChangeRegistrationResponse response) { - Log.i(TAG, "ChangeRegistration response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSetDisplayLayoutResponse(SetDisplayLayoutResponse response) { - Log.i(TAG, "SetDisplayLayout response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnLanguageChange(OnLanguageChange notification) { - Log.i(TAG, "OnLanguageChange notification from SDL: " + notification); - } - - @Override - public void onSliderResponse(SliderResponse response) { - Log.i(TAG, "Slider response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnHashChange(OnHashChange notification) { - Log.i(TAG, "OnHashChange notification from SDL: " + notification); - } - - @Override - public void onOnSystemRequest(OnSystemRequest notification) { - Log.i(TAG, "OnSystemRequest notification from SDL: " + notification); - - // Download the lockscreen icon Core desires - if(notification.getRequestType().equals(RequestType.LOCK_SCREEN_ICON_URL) && lockScreenUrlFromCore == null){ - lockScreenUrlFromCore = notification.getUrl(); - if(lockScreenUrlFromCore != null && lockScreenManager.getLockScreenIcon() == null){ - lockScreenManager.downloadLockScreenIcon(lockScreenUrlFromCore, new LockScreenDownloadedListener()); - } + private void sendRpcRequest(RPCRequest request){ + try { + sdlManager.sendRPC(request); + } catch (SdlException e) { + e.printStackTrace(); } } - @Override - public void onSystemRequestResponse(SystemRequestResponse response) { - Log.i(TAG, "SystemRequest response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnKeyboardInput(OnKeyboardInput notification) { - Log.i(TAG, "OnKeyboardInput notification from SDL: " + notification); - } - - @Override - public void onOnTouchEvent(OnTouchEvent notification) { - Log.i(TAG, "OnTouchEvent notification from SDL: " + notification); - } - - @Override - public void onDiagnosticMessageResponse(DiagnosticMessageResponse response) { - Log.i(TAG, "DiagnosticMessage response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnStreamRPC(OnStreamRPC notification) { - Log.i(TAG, "OnStreamRPC notification from SDL: " + notification); - } - - @Override - public void onStreamRPCResponse(StreamRPCResponse response) { - Log.i(TAG, "StreamRPC response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onDialNumberResponse(DialNumberResponse response) { - Log.i(TAG, "DialNumber response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSendLocationResponse(SendLocationResponse response) { - Log.i(TAG, "SendLocation response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onServiceEnded(OnServiceEnded serviceEnded) { - - } - - @Override - public void onServiceNACKed(OnServiceNACKed serviceNACKed) { - - } - - @Override - public void onShowConstantTbtResponse(ShowConstantTbtResponse response) { - Log.i(TAG, "ShowConstantTbt response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onAlertManeuverResponse(AlertManeuverResponse response) { - Log.i(TAG, "AlertManeuver response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onUpdateTurnListResponse(UpdateTurnListResponse response) { - Log.i(TAG, "UpdateTurnList response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onServiceDataACK(int dataSize) { - - } - - @Override - public void onGetWayPointsResponse(GetWayPointsResponse response) { - Log.i(TAG, "GetWayPoints response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSubscribeWayPointsResponse(SubscribeWayPointsResponse response) { - Log.i(TAG, "SubscribeWayPoints response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onUnsubscribeWayPointsResponse(UnsubscribeWayPointsResponse response) { - Log.i(TAG, "UnsubscribeWayPoints response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnWayPointChange(OnWayPointChange notification) { - Log.i(TAG, "OnWayPointChange notification from SDL: " + notification); - } - - @Override - public void onOnDriverDistraction(OnDriverDistraction notification) { - // Some RPCs (depending on region) cannot be sent when driver distraction is active. - } - - @Override - public void onError(String info, Exception e) { - } - - @Override - public void onGenericResponse(GenericResponse response) { - Log.i(TAG, "Generic response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onGetSystemCapabilityResponse(GetSystemCapabilityResponse response) { - Log.i(TAG, "GetSystemCapabilityResponse from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSendHapticDataResponse(SendHapticDataResponse response){ - Log.i(TAG, "SendHapticData response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnRCStatus(OnRCStatus notification) { - Log.i(TAG, "OnRCStatus notification from SDL: " + notification); - - } - - @Override - public void onButtonPressResponse(ButtonPressResponse response) { - Log.i(TAG, "ButtonPress response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onSetInteriorVehicleDataResponse(SetInteriorVehicleDataResponse response) { - Log.i(TAG, "SetInteriorVehicleData response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onGetInteriorVehicleDataResponse(GetInteriorVehicleDataResponse response) { - Log.i(TAG, "GetInteriorVehicleData response from SDL: " + response.getResultCode().name() + " Info: " + response.getInfo()); - } - - @Override - public void onOnInteriorVehicleData(OnInteriorVehicleData notification) { - Log.i(TAG, "OnInteriorVehicleData from SDL: " + notification); - - } - -} +}
\ No newline at end of file |