Skip to content

Commit

Permalink
Merge pull request #12 from DroidPlanner/droneapi_update
Browse files Browse the repository at this point in the history
Droneapi update
  • Loading branch information
m4gr3d committed Dec 16, 2014
2 parents 4f7da5d + e40cc70 commit 66ccb35
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 45 deletions.
4 changes: 2 additions & 2 deletions AidlLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 200016
versionName '2.0.16'
versionCode 200018
versionName '2.0.18'
}

defaultPublishConfig "release"
Expand Down
25 changes: 25 additions & 0 deletions AidlLib/src/com/o3dr/services/android/lib/model/IApiListener.aidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.o3dr.services.android.lib.model;

import com.o3dr.services.android.lib.drone.connection.ConnectionResult;

/**
* DroneAPI event listener. A valid instance must be provided at api registration.
*/
interface IApiListener {

/**
* Ping the api listener to make sure it's still up and connected.
*/
boolean ping();

/**
* Called when the connection attempt fails.
* @param result Describe why the connection failed.
*/
oneway void onConnectionFailed(in ConnectionResult result);

/**
* Retrieve the version code for the connected client.
*/
int getClientVersionCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.o3dr.services.android.lib.model;

import com.o3dr.services.android.lib.model.IDroneApi;
import com.o3dr.services.android.lib.model.IApiListener;

/**
* Used to establish connection with a drone.
Expand All @@ -14,9 +15,11 @@ interface IDroidPlannerServices {
boolean ping();

/**
* TODO: left now for backward compatibility. To be removed in next version.
* Acquire an handle to the droidplanner api.
* @param appId application id for the application acquiring the drone api handle.
* @return IDroneApi object used to interact with the drone.
* @deprecated use {@link #registerDroneApi(IApiListener listener, String appId)} instead.
*/
IDroneApi acquireDroneApi(String appId);

Expand All @@ -27,4 +30,16 @@ interface IDroidPlannerServices {
*/
void releaseDroneApi(IDroneApi droneApi);

/**
* Retrieve the version code for the api.
*/
int getApiVersionCode();

/**
* Acquire an handle to the droidplanner api.
* @param listener listener for the DroneAPI events.
* @param appId application id for the application acquiring the drone api handle.
* @return IDroneApi object used to interact with the drone.
*/
IDroneApi registerDroneApi(IApiListener listener, String appId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ oneway interface IObserver {
void onAttributeUpdated(String attributeEvent, in Bundle eventExtras);

/**
* TODO: left now for backward compatibility. To be removed in next version.
* Called when the connection attempt fails.
* @param result Describe why the connection failed.
*/
Expand Down
4 changes: 2 additions & 2 deletions ClientLib/mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

ext {
PUBLISH_ARTIFACT_ID = '3dr-services-lib'
PUBLISH_VERSION = '2.1.10'
PUBLISH_VERSION = '2.1.14'
PROJECT_DESCRIPTION = "3DR Services client library"
PROJECT_LABELS = ['3DR', '3DR Services', 'DroneAPI', 'Android']
PROJECT_LICENSES = ['Apache-2.0']
Expand All @@ -15,7 +15,7 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 20110
versionCode 20114
versionName PUBLISH_VERSION
}

Expand Down
Binary file modified ClientLib/mobile/libs/AidlLib.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions ClientLib/mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
android:noHistory="true"
android:excludeFromRecents="true"
android:launchMode="singleTask"/>

<activity
android:name=".utils.UpdateServiceDialog"
android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"
android:noHistory="true"
android:excludeFromRecents="true"
android:launchMode="singleTask"/>
</application>

</manifest>
32 changes: 16 additions & 16 deletions ClientLib/mobile/src/main/java/com/o3dr/android/client/Drone.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.os.SystemClock;
import android.util.Log;

import com.MAVLink.Messages.MAVLinkMessage;
import com.o3dr.android.client.interfaces.DroneListener;
import com.o3dr.services.android.lib.coordinate.LatLong;
import com.o3dr.services.android.lib.drone.attribute.AttributeEvent;
Expand All @@ -17,8 +16,6 @@
import com.o3dr.services.android.lib.drone.mission.Mission;
import com.o3dr.services.android.lib.drone.mission.MissionItemType;
import com.o3dr.services.android.lib.drone.mission.item.MissionItem;
import com.o3dr.services.android.lib.drone.mission.item.complex.StructureScanner;
import com.o3dr.services.android.lib.drone.mission.item.complex.Survey;
import com.o3dr.services.android.lib.drone.property.Altitude;
import com.o3dr.services.android.lib.drone.property.Attitude;
import com.o3dr.services.android.lib.drone.property.Battery;
Expand Down Expand Up @@ -65,6 +62,7 @@ public class Drone {
private final Handler handler;
private final ServiceManager serviceMgr;
private final DroneObserver droneObserver;
private final DroneApiListener apiListener;
private IDroneApi droneApi;

private ConnectionParameter connectionParameter;
Expand All @@ -78,6 +76,7 @@ public class Drone {
public Drone(ServiceManager serviceManager, Handler handler) {
this.handler = handler;
this.serviceMgr = serviceManager;
this.apiListener = new DroneApiListener(this);
this.droneObserver = new DroneObserver(this);
}

Expand All @@ -89,7 +88,8 @@ public void start() {
return;

try {
this.droneApi = serviceMgr.get3drServices().acquireDroneApi(serviceMgr.getApplicationId());
this.droneApi = serviceMgr.get3drServices().registerDroneApi(this.apiListener,
serviceMgr.getApplicationId());
} catch (RemoteException e) {
throw new IllegalStateException("Unable to retrieve a valid drone handle.");
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public long getFlightTime() {
}

public Gps getGps() {
Gps gps = getAttribute(AttributeType.GPS, Gps.class.getClassLoader());
Gps gps = getAttribute(AttributeType.GPS, Gps.class.getClassLoader());
return gps == null ? new Gps() : gps;
}

Expand All @@ -205,7 +205,7 @@ private <T extends Parcelable> T getAttribute(String type, ClassLoader classLoad
T attribute = null;
if (isStarted()) {
Bundle carrier = getAttribute(type);
if(carrier != null) {
if (carrier != null) {
carrier.setClassLoader(classLoader);
attribute = carrier.getParcelable(type);
}
Expand Down Expand Up @@ -311,12 +311,12 @@ public ConnectionParameter getConnectionParameter() {
}

public <T extends MissionItem> void buildComplexMissionItem(MissionItem.ComplexItem<T>
complexItem){
if(isStarted()){
try{
complexItem) {
if (isStarted()) {
try {
T missionItem = (T) complexItem;
Bundle payload = missionItem.getType().storeMissionItem(missionItem);
if(payload == null)
if (payload == null)
return;

droneApi.buildComplexMissionItem(payload);
Expand Down Expand Up @@ -345,8 +345,8 @@ private void addAttributesObserver(IObserver observer) {
}
}

public void addMavlinkObserver(MavlinkObserver observer){
if(isStarted()){
public void addMavlinkObserver(MavlinkObserver observer) {
if (isStarted()) {
try {
droneApi.addMavlinkObserver(observer);
} catch (RemoteException e) {
Expand All @@ -355,8 +355,8 @@ public void addMavlinkObserver(MavlinkObserver observer){
}
}

public void removeMavlinkObserver(MavlinkObserver observer){
if(isStarted()){
public void removeMavlinkObserver(MavlinkObserver observer) {
if (isStarted()) {
try {
droneApi.removeMavlinkObserver(observer);
} catch (RemoteException e) {
Expand Down Expand Up @@ -508,8 +508,8 @@ public void sendGuidedPoint(LatLong point, boolean force) {
}
}

public void sendMavlinkMessage(MavlinkMessageWrapper messageWrapper){
if(messageWrapper != null && isStarted()){
public void sendMavlinkMessage(MavlinkMessageWrapper messageWrapper) {
if (messageWrapper != null && isStarted()) {
try {
droneApi.sendMavlinkMessage(messageWrapper);
} catch (RemoteException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.o3dr.android.client;

import android.os.RemoteException;

import com.o3dr.services.android.lib.BuildConfig;
import com.o3dr.services.android.lib.drone.connection.ConnectionResult;
import com.o3dr.services.android.lib.model.IApiListener;

/**
* Created by fhuya on 12/15/14.
*/
public class DroneApiListener extends IApiListener.Stub {

private final Drone drone;

public DroneApiListener(Drone drone){
this.drone = drone;
}

@Override
public boolean ping() throws RemoteException {
return true;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) throws RemoteException {
drone.notifyDroneConnectionFailed(connectionResult);
}

@Override
public int getClientVersionCode() throws RemoteException {
return BuildConfig.VERSION_CODE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public DroneObserver(Drone drone) {
}

@Override
public void onConnectionFailed(ConnectionResult result) throws RemoteException {
drone.notifyDroneConnectionFailed(result);
}
public void onConnectionFailed(ConnectionResult result) throws RemoteException {}

@Override
public void onAttributeUpdated(String attributeEvent, Bundle eventExtras) throws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import com.o3dr.android.client.interfaces.ServiceListener;
import com.o3dr.android.client.utils.InstallServiceDialog;
import com.o3dr.android.client.utils.UpdateServiceDialog;
import com.o3dr.services.android.lib.*;
import com.o3dr.services.android.lib.BuildConfig;
import com.o3dr.services.android.lib.model.IDroidPlannerServices;

/**
Expand All @@ -26,8 +29,21 @@ public class ServiceManager {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
o3drServices = IDroidPlannerServices.Stub.asInterface(service);
notifyServiceConnected();
o3drServices = IDroidPlannerServices.Stub.asInterface(service);
try {
final int libVersionCode = o3drServices.getApiVersionCode();
if(libVersionCode < BuildConfig.VERSION_CODE){
//Prompt the user to update the 3DR Services app.
o3drServices = null;
promptFor3DRServicesUpdate();
context.unbindService(o3drServicesConnection);
}
else {
notifyServiceConnected();
}
} catch (RemoteException e) {
notifyServiceInterrupted();
}
}

@Override
Expand All @@ -40,7 +56,7 @@ public void onServiceDisconnected(ComponentName name) {
private ServiceListener serviceListener;
private IDroidPlannerServices o3drServices;

public ServiceManager(Context context){
public ServiceManager(Context context) {
this.context = context;
}

Expand All @@ -56,60 +72,64 @@ public boolean isServiceConnected() {
}
}

public void notifyServiceConnected(){
if(serviceListener == null)
public void notifyServiceConnected() {
if (serviceListener == null)
return;

serviceListener.onServiceConnected();
}

public void notifyServiceInterrupted(){
if(serviceListener == null)
public void notifyServiceInterrupted() {
if (serviceListener == null)
return;

serviceListener.onServiceInterrupted();
}

public void connect(ServiceListener listener) {
if(serviceListener != null && isServiceConnected())
if (serviceListener != null && isServiceConnected())
throw new IllegalStateException("Service is already connected.");

if(listener == null) {
if (listener == null) {
throw new IllegalArgumentException("ServiceListener argument cannot be null.");
}

serviceListener = listener;

if(is3DRServicesInstalled())
if (is3DRServicesInstalled())
context.bindService(serviceIntent, o3drServicesConnection, Context.BIND_AUTO_CREATE);
else
promptFor3DRServicesInstall();
}

public void disconnect() {
public void disconnect() {
serviceListener = null;
o3drServices = null;
try {
context.unbindService(o3drServicesConnection);
}catch(Exception e){
} catch (Exception e) {
Log.e(TAG, "Error occurred while unbinding from 3DR Services.", e);
}
}
}

String getApplicationId(){
String getApplicationId() {
return context.getPackageName();
}

private boolean is3DRServicesInstalled(){
private boolean is3DRServicesInstalled() {
final ResolveInfo info = context.getPackageManager().resolveService(serviceIntent, 0);
if(info == null)
if (info == null)
return false;

this.serviceIntent.setClassName(info.serviceInfo.packageName, info.serviceInfo.name);
return true;
}

private void promptFor3DRServicesInstall(){
private void promptFor3DRServicesInstall() {
context.startActivity(new Intent(context, InstallServiceDialog.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}

private void promptFor3DRServicesUpdate(){
context.startActivity(new Intent(context, UpdateServiceDialog.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
Loading

0 comments on commit 66ccb35

Please sign in to comment.