|
|
@@ -2,17 +2,27 @@ package com.ht.gate;
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
+import android.content.ComponentName;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.ServiceConnection;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
+ private static final String TAG = "MainActivity";
|
|
|
|
|
|
@BindView(R.id.unlock_view)
|
|
|
UnlockView unlockView;
|
|
|
@@ -20,6 +30,22 @@ public class MainActivity extends AppCompatActivity {
|
|
|
TextView tvSpaceName;
|
|
|
|
|
|
private SharedPreferences sharedPreferences;
|
|
|
+ private DoorService.DoorBinder mDoorBinder;
|
|
|
+
|
|
|
+ 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) {
|
|
|
@@ -29,8 +55,49 @@ public class MainActivity extends AppCompatActivity {
|
|
|
sharedPreferences = getSharedPreferences(SettingsActivity.PREF_NAME_SETTINGS, MODE_PRIVATE);
|
|
|
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) {
|
|
|
+ if (password.equals("8888")) {
|
|
|
+ dialog.dismiss();
|
|
|
+ if (mDoorBinder != null) {
|
|
|
+ mDoorBinder.getService().openDoor();
|
|
|
+ }
|
|
|
+ startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
|
|
|
+ } else {
|
|
|
+ Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
passwordDialog.show();
|
|
|
});
|
|
|
+ bindService(new Intent(this, DoorService.class), connection, BIND_AUTO_CREATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.btn_settings)
|
|
|
+ void onClickSettings() {
|
|
|
+ startActivity(new Intent(this, SettingsActivity.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void onMessageEvent(String msg) {
|
|
|
+ updateStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateStatus() {
|
|
|
+ if (mDoorBinder != null) {
|
|
|
+ String status = mDoorBinder.getService().getStatus();
|
|
|
+ if ("opened".equals(status)) {
|
|
|
+ unlockView.setUnlocked(true);
|
|
|
+ } else if ("closed".equals(status)) {
|
|
|
+ unlockView.setUnlocked(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -42,10 +109,27 @@ public class MainActivity extends AppCompatActivity {
|
|
|
| View.SYSTEM_UI_FLAG_IMMERSIVE;
|
|
|
decorView.setSystemUiVisibility(uiOptions);
|
|
|
tvSpaceName.setText(sharedPreferences.getString(SettingsActivity.PREF_KEY_SPACE_NAME, ""));
|
|
|
+ updateStatus();
|
|
|
}
|
|
|
|
|
|
- @OnClick(R.id.btn_settings)
|
|
|
- void onClickSettings() {
|
|
|
- startActivity(new Intent(this, SettingsActivity.class));
|
|
|
+ @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);
|
|
|
+ }
|
|
|
}
|
|
|
}
|