hotfix: 全体的なリファクタリング, 切り替えに関する不具合の修正

This commit is contained in:
ろむねこ 2024-01-17 16:25:31 +09:00
parent 9649cc3a6e
commit 38b509f5d2
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168
6 changed files with 187 additions and 122 deletions

View File

@ -3,16 +3,9 @@ package com.example.childguard;
import static android.content.ContentValues.TAG; import static android.content.ContentValues.TAG;
import static android.content.Context.MODE_PRIVATE; 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.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -20,8 +13,6 @@ import androidx.fragment.app.FragmentTransaction;
import androidx.print.PrintHelper; import androidx.print.PrintHelper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.PrecomputedText;
import android.util.AndroidRuntimeException;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -32,10 +23,6 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; 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.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.OnSuccessListener;
@ -51,7 +38,7 @@ import java.util.Objects;
* Use the {@link HomeFragment#newInstance} factory method to * Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment. * create an instance of this fragment.
*/ */
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment implements OnEventListener{
FirebaseFirestore db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ FirebaseFirestore db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
// TODO: Rename parameter arguments, choose names that match // TODO: Rename parameter arguments, choose names that match
@ -63,6 +50,7 @@ public class HomeFragment extends Fragment {
private String str_key; private String str_key;
private String mParam2; private String mParam2;
public HomeFragment() { public HomeFragment() {
// Required empty public constructor // Required empty public constructor
} }
@ -116,11 +104,17 @@ public class HomeFragment 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();
QrPrintFragment qrPrintFragment = new QrPrintFragment(); QrUtils qrUtils = new QrUtils();
replaceFragment(qrPrintFragment); 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; return;
} else Log.d("HomeFragment", "not printed yet"); // debug } else {
String valueParent = parent.getText().toString();//変数に文字列を代入 String valueParent = parent.getText().toString();//変数に文字列を代入
String valueBorn = born.getText().toString();//変数に文字列を代入 String valueBorn = born.getText().toString();//変数に文字列を代入
Map<String, String> user = new HashMap<>();//mapの宣言 Map<String, String> user = new HashMap<>();//mapの宣言
@ -147,13 +141,15 @@ public class HomeFragment extends Fragment {
//画面遷移ID受け渡し //画面遷移ID受け渡し
str_key = "" + documentReference.getId(); str_key = "" + documentReference.getId();
Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show();
QrPrintFragment qrPrintFragment = new QrPrintFragment(); QrUtils qrUtils = new QrUtils();
Bundle bundle = new Bundle(); PrintHelper printHelper = new PrintHelper(requireContext());
bundle.putString("STR_KEY", str_key); printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
//値を書き込む printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(documentReference.getId()), new PrintHelper.OnPrintFinishCallback() {
qrPrintFragment.setArguments(bundle); @Override
replaceFragment(qrPrintFragment); public void onFinish() {
Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show();
}
});
} }
}) })
.addOnFailureListener(new OnFailureListener() { .addOnFailureListener(new OnFailureListener() {
@ -163,8 +159,7 @@ public class HomeFragment extends Fragment {
Log.w(TAG, "Error adding document", e); Log.w(TAG, "Error adding document", e);
} }
}); });
}
}); });
//bluetooth設定ボタンの処理 //bluetooth設定ボタンの処理
Button bt2 = view.findViewById(R.id.Bluetooth_setup); Button bt2 = view.findViewById(R.id.Bluetooth_setup);
@ -182,11 +177,13 @@ public class HomeFragment extends Fragment {
return view; return view;
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log.d("HomeFragment", "onResume: called"); 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(); transaction.commit();
} }
public void Cargettingonandoff() { private void updateUiState(boolean state) {
//共有プリファレンス全体の準備 Log.d("HomeFragment", "updateUiState: called");
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE); // Init
//車の乗り降りを管理するtrue=乗車false=降車 TextView tv;
Boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); FrameLayout fl;
SharedPreferences.Editor e = sharedPreferences.edit(); 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_on = "\n乗車状態";
String get_off = "\n降車状態"; String get_off = "\n降車状態";
TextView tv = getView().findViewById(R.id.situation); if (state) {
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 {
//乗車状態にする //乗車状態にする
fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null));
tv.setText(get_on); 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);
} }
} }

View File

@ -41,6 +41,8 @@ import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.Insets; import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; 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.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.DocumentSnapshot;
@ -53,6 +55,8 @@ public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager; BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter; BluetoothAdapter bluetoothAdapter;
private HomeFragment homeFragment;
public static final String TAG = "InspirationQuote"; public static final String TAG = "InspirationQuote";
boolean flg = false; boolean flg = false;
@ -72,12 +76,15 @@ public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
this.homeFragment = HomeFragment.newInstance("test", "tset");
bottomNavigationView.setOnNavigationItemSelectedListener(v -> bottomNavigationView.setOnNavigationItemSelectedListener(v ->
{ {
if (v.getItemId() == findViewById(R.id.navigation_home).getId()) { if (v.getItemId() == findViewById(R.id.navigation_home).getId()) {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(findViewById(R.id.fragmentContainerView).getId(), HomeFragment.newInstance("test", "tset")) .replace(findViewById(R.id.fragmentContainerView).getId(), this.homeFragment)
.addToBackStack(null)
.commit(); .commit();
} else if (v.getItemId() == findViewById(R.id.navigation_QR).getId()) { } else if (v.getItemId() == findViewById(R.id.navigation_QR).getId()) {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
@ -88,10 +95,7 @@ public class MainActivity extends AppCompatActivity {
.replace(findViewById(R.id.fragmentContainerView).getId(), NotificationFragment.newInstance("test", "test")) .replace(findViewById(R.id.fragmentContainerView).getId(), NotificationFragment.newInstance("test", "test"))
.commit(); .commit();
} }
return true; return true;
}); });
//Bluetooth検知機能 //Bluetooth検知機能
@ -119,7 +123,7 @@ public class MainActivity extends AppCompatActivity {
return; return;
} }
DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得 DocumentReference mDocRef = FirebaseFirestore.getInstance().document("users/" + IdPref);//現在の位置を取得
// initNotification(mDocRef); initNotification(mDocRef);
super.onResume(); super.onResume();
} }
@ -136,50 +140,36 @@ public class MainActivity extends AppCompatActivity {
//共有プリファレンス 書き込みの準備 //共有プリファレンス 書き込みの準備
SharedPreferences.Editor E=sharedPreferences.edit(); SharedPreferences.Editor E=sharedPreferences.edit();
//車の乗り降りを管理するtrue=乗車false=降車 //車の乗り降りを管理するtrue=乗車false=降車
boolean zyoukouzyoutai = sharedPreferences.getBoolean("car", false); boolean isInCar = sharedPreferences.getBoolean("car", false);
if (flg && documentSnapshot != null && documentSnapshot.exists()) { if (flg && documentSnapshot != null && documentSnapshot.exists()) {
String parent = documentSnapshot.getString("parent"); String parent = documentSnapshot.getString("parent");
Log.d("nt", "レスポンスを検知しました1"); Log.d("nt", "レスポンスを検知しました1");
if (parent.equals("s")) {//FireBaseの更新情報が"S"のときサイト上で第三者ボタンが押されたとき if (parent.equals("s")) {//FireBaseの更新情報が"S"のときサイト上で第三者ボタンが押されたとき
if(zyoukouzyoutai==false) {//いたずら防止 if (isInCar) {
//通知のやつ
int importance = NotificationManager.IMPORTANCE_DEFAULT; int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance); NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
//説明説明 ここに通知の説明を書くことができる
channel.setDescription("第3者からの通報を検知しました"); channel.setDescription("第3者からの通報を検知しました");
NotificationManager notificationManager = getSystemService(NotificationManager.class); NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
//通知のやつ
Log.d("nt", "レスポンスを検知しました2"); Log.d("nt", "レスポンスを検知しました2");
notifyMain(); notifyMain();
}
} else {
if (isInCar) {
E.putBoolean("car", false);
E.apply();
} else {
E.putBoolean("car", true);
E.apply();
}
}
} 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(); HomeFragment fragment = new HomeFragment();
Bundle bundle = new Bundle(); getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, fragment).commit();
bundle.putBoolean("",zyoukouzyoutai);
fragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, fragment)
.commit();
}
homeFragment.onEvent(!isInCar);
}
} }
flg = true; flg = true;
} }
@ -258,4 +248,6 @@ public class MainActivity extends AppCompatActivity {
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(enableBackButton); actionBar.setDisplayHomeAsUpEnabled(enableBackButton);
} }
} }

View File

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

View File

@ -78,20 +78,21 @@ public class QRFragment extends Fragment {
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(), private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> { result -> {
//result.getContents()でURLを入手 String contents = result.getContents();
//読み取ったQRコードがChiled Guard用サイトのドメインを含むかの判定 if (contents == null) {
if(!((result.getContents()).contains("https://practicefirestore1-8808c.web.app/"))) { 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(); Toast.makeText(getContext(), "Chiled Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show();
} } else {
else {
//URLの表示 //URLの表示
Toast.makeText(getContext(), result.getContents(), Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), contents, Toast.LENGTH_SHORT).show();
//ブラウザを起動しURL先のサイトを開く //ブラウザを起動しURL先のサイトを開く
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build(); CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(requireContext(), Uri.parse(result.getContents())); customTabsIntent.launchUrl(requireContext(), Uri.parse(contents));
} }
getParentFragmentManager().popBackStack();
}); });
@Override @Override
@ -104,8 +105,7 @@ public class QRFragment extends Fragment {
Log.d("QRFragment", "onClick: called"); Log.d("QRFragment", "onClick: called");
//QRリーダ起動 //QRリーダ起動
fragmentLauncher.launch(new ScanOptions()); fragmentLauncher.launch(new ScanOptions());
HomeFragment homeFragment=new HomeFragment();
replaceFragment(homeFragment);
return view; return view;
} }
//画面遷移メソッド //画面遷移メソッド

View File

@ -137,7 +137,7 @@ public class QrPrintFragment extends Fragment {
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
printHelper.printBitmap("job_name", QRGazou); printHelper.printBitmap("job_name", QRGazou);
HomeFragment homeFragment=new HomeFragment(); HomeFragment homeFragment=new HomeFragment();
replaceFragment(homeFragment); // replaceFragment(homeFragment);
return view; return view;
} }
//画面遷移メソッド //画面遷移メソッド

View File

@ -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;
}
}