From 473dde00aa32ab747f07a26586174e19ef36937d Mon Sep 17 00:00:00 2001 From: it232115 Date: Tue, 16 Jan 2024 15:51:49 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=82=B5=E3=82=A4=E3=83=88=E4=B8=8A?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E4=BF=9D=E8=AD=B7=E8=80=85=E3=81=8B=E3=82=89?= =?UTF-8?q?=E3=81=AE=E9=80=9A=E7=9F=A5=E3=82=92=E5=8F=97=E3=81=91=E5=8F=96?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E6=99=82=E3=81=AB=E4=B9=97=E9=99=8D=E7=8A=B6?= =?UTF-8?q?=E6=85=8B=E3=82=92=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=80=82(=E6=9C=AA=E5=AE=8C=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 190 ++++++++++-------- .../com/example/childguard/MainActivity.java | 50 ++++- .../main/java/com/example/childguard/QR.java | 42 ++-- app/src/main/res/layout/activity_main.xml | 3 +- app/src/main/res/layout/fragment_home.xml | 10 +- 5 files changed, 177 insertions(+), 118 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index b7c8ce0..bcd6d86 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -3,7 +3,6 @@ package com.example.childguard; import static android.content.ContentValues.TAG; import android.content.SharedPreferences; -import android.graphics.Bitmap; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; @@ -12,7 +11,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; -import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; @@ -89,102 +87,95 @@ public class HomeFragment extends Fragment { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); //QRコード印刷の処理 Button bt1 = view.findViewById(R.id.QRprinting); - bt1.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //初回起動かを保存する変数 - boolean alreadySaved = preferences.getBoolean("alreadySaved", false); - //ボタン変数の宣言 - Button parent = view.findViewById(R.id.QRprinting); - Button born = view.findViewById(R.id.QRprinting); - //falseのときにFirebaseへの登録 - if (alreadySaved) { - Log.d("HomeFragment", "already printed"); - //画面遷移&ID受け渡し - Toast.makeText(getActivity(),"再印刷",Toast.LENGTH_SHORT).show(); - QrPrintFragment qrPrintFragment = new QrPrintFragment(); - replaceFragment(qrPrintFragment); - return; - } else Log.d("HomeFragment", "not printed yet"); // debug + bt1.setOnClickListener(v -> { + //初回起動かを保存する変数 + boolean alreadySaved = preferences.getBoolean("alreadySaved", false); + //ボタン変数の宣言 + Button parent = view.findViewById(R.id.QRprinting); + Button born = view.findViewById(R.id.QRprinting); + //falseのときにFirebaseへの登録 + if (alreadySaved) { + Log.d("HomeFragment", "already printed"); + //画面遷移&ID受け渡し + Toast.makeText(getActivity(),"再印刷",Toast.LENGTH_SHORT).show(); + QrPrintFragment qrPrintFragment = new QrPrintFragment(); + replaceFragment(qrPrintFragment); + return; + } else Log.d("HomeFragment", "not printed yet"); // debug - String valueParent = parent.getText().toString();//変数に文字列を代入 - String valueBorn = born.getText().toString();//変数に文字列を代入 - Map user = new HashMap<>();//mapの宣言 + String valueParent = parent.getText().toString();//変数に文字列を代入 + String valueBorn = born.getText().toString();//変数に文字列を代入 + Map user = new HashMap<>();//mapの宣言 - Log.d("HomeFragment", "onClick is called"); + Log.d("HomeFragment", "onClick is called"); - //mapに入れる - user.put("parent", valueParent); - user.put("born", valueBorn); - //新しいドキュメントにIDを作って追加 - db.collection("users") - .add(user) - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(DocumentReference documentReference) { - //成功したら - //documentReference.getId()でID取得 - Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); - SharedPreferences.Editor e = preferences.edit(); - // キー"alreadySaved"の値をtrueにする - e.putBoolean("alreadySaved", true); - //確定処理 - e.apply(); - //画面遷移&ID受け渡し - str_key = "" + documentReference.getId(); - Toast.makeText(getActivity(),"初回登録",Toast.LENGTH_SHORT).show(); - QrPrintFragment qrPrintFragment = new QrPrintFragment(); - Bundle bundle = new Bundle(); - bundle.putString("STR_KEY",str_key); - //値を書き込む - qrPrintFragment.setArguments(bundle); - replaceFragment(qrPrintFragment); + //mapに入れる + user.put("parent", valueParent); + user.put("born", valueBorn); + //新しいドキュメントにIDを作って追加 + db.collection("users") + .add(user) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(DocumentReference documentReference) { + //成功したら + //documentReference.getId()でID取得 + Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); + SharedPreferences.Editor e = preferences.edit(); + // キー"alreadySaved"の値をtrueにする + e.putBoolean("alreadySaved", true); + //確定処理 + e.apply(); + //画面遷移&ID受け渡し + str_key = "" + documentReference.getId(); + Toast.makeText(getActivity(),"初回登録",Toast.LENGTH_SHORT).show(); + QrPrintFragment qrPrintFragment = new QrPrintFragment(); + Bundle bundle = new Bundle(); + bundle.putString("STR_KEY",str_key); + //値を書き込む + qrPrintFragment.setArguments(bundle); + replaceFragment(qrPrintFragment); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception e) { - //失敗したら - Log.w(TAG, "Error adding document", e); - } - }); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + //失敗したら + Log.w(TAG, "Error adding document", e); + } + }); - } }); //bluetooth設定ボタンの処理 Button bt2 = view.findViewById(R.id.Bluetooth_setup); - bt2.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - replaceFragment(new bluetooth_setupFragment()); - } - }); + bt2.setOnClickListener(v -> replaceFragment(new bluetooth_setupFragment())); return view; } - @Override - public void onResume() { - super.onResume(); - Log.d("HomeFragment", "onResume: called"); - TextView situationTextView = getView().findViewById(R.id.situation); - FrameLayout situation_bg = getView().findViewById(R.id.situation_bg); - updateInCarStatus(situationTextView, situation_bg); - } - public void updateInCarStatus(TextView situationTextView, FrameLayout situation_bg) { - SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); - - Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); - if (sharedPreferences.getBoolean("inCar", false)) { - situationTextView.setText("\n降車状態"); - situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); - } else { - situationTextView.setText("\n乗車状態"); - situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); - } - - } +// @Override +// public void onResume() { +// super.onResume(); +// Log.d("HomeFragment", "onResume: called"); +// Cargettingonandoff(); +// TextView situationTextView = getView().findViewById(R.id.situation); +// FrameLayout situation_bg = getView().findViewById(R.id.situation_bg); +// updateInCarStatus(situationTextView, situation_bg); + // } +// public void updateInCarStatus(TextView situationTextView, FrameLayout situation_bg) { +// SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); +// +// Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); +// if (sharedPreferences.getBoolean("inCar", false)) { +// situationTextView.setText("\n降車状態"); +// situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); +// } else { +// situationTextView.setText("\n乗車状態"); +// situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); +// } +// +// } //画面遷移メソッド private void replaceFragment(Fragment fragment) { @@ -199,4 +190,31 @@ public class HomeFragment extends Fragment { // フラグメントトランザクションをコミット transaction.commit(); } +// public void Cargettingonandoff() { +// //共有プリファレンス 全体の準備 +// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); +// //車の乗り降りを管理するtrue=乗車、false=降車 +// boolean zyoukouzyoutai = preferences.getBoolean("car", false); +// SharedPreferences.Editor e = preferences.edit(); +// String get_on = "\n乗車状態"; +// String get_off = "\n降車状態"; +// TextView tv = getView().findViewById(R.id.situation); +// FrameLayout fl = getView().findViewById(R.id.situation_bg); +// +// if (zyoukouzyoutai == true) { //乗降状態の判定 +// //降車状態にする +// fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); +// tv.setText(get_off); +// e.putBoolean("car", false); +// e.apply(); +// } else { +// //乗車状態にする +// fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); +// tv.setText(get_on); +// e.putBoolean("car", true); +// e.apply(); +// } +// +// +// } } \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 9d01fb7..beac7a2 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -12,7 +12,9 @@ import android.app.NotificationManager; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothManager; import android.content.Context; +import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.graphics.Color; import android.os.Bundle; import android.os.Vibrator; @@ -24,10 +26,15 @@ import java.util.Date; import java.util.Random; import java.util.UUID; +import android.preference.PreferenceManager; import android.util.Log; +import android.widget.FrameLayout; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.res.ResourcesCompat; + import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.EventListener; @@ -40,7 +47,7 @@ public class MainActivity extends AppCompatActivity { BluetoothAdapter bluetoothAdapter; public static final String TAG = "InspirationQuote"; - private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/q6t702C8nsXyehckByrr");//現在の位置を取得 + private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/rrVGKi77MAemxvPZrktm");//現在の位置を取得 boolean flg = false; //↓日付を取得するやつ @@ -82,11 +89,11 @@ public class MainActivity extends AppCompatActivity { mDocRef.addSnapshotListener(this, new EventListener() { @Override public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) { - Log.d("nt","イベント開始"); + Log.d("nt", "イベント開始"); if (flg && documentSnapshot != null && documentSnapshot.exists()) { String parent = documentSnapshot.getString("parent"); - Log.d("nt","レスポンスを検知しました1"); + Log.d("nt", "レスポンスを検知しました1"); if (parent.equals("s")) { //通知のやつ↓ @@ -99,12 +106,12 @@ public class MainActivity extends AppCompatActivity { NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); //通知のやつ↑ - Log.d("nt","レスポンスを検知しました2"); - + Log.d("nt", "レスポンスを検知しました2"); notifyMain(); - } else if (e != null) { + } else { Log.w(TAG, "Got an exceptiion!", e); + Cargettingonandoff(); } } @@ -147,4 +154,33 @@ public class MainActivity extends AppCompatActivity { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(enableBackButton); } -} + + public void Cargettingonandoff() { + //共有プリファレンス 全体の準備 + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + //車の乗り降りを管理するtrue=乗車、false=降車 + boolean zyoukouzyoutai = preferences.getBoolean("car", false); + SharedPreferences.Editor e = preferences.edit(); + String get_on = "\n乗車状態"; + String get_off = "\n降車状態"; + setContentView(R.layout.fragment_home); + TextView tv = findViewById(R.id.situation); + FrameLayout fl = findViewById(R.id.situation_bg); + + if (zyoukouzyoutai == true) { //乗降状態の判定 + //降車状態にする + fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); + tv.setText(get_off); + e.putBoolean("car", false); + e.apply(); + } else { + //乗車状態にする + fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); + tv.setText(get_on); + e.putBoolean("car", true); + e.apply(); + } + + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/QR.java b/app/src/main/java/com/example/childguard/QR.java index 1d4e862..1c9c094 100644 --- a/app/src/main/java/com/example/childguard/QR.java +++ b/app/src/main/java/com/example/childguard/QR.java @@ -1,21 +1,21 @@ -package com.example.childguard; - -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.widget.TextView; - -import androidx.appcompat.app.AppCompatActivity; - -public class QR extends AppCompatActivity { - String get_on="乗車状態"; - String get_off ="降車状態"; - TextView tv=findViewById(R.id.situation); - @Override - protected void onCreate(Bundle savedInstanceState){ - super.onCreate(savedInstanceState); - setContentView(R.layout.fragment_qr); - //多分いらないコード +//package com.example.childguard; +// +//import android.graphics.Color; +//import android.graphics.drawable.Drawable; +//import android.os.Bundle; +//import android.widget.TextView; +// +//import androidx.appcompat.app.AppCompatActivity; +// +//public class QR extends AppCompatActivity { +// String get_on="乗車状態"; +// String get_off ="降車状態"; +// TextView tv=findViewById(R.id.situation); +// @Override +// protected void onCreate(Bundle savedInstanceState){ +// super.onCreate(savedInstanceState); +// setContentView(R.layout.fragment_qr); +// //多分いらないコード // findViewById(R.id.camera).setOnClickListener( // v -> { // if(get_on.equals(tv.getText().toString())){ @@ -28,6 +28,6 @@ public class QR extends AppCompatActivity { // } // } // ); - } - -} +// } +// +//} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5377215..80425d1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,7 +8,8 @@ android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white" - tools:context=".MainActivity"> + tools:context=".MainActivity" + > + tools:context=".HomeFragment" + > + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.0"> + android:orientation="vertical"> + Date: Tue, 16 Jan 2024 16:00:42 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=82=B5=E3=82=A4=E3=83=88=E4=B8=8A?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E4=BF=9D=E8=AD=B7=E8=80=85=E3=81=8B=E3=82=89?= =?UTF-8?q?=E3=81=AE=E9=80=9A=E7=9F=A5=E3=82=92=E5=8F=97=E3=81=91=E5=8F=96?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E6=99=82=E3=81=AB=E4=B9=97=E9=99=8D=E7=8A=B6?= =?UTF-8?q?=E6=85=8B=E3=82=92=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=80=82(=E5=AE=8C=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 85 ++++++++----------- .../com/example/childguard/MainActivity.java | 38 ++------- 2 files changed, 44 insertions(+), 79 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index bcd6d86..2ba2d25 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -154,28 +154,15 @@ public class HomeFragment extends Fragment { return view; } -// @Override -// public void onResume() { -// super.onResume(); -// Log.d("HomeFragment", "onResume: called"); -// Cargettingonandoff(); -// TextView situationTextView = getView().findViewById(R.id.situation); -// FrameLayout situation_bg = getView().findViewById(R.id.situation_bg); -// updateInCarStatus(situationTextView, situation_bg); - // } -// public void updateInCarStatus(TextView situationTextView, FrameLayout situation_bg) { -// SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); -// -// Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); -// if (sharedPreferences.getBoolean("inCar", false)) { -// situationTextView.setText("\n降車状態"); -// situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); -// } else { -// situationTextView.setText("\n乗車状態"); -// situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); -// } -// -// } + @Override + public void onResume() { + super.onResume(); + Log.d("HomeFragment", "onResume: called"); + Bundle args = getArguments(); + if (args != null) {//argsの中に値が入っている。 + Cargettingonandoff();//メソッドCargettingonandoff()を実行 + } + } //画面遷移メソッド private void replaceFragment(Fragment fragment) { @@ -190,31 +177,31 @@ public class HomeFragment extends Fragment { // フラグメントトランザクションをコミット transaction.commit(); } -// public void Cargettingonandoff() { -// //共有プリファレンス 全体の準備 -// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); -// //車の乗り降りを管理するtrue=乗車、false=降車 -// boolean zyoukouzyoutai = preferences.getBoolean("car", false); -// SharedPreferences.Editor e = preferences.edit(); -// String get_on = "\n乗車状態"; -// String get_off = "\n降車状態"; -// TextView tv = getView().findViewById(R.id.situation); -// FrameLayout fl = getView().findViewById(R.id.situation_bg); -// -// if (zyoukouzyoutai == true) { //乗降状態の判定 -// //降車状態にする -// fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); -// tv.setText(get_off); -// e.putBoolean("car", false); -// e.apply(); -// } else { -// //乗車状態にする -// fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); -// tv.setText(get_on); -// e.putBoolean("car", true); -// e.apply(); -// } -// -// -// } + public void Cargettingonandoff() { + //共有プリファレンス 全体の準備 + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + //車の乗り降りを管理するtrue=乗車、false=降車 + boolean zyoukouzyoutai = preferences.getBoolean("car", false); + SharedPreferences.Editor e = preferences.edit(); + String get_on = "\n乗車状態"; + String get_off = "\n降車状態"; + TextView tv = getView().findViewById(R.id.situation); + FrameLayout fl = getView().findViewById(R.id.situation_bg); + + if (zyoukouzyoutai == true) { //乗降状態の判定 + //降車状態にする + fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); + tv.setText(get_off); + e.putBoolean("car", false); + e.apply(); + } else { + //乗車状態にする + fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); + tv.setText(get_on); + e.putBoolean("car", true); + e.apply(); + } + + + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index beac7a2..41e12a7 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -111,7 +111,14 @@ public class MainActivity extends AppCompatActivity { notifyMain(); } else { Log.w(TAG, "Got an exceptiion!", e); - Cargettingonandoff(); + HomeFragment fragment = new HomeFragment(); + Bundle bundle = new Bundle(); + bundle.putBoolean("親",true); + fragment.setArguments(bundle); + getSupportFragmentManager() + .beginTransaction() + .add(android.R.id.content, fragment) + .commit(); } } @@ -154,33 +161,4 @@ public class MainActivity extends AppCompatActivity { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(enableBackButton); } - - public void Cargettingonandoff() { - //共有プリファレンス 全体の準備 - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - //車の乗り降りを管理するtrue=乗車、false=降車 - boolean zyoukouzyoutai = preferences.getBoolean("car", false); - SharedPreferences.Editor e = preferences.edit(); - String get_on = "\n乗車状態"; - String get_off = "\n降車状態"; - setContentView(R.layout.fragment_home); - TextView tv = findViewById(R.id.situation); - FrameLayout fl = findViewById(R.id.situation_bg); - - if (zyoukouzyoutai == true) { //乗降状態の判定 - //降車状態にする - fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); - tv.setText(get_off); - e.putBoolean("car", false); - e.apply(); - } else { - //乗車状態にする - fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); - tv.setText(get_on); - e.putBoolean("car", true); - e.apply(); - } - - - } } \ No newline at end of file -- 2.45.1 From f9c4ff6aab08902a6c19fa1f5a580ec31d02a2b1 Mon Sep 17 00:00:00 2001 From: it232115 Date: Wed, 17 Jan 2024 12:16:13 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BB=A5=E4=B8=8B=E3=81=AE=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=80=82=20=E3=83=BB=E4=B9=97=E8=BB=8A=E7=8A=B6?= =?UTF-8?q?=E6=85=8B=E3=81=AE=E3=81=A8=E3=81=8D=E3=81=AE=E3=81=BF=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E8=80=85=E9=80=9A=E5=A0=B1=E3=82=92=E6=9C=89=E5=8A=B9?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B=E3=81=93=E3=81=A8=E3=81=A7=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=81=9A=E3=82=89=E3=82=92=E9=98=B2=E6=AD=A2=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=87=A6=E7=90=86=20=E3=83=BB=E7=94=BB=E9=9D=A2?= =?UTF-8?q?=E3=81=AE=E5=90=91=E3=81=8D=E3=82=92=E5=A4=89=E3=81=88=E3=81=9F?= =?UTF-8?q?=E3=81=A8=E3=81=8D=E3=81=AB=E4=B9=97=E9=99=8D=E7=8A=B6=E6=85=8B?= =?UTF-8?q?=E3=81=8C=E3=81=9D=E3=82=8C=E3=81=9E=E3=82=8C=E3=81=AE=E5=90=91?= =?UTF-8?q?=E3=81=8D=E3=81=A7=E7=8B=AC=E7=AB=8B=E3=81=97=E3=81=A6=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=86=E3=83=90=E3=82=B0=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=20=E3=83=BB=E3=82=A2=E3=83=97=E3=83=AA=E3=82=92=E4=B8=80?= =?UTF-8?q?=E5=BA=A6=E7=B5=82=E4=BA=86=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=81=AB=E4=B9=97=E9=99=8D=E7=8A=B6=E6=85=8B=E3=81=8C=E3=83=AA?= =?UTF-8?q?=E3=82=BB=E3=83=83=E3=83=88=E3=81=97=E3=81=A6=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=86=E3=83=90=E3=82=B0=E3=81=AE=E4=BF=AE=E6=AD=A3=20?= =?UTF-8?q?=E3=83=BB=E3=82=A2=E3=83=97=E3=83=AA=E3=82=92FireBase=E3=81=AEI?= =?UTF-8?q?D=E3=81=A8=E7=B4=90=E3=81=A5=E3=81=91=E3=82=8B=E3=81=93?= =?UTF-8?q?=E3=81=A8=E3=81=A7=E3=80=81=E3=81=9D=E3=81=AE=E3=82=B9=E3=83=9E?= =?UTF-8?q?=E3=83=9B=E3=81=A7=E7=94=9F=E6=88=90=E3=81=97=E3=81=9FQR?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E3=82=B5=E3=82=A4=E3=83=88?= =?UTF-8?q?=E3=81=A7=E3=83=9C=E3=82=BF=E3=83=B3=E3=82=92=E6=8A=BC=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=81=A8=E3=81=8D=E3=81=AB=E3=82=A2=E3=83=97=E3=83=AA?= =?UTF-8?q?=E3=81=AE=E7=8A=B6=E6=85=8B=E3=81=8C=E5=A4=89=E5=8C=96=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=87=A6=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 36 +++++----- .../com/example/childguard/MainActivity.java | 65 +++++++++++++++---- .../example/childguard/QrPrintFragment.java | 10 +-- 3 files changed, 73 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 2ba2d25..1e46b64 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -1,6 +1,7 @@ package com.example.childguard; import static android.content.ContentValues.TAG; +import static android.content.Context.MODE_PRIVATE; import android.content.SharedPreferences; import android.os.Bundle; @@ -83,21 +84,21 @@ public class HomeFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_home, container, false); MainActivity activity = (MainActivity) getActivity(); - //共有プリファレンス 全体の準備 - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + //共有プリファレンス全体の準備 + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE); //QRコード印刷の処理 Button bt1 = view.findViewById(R.id.QRprinting); bt1.setOnClickListener(v -> { //初回起動かを保存する変数 - boolean alreadySaved = preferences.getBoolean("alreadySaved", false); + boolean alreadySaved = sharedPreferences.getBoolean("alreadySaved", false); //ボタン変数の宣言 Button parent = view.findViewById(R.id.QRprinting); Button born = view.findViewById(R.id.QRprinting); //falseのときにFirebaseへの登録 if (alreadySaved) { Log.d("HomeFragment", "already printed"); - //画面遷移&ID受け渡し - Toast.makeText(getActivity(),"再印刷",Toast.LENGTH_SHORT).show(); + //画面遷移&ID受け渡し + Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show(); QrPrintFragment qrPrintFragment = new QrPrintFragment(); replaceFragment(qrPrintFragment); return; @@ -121,17 +122,17 @@ public class HomeFragment extends Fragment { //成功したら //documentReference.getId()でID取得 Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); - SharedPreferences.Editor e = preferences.edit(); + SharedPreferences.Editor e = sharedPreferences.edit(); // キー"alreadySaved"の値をtrueにする e.putBoolean("alreadySaved", true); //確定処理 e.apply(); //画面遷移&ID受け渡し str_key = "" + documentReference.getId(); - Toast.makeText(getActivity(),"初回登録",Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show(); QrPrintFragment qrPrintFragment = new QrPrintFragment(); Bundle bundle = new Bundle(); - bundle.putString("STR_KEY",str_key); + bundle.putString("STR_KEY", str_key); //値を書き込む qrPrintFragment.setArguments(bundle); replaceFragment(qrPrintFragment); @@ -158,10 +159,7 @@ public class HomeFragment extends Fragment { public void onResume() { super.onResume(); Log.d("HomeFragment", "onResume: called"); - Bundle args = getArguments(); - if (args != null) {//argsの中に値が入っている。 - Cargettingonandoff();//メソッドCargettingonandoff()を実行 - } + Cargettingonandoff();//メソッドCargettingonandoff()を実行 } //画面遷移メソッド @@ -177,12 +175,13 @@ public class HomeFragment extends Fragment { // フラグメントトランザクションをコミット transaction.commit(); } + public void Cargettingonandoff() { - //共有プリファレンス 全体の準備 - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + //共有プリファレンス全体の準備 + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE); //車の乗り降りを管理するtrue=乗車、false=降車 - boolean zyoukouzyoutai = preferences.getBoolean("car", false); - SharedPreferences.Editor e = preferences.edit(); + Boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); + SharedPreferences.Editor e = sharedPreferences.edit(); String get_on = "\n乗車状態"; String get_off = "\n降車状態"; TextView tv = getView().findViewById(R.id.situation); @@ -192,14 +191,11 @@ public class HomeFragment extends Fragment { //降車状態にする fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); tv.setText(get_off); - e.putBoolean("car", false); - e.apply(); + } else { //乗車状態にする fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); tv.setText(get_on); - e.putBoolean("car", true); - e.apply(); } diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index 41e12a7..23928bd 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -47,7 +47,7 @@ public class MainActivity extends AppCompatActivity { BluetoothAdapter bluetoothAdapter; public static final String TAG = "InspirationQuote"; - private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/rrVGKi77MAemxvPZrktm");//現在の位置を取得 + boolean flg = false; //↓日付を取得するやつ @@ -86,34 +86,71 @@ public class MainActivity extends AppCompatActivity { }); + + } + + @Override + protected void onResume() { + super.onResume(); + SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); + String IdPref = sharedPreferences.getString("ID", null); + if (IdPref == null) { + Log.d("onResume", "ID not initialized."); + return; + } + DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得 + initNotification(mDocRef); + } + + private void initNotification(DocumentReference mDocRef) { + + // Init pref + SharedPreferences sharedPreferences = getSharedPreferences("app_situation",MODE_PRIVATE); + mDocRef.addSnapshotListener(this, new EventListener() { @Override public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) { Log.d("nt", "イベント開始"); + //共有プリファレンス 書き込みの準備 + SharedPreferences.Editor E=sharedPreferences.edit(); + //車の乗り降りを管理するtrue=乗車、false=降車 + boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); if (flg && documentSnapshot != null && documentSnapshot.exists()) { String parent = documentSnapshot.getString("parent"); Log.d("nt", "レスポンスを検知しました1"); - if (parent.equals("s")) { - //通知のやつ↓ - int importance = NotificationManager.IMPORTANCE_DEFAULT; + if (parent.equals("s")) {//FireBaseの更新情報が"S"のとき=サイト上で第三者ボタンが押されたとき + if(zyoukouzyoutai==false) {//いたずら防止 + //通知のやつ↓ - NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance); - //説明・説明 ここに通知の説明を書くことができる↓ - channel.setDescription("第3者からの通報を検知しました"); + int importance = NotificationManager.IMPORTANCE_DEFAULT; - NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(channel); - //通知のやつ↑ - Log.d("nt", "レスポンスを検知しました2"); + NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance); + //説明・説明 ここに通知の説明を書くことができる↓ + channel.setDescription("第3者からの通報を検知しました"); - notifyMain(); + NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + //通知のやつ↑ + Log.d("nt", "レスポンスを検知しました2"); + + notifyMain(); + + } } else { + if(zyoukouzyoutai==true){//乗降状態の判定 + E.putBoolean("car", false);//降車状態にする + E.apply();//確定処理 + }else{ + E.putBoolean("car", true);//乗車状態にする + E.apply();//確定処理 + } Log.w(TAG, "Got an exceptiion!", e); + //HomeFragmentへ遷移する HomeFragment fragment = new HomeFragment(); Bundle bundle = new Bundle(); - bundle.putBoolean("親",true); + bundle.putBoolean("親",zyoukouzyoutai); fragment.setArguments(bundle); getSupportFragmentManager() .beginTransaction() @@ -161,4 +198,4 @@ public class MainActivity extends AppCompatActivity { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(enableBackButton); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/childguard/QrPrintFragment.java b/app/src/main/java/com/example/childguard/QrPrintFragment.java index fb8e755..32b224b 100644 --- a/app/src/main/java/com/example/childguard/QrPrintFragment.java +++ b/app/src/main/java/com/example/childguard/QrPrintFragment.java @@ -1,5 +1,7 @@ package com.example.childguard; +import static android.content.Context.MODE_PRIVATE; + import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -75,10 +77,10 @@ public class QrPrintFragment extends Fragment { // @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - //共有プリファレンス 全体の準備 - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + //共有プリファレンス全体の準備 + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE); //User毎のドメインを保存する - String IdPref=preferences.getString("ID",null); + String IdPref=sharedPreferences.getString("ID",null); // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_qr_print, container, false);; //固定のドメイン @@ -90,7 +92,7 @@ public class QrPrintFragment extends Fragment { //User毎のドメイン String userURL = getArguments().getString("STR_KEY"); //キー"ID"の値をuserURLの値にする - SharedPreferences.Editor e = preferences.edit(); + SharedPreferences.Editor e = sharedPreferences.edit(); e.putString("ID", userURL); //確定処理 e.apply(); -- 2.45.1