diff --git a/.idea/misc.xml b/.idea/misc.xml index 37a7509..dfd2c79 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/app/build.gradle b/app/build.gradle index f34f8ff..feb7b35 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,4 +35,6 @@ //cropView api 'com.theartofdev.edmodo:android-image-cropper:2.7.+' implementation project(path: ':dynamicgrid') + //Gson + implementation 'com.google.code.gson:gson:2.2.4' } 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 08f8cc6..9a9a4eb 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -7,6 +7,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.Context; import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; @@ -15,11 +16,16 @@ import android.support.v4.content.ContextCompat; import android.util.Log; +import com.example.cosmosclient.entities.Area; import com.example.cosmosclient.entities.AreaInformation; +import com.example.cosmosclient.entities.CachePref; import com.example.cosmosclient.entities.Group; import com.example.cosmosclient.services.CosmosBackgroundService; import com.example.cosmosclient.views.RequestPermissionsActivity; +import com.example.cosmosclient.entities.ObjectStorage; +import com.google.gson.Gson; + import android.content.SharedPreferences; import android.view.Gravity; import android.widget.Toast; @@ -38,6 +44,7 @@ private ArrayList areaInfoKey= new ArrayList<>(); private static final int REQUEST_MULTI_PERMISSIONS = 101; ArrayList reqPermissions = new ArrayList<>(); + private Area theWorld = new Area(); public Cosmos() { super(); @@ -241,11 +248,22 @@ return areaInfo.get(areaId); } + public ArrayList AreaInfoGetKey(){ - for(int key : areaInfo.keySet()){ - areaInfoKey.add(key); + SharedPreferences pref = getSharedPreferences("pref",MODE_PRIVATE); + Gson gson = new Gson(); + theWorld = gson.fromJson(pref.getString("Area",""),Area.class); + if(theWorld != null) { + this.areaInfo = this.theWorld.getAreaInfo(); + this.areaInfoKey.clear(); + for (int key : areaInfo.keySet()) { + areaInfoKey.add(key); + } + return areaInfoKey; + }else{ + theWorld =new Area(); + return new ArrayList<>(); } - return areaInfoKey; } // 位置情報許可の確認、外部ストレージのPermissionにも対応できるようにしておく diff --git a/app/src/main/java/com/example/cosmosclient/entities/Area.java b/app/src/main/java/com/example/cosmosclient/entities/Area.java new file mode 100644 index 0000000..2460f5f --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/entities/Area.java @@ -0,0 +1,15 @@ +package com.example.cosmosclient.entities; + +import java.util.HashMap; + +public class Area { + private HashMap areaInfo = new HashMap<>(); + + public void setAreaInfo(HashMap areaInfo) { + this.areaInfo = areaInfo; + } + + public HashMap getAreaInfo() { + return areaInfo; + } +} 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 3dc2816..635eeb6 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java +++ b/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java @@ -19,6 +19,9 @@ public void setFeature(HashMap> feature) { this.feature = feature; } + public void setFeature(int code,ArrayList feature){ + this.feature.put(code,feature); + } public HashMap> getFeature() { return feature; } diff --git a/app/src/main/java/com/example/cosmosclient/entities/CachePref.java b/app/src/main/java/com/example/cosmosclient/entities/CachePref.java new file mode 100644 index 0000000..17b717d --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/entities/CachePref.java @@ -0,0 +1,20 @@ +package com.example.cosmosclient.entities; + +import android.content.SharedPreferences; + +import com.google.gson.Gson; + +public class CachePref { + private final static String RPEF_NAME = "cache"; + private SharedPreferences pref; + private SharedPreferences.Editor editor; + + public static final String KEY_AREA_INFO = "AreaInfo"; + + public String get(String key, String defaultValue) { + return pref.getString(key, defaultValue); + } + public void put(String key, String value) { + editor.putString(key, value).commit(); + } +} diff --git a/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java b/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java index a13d9db..bec955a 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java +++ b/app/src/main/java/com/example/cosmosclient/entities/CosmosLocation.java @@ -19,6 +19,6 @@ } public int hashCode(){ - return (int)((latitude+90)/0.1)+(int)((longitude/0.1)*1800); + return (int)((latitude+90)/0.1)+((int)(longitude/0.1)*1800); } } diff --git a/app/src/main/java/com/example/cosmosclient/entities/ObjectStorage.java b/app/src/main/java/com/example/cosmosclient/entities/ObjectStorage.java new file mode 100644 index 0000000..4e3226d --- /dev/null +++ b/app/src/main/java/com/example/cosmosclient/entities/ObjectStorage.java @@ -0,0 +1,18 @@ +package com.example.cosmosclient.entities; + +import com.google.gson.Gson; + +public class ObjectStorage { + public static void save(Object src, String key) { + String json = new Gson().toJson(src); + new CachePref().put(key, json); + } + + public static T get(String key, Class klazz) { + String jsonStr = new CachePref().get(key, ""); + if (jsonStr.equals("")) { + return null; + } + return new Gson().fromJson(jsonStr, klazz); + } +} 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 af90275..d222537 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java @@ -25,6 +25,7 @@ import com.example.cosmosclient.R; import com.example.cosmosclient.app.Cosmos; +import com.example.cosmosclient.entities.AreaInformation; import com.example.cosmosclient.entities.CosmosLocation; import com.example.cosmosclient.entities.Feature; import com.example.cosmosclient.entities.Group; @@ -38,6 +39,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -66,6 +68,14 @@ private CosmosLocation LOC = new CosmosLocation(); private double leftLat,leftLon,rightLat,rightLon; //ここに計算予定 private int count = 0; + private int areacount = 0; + private ArrayList Electrical = new ArrayList<>(); + private ArrayList Home = new ArrayList<>(); + private ArrayList Phone = new ArrayList<>(); + private ArrayList PC = new ArrayList<>(); + private ArrayList Convenience = new ArrayList<>(); + private ArrayList Supermarket = new ArrayList<>(); + private AreaInformation areaInfo = null; private NotificationManager notificationManager; public CosmosBackgroundService() { @@ -249,16 +259,21 @@ } @Override - public void onLocationChanged(Location location) { + public void onLocationChanged(final 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); - sendNotifications(this, notifications); + new Thread(new Runnable() { + private List notifications; + public void run() { + updateAreaInformation(location); + this.notifications=searchNotifications(cosmos,location); + sendNotifications(CosmosBackgroundService.this, notifications); + } + }).start(); } @@ -399,14 +414,16 @@ startActivity(settingsIntent); } - private void updateAreaInformation(Location location) { + private void updateAreaInformation(final Location location){ Cosmos app = (Cosmos) getApplication(); count = 0; LOC.setLatitude(location.getLatitude()); LOC.setLongitude(location.getLongitude()); - for (int i = 0; i < app.AreaInfoGetKey().size(); i++) { - if (LOC.hashCode() != app.AreaInfoGetKey().get(i)) { - count++; + if(app.AreaInfoGetKey().size() != 0) { + for (int i = 0; i < app.AreaInfoGetKey().size(); i++) { + if (LOC.hashCode() != app.AreaInfoGetKey().get(i)) { + count++; + } } } if (count == 0) { @@ -428,33 +445,115 @@ Call> 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(); + Response> areaInformationResponse; + try{ + areaInformationResponse = call.execute(); + if (areaInformationResponse.isSuccessful()) { + areaInfo = new AreaInformation(); + ArrayList result = areaInformationResponse.body(); + for(int i = 0; i < result.size(); i++){ + switch (result.get(i).getCode()) { + case 203001: + Electrical.add(result.get(i)); + areaInfo.setFeature(Electrical.get(0).getCode(),Electrical); + break; + case 203002: + Home.add(result.get(i)); + areaInfo.setFeature(Home.get(0).getCode(),Home); + break; + case 203003: + Phone.add(result.get(i)); + areaInfo.setFeature(Phone.get(0).getCode(),Phone); + break; + case 203004: + PC.add(result.get(i)); + areaInfo.setFeature(PC.get(0).getCode(),PC); + break; + case 205001: + Convenience.add(result.get(i)); + areaInfo.setFeature(Convenience.get(0).getCode(),Convenience); + break; + case 205002: + Supermarket.add(result.get(i)); + areaInfo.setFeature(Supermarket.get(0).getCode(),Supermarket); + break; + default: + continue; + } } - } - //失敗時 - @Override - public void onFailure(Call> call, Throwable t) { - t.printStackTrace(); + //app/Cosmosに情報保存 + app = (Cosmos) getApplication(); + areaInfo.setLocation(LOC); + app.setAreaInfo(areaInfo.getLocation().hashCode(),areaInfo); + + } else { + //onFailureでキャッチできないエラー用 Toast.makeText(CosmosBackgroundService.this, - "区画情報の更新に失敗しました", Toast.LENGTH_SHORT).show(); + "区画情報取得失敗", Toast.LENGTH_LONG).show(); } - }); + }catch (IOException e){ + e.printStackTrace(); + Log.d(TAG, "区画情報を取得できていません。"); + } + + //サーバからデータ受け取り +// call.enqueue(new Callback>() { +// //成功時 +// @Override +// public void onResponse(Call> call, Response> response) { +// if (response.isSuccessful()) { +// ArrayList result = response.body(); +// for(int i = 0; i < result.size(); i++){ +// switch (result.get(i).getCode()) { +// case 203001: +// Electrical.add(result.get(i)); +// areaInfo.setFeature(Electrical.get(0).getCode(),Electrical); +// break; +// case 203002: +// Home.add(result.get(i)); +// areaInfo.setFeature(Home.get(0).getCode(),Home); +// break; +// case 203003: +// Phone.add(result.get(i)); +// areaInfo.setFeature(Phone.get(0).getCode(),Phone); +// break; +// case 203004: +// PC.add(result.get(i)); +// areaInfo.setFeature(PC.get(0).getCode(),PC); +// break; +// case 205001: +// Convenience.add(result.get(i)); +// areaInfo.setFeature(Convenience.get(0).getCode(),Convenience); +// break; +// case 205002: +// Supermarket.add(result.get(i)); +// areaInfo.setFeature(Supermarket.get(0).getCode(),Supermarket); +// break; +// default: +// continue; +// } +// } +// +// //app/Cosmosに情報保存 +// Cosmos app = (Cosmos) getApplication(); +// app.setAreaInfo(areaInfo.getLocation().hashCode(),areaInfo); +// +// } 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(); +// } +// }); } } @@ -466,8 +565,7 @@ HashMap> codeToNotification = new HashMap<>(); //業番:feature HashMap> codeToFeature = new HashMap<>(); - ArrayList groups =(ArrayList) cosmos.getGroups(); - + ArrayList groups = new ArrayList<>(cosmos.getGroups()); //現在時刻取得 Date nowDate = new Date(); //codeToNotificationを作成。Featureはnull @@ -475,7 +573,7 @@ //requestのArrayListを回す for (int j = 0; j < groups.get(i).getRequestList().getRequests().size(); j++) { //codeToNotificationに格納するArrayListを初期化 - ArrayList notifications = null; + ArrayList notifications = new ArrayList<>(); //期限内かつ未達成のrequestのみ取得 if (groups.get(i).getRequestList().getRequests().get(j).getDeadline().after(nowDate) && groups.get(i).getRequestList().getRequests().get(j).isDone() == false) { //notificationを作成 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 1070a00..ba12736 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -12,6 +12,7 @@ import com.example.cosmosclient.R; import com.example.cosmosclient.app.Cosmos; +import com.example.cosmosclient.entities.AreaInformation; import com.example.cosmosclient.entities.SigninResponse; import com.example.cosmosclient.resources.UsersRest;