diff --git a/app/build.gradle b/app/build.gradle index 8a5b3fa..c635f8b 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 d050520..b774c0b 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -2,11 +2,17 @@ import android.app.Application; import android.content.ComponentCallbacks; +import android.content.Context; import android.content.res.Configuration; 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.entities.ObjectStorage; +import com.google.gson.Gson; + import android.content.SharedPreferences; import java.util.ArrayList; @@ -21,6 +27,7 @@ private HashMap areaInfo = new HashMap<>(); private ArrayList areaInfoKey= new ArrayList<>(); private String TAG = Cosmos.class.getSimpleName(); + private Area theWorld = new Area(); public Cosmos() { super(); @@ -150,16 +157,47 @@ //areaInfo処理 public void setAreaInfo(int areaId, AreaInformation AreaInfo){ this.areaInfo.put(areaId,AreaInfo); + this.theWorld.setAreaInfo(areaInfo); + SharedPreferences pref = getSharedPreferences("pref",MODE_PRIVATE); +//// SharedPreferences prefData = getSharedPreferences("pref_data", MODE_PRIVATE); +//// SharedPreferences.Editor editor = prefData.edit(); +//// editor.putString("AreaInfo", String.valueOf(areaInfo)); +//// editor.commit(); + Gson gson = new Gson(); + gson.toJson(theWorld); + pref.edit().putString("Area",gson.toJson(theWorld)).commit(); +// ObjectStorage.save(theWorld,CachePref.KEY_AREA_INFO); } public AreaInformation getAreaInfo(int areaId){ +// SharedPreferences prefData = getSharedPreferences("pref_data", MODE_PRIVATE); +// String areaInfo = prefData.getString("AreaInfo", ""); + +// this.theWorld = ObjectStorage.get(CachePref.KEY_AREA_INFO,Area.class); +// this.areaInfo = this.theWorld.getAreaInfo(); + + SharedPreferences pref = getSharedPreferences("pref",MODE_PRIVATE); + Gson gson = new Gson(); + theWorld = gson.fromJson(pref.getString("Area",""),Area.class); + this.areaInfo = this.theWorld.getAreaInfo(); + 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(); + + for (int key : areaInfo.keySet()) { + areaInfoKey.add(key); + } + return areaInfoKey; + }else{ + return new ArrayList<>(); } - return areaInfoKey; } } 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..affae53 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,12 @@ public void setFeature(HashMap> feature) { this.feature = feature; } + public void setFeature(int code,ArrayList feature){ + this.feature.put(code,feature); + if(this.location.getLatitude() == 0){ + setLocation(feature.get(0).getLocation()); + } + } 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/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 055ec70..ce01d17 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java @@ -26,6 +26,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; @@ -39,6 +40,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.TimeZone; import java.util.Timer; @@ -62,8 +64,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 = new AreaInformation(); public CosmosBackgroundService() { super("CosmosBackgroundService"); @@ -362,14 +370,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> 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.setFeature(Electrical.get(0).getCode(),Electrical); +// areaInfo.setFeature(Home.get(0).getCode(),Home); +// areaInfo.setFeature(Phone.get(0).getCode(),Phone); +// areaInfo.setFeature(PC.get(0).getCode(),PC); +// areaInfo.setFeature(Convenience.get(0).getCode(),Convenience); +// areaInfo.setFeature(Supermarket.get(0).getCode(),Supermarket); + + app.setAreaInfo(areaInfo.getLocation().hashCode(),areaInfo); } else { //onFailureでキャッチできないエラー用 Toast.makeText(CosmosBackgroundService.this, - "通信エラー", Toast.LENGTH_LONG).show(); + "区画情報取得失敗", Toast.LENGTH_LONG).show(); } } 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 9b75c51..495d8b8 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -22,6 +22,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; import com.example.cosmosclient.services.CosmosBackgroundService; @@ -40,6 +41,7 @@ private Button SigninButton; private Intent intentservice; private static final int REQUEST_MULTI_PERMISSIONS = 101; + private AreaInformation area = new AreaInformation(); @@ -67,6 +69,15 @@ Cosmos app = (Cosmos) getApplication(); String account = app.getuId(); String token = app.getToken(); + ArrayList key = app.AreaInfoGetKey(); + + + + if(key.size() != 0){ + for(int i = 0;i