権限の要求を追加
This commit is contained in:
parent
36e558a88b
commit
dc1ae55a9e
|
@ -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;
|
||||||
|
@ -13,6 +14,9 @@ import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
|
|
||||||
|
import com.google.firebase.firestore.DocumentReference;
|
||||||
|
import com.google.firebase.firestore.FirebaseFirestore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple {@link Fragment} subclass.
|
* A simple {@link Fragment} subclass.
|
||||||
* Use the {@link HomeFragment#newInstance} factory method to
|
* Use the {@link HomeFragment#newInstance} factory method to
|
||||||
|
@ -68,9 +72,47 @@ public class HomeFragment extends Fragment implements OnEventListener{
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.fragment_home, container, false);
|
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;
|
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
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
|
@ -111,7 +153,7 @@ public class HomeFragment extends Fragment implements OnEventListener{
|
||||||
}
|
}
|
||||||
String get_on = "\n乗車状態";
|
String get_on = "\n乗車状態";
|
||||||
String get_off = "\n降車状態";
|
String get_off = "\n降車状態";
|
||||||
if (!isInCar) {
|
if (isInCar) {
|
||||||
//乗車状態にする
|
//乗車状態にする
|
||||||
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);
|
||||||
|
|
|
@ -24,6 +24,7 @@ import android.widget.FrameLayout;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.browser.customtabs.CustomTabsIntent;
|
import androidx.browser.customtabs.CustomTabsIntent;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
@ -36,6 +37,9 @@ import com.google.firebase.firestore.FirebaseFirestore;
|
||||||
import com.journeyapps.barcodescanner.ScanContract;
|
import com.journeyapps.barcodescanner.ScanContract;
|
||||||
import com.journeyapps.barcodescanner.ScanOptions;
|
import com.journeyapps.barcodescanner.ScanOptions;
|
||||||
|
|
||||||
|
// Manifest
|
||||||
|
import android.Manifest;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -79,6 +83,11 @@ public class MainActivity extends AppCompatActivity {
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
|
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
|
||||||
|
|
||||||
|
if (!hasPermissions()) {
|
||||||
|
requestPermissions();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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", "tset");
|
||||||
|
@ -140,6 +149,47 @@ public class MainActivity extends AppCompatActivity {
|
||||||
firebaselink();
|
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) {//サイト上で押されたボタンの管理
|
private void initNotification(DocumentReference mDocRef) {//サイト上で押されたボタンの管理
|
||||||
// 共有プリファレンス全体の準備
|
// 共有プリファレンス全体の準備
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences("app_situation", MODE_PRIVATE);
|
||||||
|
@ -158,9 +208,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) {//fragementがHomeFragmentのインスタンスかの判定
|
||||||
((HomeFragment) fragment).onEvent(!isInCar);//onEvent()メソッドを処理→HomeFragmentのonEvent()メソッドを処理
|
// ((HomeFragment) fragment).onEvent(!isInCar);//onEvent()メソッドを処理→HomeFragmentのonEvent()メソッドを処理
|
||||||
}
|
// }
|
||||||
} else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
|
} else if (isInCar) {//第三者ボタンが押されたときにisInCarがtrueのとき=乗車状態のとき→いたずら防止
|
||||||
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||||
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
|
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user