Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / services / CosomosBackgroundService.java
package com.example.cosmosclient.services;

import android.Manifest;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.content.pm.PackageManager;
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.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.example.cosmosclient.R;
import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.Group;
import com.example.cosmosclient.entities.GroupListResponse;
import com.example.cosmosclient.entities.RequestList;
import com.example.cosmosclient.entities.jsons.GroupJson;
import com.example.cosmosclient.resources.GroupsRest;

import java.io.IOException;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;


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");
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        Log.d("debug", "onCreate");
        context = getApplicationContext();
        // LocationManager インスタンス生成
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

//        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();
            }
        };
        //timer.scheduleAtFixedRate(定期的に実行したいタスク,初回のタスク実行までの時間(ms),実行するタスクの間隔(ms));
        timer.scheduleAtFixedRate(task, 10000, 30000);

        return START_NOT_STICKY;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onHandleIntent(Intent intent) {
//        Log.d("debug", "onHandleIntent");
    }

    protected void startGPS() {
        Log.d("debug", "startGPS");
        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;
                }
//                Log.d("debug", "requestLocationUpdates:");

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                        MinTime, MinDistance, this);
            } catch (Exception e) {
//                Log.d("debug", "Exception:");

                e.printStackTrace();
            }
        } else {
        }
    }

    @Override
    public void 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());

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    private void startUpdateRequest() {
        final Cosmos app = (Cosmos) getApplication();
        if(app.getuId() != null) {
            final String uId, token;
            uId = app.getuId();
            token = app.getToken();

            //retrofitの処理
            final Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/rest/")
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build();
            //interfaceから実装を取得
            final GroupsRest getGroupsService = retrofit.create(GroupsRest.class);
            final GroupsRest getRequestsService = retrofit.create(GroupsRest.class);

            //API呼び出しのための値入力
            final Call<GroupListResponse> call = getGroupsService.getGroups(uId, token);

            //サーバからデータ受け取り
            call.enqueue(new Callback<GroupListResponse>() {
                //成功時
                @Override
                public void onResponse(Call<GroupListResponse> call, Response<GroupListResponse> response) {
                    if (response.isSuccessful()) {
                        final GroupListResponse groupListresult = response.body();
                        Log.d("debug", "GroupListResponseグループ情報通信取得しました");

                        new Thread(new Runnable() {
                            public void run() {
                                for (GroupJson groupJson : groupListresult.getGroups()) {
                                    app.setGroup(new Group(groupJson));
                                    if (groupJson.getRequestHash() != app.getGroup(groupJson.getgId()).getRequestHash()) {
                                        //RequestList取得するための必要な情報
                                        final String gId = groupJson.getgId();
                                        final boolean detail = true;
                                        int quantity = 20;

                                        final Call<RequestList> requestsListByGidCall = getRequestsService.getRequestsListByGid(gId, token, detail, quantity);
                                        Response<RequestList> requestListResponse;
                                        try {
                                            requestListResponse = requestsListByGidCall.execute();
                                            if (requestListResponse.isSuccessful()) {
                                                TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
                                                RequestList requestList = requestListResponse.body();
                                                app.getGroup(groupJson.getgId()).setRequestList(requestList);
                                                app.getGroup(groupJson.getgId()).setRequestHash(groupJson.getRequestHash());
                                                Log.d("debug", groupJson.getName() + "のRequestListを取得しました");
                                            } else {
                                                // onFailure
                                                try {
                                                    System.out.println(requestListResponse.errorBody().string());
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }

                                                //onFailureでキャッチできないエラーの処理
                                                Log.d("debug", groupJson.getName() + "の通信エラー");
                                            }
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                            Log.d("debug", groupJson.getName() + "のRequestListの取得失敗しました");
                                        }
                                    }
                                }
                            }
                        }).start();
                    } else {
                        //onFailureでキャッチできないエラー用
                        Log.d("debug", "GroupListResponseグループ情報通信エラー");
                    }
                }

                //失敗時
                @Override
                public void onFailure(Call<GroupListResponse> call, Throwable t) {
                    t.printStackTrace();
                    Log.d("debug", "GroupListResponseグループ情報取得失敗");
                }
            });
        } else {
            Log.d("debug", "app.getuId()がnull");
        }

    }

    private void enableLocationSettings() {
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(settingsIntent);
    }

}