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 a9a702e..986dcc5 100644 --- a/app/src/main/java/com/example/cosmosclient/app/Cosmos.java +++ b/app/src/main/java/com/example/cosmosclient/app/Cosmos.java @@ -157,16 +157,16 @@ //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); +// 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); @@ -175,10 +175,10 @@ // 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(); +// 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); } @@ -190,7 +190,7 @@ 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); } 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 affae53..635eeb6 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java +++ b/app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java @@ -21,9 +21,6 @@ } 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/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/services/CosmosBackgroundService.java b/app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java index 00970ff..2d59e3d 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; @@ -73,7 +72,7 @@ private ArrayList PC = new ArrayList<>(); private ArrayList Convenience = new ArrayList<>(); private ArrayList Supermarket = new ArrayList<>(); - private AreaInformation areaInfo = new AreaInformation(); + private AreaInformation areaInfo = null; public CosmosBackgroundService() { super("CosmosBackgroundService"); @@ -252,14 +251,20 @@ } @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); + new Thread(new Runnable() { + private List notifications; + + public void run() { + updateAreaInformation(location); + this.notifications=searchNotifications(cosmos,location); + } + }).start(); } @@ -403,63 +408,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(); - 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; - } + 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; } - - //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(); + //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(); +// } +// }); } @@ -472,8 +529,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 @@ -481,7 +537,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を作成