Compare commits

..
9 Commits
5 changed files with 42 additions and 128 deletions

No files matched your search

@@ -1,5 +1,6 @@
package com.example.childguard;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
@@ -14,9 +15,6 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
@@ -65,6 +63,7 @@ public class HomeFragment extends Fragment implements OnEventListener{
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
@@ -72,52 +71,20 @@ public class HomeFragment extends Fragment implements OnEventListener{
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
view.findViewById(R.id.situation_bg).setOnClickListener(v -> {
if (updateStateByServer()) {
Log.d("HomeFragment", "onCreateView: updateStateByServer() success");
} else {
Log.d("HomeFragment", "onCreateView: updateStateByServer() failed");
}
});
return view;
}
// TODO: updateUiState()を切り出す
private boolean updateStateByServer() {
SharedPreferences pref = requireActivity().getSharedPreferences("app_situation", requireActivity().MODE_PRIVATE);
String key = pref.getString("ID", null);
DocumentReference documentReference;
if (key == null) {
Log.d("HomeFragment", "updateStateCache: key is null");
return false;
} else {
Log.d("HomeFragment", "updateStateCache: key is " + key);
documentReference = FirebaseFirestore.getInstance().document("status/" + key);
documentReference.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Log.d("HomeFragment", "updateStateCache: get document success");
Log.d("HomeFragment", "updateStateCache: isInCar is " + task.getResult().getBoolean("isInCar"));
if (pref.getBoolean("isInCar", false) != task.getResult().getBoolean("isInCar")) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isInCar", task.getResult().getBoolean("isInCar"));
editor.apply();
}
updateUiState(pref.getBoolean("isInCar", false)); // UpdateUI
} else {
Log.d("HomeFragment", "updateStateCache: get document failed");
}
});
}
return true;
}
@Override
public void onResume() {
super.onResume();
Log.d("HomeFragment", "onResume: called");
this.updateUiState(getIsInCarLocal());
}
private boolean getIsInCarLocal() {
SharedPreferences pref = requireActivity().getSharedPreferences("app_situation", requireActivity().MODE_PRIVATE);
return pref.getBoolean("isInCar", false);
}
//画面遷移メソッド
@@ -153,7 +120,7 @@ public class HomeFragment extends Fragment implements OnEventListener{
}
String get_on = "\n乗車状態";
String get_off = "\n降車状態";
if (isInCar) {
if (!isInCar) {
//乗車状態にする
fl.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null));
tv.setText(get_on);
@@ -24,7 +24,6 @@ import android.widget.FrameLayout;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.ActivityCompat;
@@ -37,9 +36,6 @@ import com.google.firebase.firestore.FirebaseFirestore;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
// Manifest
import android.Manifest;
import java.util.HashMap;
import java.util.Map;
@@ -83,11 +79,6 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
if (!hasPermissions()) {
requestPermissions();
}
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
this.homeFragment = HomeFragment.newInstance("test", "tset");
@@ -103,12 +94,7 @@ public class MainActivity extends AppCompatActivity {
.commit();
firebaselink();
} else if (v.getItemId() == findViewById(R.id.navigation_notification).getId()) {
findViewById(R.id.fab_scan_qr_code).setVisibility(FrameLayout.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(findViewById(R.id.fragmentContainerView).getId(), NotificationFragment.newInstance("test", "test"))
.addToBackStack(null)
.commit();
} else if (v.getItemId() == findViewById(R.id.navigation_settings).getId()) {
findViewById(R.id.fab_scan_qr_code).setVisibility(FrameLayout.GONE);
getSupportFragmentManager().beginTransaction()
@@ -116,6 +102,7 @@ public class MainActivity extends AppCompatActivity {
.addToBackStack(null)
.commit();
}
firebaselink();
return true;
});
@@ -146,50 +133,10 @@ public class MainActivity extends AppCompatActivity {
protected void onResume() {
super.onResume();
Log.d("onResume", "called");
Log.d("onResume", "mDocRef is null");
firebaselink();
}
private boolean hasPermissions() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.POST_NOTIFICATIONS,
Manifest.permission.VIBRATE,
Manifest.permission.FOREGROUND_SERVICE
},
2);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 2) {
if (!hasPermissions()) {
Toast.makeText(this, "Permissions not granted.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Permissions granted.", Toast.LENGTH_SHORT).show();
}
}
}
private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理
// 共有プリファレンス全体の準備
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
@@ -208,9 +155,10 @@ public class MainActivity extends AppCompatActivity {
Log.d("nt", "レスポンスを検知しました1");
//FireBaseで更新された情報の判定
if (documentSnapshot.getBoolean("isReported") == false) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき
// if (fragment instanceof HomeFragment) {//fragementがHomeFragmentのインスタンスかの判定
// ((HomeFragment) fragment).onEvent(!isInCar);//onEvent()メソッドを処理→HomeFragmentのonEvent()メソッドを処理
// }
if (fragment instanceof HomeFragment) {//fragementがHomeFragmentのインスタンスかの判定
// changessituation();// changessituation()メソッドを処理→アプリ側の乗降状態を変化
((HomeFragment) fragment).onEvent(!isInCar);
}
} else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
@@ -349,13 +297,12 @@ public class MainActivity extends AppCompatActivity {
notificationManager.notify(R.string.app_name, builder.build());//通知の表示
}
// @Override
// public void onStop() {//アプリをバックグラウンドにした時のメソッド
// super.onStop();
// Intent intent = new Intent(getApplication(), TestService.class);
// startService(intent);//TestServiceを起動
// }
@Override
public void onStop() {//アプリをバックグラウンドにした時のメソッド
super.onStop();
Intent intent = new Intent(getApplication(), TestService.class);
startService(intent);//TestServiceを起動
}
}
@@ -36,14 +36,14 @@ public class QrUtils {
//IdPrefにの値が初期値の場合
AllURL=KoteiURL+key;
int size = 1500;
int qrCodeSize = calculateQRCodeSize(); // 画面密度に応じてサイズを計算
Bitmap QRGazou;
Bitmap bitmapqr;
try {
//QRコード生成
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmapqr = barcodeEncoder.encodeBitmap(AllURL, BarcodeFormat.QR_CODE, size, size);
bitmapqr = barcodeEncoder.encodeBitmap(AllURL, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize);
} catch (WriterException e) {
throw new AndroidRuntimeException("Barcode Error.", e);
}
@@ -58,7 +58,7 @@ public class QrUtils {
// 画像のサイズの調整
int disWidth = (width - bitmapqr.getWidth()) / 2;
int disHeight = (int) ((height - bitmapqr.getHeight()) / 1.4);
int disHeight = (int) ((height - bitmapqr.getHeight()) / 1.5);
canvas.drawBitmap(bitmap, 0, 0, (Paint) null);
canvas.drawBitmap(bitmapqr, disWidth, disHeight, (Paint) null); // 画像合成
//Androidからプリンターへ印刷指示を出すサポートライブラリ
@@ -72,7 +72,7 @@ public class QrUtils {
// 画面密度に基づいてQRコードのサイズを計算
float density = context.getResources().getDisplayMetrics().density;
return (int) (1500 * density);
return (int) (800 * density);
}
}
@@ -20,6 +20,7 @@ import android.os.Looper;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
@@ -72,22 +73,16 @@ public class TestService extends Service {
E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
E.apply();//確定処理
Log.d("nt", "レスポンスを検知しました1");
if (documentSnapshot.getBoolean("isReported") == true && isInCar) {//isReportedがtrueかつisInCarがtrueのとき=サイト上で第三者ボタンが押されたときに乗車状態のとき
if (isInCar) {//isReportedがtrue=サイト上で乗車状態のとき
if (documentSnapshot.getBoolean("isReported")) {
ResetReported();// ResetReported();を処理→FireBaseのisReportedをfalseにする
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");
NotificationSetting();//通知に関する設定のメソッド
Notification(getApplicationContext());//通知を行うメソッド
} else if(isInCar){//Bluetoothの切断後5分以上乗車状態のままのとき→QRコード読み取りを忘れているとき→置き去り発生
}
} else if(!isInCar){//isReportedがfalse=サイト上で降車状態のとき
ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
periodicTaskManager.startPeriodicTask();//通知のループをストップする
}else {
ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
periodicTaskManager.stopPeriodicTask();//5分毎に通知を行う
}
}
@@ -211,7 +206,7 @@ public class TestService extends Service {
}
public class PeriodicTaskManager {//Bluetoothの切断後に乗車状態にならなかった場合に5分毎に通知を送るメソッド
private static final long INTERVAL = 5 *60* 1000; //300秒
private static final long INTERVAL = 5 * 1000; //300秒
private final Handler handler;
private final Runnable periodicTask;
@@ -232,7 +227,7 @@ public class TestService extends Service {
E.putInt("time",5);
E.apply();;
}else {
E.putInt("time",time*2);
E.putInt("time",time+5);
E.apply();
}
@@ -251,6 +246,14 @@ public class TestService extends Service {
}
public void stopPeriodicTask() {
//共有プリファレンス全体の準備
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
int time=sharedPreferences.getInt("time",0);
//共有プリファレンス 書き込みの準備
SharedPreferences.Editor E = sharedPreferences.edit();
E.putInt("time",0);
E.apply();
// 定期的な処理の停止
handler.removeCallbacks(periodicTask);
}
+1 -4
View File
@@ -4,10 +4,7 @@
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="home"/>
<item
android:id="@+id/navigation_notification"
android:icon="@drawable/notifications_fill0_wght400_grad0_opsz24"
android:title="notification"/>
<item
android:id="@+id/navigation_settings"
android:icon="@drawable/baseline_app_settings_alt_24"