SettingsActivity.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.ht.gate;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.annotation.SuppressLint;
  4. import android.app.ProgressDialog;
  5. import android.content.ComponentName;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.ServiceConnection;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.os.IBinder;
  12. import android.provider.Settings;
  13. import android.text.InputType;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.TextView;
  17. import com.ht.gate.domain.Room;
  18. import java.io.IOException;
  19. import butterknife.BindView;
  20. import butterknife.ButterKnife;
  21. import butterknife.OnClick;
  22. import es.dmoral.toasty.Toasty;
  23. import retrofit2.Call;
  24. import retrofit2.Callback;
  25. import retrofit2.Response;
  26. @SuppressLint("SetTextI18n")
  27. public class SettingsActivity extends AppCompatActivity {
  28. private static final String TAG = "SettingsActivity";
  29. @BindView(R.id.tv_title)
  30. TextView tvTitle;
  31. @BindView(R.id.tv_space_id)
  32. TextView tvSpaceId;
  33. @BindView(R.id.tv_address)
  34. TextView tvAddress;
  35. public static final String PREF_NAME_SETTINGS = "settings";
  36. public static final String PREF_KEY_ADDRESS = "address";
  37. private SharedPreferences sharedPreferences;
  38. private DoorService.DoorBinder mDoorBinder;
  39. private ServiceConnection connection = new ServiceConnection() {
  40. @Override
  41. public void onServiceConnected(ComponentName name, IBinder service) {
  42. Log.d(TAG, "onServiceConnected");
  43. mDoorBinder = (DoorService.DoorBinder) service;
  44. }
  45. @Override
  46. public void onServiceDisconnected(ComponentName name) {
  47. Log.d(TAG, "onServiceDisconnected");
  48. mDoorBinder = null;
  49. }
  50. };
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_settings);
  55. ButterKnife.bind(this);
  56. sharedPreferences = getSharedPreferences(PREF_NAME_SETTINGS, MODE_PRIVATE);
  57. bindService(new Intent(this, DoorService.class), connection, BIND_AUTO_CREATE);
  58. }
  59. @Override
  60. protected void onResume() {
  61. super.onResume();
  62. View decorView = getWindow().getDecorView();
  63. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  64. | View.SYSTEM_UI_FLAG_FULLSCREEN
  65. | View.SYSTEM_UI_FLAG_IMMERSIVE;
  66. decorView.setSystemUiVisibility(uiOptions);
  67. Room room = Room.fromBase64(sharedPreferences.getString(Constants.PREF_ROOM_INFO, null));
  68. if (room != null) {
  69. tvTitle.setText(room.getRoomName() + room.getRoomBizNum());
  70. tvSpaceId.setText(room.getDeviceCode());
  71. }
  72. tvAddress.setText(sharedPreferences.getString(PREF_KEY_ADDRESS, ""));
  73. }
  74. @Override
  75. protected void onDestroy() {
  76. super.onDestroy();
  77. if (connection != null) {
  78. unbindService(connection);
  79. }
  80. }
  81. @OnClick(R.id.btn_back)
  82. void back() {
  83. finish();
  84. }
  85. @OnClick(R.id.btn_change_space_id)
  86. void changeSpaceId() {
  87. Intent intent = new Intent(this, LoginActivity.class);
  88. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  89. | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  90. startActivity(intent);
  91. finish();
  92. }
  93. @OnClick(R.id.btn_change_address)
  94. void changeAddress() {
  95. new PromptBuilder(this)
  96. .setTitle("输入空间门禁地址")
  97. .setValue(tvAddress.getText().toString())
  98. .setPositiveButton("确定")
  99. .setNegativeButton("取消")
  100. .setPromptListener(new PromptBuilder.PromptListener() {
  101. @Override
  102. public void onCancel(DialogInterface dialog) {
  103. dialog.dismiss();
  104. }
  105. @Override
  106. public void onConfirm(DialogInterface dialog, String value) {
  107. dialog.dismiss();
  108. tvAddress.setText(value);
  109. sharedPreferences.edit()
  110. .putString(PREF_KEY_ADDRESS, value)
  111. .apply();
  112. if (mDoorBinder != null) {
  113. mDoorBinder.getService().restartSocket();
  114. }
  115. }
  116. })
  117. .show();
  118. }
  119. }