From e906343619f3397f0b75cc3e10355a19398bf299 Mon Sep 17 00:00:00 2001 From: Asura146 Date: Fri, 26 Jul 2024 01:08:23 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=A8=E3=81=8B=E3=81=AE?= =?UTF-8?q?=E5=90=8D=E5=89=8D=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 17 +++--- .../com/example/childguard/MainActivity.java | 60 +++++++++---------- app/src/main/res/layout/fragment_home.xml | 6 +- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index fab5664..6c2d776 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -139,14 +139,17 @@ public class HomeFragment extends Fragment implements OnEventListener { return true; } + /** + * Bluetoothの接続状態の画面を切り替える + */ private boolean updateBluetoothSituation(Boolean BluetoothConnect) { FrameLayout frameLayout; TextView textView; ImageView imageView; try { - frameLayout = requireView().findViewById(R.id.situation_bg2); - textView = requireView().findViewById(R.id.Bluetoothsituation); - imageView = requireView().findViewById(R.id.Bluetoothsituationimage); + frameLayout = requireView().findViewById(R.id.situation_bg_bluetooth); + textView = requireView().findViewById(R.id.BluetoothSituation); + imageView = requireView().findViewById(R.id.BluetoothSituationImage); } catch (NullPointerException e) { Log.d("HomeFragment", "updateUiState: view is null"); return false; @@ -156,17 +159,17 @@ public class HomeFragment extends Fragment implements OnEventListener { updateBluetoothSituation(BluetoothConnect); return false; } - String connect = "接続中"; - String disconnect = "切断中"; + final String CONNECT = "接続中"; + final String DISCONNECT = "切断中"; if (BluetoothConnect) { //接続状態にする frameLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); - textView.setText(connect); + textView.setText(CONNECT); imageView.setVisibility(View.GONE); } else { //降車状態にする frameLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); - textView.setText(disconnect); + textView.setText(DISCONNECT); imageView.setVisibility(View.VISIBLE); } diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index f96d300..7df7664 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -52,7 +52,6 @@ public class MainActivity extends AppCompatActivity { public static final String TAG = "InspirationQuote"; - private final ActivityResultLauncher QrLauncher = registerForActivityResult( new ScanContract(), result -> { @@ -76,10 +75,9 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); //Bluetooth接続判定用 - SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this); - SharedPreferences.Editor e=pref.edit(); - e.putBoolean("connection_status",false); - + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor e = pref.edit(); + e.putBoolean("connection_status", false); if (!hasPermissions()) { @@ -149,7 +147,7 @@ public class MainActivity extends AppCompatActivity { firebaseLink(); } - private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理 + private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理 // 共有プリファレンス全体の準備 SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); @@ -187,7 +185,6 @@ public class MainActivity extends AppCompatActivity { } } }); - } private boolean hasPermissions() { @@ -229,18 +226,18 @@ public class MainActivity extends AppCompatActivity { } - - + /** + * FireBaseのIDの取得 + */ public void firebaseLink() {//Firebaseのドキュメントの取得 //共有プリファレンス全体の準備 SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); - String IdPref = sharedPreferences.getString("ID", null);////アプリに記録されているIDの取得 + String IdPref = sharedPreferences.getString("ID", null);//アプリに記録されているIDの取得 if (IdPref == null) {//FireBaseのIDがアプリに登録されているとき Log.d("onResume", "ID not initialized."); } else { mDocRef = FirebaseFirestore.getInstance().document("status/" + IdPref);//現在の位置を取得 initNotification(mDocRef);//現在の位置を引数に initNotification()を処理 - } } @@ -261,19 +258,20 @@ public class MainActivity extends AppCompatActivity { //共有プリファレンス 書き込みの準備 SharedPreferences.Editor E = sharedPreferences.edit(); String IdPref = sharedPreferences.getString("ID", null);//アプリに記録されているIDの取得 - Boolean change=sharedPreferences.getBoolean("change",false); + Boolean change = sharedPreferences.getBoolean("change", false); db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ DocumentReference isReported = db.collection("status").document(IdPref);//更新するドキュメントとの紐づけ Map DEFAULT_ITEM = new HashMap<>();//mapの宣言 if (!change) { //isInCarをtrueに更新 - E.putBoolean("change",true); - }else{ - E.putBoolean("change",false); + E.putBoolean("change", true); + } else { + E.putBoolean("change", false); } isReported.update("isInCar", change).addOnSuccessListener(unused -> Log.d(TAG, "DocumentSnapshot successfully updated!")).addOnFailureListener(e -> Log.w(TAG, "Error updating document", e)); E.apply(); } + public void NotificationSetting() {//通知に関する設定の処理を行うメソッド int importance = NotificationManager.IMPORTANCE_DEFAULT; //通知チャネルの実装 @@ -380,7 +378,7 @@ public class MainActivity extends AppCompatActivity { } - NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); + NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { return; } @@ -388,9 +386,6 @@ public class MainActivity extends AppCompatActivity { } - - - public void Bluetooth_status() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); @@ -413,8 +408,8 @@ public class MainActivity extends AppCompatActivity { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences pref=getSharedPreferences("Bluetooth_situation",MODE_PRIVATE); - SharedPreferences.Editor e=pref.edit(); + SharedPreferences pref = getSharedPreferences("Bluetooth_situation", MODE_PRIVATE); + SharedPreferences.Editor e = pref.edit(); String action = intent.getAction(); // may need to chain this to a recognizing function BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Boolean isInCar = pref.getBoolean("isInCarPref", false); @@ -437,16 +432,16 @@ public class MainActivity extends AppCompatActivity { if (deviceHardwareAddress.equals(registeredId)) { //登録済みのデバイスだったときの処理 Log.d("BT_Judge", "登録済み"); - e.putBoolean("connection_status",true); + e.putBoolean("connection_status", true); - } else{ + } else { //登録していないデバイスだったときの処理 Log.d("BT_Judge", "未登録"); - e.putBoolean("connection_status",false); + e.putBoolean("connection_status", false); } e.apply(); - } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//bluetoothが切断されたときに乗車状態のとき + } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) && !isInCar) {//bluetoothが切断されたときに乗車状態のとき //Do something if disconnected //デバイスが切断されたときの処理 @@ -457,20 +452,21 @@ public class MainActivity extends AppCompatActivity { handler.postDelayed(new Runnable() { @Override public void run() { - if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)&&!isInCar) {//その後bluetoothを再接続したり降車状態になったりしていない=置き去りが発生した可能性大 + if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) && !isInCar) {//その後bluetoothを再接続したり降車状態になったりしていない=置き去りが発生した可能性大 NotificationBluetooth(getApplicationContext());//通知を行うメソッド - }} + } + } - }, 5 *60*1000); // 5分をミリ秒に変換 + }, 5 * 1000); // 5分をミリ秒に変換 } - }else { - Log.d("BT", "Device disconnected"); + } else { + Log.d("BT", " Device disconnected"); } } }; - public void changeBluetooth(boolean actual){ - getSharedPreferences("Bluetooth_situation",MODE_PRIVATE).edit().putBoolean("status",actual).apply(); + public void changeBluetooth(boolean actual) { + getSharedPreferences("Bluetooth_situation", MODE_PRIVATE).edit().putBoolean("status", actual).apply(); } } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 589bae9..07e986f 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -49,7 +49,7 @@