diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 67ca76b..f677df0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -76,6 +76,13 @@ + + + + + + @@ -83,8 +90,7 @@ - - + \ No newline at end of file diff --git a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java index 5fd0597..ac5e45a 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -1,6 +1,9 @@ package com.example.cosmosclient.app; import android.app.Application; +import android.content.ComponentCallbacks; +import android.content.res.Configuration; +import android.util.Log; import com.example.cosmosclient.entities.AreaInformation; import com.example.cosmosclient.entities.Group; @@ -15,6 +18,78 @@ private HashMap groups = new HashMap<>(); private String uId=null; private HashMap areaInfo = new HashMap<>(); + private String TAG = Cosmos.class.getSimpleName(); + + public Cosmos() { + super(); + Log.d(TAG, " Constructor"); + } + + @Override + public void onCreate() { + Log.d(TAG, " onCreate"); + super.onCreate(); + } + + @Override + public void onTerminate() { + Log.d(TAG, " onTerminate"); + super.onTerminate(); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + Log.d(TAG, " onConfigurationChanged"); + super.onConfigurationChanged(newConfig); + } + + @Override + public void onLowMemory() { + Log.d(TAG, " onLowMemory"); + super.onLowMemory(); + } + + @Override + public void onTrimMemory(int level) { + Log.d(TAG, " onTrimMemory"); + super.onTrimMemory(level); + } + + @Override + public void registerComponentCallbacks(ComponentCallbacks callback) { + Log.d(TAG, " registerComponentCallbacks"); + super.registerComponentCallbacks(callback); + } + + @Override + public void unregisterComponentCallbacks(ComponentCallbacks callback) { + Log.d(TAG, " unregisterComponentCallbacks"); + super.unregisterComponentCallbacks(callback); + } + + @Override + public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { + Log.d(TAG, " registerActivityLifecycleCallbacks"); + super.registerActivityLifecycleCallbacks(callback); + } + + @Override + public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { + Log.d(TAG, " unregisterActivityLifecycleCallbacks"); + super.unregisterActivityLifecycleCallbacks(callback); + } + + @Override + public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) { + Log.d(TAG, " registerOnProvideAssistDataListener"); + super.registerOnProvideAssistDataListener(callback); + } + + @Override + public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) { + Log.d(TAG, " unregisterOnProvideAssistDataListener"); + super.unregisterOnProvideAssistDataListener(callback); + } //token処理 public void setToken(String token){ diff --git a/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java index e70246a..d194049 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java @@ -9,13 +9,16 @@ import android.content.Intent; import android.content.Context; import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.graphics.Color; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Build; import android.os.Bundle; +import android.os.IBinder; import android.provider.Settings; +import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.util.Log; @@ -28,7 +31,9 @@ import com.example.cosmosclient.entities.jsons.GroupJson; import com.example.cosmosclient.resources.GroupsRest; +import java.io.FileDescriptor; import java.io.IOException; +import java.io.PrintWriter; import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; @@ -45,73 +50,90 @@ private LocationManager locationManager; private static final int MinTime = 1000;/*最小時間間隔*/ private static final float MinDistance = 1;/*最小距離間隔*/ + private boolean isServiceRunning = false; //サービスが起動しているかを確認 + private String TAG = CosmosBackgroundService.class.getSimpleName(); public CosmosBackgroundService() { super("CosmosBackgroundService"); + Log.d(TAG, "Constructor"); + } + + @Override + public void setIntentRedelivery(boolean enabled) { + Log.d(TAG, "setIntentRedelivery"); + super.setIntentRedelivery(enabled); } @Override public void onCreate() { + Log.d(TAG, "onCreate"); super.onCreate(); -// Log.d("debug", "onCreate"); context = getApplicationContext(); // LocationManager インスタンス生成 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } + @Override + public void onStart(@Nullable Intent intent, int startId) { + Log.d(TAG, "onStart"); + super.onStart(intent, startId); + } + @RequiresApi(api = Build.VERSION_CODES.O) @Override public int onStartCommand(Intent intent, int flags, int startId) { + Log.d(TAG, "onStartCommand"); + if(!isServiceRunning) { + int requestCode = 0; + String channelId = "default"; + String title = context.getString(R.string.app_name); + PendingIntent pendingIntent = + PendingIntent.getActivity(context, requestCode, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + // ForegroundにするためNotificationが必要、Contextを設定 + NotificationManager notificationManager = + (NotificationManager) context. + getSystemService(Context.NOTIFICATION_SERVICE); + // Notification Channel 設定 + NotificationChannel channel = new NotificationChannel( + channelId, title, NotificationManager.IMPORTANCE_DEFAULT); + channel.setDescription("Silent Notification"); + // 通知音を消さないと毎回通知音が出てしまう + // この辺りの設定はcleanにしてから変更 + channel.setSound(null, null); + // 通知ランプを消す + channel.enableLights(false); + channel.setLightColor(Color.BLUE); + // 通知バイブレーション無し + channel.enableVibration(false); + if (notificationManager != null) { + notificationManager.createNotificationChannel(channel); + Notification notification = new Notification.Builder(context, channelId) + .setContentTitle(title) + // 本来なら衛星のアイコンですがandroid標準アイコンを設定 + .setSmallIcon(android.R.drawable.btn_star) + .setContentText("GPS") + .setAutoCancel(true) + .setContentIntent(pendingIntent) + .setWhen(System.currentTimeMillis()) + .build(); -// Log.d("debug", "onStartCommand"); - int requestCode = 0; - String channelId = "default"; - String title = context.getString(R.string.app_name); - PendingIntent pendingIntent = - PendingIntent.getActivity(context, requestCode, - intent, PendingIntent.FLAG_UPDATE_CURRENT); - // ForegroundにするためNotificationが必要、Contextを設定 - NotificationManager notificationManager = - (NotificationManager) context. - getSystemService(Context.NOTIFICATION_SERVICE); - // Notification Channel 設定 - NotificationChannel channel = new NotificationChannel( - channelId, title, NotificationManager.IMPORTANCE_DEFAULT); - channel.setDescription("Silent Notification"); - // 通知音を消さないと毎回通知音が出てしまう - // この辺りの設定はcleanにしてから変更 - channel.setSound(null, null); - // 通知ランプを消す - channel.enableLights(false); - channel.setLightColor(Color.BLUE); - // 通知バイブレーション無し - channel.enableVibration(false); - if (notificationManager != null) { - notificationManager.createNotificationChannel(channel); - Notification notification = new Notification.Builder(context, channelId) - .setContentTitle(title) - // 本来なら衛星のアイコンですがandroid標準アイコンを設定 - .setSmallIcon(android.R.drawable.btn_star) - .setContentText("GPS") - .setAutoCancel(true) - .setContentIntent(pendingIntent) - .setWhen(System.currentTimeMillis()) - .build(); - - // startForeground - startForeground(1, notification); - } - - startGPS(); - Timer timer = new Timer(); - TimerTask task = new TimerTask() { - @Override - public void run() { - startUpdateRequest(); + // startForeground + startForeground(1, notification); } - }; - //timer.scheduleAtFixedRate(定期的に実行したいタスク,初回のタスク実行までの時間(ms),実行するタスクの間隔(ms)); - timer.scheduleAtFixedRate(task, 10000, 300000); + + startGPS(); + Timer timer = new Timer(); + TimerTask task = new TimerTask() { + @Override + public void run() { + startUpdateRequest(); + } + }; + //timer.scheduleAtFixedRate(定期的に実行したいタスク,初回のタスク実行までの時間(ms),実行するタスクの間隔(ms)); + timer.scheduleAtFixedRate(task, 10000, 30000); + isServiceRunning = true; + } return START_NOT_STICKY; } @@ -119,11 +141,11 @@ @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onHandleIntent(Intent intent) { -// Log.d("debug", "onHandleIntent"); + Log.d(TAG, "onHandleIntent"); } protected void startGPS() { - Log.d("debug", "startGPS"); + Log.d(TAG, "startGPS"); final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); @@ -139,12 +161,12 @@ PackageManager.PERMISSION_GRANTED) { return; } -// Log.d("debug", "requestLocationUpdates:"); +// Log.d(TAG, "requestLocationUpdates:"); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MinTime, MinDistance, this); } catch (Exception e) { -// Log.d("debug", "Exception:"); +// Log.d(TAG, "Exception:"); e.printStackTrace(); } @@ -154,30 +176,80 @@ @Override public void onDestroy() { -// Log.d("debug", "onDestroy"); + Log.d(TAG, "onDestroy"); super.onDestroy(); } @Override + public void onConfigurationChanged(Configuration newConfig) { + Log.d(TAG, "onConfigurationChanged"); + super.onConfigurationChanged(newConfig); + } + + @Override + public void onLowMemory() { + Log.d(TAG, "onLowMemory"); + super.onLowMemory(); + } + + @Override + public void onTrimMemory(int level) { + Log.d(TAG, "onTrimMemory"); + super.onTrimMemory(level); + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + Log.d(TAG, "onBind"); + return super.onBind(intent); + } + + @Override + public boolean onUnbind(Intent intent) { + Log.d(TAG, "onUnbind"); + return super.onUnbind(intent); + } + + @Override + public void onRebind(Intent intent) { + Log.d(TAG, "onRebind"); + super.onRebind(intent); + } + + @Override + public void onTaskRemoved(Intent rootIntent) { + Log.d(TAG, "onTaskRemoved"); + super.onTaskRemoved(rootIntent); + } + + @Override + protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { + Log.d(TAG, "dump"); + super.dump(fd, writer, args); + } + + @Override public void onLocationChanged(Location location) { -// Log.d("debug", "onLocationChanged"); - Log.d("debug", "lat:" + location.getLatitude()); - Log.d("debug", "lon:" + location.getLongitude()); +// Log.d(TAG, "onLocationChanged"); + Log.d(TAG, "lat:" + location.getLatitude()); + Log.d(TAG, "lon:" + location.getLongitude()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { - + Log.d(TAG, "onStatusChanged"); } @Override public void onProviderEnabled(String provider) { - + Log.d(TAG, "onProviderEnabled"); } @Override public void onProviderDisabled(String provider) { + Log.d(TAG, "onProviderDisabled"); } @@ -207,7 +279,7 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { final GroupListResponse groupListresult = response.body(); - Log.d("debug", "GroupListResponseグループ情報通信取得しました"); + Log.d(TAG, "GroupListResponseグループ情報通信取得しました"); new Thread(new Runnable() { public void run() { @@ -228,7 +300,7 @@ RequestList requestList = requestListResponse.body(); app.getGroup(groupJson.getgId()).setRequestList(requestList); app.getGroup(groupJson.getgId()).setRequestHash(groupJson.getRequestHash()); - Log.d("debug", groupJson.getName() + "のRequestListを取得しました"); + Log.d(TAG, groupJson.getName() + "のRequestListを取得しました"); } else { // onFailure try { @@ -238,11 +310,11 @@ } //onFailureでキャッチできないエラーの処理 - Log.d("debug", groupJson.getName() + "の通信エラー"); + Log.d(TAG, groupJson.getName() + "の通信エラー"); } } catch (IOException e) { e.printStackTrace(); - Log.d("debug", groupJson.getName() + "のRequestListの取得失敗しました"); + Log.d(TAG, groupJson.getName() + "のRequestListの取得失敗しました"); } } } @@ -250,7 +322,7 @@ }).start(); } else { //onFailureでキャッチできないエラー用 - Log.d("debug", "GroupListResponseグループ情報通信エラー"); + Log.d(TAG, "GroupListResponseグループ情報通信エラー"); } } @@ -258,11 +330,11 @@ @Override public void onFailure(Call call, Throwable t) { t.printStackTrace(); - Log.d("debug", "GroupListResponseグループ情報取得失敗"); + Log.d(TAG, "GroupListResponseグループ情報取得失敗"); } }); } else { - Log.d("debug", "app.getuId()がnull"); + Log.d(TAG, "app.getuId()がnull"); } } diff --git a/app/src/main/java/com/example/cosmosclient/services/CosmosBootReceiver.java b/app/src/main/java/com/example/cosmosclient/services/CosmosBootReceiver.java new file mode 100644 index 0000000..20ae4d2 --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/services/CosmosBootReceiver.java @@ -0,0 +1,55 @@ +package com.example.cosmosclient.services; + +import android.Manifest; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.support.v4.content.ContextCompat; +import android.util.Log; + +public class CosmosBootReceiver extends BroadcastReceiver { + private static final int REQUEST_MULTI_PERMISSIONS = 101; + private String TAG = CosmosBootReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive"); + // Android 6, API 23以上でペーミッションの確認 + if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) && Build.VERSION.SDK_INT >= 23) { + checkMultiPermissions(context); + } else{ + // サービスの起動 + startService(context); + } + } + + // 位置情報許可の確認、外部ストレージのPermissionにも対応できるようにしておく + private void checkMultiPermissions(Context context){ + // 位置情報の Permission + int permissionLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); + + // 位置情報の Permission が許可されているか確認 + if (permissionLocation == PackageManager.PERMISSION_GRANTED) { + // 許可済 + startService(context); + } else{ + // 未許可 + } + } + + //serviceをスタートさせる + private void startService(final Context context) { + final Thread thread = new Thread() { + @RequiresApi(api = Build.VERSION_CODES.O) + public void run() { + // サービスの起動 + Intent intentservice = new Intent(context, CosmosBackgroundService.class); + context.startForegroundService(intentservice); + } + }; + thread.start(); + } +}