diff --git a/app/src/main/java/com/example/childguard/GenerateQrFragment.java b/app/src/main/java/com/example/childguard/GenerateQrFragment.java index ce9915f..ed71a65 100644 --- a/app/src/main/java/com/example/childguard/GenerateQrFragment.java +++ b/app/src/main/java/com/example/childguard/GenerateQrFragment.java @@ -1,12 +1,17 @@ package com.example.childguard; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import androidx.fragment.app.Fragment; +import androidx.print.PrintHelper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.Toast; /** * A simple {@link Fragment} subclass. @@ -18,9 +23,10 @@ public class GenerateQrFragment extends Fragment { public GenerateQrFragment() { // Required empty public constructor } - public static GenerateQrFragment newInstance() { + public static GenerateQrFragment newInstance(String key) { GenerateQrFragment fragment = new GenerateQrFragment(); Bundle args = new Bundle(); + args.putString("key", key); fragment.setArguments(args); return fragment; } @@ -40,7 +46,24 @@ public class GenerateQrFragment extends Fragment { @Override public void onViewCreated(View view, Bundle savedInstanceState) { + QrUtils qrUtils = new QrUtils(); + assert getArguments() != null; + Bitmap result = qrUtils.setContext(getContext()).getBitmap(getArguments().getString("key")); + ImageView imageView = view.findViewById(R.id.result_bitmap_image_view); + imageView.setImageBitmap(result); + + view.findViewById(R.id.button_print).setOnClickListener( v -> { + PrintHelper photoPrinter = new PrintHelper(requireContext()); + photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT); + photoPrinter.printBitmap("placeholder", result, () -> { + Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); + }); + }); + + view.findViewById(R.id.button_cancel).setOnClickListener( v -> { + getParentFragmentManager().popBackStack(); + }); } } \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index edb3989..fb9d1e3 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -134,7 +134,7 @@ public class HomeFragment extends Fragment implements OnEventListener{ transaction.commit(); } - private void updateUiState(boolean state) { + private boolean updateUiState(boolean state) { Log.d("HomeFragment", "updateUiState: called"); // Init TextView tv; @@ -144,10 +144,12 @@ public class HomeFragment extends Fragment implements OnEventListener{ fl = requireView().findViewById(R.id.situation_bg); } catch (NullPointerException e) { Log.d("HomeFragment", "updateUiState: view is null"); - return; - } catch (Exception e) { - Log.d("HomeFragment", "updateUiState: unknown error"); - return; + return false; + } catch (IllegalStateException e) { + Log.d("HomeFragment", "updateUiState: view is not attached"); +// getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, HomeFragment.newInstance("test", "test")).commit(); +// updateUiState(state); + return false; } String get_on = "\n乗車状態"; String get_off = "\n降車状態"; @@ -160,12 +162,14 @@ public class HomeFragment extends Fragment implements OnEventListener{ fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); tv.setText(get_off); } + + return true; } @Override - public void onEvent(boolean state) { + public boolean onEvent(boolean state) { Log.d("HomeFragment", "onEvent: called"); - updateUiState(state); + return 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 71f4007..a1121df 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -64,6 +64,8 @@ public class MainActivity extends AppCompatActivity { BluetoothManager bluetoothManager; BluetoothAdapter bluetoothAdapter; + DocumentReference mDocRef; + private HomeFragment homeFragment; public static final String TAG = "InspirationQuote"; @@ -157,14 +159,22 @@ public class MainActivity extends AppCompatActivity { @Override protected void onResume() { - SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); - String IdPref = sharedPreferences.getString("ID", null); - if (IdPref == null) { - Log.d("onResume", "ID not initialized."); - return; + Log.d("onResume", "called"); + if (mDocRef == null) { + Log.d("onResume", "mDocRef is null"); + SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); + String IdPref = sharedPreferences.getString("ID", null); + if (IdPref == null) { + Log.d("onResume", "ID not initialized."); + return; + } + mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得 + initNotification(mDocRef); + } + + if (mDocRef.getId().equals(null)) { + Log.d("onResume", "mDocRef.getId() is null"); } - DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得 - initNotification(mDocRef); super.onResume(); } @@ -205,8 +215,11 @@ public class MainActivity extends AppCompatActivity { E.putBoolean("car", true); E.apply(); } - - homeFragment.onEvent(!isInCar); + // SupportFragmentManagerが現在表示しているFragmentを取得 + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView); + if (fragment instanceof HomeFragment) { + ((HomeFragment) fragment).updateUiState(!isInCar); + } } } flg = true; diff --git a/app/src/main/java/com/example/childguard/OnEventListener.java b/app/src/main/java/com/example/childguard/OnEventListener.java index f093cb4..8bf8757 100644 --- a/app/src/main/java/com/example/childguard/OnEventListener.java +++ b/app/src/main/java/com/example/childguard/OnEventListener.java @@ -2,5 +2,5 @@ package com.example.childguard; public interface OnEventListener { - void onEvent(boolean state); + boolean onEvent(boolean state); } diff --git a/app/src/main/java/com/example/childguard/SettingFragment.java b/app/src/main/java/com/example/childguard/SettingFragment.java index fd65f32..6fd9aa9 100644 --- a/app/src/main/java/com/example/childguard/SettingFragment.java +++ b/app/src/main/java/com/example/childguard/SettingFragment.java @@ -74,10 +74,7 @@ public class SettingFragment extends Fragment { Log.d("HomeFragment", "already printed"); //画面遷移&ID受け渡し 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(sharedPreferences.getString("ID", "placeholder")), () -> Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show()); + getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(sharedPreferences.getString("ID", "none"))).commit(); } else { String valueParent = "placeholder"; String valueBorn = "placeholder"; @@ -107,15 +104,7 @@ public class SettingFragment extends Fragment { editor.apply(); 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(); - } - }); + getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(documentReference.getId())).commit(); }) .addOnFailureListener(e -> { //失敗したら