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 d050520..ab281c2 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -1,36 +1,123 @@ package com.example.cosmosclient.app; +import android.Manifest; +import android.app.Activity; import android.app.Application; import android.content.ComponentCallbacks; import android.content.res.Configuration; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Bundle; +import android.support.annotation.RequiresApi; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.util.Log; import com.example.cosmosclient.entities.AreaInformation; import com.example.cosmosclient.entities.Group; +import com.example.cosmosclient.services.CosmosBackgroundService; +import com.example.cosmosclient.views.RequestPermissionsActivty; + import android.content.SharedPreferences; +import android.view.Gravity; +import android.widget.Toast; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; public class Cosmos extends Application { + private String TAG = Cosmos.class.getSimpleName(); private String token=null; private Group curGroup=null; private HashMap groups = new HashMap<>(); private String uId=null; private HashMap areaInfo = new HashMap<>(); private ArrayList areaInfoKey= new ArrayList<>(); - private String TAG = Cosmos.class.getSimpleName(); + private static final int REQUEST_MULTI_PERMISSIONS = 101; + ArrayList reqPermissions = new ArrayList<>(); public Cosmos() { super(); Log.d(TAG, " Constructor"); + + + registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { + @Override + public void onActivityCreated(Activity activity, Bundle bundle) { + Log.d(TAG, activity + "onActivityCreated"); + // 未許可あり + if (activity instanceof RequestPermissionsActivty && !reqPermissions.isEmpty()) { + Log.d(TAG, "isRequestPermission"); + ((RequestPermissionsActivty)activity).setOnRequestPermissionsResultCallback(new ActivityCompat.OnRequestPermissionsResultCallback() { + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + Log.d(TAG, "onRequestPermissionsResult"); + 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("外部書込の許可がないので書き込みできません"); + } + } + } + startBackgroundService(getApplicationContext()); + } + } + } + }); + ActivityCompat.requestPermissions(activity, (String[]) reqPermissions.toArray(new String[0]), REQUEST_MULTI_PERMISSIONS); + } + } + + @Override + public void onActivityStarted(Activity activity) { + } + + @Override + public void onActivityResumed(Activity activity) { + } + + @Override + public void onActivityPaused(Activity activity) { + } + + @Override + public void onActivityStopped(Activity activity) { + } + + @Override + public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { + } + + @Override + public void onActivityDestroyed(Activity activity) { + } + + }); } @Override public void onCreate() { - Log.d(TAG, " onCreate"); + Log.d(TAG, "onCreate"); super.onCreate(); + // Android 6, API 23以上でペーミッションの確認 + if (Build.VERSION.SDK_INT >= 23) { + checkMultiPermissions(getApplicationContext()); + } } @Override @@ -162,4 +249,39 @@ return areaInfoKey; } + // 位置情報許可の確認、外部ストレージのPermissionにも対応できるようにしておく + private void checkMultiPermissions(Context context){ + int permissionLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); // 位置情報の Permission + int permissionExtStorage = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE); // 外部ストレージ書き込みの Permission + // 位置情報の Permission が許可されているか確認 + if (permissionLocation == PackageManager.PERMISSION_GRANTED) { + // 許可済 + startBackgroundService(context); + } else{ + // 未許可 + reqPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + } + } + + //BackgroundServiceをスタートさせる + public void startBackgroundService(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(); + } + + // トーストの生成 + private void toastMake(String message){ + Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG); + // 位置調整 + toast.setGravity(Gravity.CENTER, 0, 200); + toast.show(); + } + }