diff options
author | Joey Grover <joeygrover@gmail.com> | 2019-02-27 05:49:18 -0500 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2019-02-27 05:49:18 -0500 |
commit | ffee04568fb0cc12374663288be9e534c0f1ab34 (patch) | |
tree | fe9fe29884c1a3d93513ed92f9fb4b2f50795dee | |
parent | 3a0813742f5941c2952392860e85907b69ccdb83 (diff) | |
download | sdl_android-ffee04568fb0cc12374663288be9e534c0f1ab34.tar.gz |
Optimize OnSystemRequest code
-rw-r--r-- | sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java | 91 | ||||
-rw-r--r-- | sdl_android/src/main/java/com/smartdevicelink/util/HttpUtils.java | 6 |
2 files changed, 49 insertions, 48 deletions
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java index aadd9ab22..07dce12b9 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java @@ -94,6 +94,7 @@ import com.smartdevicelink.transport.USBTransportConfig; import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.util.CorrelationIdGenerator;
import com.smartdevicelink.util.DebugTool;
+import com.smartdevicelink.util.HttpUtils;
import com.smartdevicelink.util.Version;
import org.json.JSONArray;
@@ -1172,26 +1173,26 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> boolean bLegacy = false;
String sURLString;
- if (!getPoliciesURL().equals(""))
+ if (!getPoliciesURL().equals("")) {
sURLString = sPoliciesURL;
- else
+ } else {
sURLString = msg.getUrl();
-
+ }
Integer iTimeout = msg.getTimeout();
if (iTimeout == null)
iTimeout = 2000;
Headers myHeader = msg.getHeader();
-
+ RequestType requestType = msg.getRequestType();
updateBroadcastIntent(sendIntent, "FUNCTION_NAME", "sendOnSystemRequestToUrl");
updateBroadcastIntent(sendIntent, "COMMENT5", "\r\nCloud URL: " + sURLString);
try
{
- if (myHeader == null)
+ if (myHeader == null) {
updateBroadcastIntent(sendIntent, "COMMENT7", "\r\nHTTPRequest Header is null");
-
+ }
String sBodyString = msg.getBody();
JSONObject jsonObjectToSendToServer;
@@ -1199,7 +1200,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> int length;
if (sBodyString == null)
{
- if(RequestType.HTTP.equals(msg.getRequestType())){
+ if(requestType == RequestType.HTTP ){
length = msg.getBulkData().length;
Intent sendIntent3 = createBroadcastIntent();
updateBroadcastIntent(sendIntent3, "FUNCTION_NAME", "replace");
@@ -1237,7 +1238,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> }
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
- if(RequestType.HTTP.equals(msg.getRequestType())){
+ if(requestType == RequestType.HTTP){
wr.write(msg.getBulkData());
}else{
wr.writeBytes(valid_json);
@@ -1274,7 +1275,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> }
rd.close();
//We've read the body
- if(RequestType.HTTP.equals(msg.getRequestType())){
+ if(requestType == RequestType.HTTP){
// Create the SystemRequest RPC to send to module.
PutFile putFile = new PutFile();
putFile.setFileType(FileType.JSON);
@@ -3617,47 +3618,47 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> final OnSystemRequest msg = new OnSystemRequest(hash);
msg.format(rpcSpecVersion,true);
- if ((msg.getUrl() != null) &&
- (((msg.getRequestType() == RequestType.PROPRIETARY) && (msg.getFileType() == FileType.JSON))
- || ((msg.getRequestType() == RequestType.HTTP) && (msg.getFileType() == FileType.BINARY)))){
- Thread handleOffboardTransmissionThread = new Thread() {
- @Override
- public void run() {
- sendOnSystemRequestToUrl(msg);
- }
- };
+ RequestType requestType = msg.getRequestType();
- handleOffboardTransmissionThread.start();
- }
-
-
- if(msg.getRequestType() == RequestType.LOCK_SCREEN_ICON_URL &&
- msg.getUrl() != null){
- lockScreenIconRequest = msg;
- }
- if(msg.getRequestType() == RequestType.ICON_URL){
- //Download file and send RPC request
- Thread handleOffBoardTransmissionThread = new Thread(){
- @Override
- public void run() {
- byte[] file = HttpUtils.downloadFile(msg.getUrl());
- if(file !=null ){
- SystemRequest systemRequest = new SystemRequest();
- systemRequest.setFileName(msg.getUrl());
- systemRequest.setBulkData(file);
- systemRequest.setRequestType(RequestType.ICON_URL);
- try {
- sendRPCRequestPrivate(systemRequest);
- } catch (SdlException e) {
- e.printStackTrace();
+ if(msg.getUrl() != null) {
+ if (((requestType == RequestType.PROPRIETARY) && (msg.getFileType() == FileType.JSON))
+ || ((requestType == RequestType.HTTP) && (msg.getFileType() == FileType.BINARY))) {
+ Thread handleOffboardTransmissionThread = new Thread() {
+ @Override
+ public void run() {
+ sendOnSystemRequestToUrl(msg);
+ }
+ };
+
+ handleOffboardTransmissionThread.start();
+ } else if (requestType == RequestType.LOCK_SCREEN_ICON_URL) {
+ //Cache this for when the lockscreen is displayed
+ lockScreenIconRequest = msg;
+ } else if (requestType == RequestType.ICON_URL) {
+ //Download the icon file and send SystemRequest RPC
+ Thread handleOffBoardTransmissionThread = new Thread() {
+ @Override
+ public void run() {
+ byte[] file = HttpUtils.downloadFile(msg.getUrl());
+ if (file != null) {
+ SystemRequest systemRequest = new SystemRequest();
+ systemRequest.setFileName(msg.getUrl());
+ systemRequest.setBulkData(file);
+ systemRequest.setRequestType(RequestType.ICON_URL);
+ try {
+ sendRPCRequestPrivate(systemRequest);
+ } catch (SdlException e) {
+ e.printStackTrace();
+ }
+ }else{
+ DebugTool.logError("File was null at: " + msg.getUrl());
}
}
- }
- };
- handleOffBoardTransmissionThread.start();
+ };
+ handleOffBoardTransmissionThread.start();
+ }
}
- msg.format(rpcSpecVersion, true);
if (_callbackToUIThread) {
// Run in UI thread
_mainUIHandler.post(new Runnable() {
diff --git a/sdl_android/src/main/java/com/smartdevicelink/util/HttpUtils.java b/sdl_android/src/main/java/com/smartdevicelink/util/HttpUtils.java index db7be4ac8..77b1a8004 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/util/HttpUtils.java +++ b/sdl_android/src/main/java/com/smartdevicelink/util/HttpUtils.java @@ -6,10 +6,10 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; -import java.nio.ByteBuffer; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.support.annotation.NonNull; public class HttpUtils{ @@ -22,7 +22,7 @@ public class HttpUtils{ return result; } - public static byte[] downloadFile(String urlStr){ + public static byte[] downloadFile(@NonNull String urlStr){ try { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); @@ -30,7 +30,7 @@ public class HttpUtils{ ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; - byte[] data = new byte[4000]; + byte[] data = new byte[4096]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); |