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.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,11 +104,17 @@ 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
} else {
String valueParent = parent.getText().toString();//変数に文字列を代入
String valueBorn = born.getText().toString();//変数に文字列を代入
Map<String, String> user = new HashMap<>();//mapの宣言
@ -147,13 +141,15 @@ public class HomeFragment extends Fragment {
//画面遷移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);
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() {
@ -163,8 +159,7 @@ public class HomeFragment extends Fragment {
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);
}
}

View File

@ -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();//確定処理
}
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();
if (isInCar) {
E.putBoolean("car", false);
E.apply();
} else {
E.putBoolean("car", true);
E.apply();
}
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);
}
}

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(),
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
@ -104,8 +105,7 @@ public class QRFragment extends Fragment {
Log.d("QRFragment", "onClick: called");
//QRリーダ起動
fragmentLauncher.launch(new ScanOptions());
HomeFragment homeFragment=new HomeFragment();
replaceFragment(homeFragment);
return view;
}
//画面遷移メソッド

View File

@ -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;
}
//画面遷移メソッド

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