-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUrho3D.java
681 lines (565 loc) · 21.3 KB
/
Urho3D.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//
// Copyright (c) 2008-2018 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
package com.github.urho3d;
import android.content.Intent;
import android.content.res.AssetManager;
import android.util.Log;
import org.libsdl.app.SDLActivity;
import org.libsdl.app.SDLSurface;
import java.io.IOException;
import java.util.*;
import android.os.*;
import android.widget.RelativeLayout;
import android.widget.LinearLayout;
import android.content.IntentFilter;
import android.content.Context;
import android.view.View;
import android.widget.Toast;
import android.view.KeyEvent;
import java.util.Date;
import android.view.*;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.*;
import com.amap.api.navi.enums.AMapNaviOnlineCarHailingType;
import com.amap.api.navi.model.AMapCarInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.AimlessModeListener;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapExitDirectionInfo;
import android.location.Location;
import com.amap.api.navi.enums.AimLessMode;
import java.time.LocalTime;
import com.amap.api.maps.AMap;
public class Urho3D extends SDLActivity implements AMapNaviViewListener, AMapNaviListener, AMap.OnMapLongClickListener, AimlessModeListener {
static final String SCRIPTS = "scripts";
static final String PICKED_SCRIPT = "pickedScript";
private static final String TAG = "Urho3D";
private static final int OBTAINING_SCRIPT = 1;
private static String[] mArguments = new String[0];
protected AMapNaviView mAMapNaviView;
protected AMapNavi mAMapNavi;
protected AMap mAMap;
private static final int SET_IP = 2;
private static final int NAVI_INIT = 4;
private static final int NAVI_ROUTE_NOTIFY = 5;
private static final int NAVI_ARRIVED = 6;
private static final int NAVI_TEXT = 7;
private static final int NAVI_INFO = 8;
private static final int NAVI_MAP_TYPE = 9;
private static final int NAVI_CAMERA_INFO = 10;
private static final int NAVI_INFO2 = 11;
private static final int NAVI_FACILITY = 12;
private boolean init_called = false;
public static final int RUN_NAVI = 0;
public static final int RUN_SIM = 1;
public static final int RUN_MAP = 2;
public static int run_type = RUN_NAVI;
private static final int FROM_NATIVE_GPS = 100;
public static String IP_ADDRESS;
@Override
protected String[] getArguments() {
return mArguments;
}
@Override
protected boolean onLoadLibrary(ArrayList<String> libraryNames) {
// Ensure "Urho3D" shared library (if any) and "Urho3DPlayer" are being sorted to the top of the list
// Also ensure STL runtime shared library (if any) is sorted to the top most entry
Collections.sort(libraryNames, new Comparator<String>() {
private String sortName(String name) {
return name.matches("^\\d+_.+$") ? name : (name.matches("^.+_shared$") ? "0000_" : "000_") + name;
}
@Override
public int compare(String lhs, String rhs) {
return sortName(lhs).compareTo(sortName(rhs));
}
});
// All shared shared libraries must always be loaded if available, so exclude it from return result and all list operations below
int startIndex = libraryNames.indexOf("Game");
// Determine the intention
Intent intent = getIntent();
String pickedLibrary = intent.getStringExtra(SampleLauncher.PICKED_LIBRARY);
if (pickedLibrary == null) {
// Intention for obtaining library names
String[] array = libraryNames.subList(startIndex, libraryNames.size()).toArray(new String[libraryNames.size() - startIndex]);
if (array.length > 1) {
setResult(RESULT_OK, intent.putExtra(SampleLauncher.LIBRARY_NAMES, array));
// End Urho3D activity lifecycle
finish();
// Return false to indicate no library is being loaded yet
return false;
} else {
// There is only one library available, so cancel the intention for obtaining the library name and by not returning any result
// However, since we have already started Urho3D activity, let's the activity runs its whole lifecycle by falling through to call the super implementation
setResult(RESULT_CANCELED);
}
} else {
// Intention for loading a picked library name (and remove all others)
libraryNames.subList(startIndex, libraryNames.size()).clear();
mArguments = pickedLibrary.split(":");
libraryNames.add(mArguments[0]);
if ("Urho3DPlayer".equals(mArguments[0]) && mArguments.length == 1) {
// Urho3DPlayer needs a script name to play
try {
final AssetManager assetManager = getAssets();
HashMap<String, ArrayList<String>> scripts = new HashMap<String, ArrayList<String>>(2) {{
put("AngelScript", new ArrayList<>(Arrays.asList(assetManager.list("Data/Scripts"))));
put("Lua", new ArrayList<>(Arrays.asList(assetManager.list("Data/LuaScripts"))));
}};
startActivityForResult(new Intent(this, ScriptPicker.class).putExtra(SCRIPTS, scripts), OBTAINING_SCRIPT);
} catch (IOException e) {
Log.e(TAG, "Could not scan assets directory for playable scripts", e);
}
}
}
return super.onLoadLibrary(libraryNames);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate !!!");
super.onCreate(savedInstanceState);
mAMapNavi.addAMapNaviListener(this);
mAMapNaviView.onCreate(savedInstanceState);
mAMapNaviView.setAMapNaviViewListener(this);
mAMap = mAMapNaviView.getMap();
mAMap.setOnMapLongClickListener(this);
if (run_type == RUN_SIM) {
Log.i(TAG, "run navi simulation");
mAMapNavi.startNavi(NaviType.EMULATOR);
mAMapNavi.setEmulatorNaviSpeed(100);
} else if (run_type == RUN_NAVI) {
Log.i(TAG, "run navi GPS");
mAMapNavi.startNavi(NaviType.GPS);
}
else if (run_type == RUN_MAP) {
Log.i(TAG, "startAimlessMode");
mAMapNavi.startAimlessMode(AimLessMode.CAMERA_AND_SPECIALROAD_DETECTED);
mAMapNavi.addAimlessModeListener(this);
}
mAMapNaviView.setTrafficLightsVisible(true);
mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE);
LocalTime time = LocalTime.now();
if (time.getHour() >= 17 || time.getHour() <= 5)
{
mAMap.setMapType(AMap.MAP_TYPE_NIGHT);
}
//
init_called = false;
// Log.i(TAG, "WTF !!!");
SDLActivity.onNativeMessage(NAVI_INIT, (double)mAMapNavi.getNaviType(), 0, 0, "Init NAVI");
SDLActivity.onNativeMessage(SET_IP, 0, 0, 0, IP_ADDRESS);
}
@Override
protected void onInitLayout() {
setContentView(R.layout.activity_basic_navi);
mSurface = findViewById(R.id.sdl_view);
mAMapNavi = AMapNavi.getInstance(getApplicationContext());
mAMapNaviView = findViewById(R.id.navi_view);
mLayout = findViewById(R.id.basic_navi);
Log.i(TAG, "layout init OK");
}
@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy !!!");
// mAMapNaviView.onDestroy();
mAMapNaviView.setAMapNaviViewListener(null);
Log.i(TAG, "onDestroy 111 !!!");
mAMapNavi.stopNavi();
mAMapNavi.stopAimlessMode();
// mAMapNavi.destroy();
Log.i(TAG, "onDestroy 222 !!!");
super.onDestroy();
}
@Override
protected void onStop() {
Log.i(TAG, "onStop !!!");
super.onStop();
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (OBTAINING_SCRIPT != requestCode || RESULT_CANCELED == resultCode)
return;
String script = data.getStringExtra(PICKED_SCRIPT);
script = (script.endsWith(".as") ? "Scripts/" : "LuaScripts/") + script;
mArguments = new String[]{mArguments[0], script};
}
@Override
protected void onResume() {
Log.i(TAG, "onResume !!!");
super.onResume();
mAMapNaviView.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "onPause !!!");
super.onPause();
mAMapNaviView.onPause();
}
@Override
public void onInitNaviFailure() {
}
@Override
public void onInitNaviSuccess() {
}
@Override
public void onStartNavi(int type) {
//开始导航回调
SDLActivity.onNativeMessage(0, (double) type, 0, 0, "");
}
@Override
public void onTrafficStatusUpdate() {
//
}
@Override
public void onLocationChange(AMapNaviLocation location) {
//当前位置回调
}
@Override
public void onGetNavigationText(int type, String text) {
//播报类型和播报文字回调
SDLActivity.onNativeMessage(NAVI_TEXT, 0, 0, 0, text);
}
@Override
public void onGetNavigationText(String s) {
SDLActivity.onNativeMessage(NAVI_TEXT, 0, 0, 0, s);
}
@Override
public void onEndEmulatorNavi() {
//结束模拟导航
}
@Override
public void onArriveDestination() {
//到达目的地
SDLActivity.onNativeMessage(NAVI_ARRIVED, 0, 0, 0, "");
}
@Override
public void onCalculateRouteFailure(int errorInfo) {
//路线计算失败
//Log.e("dm", "--------------------------------------------");
//Log.i("dm", "路线计算失败:错误码=" + errorInfo + ",Error Message= " + ErrorInfo.getError(errorInfo));
//Log.i("dm", "错误码详细链接见:http://lbs.amap.com/api/android-navi-sdk/guide/tools/errorcode/");
//Log.e("dm", "--------------------------------------------");
//Toast.makeText(this, "errorInfo:" + errorInfo + ",Message:" + ErrorInfo.getError(errorInfo), Toast.LENGTH_LONG).show();
}
@Override
public void onReCalculateRouteForYaw() {
//偏航后重新计算路线回调
}
@Override
public void onReCalculateRouteForTrafficJam() {
//拥堵后重新计算路线回调
}
@Override
public void onArrivedWayPoint(int wayID) {
//到达途径点
}
@Override
public void onGpsOpenStatus(boolean enabled) {
//GPS开关状态回调
}
@Override
public void onNaviSetting() {
//底部导航设置点击回调
}
@Override
public void onNaviMapMode(int naviMode) {
//导航态车头模式,0:车头朝上状态;1:正北朝上模式。
}
@Override
public void onNaviCancel() {
finish();
}
@Override
public void onNaviTurnClick() {
//转弯view的点击回调
}
@Override
public void onNextRoadClick() {
//下一个道路View点击回调
}
@Override
public void onScanViewButtonClick() {
//全览按钮点击回调
}
@Override
public void updateCameraInfo(AMapNaviCameraInfo[] aMapCameraInfos) {
try {
for (int i = 0; i < aMapCameraInfos.length; ++i) {
SDLActivity.onNativeMessage(NAVI_CAMERA_INFO,
(double) aMapCameraInfos[i].getCameraType(),
(double) aMapCameraInfos[i].getCameraSpeed(),
(double) aMapCameraInfos[i].getCameraDistance(),
"");
}
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
}
@Override
public void onServiceAreaUpdate(AMapServiceAreaInfo[] amapServiceAreaInfos) {
}
@Override
public void onNaviInfoUpdate(NaviInfo info) {
if (!init_called) {
init_called = true;
SDLActivity.onNativeMessage(NAVI_INIT, (double) mAMapNavi.getNaviType(), 0, 0, "Init NAVI");
SDLActivity.onNativeMessage(NAVI_MAP_TYPE, (double) mAMapNaviView.getMap().getMapType(), 0, 0, "");
}
//导航过程中的信息更新,请看NaviInfo的具体说明
String info_text = "";
try {
AMapExitDirectionInfo exit = info.getExitDirectionInfo();
if (exit != null && exit.getExitNameInfo().length > 0) {
info_text = exit.getExitNameInfo()[0];
}
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
SDLActivity.onNativeMessage(NAVI_INFO,
(double) info.getCurStepRetainDistance(),
(double) info.getCurStepRetainTime(),
(double) info.getIconType(),
info_text);
SDLActivity.onNativeMessage(NAVI_INFO2,
(double) info.getPathRetainDistance(),
(double) info.getPathRetainTime(),
(double) info.getCurStep(),
info.getCurrentRoadName());
// Log.d(TAG, info.getCurrentRoadName() + " retain_dist:" + info.getCurStepRetainDistance());
//
// AMapExitDirectionInfo exit = info.getExitDirectionInfo();
// for (int i=0; i<exit.getDirectionInfo().length; ++i)
// {
// Log.d(TAG, "exit dir: " + exit.getDirectionInfo()[i]);
// }
//
// for (int i=0; i<exit.getExitNameInfo().length; ++i)
//
// Log.d(TAG, "exit name: " + exit.getExitNameInfo()[i]);
// }
}
@Override
public void showCross(AMapNaviCross aMapNaviCross) {
//显示转弯回调
}
@Override
public void hideCross() {
//隐藏转弯回调
}
@Override
public void showLaneInfo(AMapLaneInfo[] laneInfos, byte[] laneBackgroundInfo, byte[] laneRecommendedInfo) {
//显示车道信息
}
@Override
public void hideLaneInfo() {
//隐藏车道信息
}
@Override
public void onCalculateRouteSuccess(int[] ints) {
//多路径算路成功回调
SDLActivity.onNativeMessage(2, 0, 0, 0, "onCalculateRouteSuccess");
}
@Override
public void notifyParallelRoad(int i) {
}
@Override
public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
//更新巡航模式的统计信息
}
@Override
public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
//更新巡航模式的拥堵信息
}
@Override
public void onPlayRing(int i) {
}
@Override
public void onLockMap(boolean isLock) {
//锁地图状态发生变化时回调
}
@Override
public void onNaviViewLoaded() {
Log.d(TAG, "导航页面加载成功");
Log.d(TAG, "请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");
}
@Override
public void onMapTypeChanged(int i) {
SDLActivity.onNativeMessage(NAVI_MAP_TYPE, (double) i, 0, 0, "");
}
@Override
public void onNaviViewShowMode(int i) {
}
@Override
public boolean onNaviBackClick() {
return false;
}
@Override
public void showModeCross(AMapModelCross aMapModelCross) {
}
@Override
public void hideModeCross() {
}
@Override
public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {
}
@Override
public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {
}
@Override
public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {
}
@Override
public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {
}
@Override
public void onNaviRouteNotify(AMapNaviRouteNotifyData aData) {
Log.i(TAG, "onNaviRouteNotify" + aData.getReason());
SDLActivity.onNativeMessage(NAVI_ROUTE_NOTIFY,
(double) aData.getDistance(),
aData.getLatitude(),
aData.getLongitude(),
aData.getReason());
}
@Override
protected void onMessage2(int cmd, double data1, double data2, double data3, double data4, double data5)
{
Log.i(TAG, "onMessage2 " + cmd + " " + data1 + " " + data2 + " " + data3 + " " + data4 + " " + data5);
if (cmd == FROM_NATIVE_GPS)
{
Location location = new Location("GPS");
location.setLongitude(data1);
location.setLatitude(data2);
location.setSpeed((float)data3);
location.setAccuracy((float)data4);
location.setBearing((float)data5);
location.setTime(System.currentTimeMillis());
//以上6项数据缺一不可
Log.i(TAG, "setExtraGPSData");
mAMapNavi.setExtraGPSData(1,location);
//type字段传1时代表WGS84坐标;
}
}
//@Override
public void onGpsSignalWeak(boolean var1) {
}
@Override
public void onMapLongClick(LatLng var1)
{
Log.i(TAG, "onMapLongClick!!!");
int map_type = mAMap.getMapType();
if (map_type == AMap.MAP_TYPE_SATELLITE)
{
mAMap.setMapType(AMap.MAP_TYPE_NORMAL);
LocalTime time = LocalTime.now();
if (time.getHour() >= 17 || time.getHour() <= 5)
{
mAMap.setMapType(AMap.MAP_TYPE_NIGHT);
}
}
else
{
mAMap.setMapType(AMap.MAP_TYPE_SATELLITE);
}
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo var1) {
try {
SDLActivity.onNativeMessage(NAVI_FACILITY,
(double) var1.distance,
(double) var1.type,
(double) var1.limitSpeed,
"");
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
}
@Override
public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] var1) {
try {
for (int i = 0; i < var1.length; ++i) {
SDLActivity.onNativeMessage(NAVI_FACILITY,
(double) var1[i].distance,
(double) var1[i].type,
(double) var1[i].limitSpeed,
"");
}
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
}
@Override
public void onUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] var1)
{
try {
for (int i = 0; i < var1.length; ++i) {
SDLActivity.onNativeMessage(NAVI_FACILITY,
(double) var1[i].distance,
(double) var1[i].type,
(double) var1[i].limitSpeed,
"");
}
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
}
@Override
public void onUpdateAimlessModeElecCameraInfo(AMapNaviTrafficFacilityInfo[] var1)
{
try {
for (int i = 0; i < var1.length; ++i) {
SDLActivity.onNativeMessage(NAVI_FACILITY,
(double) var1[i].distance,
(double) var1[i].type,
(double) var1[i].limitSpeed,
"");
}
}
catch(Exception e) {
Log.w(TAG, e.getMessage());
}
}
}