Skip to content

Commit d782736

Browse files
Merge pull request #111 from jjcetraro/jjcetraro
fix: Updated examples/android-client according to the new api
2 parents a142719 + d15dde6 commit d782736

File tree

5 files changed

+83
-36
lines changed

5 files changed

+83
-36
lines changed

examples/android-client/app/build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ configurations {
55
}
66

77
android {
8-
compileSdkVersion 29
8+
compileSdkVersion 30
99

1010
compileOptions {
1111
sourceCompatibility JavaVersion.VERSION_1_8
@@ -16,8 +16,8 @@ android {
1616
applicationId "io.mavsdk.androidclient"
1717
minSdkVersion 21
1818
targetSdkVersion 30
19-
versionCode 1
20-
versionName "1.0"
19+
versionCode 2
20+
versionName "1.1"
2121
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2222
}
2323
buildTypes {
@@ -59,8 +59,9 @@ dependencies {
5959
implementation 'com.google.android.material:material:1.2.0'
6060
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0'
6161
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.2.0'
62-
implementation 'io.mavsdk:mavsdk:0.7.0'
63-
implementation 'io.mavsdk:mavsdk-server:0.7.0'
62+
implementation 'com.mapbox.mapboxsdk:mapbox-android-telemetry:6.1.0'
63+
implementation 'io.mavsdk:mavsdk:1.2.0'
64+
implementation 'io.mavsdk:mavsdk-server:1.2.1'
6465
implementation 'org.slf4j:slf4j-api:1.7.28'
6566

6667
testImplementation 'junit:junit:4.13'

examples/android-client/app/src/main/java/io/mavsdk/androidclient/MapsActivity.java

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.os.Bundle;
66
import android.view.Menu;
77
import android.view.MenuItem;
8+
import android.widget.Button;
9+
810
import androidx.annotation.NonNull;
911
import androidx.annotation.Nullable;
1012
import androidx.appcompat.app.AppCompatActivity;
@@ -25,6 +27,8 @@
2527
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager;
2628
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;
2729
import com.mapbox.mapboxsdk.utils.ColorUtils;
30+
31+
import io.mavsdk.MavsdkEventQueue;
2832
import io.mavsdk.System;
2933
import io.mavsdk.mavsdkserver.MavsdkServer;
3034
import io.reactivex.disposables.Disposable;
@@ -50,11 +54,14 @@ public class MapsActivity extends AppCompatActivity implements OnMapReadyCallbac
5054
private MapView mapView;
5155
private MapboxMap map;
5256

57+
private Button buttonRunDestroyMavsdkServer;
58+
5359
private MapsViewModel viewModel;
5460
private Symbol currentPositionMarker;
5561

5662
private MavsdkServer mavsdkServer = new MavsdkServer();
5763
private System drone;
64+
private boolean isMavsdkServerRunning = false;
5865
private final List<Circle> waypoints = new ArrayList<>();
5966
private final List<Disposable> disposables = new ArrayList<>();
6067

@@ -70,6 +77,9 @@ public void onCreate(Bundle savedInstanceState) {
7077
mapView.onCreate(savedInstanceState);
7178
mapView.getMapAsync(this);
7279

80+
buttonRunDestroyMavsdkServer = findViewById(R.id.buttonRunDestroyMavsdkServer);
81+
buttonRunDestroyMavsdkServer.setOnClickListener(v -> runDestroyMavsdkServer());
82+
7383
viewModel = ViewModelProviders.of(this).get(MapsViewModel.class);
7484

7585
FloatingActionButton floatingActionButton = findViewById(R.id.fab);
@@ -88,18 +98,6 @@ public void onResume() {
8898
mapView.onResume();
8999
viewModel.currentPositionLiveData.observe(this, currentPositionObserver);
90100
viewModel.currentMissionPlanLiveData.observe(this, currentMissionPlanObserver);
91-
92-
int mavsdkServerPort = mavsdkServer.run();
93-
drone = new System(BACKEND_IP_ADDRESS, mavsdkServerPort);
94-
95-
disposables.add(drone.getTelemetry().getFlightMode().distinctUntilChanged()
96-
.subscribe(flightMode -> logger.debug("flight mode: " + flightMode)));
97-
disposables.add(drone.getTelemetry().getArmed().distinctUntilChanged()
98-
.subscribe(armed -> logger.debug("armed: " + armed)));
99-
disposables.add(drone.getTelemetry().getPosition().subscribe(position -> {
100-
LatLng latLng = new LatLng(position.getLatitudeDeg(), position.getLongitudeDeg());
101-
viewModel.currentPositionLiveData.postValue(latLng);
102-
}));
103101
}
104102

105103
@Override
@@ -108,16 +106,6 @@ public void onPause() {
108106
mapView.onPause();
109107
viewModel.currentPositionLiveData.removeObserver(currentPositionObserver);
110108
viewModel.currentMissionPlanLiveData.removeObserver(currentMissionPlanObserver);
111-
112-
for (Disposable disposable : disposables) {
113-
disposable.dispose();
114-
}
115-
disposables.clear();
116-
117-
118-
drone.dispose();
119-
drone = null;
120-
mavsdkServer.stop();
121109
}
122110

123111
@Override
@@ -211,12 +199,12 @@ private void updateMarkers(@NonNull List<LatLng> latLngs) {
211199

212200
for (LatLng latLng : latLngs) {
213201
CircleOptions circleOptions = new CircleOptions()
214-
.withLatLng(latLng)
215-
.withCircleColor(ColorUtils.colorToRgbaString(Color.BLUE))
216-
.withCircleStrokeColor(ColorUtils.colorToRgbaString(Color.BLACK))
217-
.withCircleStrokeWidth(1.0f)
218-
.withCircleRadius(12f)
219-
.withDraggable(false);
202+
.withLatLng(latLng)
203+
.withCircleColor(ColorUtils.colorToRgbaString(Color.BLUE))
204+
.withCircleStrokeColor(ColorUtils.colorToRgbaString(Color.BLACK))
205+
.withCircleStrokeWidth(1.0f)
206+
.withCircleRadius(12f)
207+
.withDraggable(false);
220208

221209
circleManager.create(circleOptions);
222210
}
@@ -239,8 +227,8 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
239227
mapboxMap.setStyle(Style.LIGHT, style -> {
240228
// Add the marker image to map
241229
style.addImage("marker-icon-id",
242-
BitmapFactory.decodeResource(
243-
MapsActivity.this.getResources(), R.drawable.mapbox_marker_icon_default));
230+
BitmapFactory.decodeResource(
231+
MapsActivity.this.getResources(), R.drawable.mapbox_marker_icon_default));
244232

245233
symbolManager = new SymbolManager(this.mapView, this.map, style);
246234
symbolManager.setIconAllowOverlap(true);
@@ -249,4 +237,51 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
249237

250238
map = mapboxMap;
251239
}
252-
}
240+
241+
private void runDestroyMavsdkServer() {
242+
if (!isMavsdkServerRunning) {
243+
runMavsdkServer();
244+
} else {
245+
destroyMavsdkServer();
246+
}
247+
}
248+
249+
private void runMavsdkServer() {
250+
MavsdkEventQueue.executor().execute(() -> {
251+
int mavsdkServerPort = mavsdkServer.run();
252+
drone = new System(BACKEND_IP_ADDRESS, mavsdkServerPort);
253+
254+
disposables.add(drone.getTelemetry().getFlightMode().distinctUntilChanged()
255+
.subscribe(flightMode -> logger.debug("flight mode: " + flightMode)));
256+
disposables.add(drone.getTelemetry().getArmed().distinctUntilChanged()
257+
.subscribe(armed -> logger.debug("armed: " + armed)));
258+
disposables.add(drone.getTelemetry().getPosition().subscribe(position -> {
259+
LatLng latLng = new LatLng(position.getLatitudeDeg(), position.getLongitudeDeg());
260+
viewModel.currentPositionLiveData.postValue(latLng);
261+
}));
262+
263+
isMavsdkServerRunning = true;
264+
runOnUiThread(() -> buttonRunDestroyMavsdkServer.setText(R.string.destroy_mavsdk_server));
265+
});
266+
}
267+
268+
private void destroyMavsdkServer() {
269+
MavsdkEventQueue.executor().execute(() -> {
270+
for (Disposable disposable : disposables) {
271+
disposable.dispose();
272+
}
273+
disposables.clear();
274+
drone.dispose();
275+
drone = null;
276+
mavsdkServer.stop();
277+
mavsdkServer.destroy();
278+
279+
isMavsdkServerRunning = false;
280+
runOnUiThread(() -> {
281+
symbolManager.delete(currentPositionMarker);
282+
currentPositionMarker = null;
283+
buttonRunDestroyMavsdkServer.setText(R.string.run_mavsdk_server);
284+
});
285+
});
286+
}
287+
}

examples/android-client/app/src/main/java/io/mavsdk/androidclient/MapsViewModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void startMission(System drone) {
5454
Float.NaN,
5555
1.0,
5656
Float.NaN,
57+
Float.NaN,
5758
Float.NaN);
5859
missionItems.add(missionItem);
5960
}

examples/android-client/app/src/main/res/layout/activity_maps.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
android:tint="@color/white"
2424
android:layout_gravity="end|bottom"/>
2525

26+
<Button
27+
android:id="@+id/buttonRunDestroyMavsdkServer"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_margin="16dp"
31+
android:text="@string/run_mavsdk_server"
32+
android:layout_gravity="start|top"/>
33+
2634
<FrameLayout
2735
android:id="@+id/start_mission_fragment_container"
2836
android:layout_width="match_parent"

examples/android-client/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
<string name="return_home">Return</string>
77
<string name="land">Land</string>
88
<string name="takeoff">Takeoff</string>
9+
<string name="run_mavsdk_server">Run Mavsdk Server</string>
10+
<string name="destroy_mavsdk_server">Destroy Mavsdk Server</string>
911
</resources>

0 commit comments

Comments
 (0)