Housinnhennkougo #17
@ -1,37 +1,17 @@
 | 
			
		||||
package com.example.childguard;
 | 
			
		||||
 | 
			
		||||
import static android.content.ContentValues.TAG;
 | 
			
		||||
import static android.content.Context.MODE_PRIVATE;
 | 
			
		||||
 | 
			
		||||
import android.content.SharedPreferences;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.view.ViewGroup;
 | 
			
		||||
import android.widget.FrameLayout;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
 | 
			
		||||
import androidx.core.content.res.ResourcesCompat;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
import androidx.fragment.app.FragmentManager;
 | 
			
		||||
import androidx.fragment.app.FragmentTransaction;
 | 
			
		||||
import androidx.print.PrintHelper;
 | 
			
		||||
 | 
			
		||||
import android.preference.PreferenceManager;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.view.ViewGroup;
 | 
			
		||||
import android.widget.Button;
 | 
			
		||||
import android.widget.FrameLayout;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import com.google.android.gms.tasks.OnFailureListener;
 | 
			
		||||
import com.google.android.gms.tasks.OnSuccessListener;
 | 
			
		||||
import com.google.firebase.firestore.DocumentReference;
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestore;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A simple {@link Fragment} subclass.
 | 
			
		||||
 | 
			
		||||
@ -24,22 +24,15 @@ import android.widget.FrameLayout;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import androidx.activity.result.ActivityResultLauncher;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity;
 | 
			
		||||
import androidx.browser.customtabs.CustomTabsIntent;
 | 
			
		||||
import androidx.core.app.ActivityCompat;
 | 
			
		||||
import androidx.core.app.NotificationCompat;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
 | 
			
		||||
import com.google.android.gms.tasks.OnFailureListener;
 | 
			
		||||
import com.google.android.gms.tasks.OnSuccessListener;
 | 
			
		||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
 | 
			
		||||
import com.google.firebase.firestore.DocumentReference;
 | 
			
		||||
import com.google.firebase.firestore.DocumentSnapshot;
 | 
			
		||||
import com.google.firebase.firestore.EventListener;
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestore;
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestoreException;
 | 
			
		||||
import com.journeyapps.barcodescanner.ScanContract;
 | 
			
		||||
import com.journeyapps.barcodescanner.ScanOptions;
 | 
			
		||||
 | 
			
		||||
@ -59,8 +52,6 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
    public static final String TAG = "InspirationQuote";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private final ActivityResultLauncher<ScanOptions> QrLauncher = registerForActivityResult(
 | 
			
		||||
            new ScanContract(),
 | 
			
		||||
            result -> {
 | 
			
		||||
@ -136,8 +127,7 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            Log.d("BT", "No permission to connect bluetooth devices");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
        } else {
 | 
			
		||||
            Log.d("BT", "Permission to connect bluetooth devices granted");
 | 
			
		||||
        }
 | 
			
		||||
        registerReceiver(receiver, intentFilter);
 | 
			
		||||
@ -150,50 +140,45 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        changessituation();
 | 
			
		||||
        Log.d("onResume", "called");
 | 
			
		||||
        Log.d("onResume", "mDocRef is null");
 | 
			
		||||
       firebaselink();
 | 
			
		||||
        firebaselink();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理
 | 
			
		||||
        // 共有プリファレンス全体の準備
 | 
			
		||||
        SharedPreferences sharedPreferences = getSharedPreferences("app_situation",MODE_PRIVATE);
 | 
			
		||||
        SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
 | 
			
		||||
 | 
			
		||||
        mDocRef.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
 | 
			
		||||
        mDocRef.addSnapshotListener(this, (documentSnapshot, e) -> {
 | 
			
		||||
 | 
			
		||||
                Log.d("nt", "イベント開始");
 | 
			
		||||
                //共有プリファレンス 書き込みの準備
 | 
			
		||||
                SharedPreferences.Editor E = sharedPreferences.edit();
 | 
			
		||||
                //車の乗り降りを管理するtrue=乗車、false=降車
 | 
			
		||||
                if (documentSnapshot.exists()) {//exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
                    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
 | 
			
		||||
                    Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
 | 
			
		||||
                    E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
 | 
			
		||||
                    E.apply();//確定処理
 | 
			
		||||
                    Log.d("nt", "レスポンスを検知しました1");
 | 
			
		||||
                    //FireBaseで更新された情報の判定
 | 
			
		||||
                    if (documentSnapshot.getBoolean("isReported")==false ) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき
 | 
			
		||||
                        if (fragment instanceof HomeFragment) {//fragementがHomeFragmentのインスタンスかの判定
 | 
			
		||||
                            changessituation();//  changessituation()メソッドを処理→アプリ側の乗降状態を変化
 | 
			
		||||
                        }
 | 
			
		||||
                    }else if(isInCar){//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
 | 
			
		||||
                        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());//通知を行うメソッド
 | 
			
		||||
                        ResetReported();// ResetReported();メソッドを処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                        } else{//第三者ボタンが押されたときにisInCarがfalseのとき=降車状態のとき
 | 
			
		||||
                        ResetReported();// ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                        Log.d("nt", "何もなし" );
 | 
			
		||||
            Log.d("nt", "イベント開始");
 | 
			
		||||
            //共有プリファレンス 書き込みの準備
 | 
			
		||||
            SharedPreferences.Editor E = sharedPreferences.edit();
 | 
			
		||||
            //車の乗り降りを管理するtrue=乗車、false=降車
 | 
			
		||||
            if (documentSnapshot.exists()) {//exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
                Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
 | 
			
		||||
                Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
 | 
			
		||||
                E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
 | 
			
		||||
                E.apply();//確定処理
 | 
			
		||||
                Log.d("nt", "レスポンスを検知しました1");
 | 
			
		||||
                //FireBaseで更新された情報の判定
 | 
			
		||||
                if (documentSnapshot.getBoolean("isReported") == false) {//isReportedがfalseのとき=サイト上で保護者ボタンが押されたとき
 | 
			
		||||
                    if (fragment instanceof HomeFragment) {//fragementがHomeFragmentのインスタンスかの判定
 | 
			
		||||
                        changessituation();//  changessituation()メソッドを処理→アプリ側の乗降状態を変化
 | 
			
		||||
                    }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                } else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
 | 
			
		||||
                    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());//通知を行うメソッド
 | 
			
		||||
                    ResetReported();// ResetReported();メソッドを処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                } else {//第三者ボタンが押されたときにisInCarがfalseのとき=降車状態のとき
 | 
			
		||||
                    ResetReported();// ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                    Log.d("nt", "何もなし");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
@ -212,7 +197,6 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
                Log.d("BT", "No permission to connect bluetooth devices");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            String deviceName = device.getName();
 | 
			
		||||
            String deviceHardwareAddress = device.getAddress(); // MAC address
 | 
			
		||||
 | 
			
		||||
            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
 | 
			
		||||
@ -236,7 +220,7 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void firebaselink(){//Firebaseのドキュメントの取得
 | 
			
		||||
    public void firebaselink() {//Firebaseのドキュメントの取得
 | 
			
		||||
        //共有プリファレンス全体の準備
 | 
			
		||||
        SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
 | 
			
		||||
        String IdPref = sharedPreferences.getString("ID", null);////アプリに記録されているIDの取得
 | 
			
		||||
@ -248,25 +232,18 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    public void ResetReported(){//FireBaseのisReportedをfalseに初期化するメソッド
 | 
			
		||||
 | 
			
		||||
    public void ResetReported() {//FireBaseのisReportedをfalseに初期化するメソッド
 | 
			
		||||
        //共有プリファレンス全体の準備
 | 
			
		||||
        SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("app_situation", MODE_PRIVATE);
 | 
			
		||||
        String IdPref = sharedPreferences.getString("ID", null);//アプリに記録されているIDの取得
 | 
			
		||||
        db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
 | 
			
		||||
        DocumentReference isReported = db.collection("status").document(IdPref);//更新するドキュメントとの紐づけ
 | 
			
		||||
        Map<String, Boolean> DEFAULT_ITEM = new HashMap<>();//mapの宣言
 | 
			
		||||
        isReported.update("isReported",false).addOnSuccessListener(new OnSuccessListener<Void>() {//isReportedをfalseに更新
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onSuccess(Void unused) {
 | 
			
		||||
                Log.d(TAG, "DocumentSnapshot successfully updated!");
 | 
			
		||||
            }
 | 
			
		||||
        }).addOnFailureListener(new OnFailureListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Exception e) {
 | 
			
		||||
                Log.w(TAG, "Error updating document", e);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        //isReportedをfalseに更新
 | 
			
		||||
        isReported.update("isReported", false).addOnSuccessListener(unused -> Log.d(TAG, "DocumentSnapshot successfully updated!")).addOnFailureListener(e -> Log.w(TAG, "Error updating document", e));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void NotificationSetting() {//通知に関する設定の処理を行うメソッド
 | 
			
		||||
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
 | 
			
		||||
        //通知チャネルの実装
 | 
			
		||||
@ -277,6 +254,7 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        notificationManager.createNotificationChannel(channel);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Notification(Context context) {//実際に通知を行うメソッド
 | 
			
		||||
        final String CHANNEL_ID = "my_channel_id";
 | 
			
		||||
        // 通知がクリックされたときに送信されるIntent
 | 
			
		||||
@ -317,7 +295,7 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
 | 
			
		||||
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
 | 
			
		||||
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@ -325,16 +303,17 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void changessituation(){//乗降状態の管理をするためにHomeFramgentを呼び出すメソッド
 | 
			
		||||
    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);
 | 
			
		||||
        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() {//アプリをバックグラウンドにした時のメソッド
 | 
			
		||||
        super.onStop();
 | 
			
		||||
        Intent intent = new Intent(getApplication(), TestService.class);
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,5 @@
 | 
			
		||||
package com.example.childguard;
 | 
			
		||||
 | 
			
		||||
import android.bluetooth.BluetoothAdapter;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.SharedPreferences;
 | 
			
		||||
import android.preference.PreferenceManager;
 | 
			
		||||
@ -16,7 +15,6 @@ import androidx.appcompat.app.AlertDialog;
 | 
			
		||||
import androidx.recyclerview.widget.RecyclerView;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.function.Predicate;
 | 
			
		||||
 | 
			
		||||
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder> {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -5,21 +5,14 @@ import static android.content.Context.MODE_PRIVATE;
 | 
			
		||||
 | 
			
		||||
import android.content.SharedPreferences;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
import androidx.print.PrintHelper;
 | 
			
		||||
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.view.ViewGroup;
 | 
			
		||||
import android.widget.Button;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import com.google.android.gms.tasks.OnFailureListener;
 | 
			
		||||
import com.google.android.gms.tasks.OnSuccessListener;
 | 
			
		||||
import com.google.firebase.firestore.DocumentReference;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestore;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
@ -17,18 +17,12 @@ import android.os.Looper;
 | 
			
		||||
import android.os.Vibrator;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
import androidx.core.app.ActivityCompat;
 | 
			
		||||
import androidx.core.app.NotificationCompat;
 | 
			
		||||
 | 
			
		||||
import com.google.android.gms.tasks.OnFailureListener;
 | 
			
		||||
import com.google.android.gms.tasks.OnSuccessListener;
 | 
			
		||||
import com.google.firebase.firestore.DocumentReference;
 | 
			
		||||
import com.google.firebase.firestore.DocumentSnapshot;
 | 
			
		||||
import com.google.firebase.firestore.EventListener;
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestore;
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestoreException;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
@ -62,39 +56,35 @@ public class TestService extends Service {
 | 
			
		||||
        // 共有プリファレンス全体の準備
 | 
			
		||||
        SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
 | 
			
		||||
        //車の乗り降りを管理するtrue=乗車、false=降車
 | 
			
		||||
        mDocRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {//exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
        //exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
        mDocRef.addSnapshotListener((documentSnapshot, e) -> {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
 | 
			
		||||
 | 
			
		||||
                Log.d("nt", "イベント開始");
 | 
			
		||||
                //共有プリファレンス 書き込みの準備
 | 
			
		||||
                SharedPreferences.Editor E = sharedPreferences.edit();
 | 
			
		||||
                //車の乗り降りを管理するtrue=乗車、false=降車
 | 
			
		||||
                if (documentSnapshot.exists()) {//exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
                    Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
 | 
			
		||||
                    E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
 | 
			
		||||
                    E.apply();//確定処理
 | 
			
		||||
                    Log.d("nt", "レスポンスを検知しました1");
 | 
			
		||||
                    if (documentSnapshot.getBoolean("isReported") == true && isInCar == true) {//isReportedがtrueかつisInCarがtrueのとき=サイト上で第三者ボタンが押されたときに乗車状態のとき
 | 
			
		||||
                        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コード読み取りを忘れているとき→置き去り発生
 | 
			
		||||
                        ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                        periodicTaskManager.stopPeriodicTask();//通知のループをストップする
 | 
			
		||||
                    }else {
 | 
			
		||||
                        ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                        periodicTaskManager.startPeriodicTask();//5分毎に通知を行う
 | 
			
		||||
                    }
 | 
			
		||||
            Log.d("nt", "イベント開始");
 | 
			
		||||
            //共有プリファレンス 書き込みの準備
 | 
			
		||||
            SharedPreferences.Editor E = sharedPreferences.edit();
 | 
			
		||||
            //車の乗り降りを管理するtrue=乗車、false=降車
 | 
			
		||||
            if (documentSnapshot.exists()) {//exists()でdocumentSnapshotの中のファイルの存在の確認
 | 
			
		||||
                Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
 | 
			
		||||
                E.putBoolean("isInCarPref", documentSnapshot.getBoolean("isInCar"));//乗降状態の判定
 | 
			
		||||
                E.apply();//確定処理
 | 
			
		||||
                Log.d("nt", "レスポンスを検知しました1");
 | 
			
		||||
                if (documentSnapshot.getBoolean("isReported") == true && isInCar) {//isReportedがtrueかつisInCarがtrueのとき=サイト上で第三者ボタンが押されたときに乗車状態のとき
 | 
			
		||||
                    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コード読み取りを忘れているとき→置き去り発生
 | 
			
		||||
                    ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                    periodicTaskManager.startPeriodicTask();//通知のループをストップする
 | 
			
		||||
                }else {
 | 
			
		||||
                    ResetReported();//ResetReported();を処理→FireBaseのisReportedをfalseにする
 | 
			
		||||
                    periodicTaskManager.stopPeriodicTask();//5分毎に通知を行う
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
@ -110,17 +100,8 @@ public class TestService extends Service {
 | 
			
		||||
        DocumentReference isReported = db.collection("status").document(IdPref);//更新するドキュメントとの紐づけ
 | 
			
		||||
        Map<String, Boolean> DEFAULT_ITEM = new HashMap<>();//mapの宣言
 | 
			
		||||
        DEFAULT_ITEM.put("isReported", false);
 | 
			
		||||
        isReported.update("isReported", false).addOnSuccessListener(new OnSuccessListener<Void>() {//isReportedをfalseに更新
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onSuccess(Void unused) {
 | 
			
		||||
                Log.d(TAG, "DocumentSnapshot successfully updated!");
 | 
			
		||||
            }
 | 
			
		||||
        }).addOnFailureListener(new OnFailureListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Exception e) {
 | 
			
		||||
                Log.w(TAG, "Error updating document", e);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        //isReportedをfalseに更新
 | 
			
		||||
        isReported.update("isReported", false).addOnSuccessListener(unused -> Log.d(TAG, "DocumentSnapshot successfully updated!")).addOnFailureListener(e -> Log.w(TAG, "Error updating document", e));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -181,7 +162,7 @@ public class TestService extends Service {
 | 
			
		||||
        }
 | 
			
		||||
        notificationManager.notify(R.string.app_name, builder.build());//通知の表示
 | 
			
		||||
    }
 | 
			
		||||
    public void NotificationBluetooth(Context context) {//実際に通知を行うメソッド
 | 
			
		||||
    public void NotificationBluetooth(Context context, int time) {//実際に通知を行うメソッド
 | 
			
		||||
        final String CHANNEL_ID = "my_channel_id";
 | 
			
		||||
        // 通知がクリックされたときに送信されるIntent
 | 
			
		||||
        Intent intent = new Intent(context, MainActivity.class);
 | 
			
		||||
@ -196,7 +177,7 @@ public class TestService extends Service {
 | 
			
		||||
        @SuppressLint("NotificationTrampoline") NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
 | 
			
		||||
                .setSmallIcon(android.R.drawable.ic_menu_info_details)
 | 
			
		||||
                .setContentTitle("子供の置き去りをしていませんか?")//通知のタイトル
 | 
			
		||||
                .setContentText("Bluetootと車の切断から5分が経過しました")//通知の本文
 | 
			
		||||
                .setContentText("Bluetootと車の切断から"+time+"分が経過しました")//通知の本文
 | 
			
		||||
                .setContentIntent(pendingIntent)//通知をタップするとActivityへ移動する
 | 
			
		||||
                .setAutoCancel(true)//通知をタップすると削除する
 | 
			
		||||
                .setPriority(NotificationCompat.PRIORITY_HIGH) // プライオリティを高く設定
 | 
			
		||||
@ -242,9 +223,20 @@ public class TestService extends Service {
 | 
			
		||||
 | 
			
		||||
                    //共有プリファレンス全体の準備
 | 
			
		||||
                    SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
 | 
			
		||||
                    Boolean isInCar = sharedPreferences.getBoolean("isInCarPref", false);//現在の乗降状態を保存する共有プリファレンス
 | 
			
		||||
 | 
			
		||||
                    int time=sharedPreferences.getInt("time",0);
 | 
			
		||||
                    //共有プリファレンス 書き込みの準備
 | 
			
		||||
                    SharedPreferences.Editor E = sharedPreferences.edit();
 | 
			
		||||
                    if(time==0) {//Bluetoot切断からの時間経過(5分刻み)
 | 
			
		||||
                        E.putInt("time",5);
 | 
			
		||||
                        E.apply();;
 | 
			
		||||
                    }else {
 | 
			
		||||
                        E.putInt("time",time*2);
 | 
			
		||||
                        E.apply();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // 5分毎に実行される処理
 | 
			
		||||
                    NotificationBluetooth(getApplicationContext());
 | 
			
		||||
                    NotificationBluetooth(getApplicationContext(),time);
 | 
			
		||||
                    Log.d("PeriodicTask", "5分後に処理を実行します");
 | 
			
		||||
 | 
			
		||||
                    handler.postDelayed(this, INTERVAL);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user