Compare commits

..
Author SHA1 Message Date
磯野 6ef92ad3c1 リスト化する機能をつけました。(一部微修正しました) 2024-01-22 04:23:50 +09:00
磯野 cf3da5b3e2 Merge remote-tracking branch 'origin/main' 2024-01-21 16:33:21 +09:00
it232115 b3972ef0e0 QR画像のサイズを調整 2024-01-20 14:31:16 +09:00
N-YOKU-jp ffd307233b Merge pull request #19 from child-guardian/housinnhennkougo
Housinnhennkougo
2024-01-19 14:12:11 +09:00
ろむねこ 3404e833e7 Merge pull request #18 from child-guardian/hotfix-1
Hotfix 1
2024-01-19 12:56:18 +09:00
r-ca 54950e9d38 fix 2024-01-19 12:53:19 +09:00
r-ca 9d431bd297 HomeFragmentがResumeされたときにローカルの最新を読み込むように 2024-01-19 12:53:01 +09:00
r-ca e84b21288f changessituationを廃止 2024-01-19 12:49:09 +09:00
ろむねこ b28911f386 Merge pull request #17 from child-guardian/housinnhennkougo
Housinnhennkougo
2024-01-19 12:20:35 +09:00
磯野 2575b074a7 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	app/src/main/java/com/example/childguard/MainActivity.java
2024-01-19 10:41:47 +09:00
磯野 3fa1fe412a 通知を送る機能を追加しました。 2024-01-16 11:21:54 +09:00
5 changed files with 122 additions and 39 deletions

No files matched your search

@@ -1,5 +1,6 @@
package com.example.childguard; package com.example.childguard;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@@ -76,6 +77,12 @@ public class HomeFragment extends Fragment implements OnEventListener{
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log.d("HomeFragment", "onResume: called"); 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);
} }
//画面遷移メソッド //画面遷移メソッド
@@ -20,6 +20,7 @@ import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.Toast; import android.widget.Toast;
@@ -52,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
public static final String TAG = "InspirationQuote"; public static final String TAG = "InspirationQuote";
private final ActivityResultLauncher<ScanOptions> QrLauncher = registerForActivityResult( private final ActivityResultLauncher<ScanOptions> QrLauncher = registerForActivityResult(
new ScanContract(), new ScanContract(),
result -> { result -> {
@@ -60,7 +62,7 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, "QRコードが読み取れませんでした", Toast.LENGTH_LONG).show(); Toast.makeText(this, "QRコードが読み取れませんでした", Toast.LENGTH_LONG).show();
} else { } else {
if (!contents.contains("https://practicefirestore1-8808c.web.app/")) { if (!contents.contains("https://practicefirestore1-8808c.web.app/")) {
Toast.makeText(this, "Chiled Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Child Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show();
} else { } else {
//URLの表示 //URLの表示
Toast.makeText(this, contents, Toast.LENGTH_SHORT).show(); Toast.makeText(this, contents, Toast.LENGTH_SHORT).show();
@@ -73,15 +75,17 @@ public class MainActivity extends AppCompatActivity {
} }
); );
@SuppressLint("MissingInflatedId")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
this.homeFragment = HomeFragment.newInstance("test", "tset"); this.homeFragment = HomeFragment.newInstance("test", "test");
bottomNavigationView.setOnNavigationItemSelectedListener(v -> bottomNavigationView.setOnNavigationItemSelectedListener(v ->
@@ -119,6 +123,7 @@ public class MainActivity extends AppCompatActivity {
}); });
//Bluetooth検知機能 //Bluetooth検知機能
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
@@ -131,13 +136,13 @@ public class MainActivity extends AppCompatActivity {
Log.d("BT", "Permission to connect bluetooth devices granted"); Log.d("BT", "Permission to connect bluetooth devices granted");
} }
registerReceiver(receiver, intentFilter); registerReceiver(receiver, intentFilter);
changessituation();
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
changessituation();
Log.d("onResume", "called"); Log.d("onResume", "called");
Log.d("onResume", "mDocRef is null"); Log.d("onResume", "mDocRef is null");
firebaselink(); firebaselink();
@@ -161,8 +166,9 @@ public class MainActivity extends AppCompatActivity {
Log.d("nt", "レスポンスを検知しました1"); Log.d("nt", "レスポンスを検知しました1");
//FireBaseで更新された情報の判定 //FireBaseで更新された情報の判定
if (documentSnapshot.getBoolean("isReported") == false) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき if (documentSnapshot.getBoolean("isReported") == false) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき
if (fragment instanceof HomeFragment) {//fragementがHomeFragmentのインスタンスかの判定 if (fragment instanceof HomeFragment) {//fragmentがHomeFragmentのインスタンスかの判定
changessituation();// changessituation()メソッドを処理→アプリ側の乗降状態を変化 // changessituation();// changessituation()メソッドを処理→アプリ側の乗降状態を変化
((HomeFragment) fragment).onEvent(!isInCar);
} }
} else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止 } else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
int importance = NotificationManager.IMPORTANCE_DEFAULT; int importance = NotificationManager.IMPORTANCE_DEFAULT;
@@ -302,17 +308,6 @@ public class MainActivity extends AppCompatActivity {
notificationManager.notify(R.string.app_name, builder.build());//通知の表示 notificationManager.notify(R.string.app_name, builder.build());//通知の表示
} }
public void changessituation() {//乗降状態の管理をするためにHomeFramgentを呼び出すメソッド
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
//共有プリファレンス 書き込みの準備
SharedPreferences.Editor E = sharedPreferences.edit();
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
((HomeFragment) fragment).onEvent(!isInCar);
}
@Override @Override
public void onStop() {//アプリをバックグラウンドにした時のメソッド public void onStop() {//アプリをバックグラウンドにした時のメソッド
super.onStop(); super.onStop();
@@ -36,14 +36,14 @@ public class QrUtils {
//IdPrefにの値が初期値の場合 //IdPrefにの値が初期値の場合
AllURL=KoteiURL+key; AllURL=KoteiURL+key;
int size = 1500;
int qrCodeSize = calculateQRCodeSize(); // 画面密度に応じてサイズを計算 int qrCodeSize = calculateQRCodeSize(); // 画面密度に応じてサイズを計算
Bitmap QRGazou; Bitmap QRGazou;
Bitmap bitmapqr; Bitmap bitmapqr;
try { try {
//QRコード生成 //QRコード生成
BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); 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) { } catch (WriterException e) {
throw new AndroidRuntimeException("Barcode Error.", e); throw new AndroidRuntimeException("Barcode Error.", e);
} }
@@ -58,7 +58,7 @@ public class QrUtils {
// 画像のサイズの調整 // 画像のサイズの調整
int disWidth = (width - bitmapqr.getWidth()) / 2; 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(bitmap, 0, 0, (Paint) null);
canvas.drawBitmap(bitmapqr, disWidth, disHeight, (Paint) null); // 画像合成 canvas.drawBitmap(bitmapqr, disWidth, disHeight, (Paint) null); // 画像合成
//Androidからプリンターへ印刷指示を出すサポートライブラリ //Androidからプリンターへ印刷指示を出すサポートライブラリ
@@ -72,7 +72,7 @@ public class QrUtils {
// 画面密度に基づいてQRコードのサイズを計算 // 画面密度に基づいてQRコードのサイズを計算
float density = context.getResources().getDisplayMetrics().density; float density = context.getResources().getDisplayMetrics().density;
return (int) (1500 * density); return (int) (800 * density);
} }
} }
@@ -1,6 +1,7 @@
package com.example.childguard; package com.example.childguard;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@@ -14,6 +15,7 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
@@ -25,10 +27,18 @@ import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -40,9 +50,76 @@ public class TestService extends Service {
public PeriodicTaskManager periodicTaskManager; public PeriodicTaskManager periodicTaskManager;
//時間を取得するやつ↓
public static String getNowDate() {
@SuppressLint("SimpleDateFormat") final DateFormat df = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
final Date date = new Date(System.currentTimeMillis());
return df.format(date);
}
@SuppressLint("NotifyDataSetChanged")
public void NotifityRecycle(){
RecyclerView recyclerView = recyclerView().findViewById(R.id.recyclerView1);
//RecyclerViewのサイズを固定
recyclerView.setHasFixedSize(true);
//RecyclerViewに区切り線を入れる
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(itemDecoration);
//レイアウトマネージャを設定
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//①リスト構造(String型の可変長の配列)を宣言
ArrayList<String> arrayList = new ArrayList<>();
//③Adapterとリスト構造を結び付け
//RecyclerAdapterクラスを呼び出す
RecyclerAdapter2 adapter = new RecyclerAdapter2(arrayList, null);
//④RecyclerViewとAdapterの結び付け
recyclerView.setAdapter(adapter);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
arrayList.add(0,"通知を検知しました: " + getNowDate());
//一番下に値が追加されたことをAdapterが画面に通知
adapter.notifyItemInserted(arrayList.size());
//共有プリファレンス 書き込みの準備
SharedPreferences.Editor e = pref.edit();
//リストを,区切りで結合する
String str = String.join(",", arrayList);
//変数名currentに、値の代入
e.putString("current", str.toString());
//確定処理
e.apply();
String strTo = pref.getString("current", null);
if (strTo == null) return;
String[] list = strTo.split(",");
arrayList.clear();
arrayList.addAll(Arrays.asList(list));
adapter.notifyDataSetChanged();
}
private Activity recyclerView() {
return null;
}
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
NotifityRecycle();
//共有プリファレンス全体の準備 //共有プリファレンス全体の準備
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE); SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
String IdPref = sharedPreferences.getString("ID", null);//アプリに記録されているIDの取得 String IdPref = sharedPreferences.getString("ID", null);//アプリに記録されているIDの取得
@@ -53,6 +130,8 @@ public class TestService extends Service {
initNotification(mDocRef);//現在の位置を引数に initNotification()を処理 initNotification(mDocRef);//現在の位置を引数に initNotification()を処理
} }
return flags; return flags;
} }
private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理 private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理
@@ -73,26 +152,16 @@ public class TestService extends Service {
E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定 E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
E.apply();//確定処理 E.apply();//確定処理
Log.d("nt", "レスポンスを検知しました1"); 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にする 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();//通知に関する設定のメソッド NotificationSetting();//通知に関する設定のメソッド
Notification(getApplicationContext());//通知を行うメソッド Notification(getApplicationContext());//通知を行うメソッド
} else if(isInCar){//Bluetoothの切断後5分以上乗車状態のままのとき→QRコード読み取りを忘れているとき→置き去り発生 }
} else if(!isInCar){//isReportedがfalse=サイト上で降車状態のとき
ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
periodicTaskManager.startPeriodicTask();//通知のループをストップする
E.putBoolean("isInCarPref", !documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
E.apply();//確定処理
}else { }else {
ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
periodicTaskManager.stopPeriodicTask();//5分毎に通知を行う
E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
E.apply();//確定処理
} }
} }
@@ -183,7 +252,7 @@ public class TestService extends Service {
@SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID") @SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
.setSmallIcon(android.R.drawable.ic_menu_info_details) .setSmallIcon(android.R.drawable.ic_menu_info_details)
.setContentTitle("子供の置き去りをしていませんか?")//通知のタイトル .setContentTitle("子供の置き去りをしていませんか?")//通知のタイトル
.setContentText("Bluetootと車の切断から"+time+"分が経過しました")//通知の本文 .setContentText("Bluetoothと車の切断から"+time+"分が経過しました")//通知の本文
.setContentIntent(pendingIntent)//通知をタップするとActivityへ移動する .setContentIntent(pendingIntent)//通知をタップするとActivityへ移動する
.setAutoCancel(true)//通知をタップすると削除する .setAutoCancel(true)//通知をタップすると削除する
.setPriority(NotificationCompat.PRIORITY_HIGH) // プライオリティを高く設定 .setPriority(NotificationCompat.PRIORITY_HIGH) // プライオリティを高く設定
@@ -233,7 +302,7 @@ public class TestService extends Service {
int time=sharedPreferences.getInt("time",0); int time=sharedPreferences.getInt("time",0);
//共有プリファレンス 書き込みの準備 //共有プリファレンス 書き込みの準備
SharedPreferences.Editor E = sharedPreferences.edit(); SharedPreferences.Editor E = sharedPreferences.edit();
if(time==0) {//Bluetoot切断からの時間経過(5分刻み) if(time==0) {//Bluetooth切断からの時間経過(5分刻み)
E.putInt("time",5); E.putInt("time",5);
E.apply();; E.apply();;
}else { }else {
@@ -6,11 +6,19 @@
tools:context=".NotificationFragment"> tools:context=".NotificationFragment">
<!-- TODO: Update blank fragment layout --> <!-- TODO: Update blank fragment layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="9" android:layout_weight="9">
>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1" android:id="@+id/recyclerView1"
@@ -20,4 +28,8 @@
</LinearLayout> </LinearLayout>
</LinearLayout>
</FrameLayout> </FrameLayout>