diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9919eb8..f872505 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -81,6 +81,8 @@ + + diff --git a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java index 4ce93a6..28b1dcf 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java @@ -13,8 +13,10 @@ import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; +import android.location.LocationProvider; import android.os.Build; import android.os.Bundle; +import android.provider.Settings; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.util.Log; @@ -25,6 +27,8 @@ public class CosomosBackgroundService extends IntentService implements LocationListener { private Context context; private LocationManager locationManager; + private static final int MinTime = 1000;/*最小時間間隔*/ + private static final float MinDistance = 1;/*最小距離間隔*/ public CosomosBackgroundService() { super("CosomosBackgroundService"); @@ -33,7 +37,7 @@ @Override public void onCreate() { super.onCreate(); - Log.d("debug", "onCreate"); +// Log.d("debug", "onCreate"); context = getApplicationContext(); // LocationManager インスタンス生成 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); @@ -43,25 +47,7 @@ @Override public int onStartCommand(Intent intent, int flags, int startId) { - Log.d("debug", "onStartCommand"); - -// Thread thread = new Thread() { -// public void run() { -// int count = 10; -// -// try { -// for(int i=0 ; i< count ; i++) { -// Thread.sleep(1000); -// -// Log.d("debug", "sleep: " + String.valueOf(i)); -// } -// -// } catch (InterruptedException e) { -// Thread.currentThread().interrupt(); -// } -// } -// }; -// thread.start(); +// Log.d("debug", "onStartCommand"); int requestCode = 0; String channelId = "default"; String title = context.getString(R.string.app_name); @@ -70,21 +56,21 @@ intent, PendingIntent.FLAG_UPDATE_CURRENT); // ForegroundにするためNotificationが必要、Contextを設定 NotificationManager notificationManager = - (NotificationManager)context. + (NotificationManager) context. getSystemService(Context.NOTIFICATION_SERVICE); // Notification Channel 設定 NotificationChannel channel = new NotificationChannel( - channelId, title , NotificationManager.IMPORTANCE_DEFAULT); + channelId, title, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Silent Notification"); // 通知音を消さないと毎回通知音が出てしまう // この辺りの設定はcleanにしてから変更 - channel.setSound(null,null); + channel.setSound(null, null); // 通知ランプを消す channel.enableLights(false); channel.setLightColor(Color.BLUE); // 通知バイブレーション無し channel.enableVibration(false); - if(notificationManager != null) { + if (notificationManager != null) { notificationManager.createNotificationChannel(channel); Notification notification = new Notification.Builder(context, channelId) .setContentTitle(title) @@ -99,65 +85,59 @@ // startForeground startForeground(1, notification); } -// startGPS(); - return super.onStartCommand(intent,flags,startId); + startGPS(); + + return START_NOT_STICKY; } + @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onHandleIntent(Intent intent) { +// Log.d("debug", "onHandleIntent"); + } - Log.d("debug", "onHandleIntent"); - int count = 10; + protected void startGPS() { +// Log.d("debug", "startGPS"); + final boolean gpsEnabled + = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); - try { - for(int i=0 ; i< count ; i++) { - Thread.sleep(1000); + if (!gpsEnabled) { + // GPSを設定するように促す + enableLocationSettings(); + } - Log.d("debug", "sleep: " + String.valueOf(i)); + if (locationManager != null) { + try { + if (ActivityCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + return; + } +// Log.d("debug", "requestLocationUpdates:"); + + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + MinTime, MinDistance, this); + } catch (Exception e) { +// Log.d("debug", "Exception:"); + + e.printStackTrace(); } - - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } else { } } -// protected void startGPS() { -// StringBuilder strBuf = new StringBuilder(); -// strBuf.append("startGPS\n"); -// -// final boolean gpsEnabled -// = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); -// if (!gpsEnabled) { -// // GPSを設定するように促す -// enableLocationSettings(); -// } -// -// if (locationManager != null) { -// try { -// if (ActivityCompat.checkSelfPermission(this, -// Manifest.permission.ACCESS_FINE_LOCATION)!= -// PackageManager.PERMISSION_GRANTED) { -// return; -// } -// -// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, -// MinTime, MinDistance, this); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } else { -// strBuf.append("locationManager=null\n"); -// } -// } + @Override public void onDestroy() { - Log.d("debug", "onDestroy"); - +// Log.d("debug", "onDestroy"); super.onDestroy(); } @Override public void onLocationChanged(Location location) { +// Log.d("debug", "onLocationChanged"); + Log.d("debug", "lat:" + location.getLatitude()); + Log.d("debug", "lon:" + location.getLongitude()); } @@ -175,4 +155,10 @@ public void onProviderDisabled(String provider) { } + + private void enableLocationSettings() { + Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + startActivity(settingsIntent); + } + } diff --git a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java index b9769aa..46845dc 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -1,15 +1,22 @@ package com.example.cosmosclient.views; +import android.Manifest; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; +import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; +import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.EditText; @@ -22,6 +29,8 @@ import com.example.cosmosclient.resources.UsersRest; import com.example.cosmosclient.services.CosomosBackgroundService; +import java.util.ArrayList; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -33,39 +42,24 @@ private boolean pwEnable; private Button SigninButton; private Intent intentservice; + private static final int REQUEST_MULTI_PERMISSIONS = 101; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signin); + Context context = getApplicationContext(); -// // 別スレ生成 -> 開始 -// HandlerThread handlerThread = new HandlerThread("other"); -// handlerThread.start(); -// -// //作成したHandlerThread(別スレ)内部のLooperを引数として、HandlerThread(のLooper)にメッセージを送るHandlerを生成する。 -// Handler handler = new Handler(handlerThread.getLooper()); -// //Handlerのpostメソッドでメッセージ(タスク:重たい処理)を送信する。 -// handler.post(new Runnable() { -// @RequiresApi(api = Build.VERSION_CODES.O) -// @Override -// public void run() { -// //重たい処理を記述 -// intentservice = new Intent(SigninActivity.this, CosomosBackgroundService.class); -// startForegroundService(intentservice); -// } -// }); + // Android 6, API 23以上でパーミッシンの確認 + if(Build.VERSION.SDK_INT >= 23){ + checkMultiPermissions(); + } + else{ + startService(); + } - Thread thread = new Thread() { - @RequiresApi(api = Build.VERSION_CODES.O) - public void run() { - intentservice = new Intent(SigninActivity.this, CosomosBackgroundService.class); - startForegroundService(intentservice); - - } - }; - thread.start(); //各種IDを取得 SigninButton = findViewById(R.id.SigninButton); Button SignupButton = findViewById(R.id.SignupButton); @@ -223,4 +217,95 @@ } } } + // 結果の受け取り + @Override + public void onRequestPermissionsResult(int requestCode, + @NonNull String[] permissions, @NonNull int[] grantResults) { + + if (requestCode == REQUEST_MULTI_PERMISSIONS) { + if (grantResults.length > 0) { + for (int i = 0; i < permissions.length; i++) { + // 位置情報 + if (permissions[i]. + equals(Manifest.permission.ACCESS_FINE_LOCATION)) { + if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { + // 許可された + + } else { + // それでも拒否された時の対応 + toastMake("位置情報の許可がないので計測できません"); + } + } + // 外部ストレージ + else if (permissions[i]. + equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { + // 許可された + } else { + // それでも拒否された時の対応 + toastMake("外部書込の許可がないので書き込みできません"); + } + } + } + + startService(); + + } + } + else{ + // + } + } + // 位置情報許可の確認、外部ストレージのPermissionにも対応できるようにしておく + private void checkMultiPermissions(){ + // 位置情報の Permission + int permissionLocation = ContextCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_FINE_LOCATION); + // 外部ストレージ書き込みの Permission + int permissionExtStorage = ContextCompat.checkSelfPermission(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE); + + ArrayList reqPermissions = new ArrayList<>(); + + // 位置情報の Permission が許可されているか確認 + if (permissionLocation == PackageManager.PERMISSION_GRANTED) { + // 許可済 + } + else{ + // 未許可 + reqPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + } + + + // 未許可 + if (!reqPermissions.isEmpty()) { + ActivityCompat.requestPermissions(this, + (String[]) reqPermissions.toArray(new String[0]), + REQUEST_MULTI_PERMISSIONS); + // 未許可あり + } + else{ + // 許可済 + startService(); + } + } + //serviceをスタートさせる + private void startService() { + Thread thread = new Thread() { + @RequiresApi(api = Build.VERSION_CODES.O) + public void run() { + intentservice = new Intent(SigninActivity.this, CosomosBackgroundService.class); + startForegroundService(intentservice); + + } + }; + thread.start(); + } + // トーストの生成 + private void toastMake(String message){ + Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG); + // 位置調整 + toast.setGravity(Gravity.CENTER, 0, 200); + toast.show(); + } }