From 114faf1ac0b13e5b72cc313c358236840e256ceb Mon Sep 17 00:00:00 2001 From: it232115 Date: Mon, 15 Jan 2024 10:04:53 +0900 Subject: [PATCH] wip --- app/build.gradle | 2 + .../com/example/childguard/HomeFragment.java | 94 +++++++++++++------ .../example/childguard/UrlPageActivity.java | 16 ---- app/src/main/res/layout/activity_url_page.xml | 14 --- build.gradle | 6 +- 5 files changed, 73 insertions(+), 59 deletions(-) delete mode 100644 app/src/main/java/com/example/childguard/UrlPageActivity.java delete mode 100644 app/src/main/res/layout/activity_url_page.xml diff --git a/app/build.gradle b/app/build.gradle index 6150f78..71a9f46 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id 'com.google.gms.google-services' } android { @@ -58,6 +59,7 @@ dependencies { implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' + implementation 'com.google.firebase:firebase-firestore:24.4.1' testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index a5ad221..9405ba8 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -1,34 +1,31 @@ package com.example.childguard; +import static android.content.ContentValues.TAG; + import android.content.SharedPreferences; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; import android.os.Bundle; - -import androidx.core.content.res.ResourcesCompat; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.print.PrintHelper; - -import android.util.AndroidRuntimeException; +import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; 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 com.google.zxing.BarcodeFormat; -import com.google.zxing.WriterException; -import com.journeyapps.barcodescanner.BarcodeEncoder; +import androidx.annotation.NonNull; +import androidx.core.content.res.ResourcesCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; + +import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.FirebaseFirestore; + +import java.util.HashMap; +import java.util.Map; /** * A simple {@link Fragment} subclass. @@ -37,6 +34,8 @@ import com.journeyapps.barcodescanner.BarcodeEncoder; */ public class HomeFragment extends Fragment { + FirebaseFirestore db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ + // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; @@ -80,20 +79,58 @@ public class HomeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // Log.d("HomeFlagment_cnt", "aaaaa"); // Inflate the layout for this fragment - View view=inflater.inflate(R.layout.fragment_home,container,false); + View view = inflater.inflate(R.layout.fragment_home, container, false); MainActivity activity = (MainActivity) getActivity(); + //共有プリファレンス 全体の準備 + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); //QRコード印刷の処理 - Button bt1=view.findViewById(R.id.QRprinting); + Button bt1 = view.findViewById(R.id.QRprinting); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - replaceFragment(new QrPrintFragment()); + //初回起動かを保存する変数 + int i = Integer.parseInt(preferences.getString("kidoukaisuu", "1")); + //ボタン変数の宣言 + Button parent = view.findViewById(R.id.QRprinting); + Button born = view.findViewById(R.id.QRprinting); + if (i == 1) {//QRコード印刷を初めて押したときにFireBaseへの登録を行う + String QRYomitorisya = parent.getText().toString();//変数に文字列を代入 + String valueBorn = born.getText().toString();//変数に文字列を代入 + Map user = new HashMap<>();//mapの宣言 - } + Log.d("HomeFragment", "onClick is called"); + + //mapに入れる + user.put("parent", QRYomitorisya); + user.put("born", valueBorn); + //新しいドキュメントにIDを作って追加 + db.collection("users") + .add(user) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(DocumentReference documentReference) { + Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) {//エラー処理 + Log.w(TAG, "Error adding document", e); + } + + }); + + } + SharedPreferences.Editor e = preferences.edit(); + e.putString("kidoukiroku", "2"); + e.apply(); + replaceFragment(new QrPrintFragment()); + } }); //bluetooth設定ボタンの処理 - Button bt2=view.findViewById(R.id.Bluetooth_setup); + Button bt2 = view.findViewById(R.id.Bluetooth_setup); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -108,11 +145,11 @@ public class HomeFragment extends Fragment { 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); + FrameLayout situation_bg = getView().findViewById(R.id.situation_bg); + updateInCarStatus(situationTextView, situation_bg); } - public void updateInCarStatus(TextView situationTextView,FrameLayout situation_bg) { + public void updateInCarStatus(TextView situationTextView, FrameLayout situation_bg) { SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); @@ -125,8 +162,9 @@ public class HomeFragment extends Fragment { } } + //画面遷移メソッド - private void replaceFragment(Fragment fragment){ + private void replaceFragment(Fragment fragment) { // フラグメントマネージャーの取得 FragmentManager manager = getParentFragmentManager(); // アクティビティではgetSupportFragmentManager()? // フラグメントトランザクションの開始 diff --git a/app/src/main/java/com/example/childguard/UrlPageActivity.java b/app/src/main/java/com/example/childguard/UrlPageActivity.java deleted file mode 100644 index 45b59c0..0000000 --- a/app/src/main/java/com/example/childguard/UrlPageActivity.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.example.childguard; - -import androidx.appcompat.app.AppCompatActivity; - -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.webkit.WebView; - -public class UrlPageActivity extends AppCompatActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_url_page); - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_url_page.xml b/app/src/main/res/layout/activity_url_page.xml deleted file mode 100644 index 61bc31b..0000000 --- a/app/src/main/res/layout/activity_url_page.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4314313..e8491f3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,8 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + dependencies { + classpath 'com.google.gms:google-services:4.3.14' + } +}// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false