MainActivity.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.ht.gate;
  2. import androidx.annotation.NonNull;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.core.content.ContextCompat;
  5. import androidx.recyclerview.widget.LinearLayoutManager;
  6. import androidx.recyclerview.widget.RecyclerView;
  7. import android.animation.ValueAnimator;
  8. import android.content.ComponentName;
  9. import android.content.Intent;
  10. import android.content.ServiceConnection;
  11. import android.media.AudioAttributes;
  12. import android.media.AudioManager;
  13. import android.media.SoundPool;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.os.IBinder;
  17. import android.os.Message;
  18. import android.util.Log;
  19. import android.util.TypedValue;
  20. import android.view.MotionEvent;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.view.animation.Animation;
  24. import android.view.animation.LinearInterpolator;
  25. import android.view.animation.RotateAnimation;
  26. import android.widget.ImageView;
  27. import android.widget.RelativeLayout;
  28. import android.widget.TextView;
  29. import com.ht.gate.domain.Meeting;
  30. import com.ht.gate.domain.RoomMeetings;
  31. import com.ht.gate.domain.emp.EmpInfo;
  32. import org.greenrobot.eventbus.EventBus;
  33. import org.greenrobot.eventbus.Subscribe;
  34. import org.greenrobot.eventbus.ThreadMode;
  35. import java.io.IOException;
  36. import java.text.ParseException;
  37. import java.util.ArrayList;
  38. import java.util.Date;
  39. import java.util.List;
  40. import butterknife.BindView;
  41. import butterknife.ButterKnife;
  42. import butterknife.OnClick;
  43. import es.dmoral.toasty.Toasty;
  44. import retrofit2.Call;
  45. import retrofit2.Callback;
  46. import retrofit2.Response;
  47. public class MainActivity extends AppCompatActivity {
  48. private static final String TAG = "MainActivity";
  49. private static final int HIDE_BAR = 10000;
  50. @BindView(R.id.unlock_view)
  51. UnlockView unlockView;
  52. @BindView(R.id.tv_room_name)
  53. TextView tvRoomName;
  54. @BindView(R.id.tv_room_num)
  55. TextView tvRoomNum;
  56. @BindView(R.id.rl_meetings)
  57. RelativeLayout rlMeetings;
  58. @BindView(R.id.rv_meetings)
  59. RecyclerView rvMeetings;
  60. @BindView(R.id.iv_arrow)
  61. ImageView ivArrow;
  62. @BindView(R.id.tv_net_err)
  63. TextView tvNetErr;
  64. private MyLinearLayoutManager layoutManager;
  65. private MeetingAdapter adapter;
  66. private DoorService.DoorBinder mDoorBinder;
  67. private Handler handler = new Handler(msg -> {
  68. switch (msg.what) {
  69. case HIDE_BAR:
  70. View decorView = getWindow().getDecorView();
  71. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  72. | View.SYSTEM_UI_FLAG_FULLSCREEN
  73. | View.SYSTEM_UI_FLAG_IMMERSIVE;
  74. decorView.setSystemUiVisibility(uiOptions);
  75. }
  76. return true;
  77. });
  78. private ServiceConnection connection = new ServiceConnection() {
  79. @Override
  80. public void onServiceConnected(ComponentName name, IBinder service) {
  81. Log.d(TAG, "onServiceConnected");
  82. mDoorBinder = (DoorService.DoorBinder) service;
  83. updateStatus();
  84. }
  85. @Override
  86. public void onServiceDisconnected(ComponentName name) {
  87. Log.d(TAG, "onServiceDisconnected");
  88. mDoorBinder = null;
  89. }
  90. };
  91. @Override
  92. protected void onCreate(Bundle savedInstanceState) {
  93. super.onCreate(savedInstanceState);
  94. setContentView(R.layout.activity_main);
  95. ButterKnife.bind(this);
  96. unlockView.setUnlockListener(() -> {
  97. PasswordDialog passwordDialog = new PasswordDialog(MainActivity.this);
  98. passwordDialog.setListener(new PasswordDialog.DialogListener() {
  99. @Override
  100. public void onCancel(PasswordDialog dialog) {
  101. dialog.dismiss();
  102. }
  103. @Override
  104. public void onConfirm(PasswordDialog dialog, String password) {
  105. dialog.dismiss();
  106. if (MyApplication.roomInfo != null && MyApplication.roomInfo.getDevicePassword().equals(password)) {
  107. if (mDoorBinder != null) {
  108. mDoorBinder.getService().openDoor();
  109. }
  110. // Toasty.normal(MainActivity.this, "已解锁").show();
  111. // SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
  112. } else {
  113. Toasty.error(MainActivity.this, "密码错误").show();
  114. SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_FAIL);
  115. }
  116. Message msg = new Message();
  117. msg.what = HIDE_BAR;
  118. handler.sendMessageDelayed(msg, 500);
  119. }
  120. });
  121. passwordDialog.show();
  122. });
  123. bindService(new Intent(this, DoorService.class), connection, BIND_AUTO_CREATE);
  124. adapter = new MeetingAdapter(this);
  125. layoutManager = new MyLinearLayoutManager(this);
  126. layoutManager.setCanScroll(false);
  127. rvMeetings.setLayoutManager(layoutManager);
  128. rvMeetings.setAdapter(adapter);
  129. ((MyApplication) getApplication()).updateMeetings();
  130. }
  131. @OnClick(R.id.btn_settings)
  132. void onClickSettings() {
  133. startActivity(new Intent(this, SettingsActivity.class));
  134. }
  135. @OnClick(R.id.iv_arrow)
  136. void clickArrow() {
  137. ViewGroup.LayoutParams layoutParams = rlMeetings.getLayoutParams();
  138. int dp36 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, getResources().getDisplayMetrics());
  139. int dp108 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 126, getResources().getDisplayMetrics());
  140. int height = layoutParams.height;
  141. ValueAnimator anim = ValueAnimator.ofInt(height, height < dp108 ? dp108 : dp36);
  142. anim.addUpdateListener(valueAnimator -> {
  143. int val = (Integer) valueAnimator.getAnimatedValue();
  144. ViewGroup.LayoutParams layoutParams1 = rlMeetings.getLayoutParams();
  145. layoutParams1.height = val;
  146. rlMeetings.setLayoutParams(layoutParams1);
  147. });
  148. anim.setDuration(200);
  149. anim.start();
  150. layoutManager.setCanScroll(height < dp108);
  151. if (height >= dp36) {
  152. rvMeetings.scrollToPosition(0);
  153. }
  154. Animation animation = new RotateAnimation(height < dp108 ? 0 : 180, height < dp108 ? 180 : 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  155. animation.setFillAfter(true);
  156. animation.setDuration(200);
  157. animation.setInterpolator(new LinearInterpolator());
  158. animation.setRepeatCount(0);
  159. ivArrow.startAnimation(animation);
  160. }
  161. @Subscribe(threadMode = ThreadMode.MAIN)
  162. public void onMessageEvent(String msg) {
  163. if (Constants.EVENT_MEETINGS.equals(msg)) {
  164. updateMeetings();
  165. } else if (Constants.EVENT_NET_STATUS.equals(msg)) {
  166. tvNetErr.setVisibility(MyApplication.netAvailable ? View.GONE : View.VISIBLE);
  167. } else {
  168. updateStatus();
  169. }
  170. }
  171. private void updateMeetings() {
  172. if (adapter.getItemCount() <= 1) {
  173. ivArrow.setVisibility(View.GONE);
  174. } else {
  175. ivArrow.setVisibility(View.VISIBLE);
  176. }
  177. if (MyApplication.todayMeetings.isEmpty()) {
  178. rlMeetings.setVisibility(View.GONE);
  179. } else {
  180. rlMeetings.setVisibility(View.VISIBLE);
  181. }
  182. adapter.notifyDataSetChanged();
  183. for (Meeting meeting : MyApplication.todayMeetings) {
  184. try {
  185. Date start = Utils.parseDateTime(meeting.getBeginTimeStr());
  186. start.setTime(start.getTime() - 10 * 60 * 1000);
  187. Date end = Utils.parseDateTime(meeting.getEndTimeStr());
  188. Date date = new Date();
  189. if ((date.equals(start) || date.after(start)) && (date.equals(end) || date.before(end))) {
  190. RetrofitManager.getInstance().getRetrofit().create(ApiService.class)
  191. .getEmpInfo(meeting.getCreateUser().getUserId()).enqueue(new Callback<EmpInfo>() {
  192. @Override
  193. public void onResponse(@NonNull Call<EmpInfo> call, @NonNull Response<EmpInfo> response) {
  194. if (response.code() == 200) {
  195. WelcomeActivity.start(MainActivity.this, meeting, response.body());
  196. } else {
  197. try {
  198. Toasty.error(MainActivity.this, response.errorBody().string()).show();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. }
  204. @Override
  205. public void onFailure(@NonNull Call<EmpInfo> call, @NonNull Throwable t) {
  206. Toasty.error(MainActivity.this, "投顾信息获取失败,请稍后再试").show();
  207. }
  208. });
  209. break;
  210. }
  211. } catch (ParseException e) {
  212. e.printStackTrace();
  213. }
  214. }
  215. }
  216. private void updateStatus() {
  217. if (mDoorBinder != null) {
  218. String status = mDoorBinder.getService().getStatus();
  219. if ("opened".equals(status)) {
  220. if (!unlockView.isUnlocked()) {
  221. SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
  222. }
  223. unlockView.setUnlocked(true);
  224. } else if ("closed".equals(status)) {
  225. unlockView.setUnlocked(false);
  226. }
  227. }
  228. }
  229. @Override
  230. protected void onResume() {
  231. super.onResume();
  232. View decorView = getWindow().getDecorView();
  233. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  234. | View.SYSTEM_UI_FLAG_FULLSCREEN
  235. | View.SYSTEM_UI_FLAG_IMMERSIVE;
  236. decorView.setSystemUiVisibility(uiOptions);
  237. if (MyApplication.roomInfo != null) {
  238. tvRoomName.setText(MyApplication.roomInfo.getRoomName());
  239. tvRoomNum.setText(MyApplication.roomInfo.getRoomBizNum());
  240. }
  241. updateMeetings();
  242. updateStatus();
  243. }
  244. @Override
  245. public boolean dispatchTouchEvent(MotionEvent me) {
  246. if (me.getAction() == MotionEvent.ACTION_DOWN) {
  247. View decorView = getWindow().getDecorView();
  248. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  249. | View.SYSTEM_UI_FLAG_FULLSCREEN
  250. | View.SYSTEM_UI_FLAG_IMMERSIVE;
  251. decorView.setSystemUiVisibility(uiOptions);
  252. }
  253. return super.dispatchTouchEvent(me);
  254. }
  255. @Override
  256. public void onStart() {
  257. super.onStart();
  258. EventBus.getDefault().register(this);
  259. }
  260. @Override
  261. public void onStop() {
  262. super.onStop();
  263. EventBus.getDefault().unregister(this);
  264. }
  265. @Override
  266. protected void onDestroy() {
  267. super.onDestroy();
  268. if (connection != null) {
  269. Log.d(TAG, "destroy and unbind service");
  270. unbindService(connection);
  271. }
  272. }
  273. }