Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring SdlRouterService and SdlBroadcastReceiver #1352

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import android.os.Build;
import android.util.Log;

import com.smartdevicelink.transport.SdlBroadcastReceiver;
import com.smartdevicelink.transport.SdlRouterService;
import com.smartdevicelink.transport.BaseBroadcastReceiver;
import com.smartdevicelink.transport.BaseRouterService;

public class SdlReceiver extends SdlBroadcastReceiver {
public class SdlReceiver extends BaseBroadcastReceiver {
private static final String TAG = "SdlBroadcastReciever";

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sdl.hellosdlandroid;


public class SdlRouterService extends com.smartdevicelink.transport.SdlRouterService {
public class SdlRouterService extends com.smartdevicelink.transport.BaseRouterService {



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import android.os.Message;

import com.smartdevicelink.test.util.DeviceUtil;
import com.smartdevicelink.transport.BaseRouterService;
import com.smartdevicelink.transport.MultiplexBluetoothTransport;
import com.smartdevicelink.transport.SdlRouterService;

import junit.framework.TestCase;

Expand Down Expand Up @@ -34,7 +34,7 @@ public void handleMessage(Message msg) {
return;
}
switch(msg.what){
case SdlRouterService.MESSAGE_STATE_CHANGE:
case BaseRouterService.MESSAGE_STATE_CHANGE:
if(msg.arg1 == stateDesired){
didCorrectThing = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testLocalRouterServiceParcel(){
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.setDataPosition(0);

SdlRouterService.LocalRouterService local = new SdlRouterService.LocalRouterService(p);
BaseRouterService.LocalRouterService local = new BaseRouterService.LocalRouterService(p);

assertNotNull(local);
assertEquals(local.version,4);
Expand All @@ -36,20 +36,20 @@ public void testLocalRouterServiceParcelCreator(){
p.writeParcelable(new Intent(), 0);
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.setDataPosition(0);
SdlRouterService.LocalRouterService local = SdlRouterService.LocalRouterService.CREATOR.createFromParcel(p);

BaseRouterService.LocalRouterService local = BaseRouterService.LocalRouterService.CREATOR.createFromParcel(p);

assertNotNull(local);
assertEquals(local.version,4);
p.recycle();

}

public SdlRouterService.LocalRouterService getLocalRouterService(int testWith, Parcel p){
public BaseRouterService.LocalRouterService getLocalRouterService(int testWith, Parcel p){
if(testWith == TEST_WITH_CONSTRUCTOR){
return new SdlRouterService.LocalRouterService(p);
return new BaseRouterService.LocalRouterService(p);
}else{
return SdlRouterService.LocalRouterService.CREATOR.createFromParcel(p);
return BaseRouterService.LocalRouterService.CREATOR.createFromParcel(p);
}
}

Expand All @@ -60,8 +60,8 @@ public void corruptParcel(int testWith){
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.writeParcelable(new Intent(), 0);
p.setDataPosition(0);
SdlRouterService.LocalRouterService local = getLocalRouterService(testWith, p);

BaseRouterService.LocalRouterService local = getLocalRouterService(testWith, p);

assertNotNull(local);
assertNull(local.launchIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void testHandleMessage() {
}

// Instantiate SdlRouterService and Registered App class
SdlRouterService router = new SdlRouterService();
SdlRouterService.RegisteredApp app = router.new RegisteredApp(APP_ID, messenger);
BaseRouterService router = new BaseRouterService();
BaseRouterService.RegisteredApp app = router.new RegisteredApp(APP_ID, messenger);

// Call Handle Message
app.handleMessage(TransportConstants.BYTES_TO_SEND_FLAG_LARGE_PACKET_START,bytes);
Expand All @@ -42,8 +42,8 @@ public void testNullBuffer() {
}

// Instantiate SdlRouterService and Registered App class
SdlRouterService router = new SdlRouterService();
SdlRouterService.RegisteredApp app = router.new RegisteredApp(APP_ID, messenger);
BaseRouterService router = new BaseRouterService();
BaseRouterService.RegisteredApp app = router.new RegisteredApp(APP_ID, messenger);

// Force Null Buffer
app.buffer = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/*
* Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.smartdevicelink.SdlConnection;


Expand All @@ -44,12 +44,12 @@
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.transport.BTTransport;
import com.smartdevicelink.transport.BTTransportConfig;
import com.smartdevicelink.transport.BaseBroadcastReceiver;
import com.smartdevicelink.transport.BaseTransportConfig;
import com.smartdevicelink.transport.ITransportListener;
import com.smartdevicelink.transport.MultiplexTransport;
import com.smartdevicelink.transport.MultiplexTransportConfig;
import com.smartdevicelink.transport.RouterServiceValidator;
import com.smartdevicelink.transport.SdlBroadcastReceiver;
import com.smartdevicelink.transport.SdlTransport;
import com.smartdevicelink.transport.TCPTransport;
import com.smartdevicelink.transport.TCPTransportConfig;
Expand Down Expand Up @@ -90,7 +90,7 @@ public SdlConnection(BaseTransportConfig transportConfig) {
RouterServiceValidator vlad = null;
//Let's check if we can even do multiplexing
if(transportConfig.getTransportType() == TransportType.MULTIPLEX){
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
ComponentName tempCompName = BaseBroadcastReceiver.consumeQueuedRouterService();
MultiplexTransportConfig multiConfig = (MultiplexTransportConfig)transportConfig;
if(tempCompName!=null){
vlad =new RouterServiceValidator(multiConfig.getContext(),tempCompName);
Expand Down Expand Up @@ -582,7 +582,7 @@ public void forceHardwareConnectEvent(TransportType type){
if(_transport!=null && (_transport.getTransportType()==TransportType.MULTIPLEX)){ //This is only valid for the multiplex connection
MultiplexTransport multi = ((MultiplexTransport)_transport);
MultiplexTransportConfig config = multi.getConfig();
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
ComponentName tempCompName = BaseBroadcastReceiver.consumeQueuedRouterService();
if(config.getService() != null && config.getService().equals(tempCompName)){ //If this is the same service that just connected that we are already looking at. Attempt to reconnect
if(!multi.getIsConnected() && multi.isDisconnecting() ){ //If we aren't able to force a connection it means the
_transport = new MultiplexTransport(config,this);
Expand Down Expand Up @@ -611,7 +611,7 @@ public void forceHardwareConnectEvent(TransportType type){
&& !_transport.getIsConnected()){
if(cachedMultiConfig!=null){
//We are in legacy mode, but just received a force connect. The router service should never be pointing us here if we are truely in legacy mode
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
ComponentName tempCompName = BaseBroadcastReceiver.consumeQueuedRouterService();
RouterServiceValidator vlad = new RouterServiceValidator(cachedMultiConfig.getContext(),tempCompName);
if(vlad.validate()){
cachedMultiConfig.setService(tempCompName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartdevicelink.transport;

public abstract class BaseBroadcastReceiver extends SdlBroadcastReceiver {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartdevicelink.transport;

public class BaseRouterService extends SdlRouterService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected synchronized void setState(int state) {

// Give the new state to the Handler so the UI Activity can update
//Also sending the previous state so we know if we lost a connection
handler.obtainMessage(SdlRouterService.MESSAGE_STATE_CHANGE, state, previousState, getTransportRecord()).sendToTarget();
handler.obtainMessage(BaseRouterService.MESSAGE_STATE_CHANGE, state, previousState, getTransportRecord()).sendToTarget();
}

public String getAddress(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class MultiplexBluetoothTransport extends MultiplexBaseTransport{
public MultiplexBluetoothTransport(Handler handler) {
super(handler, TransportType.BLUETOOTH);
//This will keep track of which method worked last night
mBluetoothLevel = SdlRouterService.getBluetoothPrefs(SHARED_PREFS);
mBluetoothLevel = BaseRouterService.getBluetoothPrefs(SHARED_PREFS);
}


Expand Down Expand Up @@ -248,7 +248,7 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
}

// Send the name of the connected device back to the UI Activity
Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_DEVICE_NAME);
Message msg = handler.obtainMessage(BaseRouterService.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
if(connectedDeviceName != null) {
bundle.putString(DEVICE_NAME, connectedDeviceName);
Expand Down Expand Up @@ -310,7 +310,7 @@ public void write(byte[] out, int offset, int count) {
*/
private void connectionFailed() {
// Send a failure message back to the Activity
Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_LOG);
Message msg = handler.obtainMessage(BaseRouterService.MESSAGE_LOG);
Bundle bundle = new Bundle();
bundle.putString(LOG, "Unable to connect device");
msg.setData(bundle);
Expand All @@ -325,7 +325,7 @@ private void connectionFailed() {
*/
private void connectionLost() {
// Send a failure message back to the Activity
Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_LOG);
Message msg = handler.obtainMessage(BaseRouterService.MESSAGE_LOG);
Bundle bundle = new Bundle();
bundle.putString(LOG, "Device connection was lost");
msg.setData(bundle);
Expand Down Expand Up @@ -509,7 +509,7 @@ public void run() {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mBluetoothLevel = SdlRouterService.getBluetoothPrefs(SHARED_PREFS);
mBluetoothLevel = BaseRouterService.getBluetoothPrefs(SHARED_PREFS);
long waitTime = 3000;
try {
Thread.sleep(waitTime);
Expand All @@ -525,7 +525,7 @@ public void run() {

if(mBluetoothLevel<=1){
try {
SdlRouterService.setBluetoothPrefs(2,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(2,SHARED_PREFS);
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
//Log.i(TAG,"connecting using createRfcommSocket");
mmSocket = (BluetoothSocket) m.invoke(mmDevice, Integer.valueOf(1));
Expand All @@ -539,13 +539,13 @@ public void run() {
Looper.myLooper().quit();
}
success=true;
SdlRouterService.setBluetoothPrefs(1,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(1,SHARED_PREFS);
break;
} else{trySecure = true;}

} catch (Exception e) {
//Log.e(TAG,"createRfcommSocket exception - " + e.toString());
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);

trySecure = true;
try {
Expand All @@ -557,7 +557,7 @@ public void run() {
}else{trySecure = true;}
if(trySecure && mBluetoothLevel<=2){
try {
SdlRouterService.setBluetoothPrefs(3,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(3,SHARED_PREFS);
//Log.i(TAG, "connecting using createRfcommSocketToServiceRecord ");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(SERVER_UUID);
if(mmSocket!=null){
Expand All @@ -570,25 +570,25 @@ public void run() {
Looper.myLooper().quit();
}
success=true;
SdlRouterService.setBluetoothPrefs(2,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(2,SHARED_PREFS);
break;
}else{tryInsecure = true;}
} catch (IOException io) {
tryInsecure = true;
Log.e(TAG,"createRfcommSocketToServiceRecord exception - " + io.toString());
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);

} catch (Exception e){
Log.e(TAG,"createRfcommSocketToServiceRecord exception - " + e.toString());
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);

}
}else{tryInsecure = true;}

if (tryInsecure && mBluetoothLevel<=3) {
// try again using insecure comm if available
try {
SdlRouterService.setBluetoothPrefs(4,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(4,SHARED_PREFS);
//Log.i(TAG,"connecting using createInsecureRfcommSocketToServiceRecord");
Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {UUID.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, new Object[] {SERVER_UUID});
Expand All @@ -602,20 +602,20 @@ public void run() {
}
success=true;
tryInsecure = false;
SdlRouterService.setBluetoothPrefs(3,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(3,SHARED_PREFS);
break;
} catch (NoSuchMethodException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
} catch (IllegalAccessException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
} catch (InvocationTargetException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
}
}
if (tryInsecure && mBluetoothLevel<=4) {
// try again using insecure comm if available
try {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
//Log.i(TAG,"connecting using createInsecureRfcommSocket()");
Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket()", new Class[] {UUID.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, new Object[] {SERVER_UUID});
Expand All @@ -628,14 +628,14 @@ public void run() {
Looper.myLooper().quit();
}
success=true;
SdlRouterService.setBluetoothPrefs(4,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(4,SHARED_PREFS);
break;
} catch (NoSuchMethodException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
} catch (IllegalAccessException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
} catch (InvocationTargetException ie) {
SdlRouterService.setBluetoothPrefs(0,SHARED_PREFS);
BaseRouterService.setBluetoothPrefs(0,SHARED_PREFS);
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -796,7 +796,7 @@ public void run() {
//Log.d(TAG, "Packet formed, sending off");
SdlPacket packet = psm.getFormedPacket();
packet.setTransportRecord(getTransportRecord());
handler.obtainMessage(SdlRouterService.MESSAGE_READ, packet).sendToTarget();
handler.obtainMessage(BaseRouterService.MESSAGE_READ, packet).sendToTarget();
psm.reset();
}
}
Expand Down