This commit is contained in:
ろむねこ 2024-01-17 18:58:25 +09:00
parent 6d636c514f
commit d6af5b935b
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168
5 changed files with 60 additions and 31 deletions

View File

@ -1,12 +1,17 @@
package com.example.childguard; package com.example.childguard;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.print.PrintHelper;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
@ -18,9 +23,10 @@ public class GenerateQrFragment extends Fragment {
public GenerateQrFragment() { public GenerateQrFragment() {
// Required empty public constructor // Required empty public constructor
} }
public static GenerateQrFragment newInstance() { public static GenerateQrFragment newInstance(String key) {
GenerateQrFragment fragment = new GenerateQrFragment(); GenerateQrFragment fragment = new GenerateQrFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("key", key);
fragment.setArguments(args); fragment.setArguments(args);
return fragment; return fragment;
} }
@ -40,7 +46,24 @@ public class GenerateQrFragment extends Fragment {
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { 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();
});
} }
} }

View File

@ -134,7 +134,7 @@ public class HomeFragment extends Fragment implements OnEventListener{
transaction.commit(); transaction.commit();
} }
private void updateUiState(boolean state) { private boolean updateUiState(boolean state) {
Log.d("HomeFragment", "updateUiState: called"); Log.d("HomeFragment", "updateUiState: called");
// Init // Init
TextView tv; TextView tv;
@ -144,10 +144,12 @@ public class HomeFragment extends Fragment implements OnEventListener{
fl = requireView().findViewById(R.id.situation_bg); fl = requireView().findViewById(R.id.situation_bg);
} catch (NullPointerException e) { } catch (NullPointerException e) {
Log.d("HomeFragment", "updateUiState: view is null"); Log.d("HomeFragment", "updateUiState: view is null");
return; return false;
} catch (Exception e) { } catch (IllegalStateException e) {
Log.d("HomeFragment", "updateUiState: unknown error"); Log.d("HomeFragment", "updateUiState: view is not attached");
return; // getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, HomeFragment.newInstance("test", "test")).commit();
// updateUiState(state);
return false;
} }
String get_on = "\n乗車状態"; String get_on = "\n乗車状態";
String get_off = "\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)); fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null));
tv.setText(get_off); tv.setText(get_off);
} }
return true;
} }
@Override @Override
public void onEvent(boolean state) { public boolean onEvent(boolean state) {
Log.d("HomeFragment", "onEvent: called"); Log.d("HomeFragment", "onEvent: called");
updateUiState(state); return updateUiState(state);
} }
} }

View File

@ -64,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager; BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter; BluetoothAdapter bluetoothAdapter;
DocumentReference mDocRef;
private HomeFragment homeFragment; private HomeFragment homeFragment;
public static final String TAG = "InspirationQuote"; public static final String TAG = "InspirationQuote";
@ -157,14 +159,22 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); Log.d("onResume", "called");
String IdPref = sharedPreferences.getString("ID", null); if (mDocRef == null) {
if (IdPref == null) { Log.d("onResume", "mDocRef is null");
Log.d("onResume", "ID not initialized."); SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
return; 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(); super.onResume();
} }
@ -205,8 +215,11 @@ public class MainActivity extends AppCompatActivity {
E.putBoolean("car", true); E.putBoolean("car", true);
E.apply(); E.apply();
} }
// SupportFragmentManagerが現在表示しているFragmentを取得
homeFragment.onEvent(!isInCar); Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
if (fragment instanceof HomeFragment) {
((HomeFragment) fragment).updateUiState(!isInCar);
}
} }
} }
flg = true; flg = true;

View File

@ -2,5 +2,5 @@ package com.example.childguard;
public interface OnEventListener { public interface OnEventListener {
void onEvent(boolean state); boolean onEvent(boolean state);
} }

View File

@ -74,10 +74,7 @@ public class SettingFragment extends Fragment {
Log.d("HomeFragment", "already printed"); Log.d("HomeFragment", "already printed");
//画面遷移ID受け渡し //画面遷移ID受け渡し
Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show();
QrUtils qrUtils = new QrUtils(); getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(sharedPreferences.getString("ID", "none"))).commit();
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());
} else { } else {
String valueParent = "placeholder"; String valueParent = "placeholder";
String valueBorn = "placeholder"; String valueBorn = "placeholder";
@ -107,15 +104,7 @@ public class SettingFragment extends Fragment {
editor.apply(); editor.apply();
Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show();
QrUtils qrUtils = new QrUtils(); getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(documentReference.getId())).commit();
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(e -> { .addOnFailureListener(e -> {
//失敗したら //失敗したら