| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- package com.ht.gate;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.content.ContextCompat;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import android.animation.ValueAnimator;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.media.AudioAttributes;
- import android.media.AudioManager;
- import android.media.SoundPool;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.util.Log;
- import android.util.TypedValue;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.ht.gate.domain.Meeting;
- import com.ht.gate.domain.RoomMeetings;
- import com.ht.gate.domain.emp.EmpInfo;
- import org.greenrobot.eventbus.EventBus;
- import org.greenrobot.eventbus.Subscribe;
- import org.greenrobot.eventbus.ThreadMode;
- import java.io.IOException;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import es.dmoral.toasty.Toasty;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "MainActivity";
- private static final int HIDE_BAR = 10000;
- @BindView(R.id.unlock_view)
- UnlockView unlockView;
- @BindView(R.id.tv_room_name)
- TextView tvRoomName;
- @BindView(R.id.tv_room_num)
- TextView tvRoomNum;
- @BindView(R.id.rl_meetings)
- RelativeLayout rlMeetings;
- @BindView(R.id.rv_meetings)
- RecyclerView rvMeetings;
- @BindView(R.id.iv_arrow)
- ImageView ivArrow;
- @BindView(R.id.tv_net_err)
- TextView tvNetErr;
- private MyLinearLayoutManager layoutManager;
- private MeetingAdapter adapter;
- private DoorService.DoorBinder mDoorBinder;
- private Handler handler = new Handler(msg -> {
- switch (msg.what) {
- case HIDE_BAR:
- View decorView = getWindow().getDecorView();
- int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE;
- decorView.setSystemUiVisibility(uiOptions);
- }
- return true;
- });
- private ServiceConnection connection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.d(TAG, "onServiceConnected");
- mDoorBinder = (DoorService.DoorBinder) service;
- updateStatus();
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.d(TAG, "onServiceDisconnected");
- mDoorBinder = null;
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- unlockView.setUnlockListener(() -> {
- PasswordDialog passwordDialog = new PasswordDialog(MainActivity.this);
- passwordDialog.setListener(new PasswordDialog.DialogListener() {
- @Override
- public void onCancel(PasswordDialog dialog) {
- dialog.dismiss();
- }
- @Override
- public void onConfirm(PasswordDialog dialog, String password) {
- dialog.dismiss();
- if (MyApplication.roomInfo != null && MyApplication.roomInfo.getDevicePassword().equals(password)) {
- if (mDoorBinder != null) {
- mDoorBinder.getService().openDoor();
- }
- // Toasty.normal(MainActivity.this, "已解锁").show();
- // SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
- } else {
- Toasty.error(MainActivity.this, "密码错误").show();
- SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_FAIL);
- }
- Message msg = new Message();
- msg.what = HIDE_BAR;
- handler.sendMessageDelayed(msg, 500);
- }
- });
- passwordDialog.show();
- });
- bindService(new Intent(this, DoorService.class), connection, BIND_AUTO_CREATE);
- adapter = new MeetingAdapter(this);
- layoutManager = new MyLinearLayoutManager(this);
- layoutManager.setCanScroll(false);
- rvMeetings.setLayoutManager(layoutManager);
- rvMeetings.setAdapter(adapter);
- ((MyApplication) getApplication()).updateMeetings();
- }
- @OnClick(R.id.btn_settings)
- void onClickSettings() {
- startActivity(new Intent(this, SettingsActivity.class));
- }
- @OnClick(R.id.iv_arrow)
- void clickArrow() {
- ViewGroup.LayoutParams layoutParams = rlMeetings.getLayoutParams();
- int dp36 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, getResources().getDisplayMetrics());
- int dp108 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 126, getResources().getDisplayMetrics());
- int height = layoutParams.height;
- ValueAnimator anim = ValueAnimator.ofInt(height, height < dp108 ? dp108 : dp36);
- anim.addUpdateListener(valueAnimator -> {
- int val = (Integer) valueAnimator.getAnimatedValue();
- ViewGroup.LayoutParams layoutParams1 = rlMeetings.getLayoutParams();
- layoutParams1.height = val;
- rlMeetings.setLayoutParams(layoutParams1);
- });
- anim.setDuration(200);
- anim.start();
- layoutManager.setCanScroll(height < dp108);
- if (height >= dp36) {
- rvMeetings.scrollToPosition(0);
- }
- Animation animation = new RotateAnimation(height < dp108 ? 0 : 180, height < dp108 ? 180 : 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- animation.setFillAfter(true);
- animation.setDuration(200);
- animation.setInterpolator(new LinearInterpolator());
- animation.setRepeatCount(0);
- ivArrow.startAnimation(animation);
- }
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onMessageEvent(String msg) {
- if (Constants.EVENT_MEETINGS.equals(msg)) {
- updateMeetings();
- } else if (Constants.EVENT_NET_STATUS.equals(msg)) {
- tvNetErr.setVisibility(MyApplication.netAvailable ? View.GONE : View.VISIBLE);
- } else {
- updateStatus();
- }
- }
- private void updateMeetings() {
- if (adapter.getItemCount() <= 1) {
- ivArrow.setVisibility(View.GONE);
- } else {
- ivArrow.setVisibility(View.VISIBLE);
- }
- if (MyApplication.todayMeetings.isEmpty()) {
- rlMeetings.setVisibility(View.GONE);
- } else {
- rlMeetings.setVisibility(View.VISIBLE);
- }
- adapter.notifyDataSetChanged();
- for (Meeting meeting : MyApplication.todayMeetings) {
- try {
- Date start = Utils.parseDateTime(meeting.getBeginTimeStr());
- start.setTime(start.getTime() - 10 * 60 * 1000);
- Date end = Utils.parseDateTime(meeting.getEndTimeStr());
- Date date = new Date();
- if ((date.equals(start) || date.after(start)) && (date.equals(end) || date.before(end))) {
- RetrofitManager.getInstance().getRetrofit().create(ApiService.class)
- .getEmpInfo(meeting.getCreateUser().getUserId()).enqueue(new Callback<EmpInfo>() {
- @Override
- public void onResponse(@NonNull Call<EmpInfo> call, @NonNull Response<EmpInfo> response) {
- if (response.code() == 200) {
- WelcomeActivity.start(MainActivity.this, meeting, response.body());
- } else {
- try {
- Toasty.error(MainActivity.this, response.errorBody().string()).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onFailure(@NonNull Call<EmpInfo> call, @NonNull Throwable t) {
- Toasty.error(MainActivity.this, "投顾信息获取失败,请稍后再试").show();
- }
- });
- break;
- }
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- }
- private void updateStatus() {
- if (mDoorBinder != null) {
- String status = mDoorBinder.getService().getStatus();
- if ("opened".equals(status)) {
- if (!unlockView.isUnlocked()) {
- SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
- }
- unlockView.setUnlocked(true);
- } else if ("closed".equals(status)) {
- unlockView.setUnlocked(false);
- }
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- View decorView = getWindow().getDecorView();
- int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE;
- decorView.setSystemUiVisibility(uiOptions);
- if (MyApplication.roomInfo != null) {
- tvRoomName.setText(MyApplication.roomInfo.getRoomName());
- tvRoomNum.setText(MyApplication.roomInfo.getRoomBizNum());
- }
- updateMeetings();
- updateStatus();
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent me) {
- if (me.getAction() == MotionEvent.ACTION_DOWN) {
- View decorView = getWindow().getDecorView();
- int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE;
- decorView.setSystemUiVisibility(uiOptions);
- }
- return super.dispatchTouchEvent(me);
- }
- @Override
- public void onStart() {
- super.onStart();
- EventBus.getDefault().register(this);
- }
- @Override
- public void onStop() {
- super.onStop();
- EventBus.getDefault().unregister(this);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (connection != null) {
- Log.d(TAG, "destroy and unbind service");
- unbindService(connection);
- }
- }
- }
|