diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 7d04874..499a8b6 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -3,16 +3,9 @@ package com.example.childguard; import static android.content.ContentValues.TAG; import static android.content.Context.MODE_PRIVATE; -import android.bluetooth.BluetoothDevice; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.PackageManager; -import android.graphics.Bitmap; import android.os.Bundle; -import androidx.core.app.ActivityCompat; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -20,8 +13,6 @@ import androidx.fragment.app.FragmentTransaction; import androidx.print.PrintHelper; import android.preference.PreferenceManager; -import android.text.PrecomputedText; -import android.util.AndroidRuntimeException; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -32,10 +23,6 @@ import android.widget.TextView; import android.widget.Toast; 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; @@ -51,7 +38,7 @@ import java.util.Objects; * Use the {@link HomeFragment#newInstance} factory method to * create an instance of this fragment. */ -public class HomeFragment extends Fragment { +public class HomeFragment extends Fragment implements OnEventListener{ FirebaseFirestore db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ // TODO: Rename parameter arguments, choose names that match @@ -63,6 +50,7 @@ public class HomeFragment extends Fragment { private String str_key; private String mParam2; + public HomeFragment() { // Required empty public constructor } @@ -116,55 +104,62 @@ public class HomeFragment extends Fragment { Log.d("HomeFragment", "already printed"); //画面遷移&ID受け渡し Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show(); - QrPrintFragment qrPrintFragment = new QrPrintFragment(); - replaceFragment(qrPrintFragment); + QrUtils qrUtils = new QrUtils(); + PrintHelper printHelper = new PrintHelper(requireContext()); + printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); + printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(sharedPreferences.getString("ID", "placeholder")), new PrintHelper.OnPrintFinishCallback() { + @Override + public void onFinish() { + Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); + } + }); return; - } else Log.d("HomeFragment", "not printed yet"); // debug - - String valueParent = parent.getText().toString();//変数に文字列を代入 - String valueBorn = born.getText().toString();//変数に文字列を代入 - Map user = new HashMap<>();//mapの宣言 - - 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 = sharedPreferences.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); - } - }); + } else { + String valueParent = parent.getText().toString();//変数に文字列を代入 + String valueBorn = born.getText().toString();//変数に文字列を代入 + Map user = new HashMap<>();//mapの宣言 + 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 = sharedPreferences.edit(); + // キー"alreadySaved"の値をtrueにする + e.putBoolean("alreadySaved", true); + //確定処理 + e.apply(); + //画面遷移&ID受け渡し + str_key = "" + documentReference.getId(); + Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show(); + QrUtils qrUtils = new QrUtils(); + PrintHelper printHelper = new PrintHelper(requireContext()); + printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); + printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(documentReference.getId()), new PrintHelper.OnPrintFinishCallback() { + @Override + public void onFinish() { + Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); + } + }); + } + }) + .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); @@ -182,11 +177,13 @@ public class HomeFragment extends Fragment { return view; } + + @Override public void onResume() { super.onResume(); Log.d("HomeFragment", "onResume: called"); - Cargettingonandoff();//メソッドCargettingonandoff()を実行 + updateUiState(getActivity().getSharedPreferences("app_situation", MODE_PRIVATE).getBoolean("car", false)); } //画面遷移メソッド @@ -203,29 +200,35 @@ public class HomeFragment extends Fragment { transaction.commit(); } - public void Cargettingonandoff() { - //共有プリファレンス全体の準備 - SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE); - //車の乗り降りを管理するtrue=乗車、false=降車 - Boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); - SharedPreferences.Editor e = sharedPreferences.edit(); + private void updateUiState(boolean state) { + Log.d("HomeFragment", "updateUiState: called"); + // Init + TextView tv; + FrameLayout fl; + try { + tv = requireView().findViewById(R.id.situation); + fl = getView().findViewById(R.id.situation_bg); + } catch (NullPointerException e) { + Log.d("HomeFragment", "updateUiState: view is null"); + return; + } 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); - - } else { + if (state) { //乗車状態にする fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); tv.setText(get_on); + } else { + //降車状態にする + fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); + tv.setText(get_off); } + } - + @Override + public void onEvent(boolean state) { + Log.d("HomeFragment", "onEvent: called"); + updateUiState(state); } } diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index be0ba76..f33c9e3 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -41,6 +41,8 @@ import androidx.core.content.res.ResourcesCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; @@ -53,6 +55,8 @@ public class MainActivity extends AppCompatActivity { BluetoothManager bluetoothManager; BluetoothAdapter bluetoothAdapter; + private HomeFragment homeFragment; + public static final String TAG = "InspirationQuote"; boolean flg = false; @@ -72,12 +76,15 @@ public class MainActivity extends AppCompatActivity { BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); + this.homeFragment = HomeFragment.newInstance("test", "tset"); + bottomNavigationView.setOnNavigationItemSelectedListener(v -> { if (v.getItemId() == findViewById(R.id.navigation_home).getId()) { getSupportFragmentManager().beginTransaction() - .replace(findViewById(R.id.fragmentContainerView).getId(), HomeFragment.newInstance("test", "tset")) + .replace(findViewById(R.id.fragmentContainerView).getId(), this.homeFragment) + .addToBackStack(null) .commit(); } else if (v.getItemId() == findViewById(R.id.navigation_QR).getId()) { getSupportFragmentManager().beginTransaction() @@ -88,10 +95,7 @@ public class MainActivity extends AppCompatActivity { .replace(findViewById(R.id.fragmentContainerView).getId(), NotificationFragment.newInstance("test", "test")) .commit(); } - return true; - - }); //Bluetooth検知機能 @@ -119,7 +123,7 @@ public class MainActivity extends AppCompatActivity { return; } DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得 -// initNotification(mDocRef); + initNotification(mDocRef); super.onResume(); } @@ -136,50 +140,36 @@ public class MainActivity extends AppCompatActivity { //共有プリファレンス 書き込みの準備 SharedPreferences.Editor E=sharedPreferences.edit(); //車の乗り降りを管理するtrue=乗車、false=降車 - boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); + boolean isInCar = sharedPreferences.getBoolean("car", false); if (flg && documentSnapshot != null && documentSnapshot.exists()) { String parent = documentSnapshot.getString("parent"); Log.d("nt", "レスポンスを検知しました1"); if (parent.equals("s")) {//FireBaseの更新情報が"S"のとき=サイト上で第三者ボタンが押されたとき - if(zyoukouzyoutai==false) {//いたずら防止 - //通知のやつ↓ - + if (isInCar) { int importance = NotificationManager.IMPORTANCE_DEFAULT; - NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance); - //説明・説明 ここに通知の説明を書くことができる↓ channel.setDescription("第3者からの通報を検知しました"); - 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();//確定処理 + if (isInCar) { + 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("親",zyoukouzyoutai); - fragment.setArguments(bundle); - getSupportFragmentManager() - .beginTransaction() - .add(android.R.id.content, fragment) - .commit(); - } + HomeFragment fragment = new HomeFragment(); + getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, fragment).commit(); + + homeFragment.onEvent(!isInCar); + } } flg = true; } @@ -258,4 +248,6 @@ public class MainActivity extends AppCompatActivity { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(enableBackButton); } + } + diff --git a/app/src/main/java/com/example/childguard/OnEventListener.java b/app/src/main/java/com/example/childguard/OnEventListener.java new file mode 100644 index 0000000..f093cb4 --- /dev/null +++ b/app/src/main/java/com/example/childguard/OnEventListener.java @@ -0,0 +1,6 @@ +package com.example.childguard; + +public interface OnEventListener { + + void onEvent(boolean state); +} diff --git a/app/src/main/java/com/example/childguard/QRFragment.java b/app/src/main/java/com/example/childguard/QRFragment.java index 0d694ae..5870462 100644 --- a/app/src/main/java/com/example/childguard/QRFragment.java +++ b/app/src/main/java/com/example/childguard/QRFragment.java @@ -78,20 +78,21 @@ public class QRFragment extends Fragment { private final ActivityResultLauncher fragmentLauncher = registerForActivityResult(new ScanContract(), result -> { - //result.getContents()でURLを入手 - //読み取ったQRコードがChiled Guard用サイトのドメインを含むかの判定 - if(!((result.getContents()).contains("https://practicefirestore1-8808c.web.app/"))) { + String contents = result.getContents(); + if (contents == null) { + Toast.makeText(getContext(), "QRコードが読み取れませんでした", Toast.LENGTH_LONG).show(); + } else if (!contents.contains("https://practicefirestore1-8808c.web.app/")) { Toast.makeText(getContext(), "Chiled Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show(); - } - else { + } else { //URLの表示 - Toast.makeText(getContext(), result.getContents(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getContext(), contents, Toast.LENGTH_SHORT).show(); //ブラウザを起動し、URL先のサイトを開く CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(requireContext(), Uri.parse(result.getContents())); + customTabsIntent.launchUrl(requireContext(), Uri.parse(contents)); } + getParentFragmentManager().popBackStack(); }); @Override @@ -101,11 +102,10 @@ public class QRFragment extends Fragment { View view = inflater.inflate(R.layout.fragment_qr, container, false); - Log.d("QRFragment", "onClick: called"); - //QRリーダ起動 - fragmentLauncher.launch(new ScanOptions()); - HomeFragment homeFragment=new HomeFragment(); - replaceFragment(homeFragment); + Log.d("QRFragment", "onClick: called"); + //QRリーダ起動 + fragmentLauncher.launch(new ScanOptions()); + return view; } //画面遷移メソッド diff --git a/app/src/main/java/com/example/childguard/QrPrintFragment.java b/app/src/main/java/com/example/childguard/QrPrintFragment.java index 32b224b..88ac807 100644 --- a/app/src/main/java/com/example/childguard/QrPrintFragment.java +++ b/app/src/main/java/com/example/childguard/QrPrintFragment.java @@ -137,7 +137,7 @@ public class QrPrintFragment extends Fragment { printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); printHelper.printBitmap("job_name", QRGazou); HomeFragment homeFragment=new HomeFragment(); - replaceFragment(homeFragment); +// replaceFragment(homeFragment); return view; } //画面遷移メソッド diff --git a/app/src/main/java/com/example/childguard/QrUtils.java b/app/src/main/java/com/example/childguard/QrUtils.java new file mode 100644 index 0000000..dc4f493 --- /dev/null +++ b/app/src/main/java/com/example/childguard/QrUtils.java @@ -0,0 +1,64 @@ +package com.example.childguard; + +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.BitmapDrawable; +import android.util.AndroidRuntimeException; +import android.util.Log; +import android.widget.ImageView; + +import androidx.print.PrintHelper; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.journeyapps.barcodescanner.BarcodeEncoder; + +public class QrUtils { + + private Context context; + + public QrUtils setContext(Context context) { + this.context = context; + return this; + } + + public Bitmap getBitmap(String key) { + Log.d("getBitmap", "getBitmap: " + key); + String KoteiURL = "https://practicefirestore1-8808c.web.app/?id="; + //すべてのドメイン + String AllURL; + //IdPrefにの値が初期値の場合 + AllURL=KoteiURL+key; + + int size = 1500; + Bitmap QRGazou; + Bitmap bitmapqr; + try { + //QRコード生成 + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + bitmapqr = barcodeEncoder.encodeBitmap(AllURL, BarcodeFormat.QR_CODE, size, size); + } catch (WriterException e) { + throw new AndroidRuntimeException("Barcode Error.", e); + } + // 画像合成の準備 + Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.a_group_qr_sos); + int width = bitmap.getWidth(); + int height = bitmap.getHeight(); + QRGazou = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + + // Canvasの準備 + Canvas canvas = new Canvas(QRGazou); + + // 画像のサイズの調整 + int disWidth = (width - bitmapqr.getWidth()) / 2; + int disHeight = (int) ((height - bitmapqr.getHeight()) / 1.3); + canvas.drawBitmap(bitmap, 0, 0, (Paint) null); + canvas.drawBitmap(bitmapqr, disWidth, disHeight, (Paint) null); // 画像合成 + //Androidからプリンターへ印刷指示を出すサポートライブラリ + return QRGazou; + } +}