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 8ea8e03..407751a 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java @@ -2,7 +2,6 @@ import android.Manifest; import android.app.IntentService; -import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -25,8 +24,10 @@ import com.example.cosmosclient.R; import com.example.cosmosclient.app.Cosmos; +import com.example.cosmosclient.entities.Feature; import com.example.cosmosclient.entities.Group; import com.example.cosmosclient.entities.GroupListResponse; +import com.example.cosmosclient.entities.Request; import com.example.cosmosclient.entities.RequestList; import com.example.cosmosclient.entities.jsons.GroupJson; import com.example.cosmosclient.resources.GroupsRest; @@ -34,7 +35,13 @@ import java.io.FileDescriptor; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; @@ -111,7 +118,7 @@ channel.enableVibration(false); if (notificationManager != null) { notificationManager.createNotificationChannel(channel); - Notification notification = new Notification.Builder(context, channelId) + android.app.Notification notification = new android.app.Notification.Builder(context, channelId) .setContentTitle(title) // 本来なら衛星のアイコンですがandroid標準アイコンを設定 .setSmallIcon(android.R.drawable.btn_star) @@ -357,8 +364,80 @@ } public List searchNotifications(Cosmos cosmos, Location location){ + double NowLat =location.getLatitude(); + double NowLon =location.getLongitude(); + //業番:notification + HashMap> codeToNotification = new HashMap<>(); + //業番:feature + HashMap> codeToFeature = new HashMap<>(); + ArrayList groups =(ArrayList) cosmos.getGroups(); - return null; + //現在時刻取得 + Date nowDate = new Date(); + //codeToNotificationを作成。Featureはnull + for(int i = 0 ; i < groups.size(); i++) { + //requestのArrayListを回す + for (int j = 0; j < groups.get(i).getRequestList().getRequests().size(); j++) { + //codeToNotificationに格納するArrayListを初期化 + ArrayList notifications = null; + //期限内かつ未達成のrequestのみ取得 + if (groups.get(i).getRequestList().getRequests().get(j).getDeadline().after(nowDate) && groups.get(i).getRequestList().getRequests().get(j).isDone() == false) { + //notificationを作成 + Notification notification = new Notification(groups.get(i).getRequestList().getRequests().get(j),groups.get(i),null); + //指定したkey(業番)を持っているHashMapがなければ作成する。 + if(codeToNotification.get(groups.get(i).getRequestList().getRequests().get(j))==null){ + notifications.add(notification); + codeToNotification.put(groups.get(i).getRequestList().getRequests().get(j).getLocation(),notifications); + }else{ + //keyをすでに持っている場合はArrayListにnotification単体を追加していく + codeToNotification.get(groups.get(i).getRequestList().getRequests().get(j).getLocation()).add(notification); + } + } + } + } + //cosmosから区画情報から取得 + int areaInfoId =(int)((NowLat+90)/0.1)+(int)((NowLon/0.1)*1800); + codeToFeature =cosmos.getAreaInfo(areaInfoId).getFeature(); + //codeToFeatureの中の50m圏外Featureを削除していく。 + //HashMapを順番に実行 + for(HashMap.Entry> e : codeToFeature.entrySet()) { + //ArrayList>を回していく + for(int i=e.getValue().size(); i >= 0;i--) { + float[] distance = getDistance(NowLat, NowLon, e.getValue().get(i).getLocation().getLatitude(), e.getValue().get(i).getLocation().getLongitude()); + // distance[0] = [2点間の距離] + //50m圏外のFeatureを削除 + if (distance[0] > 50) { + e.getValue().remove(i); + } + } + //codeToNotificationのkeyとcodeToFeatureのkeyが一致すれば + //codeToFeatureのArrayをcodeToNotificationのArrayfeaturesに代入する + if(codeToNotification.containsKey(e.getKey())==true){ + for(int j=0;j result = new ArrayList(); + + for (ArrayList list: codeToNotification.values()) { + result.addAll(list); + } + + return result; + } + public float[] getDistance(double x, double y, double x2, double y2) { + // 結果を格納するための配列を生成 + float[] results = new float[3]; + // results[0] = [2点間の距離] + // results[1] = [始点から見た方位角] + // results[2] = [終点から見た方位角] + + // 距離計算 + Location.distanceBetween(x, y, x2, y2, results); + + return results; } }