diff --git a/app/src/main/java/com/example/cosmosclient/GPSresources/StoreLocationsMethods.java b/app/src/main/java/com/example/cosmosclient/GPSresources/StoreLocationsMethods.java deleted file mode 100644 index 51c215e..0000000 --- a/app/src/main/java/com/example/cosmosclient/GPSresources/StoreLocationsMethods.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.example.cosmosclient.GPSresources; - -import android.location.Location; - -import com.example.cosmosclient.entities.Feature; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -public class StoreLocationsMethods { - private double NowLat; - private double NowLon; - private HashMap> feature = new HashMap<>(); - /*戻り値となる通知が必要なお店情報を格納する連想配列を作成*/ - private ArrayList nearShops=new ArrayList<>(); - - public StoreLocationsMethods(double NowLat, double NowLon){ - this.NowLat=NowLat; - this.NowLon=NowLon; - } - public void updateLocations(double NowLat,double NowLon){ - this.NowLat=NowLat; - this.NowLon=NowLon; - } - public double getNowLat(){ - return this.NowLat; - } - public double getNowLon(){ - return this.NowLon; - } - - public void setNearShops(HashMap> feature){ - this.feature=feature; - //HashMapを順番に実行 - for(HashMap.Entry> e : feature.entrySet()) { - //ArrayList>を回していく - for(int i=0; i < e.getValue().size();i++) { - float[] distance = getDistance(this.NowLat, this.NowLon, e.getValue().get(i).getLocation().getLatitude(), e.getValue().get(i).getLocation().getLongitude()); - // distance[0] = [2点間の距離] - //m単位 - if (distance[0] <= 50) { - /*ここから通知に必要な近い店の情報を格納する処理を記述*/ - /*中身を作成*/ - Feature childFeature = new Feature(); - childFeature.setCode(e.getValue().get(i).getCode()); - childFeature.setLocation(e.getValue().get(i).getLocation()); - childFeature.setName(e.getValue().get(i).getName()); - if(nearShops.indexOf(childFeature)==-1){ - /*通知に必要な近い店の情報を格納する処理を格納*/ - nearShops.add(childFeature); - } - } - } - } - } - public ArrayList getNearShops() { - return nearShops; - } - - /*通知送信後、通知すべきショップ情報リストから削除する*/ - /*引数はArrayList型*/ - /*引数のArrayListと一致するものをnearShopsから削除する*/ - public void deleteNearShops(ArrayList deleteList) { - for(int i=0; i < deleteList.size();i++){ - if(nearShops.indexOf(deleteList)!=-1){ - /*ここで削除*/ - nearShops.remove(nearShops.indexOf(deleteList)); - } - - } - - } - - - 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; - } -} 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 7764d15..ab281c2 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -4,6 +4,7 @@ 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; @@ -34,6 +35,7 @@ private HashMap groups = new HashMap<>(); private String uId=null; private HashMap areaInfo = new HashMap<>(); + private ArrayList areaInfoKey= new ArrayList<>(); private static final int REQUEST_MULTI_PERMISSIONS = 101; ArrayList reqPermissions = new ArrayList<>(); @@ -124,6 +126,11 @@ super.onTerminate(); } + @Override + public void onConfigurationChanged(Configuration newConfig) { + Log.d(TAG, " onConfigurationChanged"); + super.onConfigurationChanged(newConfig); + } @Override public void onLowMemory() { @@ -235,6 +242,13 @@ return areaInfo.get(areaId); } + public ArrayList AreaInfoGetKey(){ + for(int key : areaInfo.keySet()){ + areaInfoKey.add(key); + } + return areaInfoKey; + } + // 位置情報許可の確認、外部ストレージのPermissionにも対応できるようにしておく private void checkMultiPermissions(Context context){ int permissionLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); // 位置情報の Permission diff --git a/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java b/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java index 4a581ac..3dc2816 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java +++ b/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java @@ -5,14 +5,14 @@ import java.util.HashMap; public class AreaInformation { - private Location location = new Location(); + private CosmosLocation location = new CosmosLocation(); private HashMap> feature = new HashMap<>(); private Time lastUpdated; - public void setLocation(Location location){ + public void setLocation(CosmosLocation location){ this.location = location; } - public Location getLocation(){ + public CosmosLocation getLocation(){ return this.location; } diff --git a/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java b/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java new file mode 100644 index 0000000..a13d9db --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java @@ -0,0 +1,24 @@ +package com.example.cosmosclient.entities; + +public class CosmosLocation { + private double latitude; + private double longitude; + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + public double getLatitude() { + return latitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + public double getLongitude() { + return longitude; + } + + public int hashCode(){ + return (int)((latitude+90)/0.1)+(int)((longitude/0.1)*1800); + } +} diff --git a/app/src/main/java/com/example/cosmosclient/entities/Feature.java b/app/src/main/java/com/example/cosmosclient/entities/Feature.java index 63c3374..e498d67 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/Feature.java +++ b/app/src/main/java/com/example/cosmosclient/entities/Feature.java @@ -1,15 +1,22 @@ package com.example.cosmosclient.entities; public class Feature { - private String name; + private String genre; private int code; - private Location location = new Location(); + private CosmosLocation location = new CosmosLocation(); - public void setName(String name){ - this.name = name; + public void setLatitude(double latitude) { + location.setLatitude(latitude); } - public String getName(){ - return this.name; + public void setLongitude(double longitude) { + location.setLongitude(longitude); + } + + public String getGenre() { + return genre; + } + public void setGenre(String genre) { + this.genre = genre; } public void setCode(int code){ @@ -19,10 +26,10 @@ return this.code; } - public void setLocation(Location location){ + public void setLocation(CosmosLocation location){ this.location = location; } - public Location getLocation(){ + public CosmosLocation getLocation(){ return this.location; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/cosmosclient/entities/Location.java b/app/src/main/java/com/example/cosmosclient/entities/Location.java deleted file mode 100644 index 113b555..0000000 --- a/app/src/main/java/com/example/cosmosclient/entities/Location.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.cosmosclient.entities; - -public class Location { - private double latitude; - private double longitude; - - public void setLatitude(double latitude) { - this.latitude = latitude; - } - public double getLatitude() { - return latitude; - } - - public void setLongitude(double longitude) { - this.longitude = longitude; - } - public double getLongitude() { - return longitude; - } - - public int hashCode(){ - return (int)((latitude+90)/0.1)+(int)((longitude/0.1)*1800); - } -} diff --git a/app/src/main/java/com/example/cosmosclient/resources/LocationRest.java b/app/src/main/java/com/example/cosmosclient/resources/LocationRest.java new file mode 100644 index 0000000..0e74d20 --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/resources/LocationRest.java @@ -0,0 +1,20 @@ +package com.example.cosmosclient.resources; + +import com.example.cosmosclient.entities.AreaInformation; +import com.example.cosmosclient.entities.Feature; + +import java.util.ArrayList; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Query; + + +public interface LocationRest { + + @GET("shops") + Call> LocationService(@Query("leftLatitude") String leftLat, + @Query("leftLongitude") String leftLon, + @Query("rightLatitude") String rightLat, + @Query("rightLongitude") String rightLon); +} \ No newline at end of file diff --git a/app/src/main/java/com/example/cosmosclient/resources/ShopRest.java b/app/src/main/java/com/example/cosmosclient/resources/ShopRest.java deleted file mode 100644 index 0d9a51c..0000000 --- a/app/src/main/java/com/example/cosmosclient/resources/ShopRest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.cosmosclient.resources; - -import com.example.cosmosclient.entities.Feature; - -import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.Query; - -public interface ShopRest { - - @GET("rest") - Call ShopRequest(@Query("longitude") double longitude, - @Query("latitude") double latitude, - @Query("longitudeRange") double longitudeRange, - @Query("latitudeRange") double latitudeRange, - @Query("shop") int shop); -} 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 d8127a6..b5939ef 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; @@ -22,18 +21,26 @@ import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.util.Log; +import android.widget.Toast; import com.example.cosmosclient.R; import com.example.cosmosclient.app.Cosmos; +import com.example.cosmosclient.entities.CosmosLocation; +import com.example.cosmosclient.entities.Feature; 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 com.example.cosmosclient.resources.LocationRest; import java.io.FileDescriptor; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; @@ -52,6 +59,12 @@ private static final float MinDistance = 1;/*最小距離間隔*/ private static boolean isServiceRunning = false; //サービスが起動しているかを確認 private String TAG = CosmosBackgroundService.class.getSimpleName(); + private List notifications =null; + private CosmosLocation LOC = new CosmosLocation(); + private double leftLat,leftLon,rightLat,rightLon; //ここに計算予定 + private int count = 0; + + public CosmosBackgroundService() { super("CosmosBackgroundService"); @@ -108,7 +121,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) @@ -231,10 +244,15 @@ @Override public void onLocationChanged(Location location) { + final Cosmos cosmos = (Cosmos) getApplication(); // Log.d(TAG, "onLocationChanged"); Log.d(TAG, "lat:" + location.getLatitude()); Log.d(TAG, "lon:" + location.getLongitude()); + updateAreaInformation(location); + this.notifications=searchNotifications(cosmos,location); + + } @Override @@ -345,4 +363,143 @@ startActivity(settingsIntent); } + private void updateAreaInformation(Location location){ + Cosmos app = (Cosmos) getApplication(); + count = 0; + LOC.setLatitude(location.getLatitude()); + LOC.setLongitude(location.getLongitude()); + for(int i = 0;i> call = LocationService.LocationService(String.valueOf(leftLat), + String.valueOf(leftLon), String.valueOf(rightLat), String.valueOf(rightLon)); + + //サーバからデータ受け取り + call.enqueue(new Callback>() { + //成功時 + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful()) { + ArrayList result = response.body(); + + //app/Cosmosに情報保存 + Cosmos app = (Cosmos) getApplication(); +// app.setAreaInfo(); + + } else { + //onFailureでキャッチできないエラー用 + Toast.makeText(CosmosBackgroundService.this, + "通信エラー", Toast.LENGTH_LONG).show(); + } + } + + //失敗時 + @Override + public void onFailure(Call> call, Throwable t) { + t.printStackTrace(); + Toast.makeText(CosmosBackgroundService.this, + "区画情報の更新に失敗しました", Toast.LENGTH_SHORT).show(); + } + }); + } + + + } + 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(); + + //現在時刻取得 + 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; + + } + } diff --git a/app/src/main/java/com/example/cosmosclient/views/RequestPermissionsActivty.java b/app/src/main/java/com/example/cosmosclient/views/RequestPermissionsActivty.java new file mode 100644 index 0000000..794e5ec --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/views/RequestPermissionsActivty.java @@ -0,0 +1,21 @@ +package com.example.cosmosclient.views; + +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AppCompatActivity; + +import com.example.cosmosclient.app.Cosmos; + +public class RequestPermissionsActivty extends AppCompatActivity { + private ActivityCompat.OnRequestPermissionsResultCallback listener = null; + + public void setOnRequestPermissionsResultCallback(ActivityCompat.OnRequestPermissionsResultCallback listener) { + this.listener = listener; + } + + // 結果の受け取り + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + listener.onRequestPermissionsResult(requestCode, permissions, grantResults); + } +} 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 f6bf407..115ff08 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -40,6 +40,14 @@ Cosmos app = (Cosmos) getApplication(); String account = app.getuId(); + String token = app.getToken(); + +// //ログアウトしていない場合singnin画面を表示しない +// if(token != ""){ +// Intent intent = new Intent(getApplication(), GroupListActivity.class); +// startActivity(intent); +// finish(); +// } // 空チェック if (account != null && account.length() > 0) {