|
12 | 12 | import android.content.res.AssetManager;
|
13 | 13 | import android.graphics.Bitmap;
|
14 | 14 | import android.graphics.Color;
|
| 15 | +import android.graphics.Matrix; |
15 | 16 | import android.graphics.SurfaceTexture;
|
16 | 17 | import android.graphics.drawable.BitmapDrawable;
|
17 | 18 | import android.graphics.drawable.Drawable;
|
|
25 | 26 | import android.os.Handler;
|
26 | 27 | import android.os.IBinder;
|
27 | 28 | import android.os.Looper;
|
| 29 | +import android.text.format.DateFormat; |
28 | 30 | import android.util.Log;
|
29 | 31 | import android.util.TypedValue;
|
30 | 32 | import android.view.MenuInflater;
|
|
72 | 74 | import java.net.PortUnreachableException;
|
73 | 75 | import java.net.SocketTimeoutException;
|
74 | 76 | import java.util.ArrayList;
|
| 77 | +import java.util.Date; |
75 | 78 | import java.util.List;
|
76 | 79 | import java.util.Objects;
|
77 | 80 | import java.util.Timer;
|
|
118 | 121 | public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
|
119 | 122 | private final static int RESULT_SETTINGS = 1001;
|
120 | 123 | private final static int RESULT_HELP = 1002;
|
121 |
| - private static int compare_height = 0; |
| 124 | + |
| 125 | + private enum FocusedView { |
| 126 | + VideoFeed, |
| 127 | + Map, |
| 128 | + } |
| 129 | + private static FocusedView focusedView = FocusedView.VideoFeed; |
122 | 130 |
|
123 | 131 | public static boolean FLAG_PREFS_CHANGED = false;
|
124 | 132 | public static List<String> changedSettings = new ArrayList<String>();
|
@@ -188,14 +196,12 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
|
188 | 196 | private VideoService videoService = null;
|
189 | 197 | private boolean mIsBound;
|
190 | 198 | private int m_videoMode = 1;
|
| 199 | + private int mPrevVideoBufferSize = 0; |
191 | 200 |
|
192 | 201 | private VideoFeeder.VideoFeed standardVideoFeeder;
|
193 | 202 | protected VideoFeeder.VideoDataListener mReceivedVideoDataListener;
|
194 | 203 | private TextureView videostreamPreviewTtView;
|
195 |
| - private TextureView videostreamPreviewTtViewSmall; |
196 | 204 | public DJICodecManager mCodecManager;
|
197 |
| - private int videoViewWidth; |
198 |
| - private int videoViewHeight; |
199 | 205 | private boolean mIsTranscodedVideoFeedNeeded = false;
|
200 | 206 |
|
201 | 207 | private int mMaptype = GoogleMap.MAP_TYPE_HYBRID;
|
@@ -534,11 +540,18 @@ private void updateDroneLocation() {
|
534 | 540 | BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.pilot, null);
|
535 | 541 | Bitmap smallMarker;
|
536 | 542 |
|
537 |
| - if (compare_height == 0) { |
538 |
| - smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false); |
539 |
| - }else{ |
540 |
| - smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false); |
| 543 | + switch (focusedView) { |
| 544 | + case Map: { |
| 545 | + smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false); |
| 546 | + break; |
| 547 | + } |
| 548 | + case VideoFeed: |
| 549 | + default: { |
| 550 | + smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false); |
| 551 | + break; |
| 552 | + } |
541 | 553 | }
|
| 554 | + |
542 | 555 | GCS_markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
|
543 | 556 |
|
544 | 557 | runOnUiThread(() -> {
|
@@ -568,10 +581,16 @@ private void updateDroneLocation() {
|
568 | 581 | BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.drawable.drone_img, null);
|
569 | 582 | Bitmap smallMarker;
|
570 | 583 |
|
571 |
| - if (compare_height == 0) { |
572 |
| - smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false); |
573 |
| - }else{ |
574 |
| - smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false); |
| 584 | + switch (focusedView) { |
| 585 | + case Map: { |
| 586 | + smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 64, 64, false); |
| 587 | + break; |
| 588 | + } |
| 589 | + case VideoFeed: |
| 590 | + default: { |
| 591 | + smallMarker = Bitmap.createScaledBitmap(bitmapdraw.getBitmap(), 32, 32, false); |
| 592 | + break; |
| 593 | + } |
575 | 594 | }
|
576 | 595 | markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
|
577 | 596 |
|
@@ -745,8 +764,6 @@ protected void onCreate(Bundle savedInstanceState) {
|
745 | 764 | }
|
746 | 765 |
|
747 | 766 | videostreamPreviewTtView = findViewById(R.id.livestream_preview_ttv);
|
748 |
| - videostreamPreviewTtViewSmall = findViewById(R.id.livestream_preview_ttv_small); |
749 |
| - videostreamPreviewTtView.setVisibility(View.VISIBLE); |
750 | 767 |
|
751 | 768 | deleteApplicationDirectory();
|
752 | 769 | initLogs();
|
@@ -840,6 +857,10 @@ private void notifyStatusChange() {
|
840 | 857 | if (mCodecManager != null) {
|
841 | 858 | // Render on screen
|
842 | 859 | mCodecManager.sendDataToDecoder(videoBuffer, size);
|
| 860 | + if (mPrevVideoBufferSize != size && videostreamPreviewTtView.getSurfaceTexture() != null) { |
| 861 | + mPrevVideoBufferSize = size; |
| 862 | + mSurfaceTextureListener.onSurfaceTextureSizeChanged(videostreamPreviewTtView.getSurfaceTexture(), videostreamPreviewTtView.getWidth(), videostreamPreviewTtView.getHeight()); |
| 863 | + } |
843 | 864 | }
|
844 | 865 |
|
845 | 866 | } else {
|
@@ -953,32 +974,43 @@ private int getVideoMode(Model model) {
|
953 | 974 | private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
|
954 | 975 | @Override
|
955 | 976 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
956 |
| - Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height); |
957 |
| - if (compare_height == 1) { |
958 |
| - height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics())); |
959 |
| - width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics())); |
960 |
| - |
961 |
| - } |
962 |
| - videoViewWidth = width; |
963 |
| - videoViewHeight = height; |
964 |
| - |
965 |
| - Log.d(TAG, "real onSurfaceTextureAvailable: width " + videoViewWidth + " height " + videoViewHeight + " Mode: " + compare_height); |
966 | 977 | if (mCodecManager == null) {
|
967 | 978 | mCodecManager = new DJICodecManager(getApplicationContext(), surface, width, height);
|
968 | 979 | pluginManager.onVideoChange();
|
969 |
| - } else { |
970 |
| - if(useOutputSurface) { |
971 |
| - mCodecManager.changeOutputSurface(surface); |
972 |
| - mCodecManager.onSurfaceSizeChanged(width, height, 0); |
973 |
| - } |
974 | 980 | }
|
| 981 | + |
| 982 | + Log.d(TAG, "real onSurfaceTextureAvailable: width " + width + " height " + height); |
| 983 | + |
| 984 | + onSurfaceTextureSizeChanged(surface, width, height); |
975 | 985 | }
|
976 | 986 |
|
977 | 987 | @Override
|
978 | 988 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
|
979 |
| - videoViewWidth = width; |
980 |
| - videoViewHeight = height; |
981 |
| - Log.d(TAG, "real onSurfaceTextureAvailable2: width " + videoViewWidth + " height " + videoViewHeight); |
| 989 | + double aspectRatio = (double) mCodecManager.getVideoHeight() / mCodecManager.getVideoWidth(); |
| 990 | + int newWidth, newHeight; |
| 991 | + if (height > (int) (width * aspectRatio)) { |
| 992 | + newWidth = width; |
| 993 | + newHeight = (int) (width * aspectRatio); |
| 994 | + } else { |
| 995 | + newWidth = (int) (height / aspectRatio); |
| 996 | + newHeight = height; |
| 997 | + } |
| 998 | + int xOffset = (width - newWidth) / 2; |
| 999 | + int yOffset = (height - newHeight) / 2; |
| 1000 | + |
| 1001 | + Matrix transform = new Matrix(); |
| 1002 | + TextureView textureView = videostreamPreviewTtView; |
| 1003 | + textureView.getTransform(transform); |
| 1004 | + transform.setScale((float) newWidth / width, (float) newHeight / height); |
| 1005 | + transform.postTranslate(xOffset, yOffset); |
| 1006 | + textureView.setTransform(transform); |
| 1007 | + |
| 1008 | + Log.d(TAG, "real onSurfaceTextureAvailable2: width " + width + " height " + height); |
| 1009 | + |
| 1010 | + if (useOutputSurface) { |
| 1011 | + mCodecManager.changeOutputSurface(surface); |
| 1012 | + mCodecManager.onSurfaceSizeChanged(width, height, 0); |
| 1013 | + } |
982 | 1014 | }
|
983 | 1015 |
|
984 | 1016 | @Override
|
@@ -1117,7 +1149,7 @@ private void downloadLogs() {
|
1117 | 1149 | final int BUF_LEN = 2048;
|
1118 | 1150 | byte[] buffer = new byte[BUF_LEN];
|
1119 | 1151 |
|
1120 |
| - String zipName = "RD_LOG_" + android.text.format.DateFormat.format("yyyy-MM-dd-hh:mm:ss", new java.util.Date()); |
| 1152 | + String zipName = "RD_LOG_" + DateFormat.format("yyyy-MM-dd-hh:mm:ss", new Date()); |
1121 | 1153 | String[] fileNames = {"DJI_LOG", "OUTBOUND_LOG", "INBOUND_LOG"};
|
1122 | 1154 |
|
1123 | 1155 | File directory = new File(Environment.getExternalStorageDirectory().getPath()
|
@@ -1283,64 +1315,48 @@ public boolean onMenuItemClick(MenuItem item) {
|
1283 | 1315 | }
|
1284 | 1316 |
|
1285 | 1317 | public void onSmallMapClick(View v) {
|
1286 |
| - |
1287 | 1318 | LinearLayout map_layout = findViewById(R.id.map_view);
|
1288 |
| - FrameLayout video_layout_small = findViewById(R.id.fragment_container_small); |
1289 |
| - ViewGroup.LayoutParams map_para = map_layout.getLayoutParams(); |
1290 |
| - |
1291 |
| - if (compare_height == 0) { |
1292 |
| - logMessageDJI("Set Small screen..."); |
1293 |
| - videostreamPreviewTtView.clearFocus(); |
1294 |
| - videostreamPreviewTtView.setVisibility(View.GONE); |
1295 |
| - |
1296 |
| - // safeSleep(200); |
1297 |
| - videostreamPreviewTtViewSmall.setSurfaceTextureListener(mSurfaceTextureListener); |
1298 |
| - videostreamPreviewTtViewSmall.setVisibility(View.VISIBLE); |
1299 |
| - video_layout_small.setZ(100.f); |
1300 |
| - |
1301 |
| - // MAP ok... |
1302 |
| - map_layout.setZ(0.f); |
1303 |
| - map_para.height = LayoutParams.WRAP_CONTENT; |
1304 |
| - map_para.width = LayoutParams.WRAP_CONTENT; |
1305 |
| - map_layout.setLayoutParams(map_para); |
1306 |
| - |
1307 |
| - changeOutputSurface(videostreamPreviewTtViewSmall); |
1308 |
| - |
1309 |
| - compare_height = 1; |
| 1319 | + FrameLayout video_layout = findViewById(R.id.fragment_container); |
| 1320 | + |
| 1321 | + ViewGroup focusedLayout; |
| 1322 | + ViewGroup previewLayout; |
| 1323 | + switch (focusedView) { |
| 1324 | + case Map: { |
| 1325 | + focusedLayout = map_layout; |
| 1326 | + previewLayout = video_layout; |
| 1327 | + focusedView = FocusedView.VideoFeed; |
| 1328 | + break; |
| 1329 | + } |
| 1330 | + case VideoFeed: |
| 1331 | + default: { |
| 1332 | + focusedLayout = video_layout; |
| 1333 | + previewLayout = map_layout; |
| 1334 | + focusedView = FocusedView.Map; |
| 1335 | + break; |
| 1336 | + } |
| 1337 | + } |
1310 | 1338 |
|
1311 |
| - } else { |
1312 |
| - logMessageDJI("Set Main screen..."); |
1313 |
| - videostreamPreviewTtViewSmall.clearFocus(); |
1314 |
| - videostreamPreviewTtViewSmall.setVisibility(View.GONE); |
| 1339 | + logMessageDJI("Swap the map and the video feed..."); |
1315 | 1340 |
|
1316 |
| - //safeSleep(200); |
1317 |
| - videostreamPreviewTtView.setSurfaceTextureListener(mSurfaceTextureListener); |
1318 |
| - videostreamPreviewTtView.setVisibility(View.VISIBLE); |
1319 |
| - video_layout_small.setZ(0.f); |
| 1341 | + int previewWidth = previewLayout.getWidth(); |
| 1342 | + int previewHeight = previewLayout.getHeight(); |
1320 | 1343 |
|
1321 |
| - // MAP OK... |
1322 |
| - map_layout.setZ(100.f); |
1323 |
| - map_para.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 86, getResources().getDisplayMetrics())); |
1324 |
| - map_para.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 164, getResources().getDisplayMetrics())); |
1325 |
| - map_layout.setLayoutParams(map_para); |
1326 |
| - map_layout.setBottom(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()))); |
1327 |
| - map_layout.setLeft(((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()))); |
| 1344 | + LayoutParams focusedLayoutParams = focusedLayout.getLayoutParams(); |
| 1345 | + focusedLayoutParams.width = previewWidth; |
| 1346 | + focusedLayoutParams.height = previewHeight; |
| 1347 | + focusedLayout.setLayoutParams(focusedLayoutParams); |
| 1348 | + focusedLayout.setZ(100.f); |
1328 | 1349 |
|
1329 |
| - changeOutputSurface(videostreamPreviewTtView); |
| 1350 | + LayoutParams previewLayoutParams = previewLayout.getLayoutParams(); |
| 1351 | + previewLayoutParams.height = LayoutParams.WRAP_CONTENT; |
| 1352 | + previewLayoutParams.width = LayoutParams.WRAP_CONTENT; |
| 1353 | + previewLayout.setLayoutParams(previewLayoutParams); |
| 1354 | + previewLayout.setZ(0.f); |
1330 | 1355 |
|
1331 |
| - compare_height = 0; |
1332 |
| - } |
1333 | 1356 | v.setZ(101.f);
|
1334 | 1357 | //updateDroneLocation();
|
1335 | 1358 | }
|
1336 | 1359 |
|
1337 |
| - void changeOutputSurface(TextureView textureView) { |
1338 |
| - if (mCodecManager != null && useOutputSurface) { |
1339 |
| - mCodecManager.changeOutputSurface(textureView.getSurfaceTexture()); |
1340 |
| - mCodecManager.onSurfaceSizeChanged(textureView.getWidth(), textureView.getHeight(), 0); |
1341 |
| - } |
1342 |
| - } |
1343 |
| - |
1344 | 1360 | // Hmm is this ever called...
|
1345 | 1361 | @Override
|
1346 | 1362 | public boolean onContextItemSelected(MenuItem item) {
|
@@ -1783,7 +1799,7 @@ protected Integer doInBackground(Integer... ints2) {
|
1783 | 1799 | timer.scheduleAtFixedRate(gcsSender, 0, 100);
|
1784 | 1800 |
|
1785 | 1801 | for(MAVLinkConnection mavLinkConnection : mainActivityRef.mMavlinkReceiver.mavLinkConnections) {
|
1786 |
| - Listener listener = new MainActivity.GCSCommunicatorAsyncTask.Listener(mavLinkConnection, mainActivityRef); |
| 1802 | + Listener listener = new Listener(mavLinkConnection, mainActivityRef); |
1787 | 1803 | mavLinkConnection.listen(listener);
|
1788 | 1804 | }
|
1789 | 1805 |
|
|
0 commit comments