DirectionNaviSignalFragment.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package com.tsi.navi.activity.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.view.animation.Animation;
  12. import android.view.animation.AnimationUtils;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15. import androidx.annotation.NonNull;
  16. import androidx.annotation.Nullable;
  17. import androidx.fragment.app.Fragment;
  18. import com.tsi.navi.R;
  19. import com.tsi.navi.TsiNaviApplication;
  20. import com.tsi.navi.activity.model.CpuControl;
  21. import com.tsi.navi.activity.model.SignalItem;
  22. import com.tsi.navi.dto.CpuNodeStatusDTO;
  23. import com.tsi.navi.util.TsiUtils;
  24. import com.tsi.navi.vo.NodeVo;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.HashSet;
  28. import java.util.List;
  29. import java.util.Set;
  30. public class DirectionNaviSignalFragment extends Fragment {
  31. private static final String TAG = DirectionNaviSignalFragment.class.getSimpleName();
  32. private Context context;
  33. private NodeVo node;
  34. private long nodeId;
  35. private CpuControl cpuControl;
  36. private TextView tvCycle;
  37. private ImageView[] imgSignal = new ImageView[4];
  38. private int directionCode;
  39. private boolean first;
  40. private Set<String> directions = new HashSet();
  41. private final int MAX_SIGNAL = 4;
  42. private int[] sigIdx = new int[MAX_SIGNAL];
  43. private int[][] signalId = {
  44. { // RED
  45. R.drawable.ic_sig_off,
  46. R.drawable.ic_sig_red,
  47. },
  48. { // YELLOW
  49. R.drawable.ic_sig_off,
  50. R.drawable.ic_sig_yellow,
  51. },
  52. { // LEFT
  53. R.drawable.ic_sig_left_off,
  54. R.drawable.ic_sig_left_on,
  55. },
  56. { // STRAIGHT
  57. R.drawable.ic_sig_off,
  58. R.drawable.ic_sig_green,
  59. },
  60. };
  61. private int[][] signalIdUn = {
  62. { // none
  63. R.drawable.rect_none, // 데이터 없음.
  64. R.drawable.rect_none, // 데이터 없음.
  65. R.drawable.rect_none, // 데이터 없음.
  66. R.drawable.rect_none, // 데이터 없음.
  67. R.drawable.rect_none, // 데이터 없음.
  68. R.drawable.rect_none, // 데이터 없음.
  69. R.drawable.rect_none, // 데이터 없음.
  70. R.drawable.rect_none, // 데이터 없음.
  71. },
  72. { // 직진
  73. R.drawable.ic_signal_straight_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  74. R.drawable.ic_signal_straight_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  75. R.drawable.ic_signal_straight_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  76. R.drawable.ic_signal_straight_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  77. R.drawable.ic_signal_straight_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  78. R.drawable.ic_signal_straight_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  79. R.drawable.ic_signal_straight_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  80. R.drawable.rect_none, // 데이터 없음.
  81. },
  82. { // 좌회전
  83. R.drawable.ic_signal_left_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  84. R.drawable.ic_signal_left_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  85. R.drawable.ic_signal_left_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  86. R.drawable.ic_signal_left_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  87. R.drawable.ic_signal_left_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  88. R.drawable.ic_signal_left_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  89. R.drawable.ic_signal_left_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  90. R.drawable.rect_none, // 데이터 없음.
  91. },
  92. { // 보행자
  93. R.drawable.ic_signal_walk_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  94. R.drawable.ic_signal_walk_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  95. R.drawable.ic_signal_walk_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  96. R.drawable.ic_signal_walk_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  97. R.drawable.ic_signal_walk_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  98. R.drawable.ic_signal_walk_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  99. R.drawable.ic_signal_walk_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  100. R.drawable.rect_none, // 데이터 없음.
  101. },
  102. { // 자전거
  103. R.drawable.ic_signal_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  104. R.drawable.ic_signal_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  105. R.drawable.ic_signal_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  106. R.drawable.ic_signal_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  107. R.drawable.ic_signal_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  108. R.drawable.ic_signal_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  109. R.drawable.ic_signal_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  110. R.drawable.rect_none, // 데이터 없음.
  111. },
  112. { // 우회전
  113. R.drawable.ic_signal_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  114. R.drawable.ic_signal_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  115. R.drawable.ic_signal_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  116. R.drawable.ic_signal_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  117. R.drawable.ic_signal_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  118. R.drawable.ic_signal_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  119. R.drawable.ic_signal_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  120. R.drawable.rect_none, // 데이터 없음.
  121. },
  122. { // 버스
  123. R.drawable.ic_signal_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  124. R.drawable.ic_signal_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  125. R.drawable.ic_signal_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  126. R.drawable.ic_signal_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  127. R.drawable.ic_signal_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  128. R.drawable.ic_signal_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  129. R.drawable.ic_signal_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  130. R.drawable.rect_none, // 데이터 없음.
  131. },
  132. { // 유턴
  133. R.drawable.ic_signal_uturn_off_un, // LIGHTS_OUT (0, "LIGHTS_OUT"), //소등
  134. R.drawable.ic_signal_uturn_red_un, // RED_SOLID (1, "RED_SOLID"), //적색점등
  135. R.drawable.ic_signal_uturn_yellow_un, // YELLOW_SOLID (2, "YELLOW_SOLID"), //황색점등
  136. R.drawable.ic_signal_uturn_green_un, // GREEN_SOLID (3, "GREEN_SOLID"), //녹색점등
  137. R.drawable.ic_signal_uturn_red_un, // RED_BLINK (4, "RED_BLINK"), //적색점멸
  138. R.drawable.ic_signal_uturn_yellow_un, // YELLOW_BLINK (5, "YELLOW_BLINK"), //황색점멸
  139. R.drawable.ic_signal_uturn_green_un, // GREEN_BLINK (6, "GREEN_BLINK"); //녹색점멸
  140. R.drawable.rect_none, // 데이터 없음.
  141. },
  142. };
  143. private SignalItem signalCurr = null;
  144. private SignalItem signalPrev = null;
  145. // 소등(0), 적색점등(1), 황색점등(2), 녹색점등(3), 적색점멸(4), 황색점멸(5), 녹색점멸(6)
  146. // 미지정(0), 직진(1), 좌회전(2), 보행자(3), 자전거(4), 우회전(5), 버스(6), 유턴(7)
  147. @Override
  148. public void onAttach(Context context) {
  149. super.onAttach(context);
  150. this.context = context;
  151. }
  152. @Override
  153. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  154. View v = inflater.inflate(R.layout.fragment_direction_navi_signal, container, false);
  155. this.tvCycle = v.findViewById(R.id.tv_cycle);
  156. this.tvCycle.setOnClickListener(view -> {
  157. if (this.directions.size() == 0) {
  158. return;
  159. }
  160. Log.e("Direction", this.directions.toString());
  161. List<String> directionList = new ArrayList(this.directions);
  162. Collections.sort(directionList);
  163. String[] dirs = new String[directionList.size()];
  164. for(int ii = 0; ii < directionList.size(); ii++) {
  165. dirs[ii] = TsiNaviApplication.getDirection().getName(Integer.parseInt(directionList.get(ii)));
  166. }
  167. Log.e("Direction", dirs.toString());
  168. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  169. builder.setTitle("방향코드 선택");
  170. builder.setItems(dirs, (dialog, which) -> {
  171. int selDirection = TsiNaviApplication.getDirection().getNameCode(dirs[which]);
  172. if (selDirection != directionCode) {
  173. initStatus();
  174. setDirectionCode(selDirection);
  175. }
  176. }
  177. );
  178. builder.show();
  179. });
  180. this.imgSignal[0] = (ImageView) v.findViewById(R.id.img_red);
  181. this.imgSignal[1] = (ImageView) v.findViewById(R.id.img_yellow);
  182. this.imgSignal[2] = (ImageView) v.findViewById(R.id.img_left);
  183. this.imgSignal[3] = (ImageView) v.findViewById(R.id.img_straight);
  184. this.first = false;
  185. this.directionCode = 0;
  186. if (this.signalCurr == null) {
  187. this.signalCurr = new SignalItem();
  188. }
  189. if (this.signalPrev == null) {
  190. this.signalPrev = new SignalItem();
  191. }
  192. this.cpuControl = new CpuControl();
  193. this.nodeId = 0;
  194. init(0);
  195. return v;
  196. }
  197. protected void setDirectionCode(int directionCode) {
  198. this.directionCode = directionCode;
  199. String name = TsiNaviApplication.getDirection().getName(this.directionCode);
  200. this.signalCurr.setDirection(this.directionCode);
  201. this.signalPrev.setDirection(this.directionCode);
  202. }
  203. protected void init(int directionCode) {
  204. if (this.node != null) {
  205. this.nodeId = this.node.getNodeId();
  206. }
  207. else {
  208. this.nodeId = 0;
  209. }
  210. initStatus();
  211. setDirectionCode(directionCode);
  212. }
  213. @SuppressLint("ObsoleteSdkInt")
  214. public void initStatus() {
  215. this.cpuControl.init();
  216. for (int ii = 0; ii < this.MAX_SIGNAL; ii++) {
  217. if (this.imgSignal[ii] != null) {
  218. if (this.imgSignal[ii].getAnimation() != null) {
  219. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  220. this.imgSignal[ii].getAnimation().cancel();
  221. }
  222. this.imgSignal[ii].clearAnimation();
  223. this.imgSignal[ii].setAnimation(null);
  224. }
  225. this.imgSignal[ii].setImageResource(signalId[ii][0]);
  226. }
  227. }
  228. this.signalCurr.initStatus();
  229. this.signalPrev.initStatus();
  230. }
  231. public long getNodeId() {
  232. return this.nodeId;
  233. }
  234. public void setNodeCross(NodeVo node, int directionCode) {
  235. this.first = true;
  236. this.node = node;
  237. init(directionCode);
  238. }
  239. @SuppressLint("ObsoleteSdkInt")
  240. public void updateSignal(CpuNodeStatusDTO status) {
  241. if (this.node == null || this.nodeId != status.getA()) {
  242. return;
  243. }
  244. if (this.first) {
  245. this.directions.clear();
  246. int directionCode = this.directionCode;
  247. for (int ii = 0; ii < status.getG().size(); ii++) {
  248. this.directions.add(String.valueOf(status.getG().get(ii).getH()));
  249. if (ii == 0) {
  250. directionCode = status.getG().get(ii).getH();
  251. }
  252. if (this.directionCode == status.getG().get(ii).getH()) {
  253. directionCode = this.directionCode;
  254. }
  255. }
  256. // 프로그램에서 설정한 방향코드가 수집한 상태정보의 방향코드에 존재하지 않으면 첫번째 방향코드로 변경한다.
  257. if (this.directionCode != directionCode) {
  258. setDirectionCode(directionCode);
  259. }
  260. this.first = false;
  261. }
  262. this.cpuControl.updateStatus(status.getD());
  263. // 방향코드에 맞는 데이터 표출
  264. for (int ii = 0; ii < status.getG().size(); ii++) {
  265. if (status.getG().get(ii).getH() == this.directionCode) {
  266. this.signalCurr.setStatus(status.getG().get(ii));
  267. }
  268. }
  269. int lightStatus;
  270. int remainSeconds;
  271. for (int ii = 0; ii < this.MAX_SIGNAL; ii++) {
  272. this.sigIdx[ii] = 0;
  273. }
  274. boolean isBlink = false;
  275. remainSeconds = this.signalCurr.getRemainTime(TsiUtils.SIGNAL_STRAIGHT);
  276. lightStatus = this.signalCurr.getLightStatus(TsiUtils.SIGNAL_STRAIGHT);
  277. switch (lightStatus) {
  278. case 0: // light out
  279. break;
  280. case 1: // red solid
  281. this.sigIdx[0] = 1;
  282. break;
  283. case 4: // red blink
  284. this.sigIdx[0] = 1;
  285. isBlink = true;
  286. break;
  287. case 2: // yellow solid
  288. this.sigIdx[1] = 1;
  289. break;
  290. case 5: // yellow blink
  291. this.sigIdx[1] = 1;
  292. isBlink = true;
  293. break;
  294. case 3: // green solid
  295. this.sigIdx[3] = 1;
  296. break;
  297. case 6: // green blink
  298. this.sigIdx[3] = 1;
  299. isBlink = true;
  300. break;
  301. }
  302. lightStatus = this.signalCurr.getLightStatus(TsiUtils.SIGNAL_LEFT_TURN);
  303. switch (lightStatus) {
  304. case 0: // light out
  305. break;
  306. case 1: // red solid
  307. this.sigIdx[0] = 1;
  308. break;
  309. case 4: // red blink
  310. this.sigIdx[0] = 1;
  311. isBlink = true;
  312. break;
  313. case 2: // yellow solid
  314. this.sigIdx[1] = 1;
  315. break;
  316. case 5: // yellow blink
  317. this.sigIdx[1] = 1;
  318. isBlink = true;
  319. break;
  320. case 3: // green solid
  321. this.sigIdx[2] = 1;
  322. break;
  323. case 6: // green blink
  324. this.sigIdx[2] = 1;
  325. isBlink = true;
  326. break;
  327. }
  328. if (lightStatus == 2 || lightStatus == 5) {
  329. // YELLOW 일때
  330. if (this.sigIdx[0] == 1) {
  331. remainSeconds = this.signalCurr.getRemainTime(TsiUtils.SIGNAL_LEFT_TURN);
  332. }
  333. }
  334. if (this.sigIdx[2] == 1 && this.sigIdx[0] == 1) {
  335. // 좌회전, 레드가 모구 켜져있으면 좌회전 신호 표출
  336. this.sigIdx[0] = 0;
  337. remainSeconds = this.signalCurr.getRemainTime(TsiUtils.SIGNAL_LEFT_TURN);
  338. }
  339. if (this.sigIdx[1] == 1 && this.sigIdx[0] == 1) {
  340. // 레드, 옐로우 켜져 있으면 옐로우 신호 표출
  341. this.sigIdx[0] = 0;
  342. }
  343. if (remainSeconds < 0) remainSeconds = 255;
  344. this.tvCycle.setText(" " +String.valueOf(remainSeconds));
  345. for (int ii = 0; ii < this.MAX_SIGNAL; ii++) {
  346. if (this.imgSignal[ii].getAnimation() != null) {
  347. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  348. this.imgSignal[ii].getAnimation().cancel();
  349. }
  350. this.imgSignal[ii].clearAnimation();
  351. this.imgSignal[ii].setAnimation(null);
  352. }
  353. this.imgSignal[ii].setImageResource(signalId[ii][this.sigIdx[ii]]);
  354. if (isBlink && this.sigIdx[ii] == 1) {
  355. Animation animation = AnimationUtils.loadAnimation(this.context, R.anim.blink);
  356. this.imgSignal[ii].startAnimation(animation);
  357. }
  358. }
  359. }
  360. }