区画情報保存する処理(エラーあり)
1 parent b5fdf1e commit 48b0fbfee1231906fb895128af5a2cf9d807c671
h-fuji authored on 12 Nov 2019
Showing 8 changed files
View
2
■■■
app/build.gradle
implementation 'com.android.support:design:28.0.0'
//cropView
api 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
implementation project(path: ':dynamicgrid')
//Gson
implementation 'com.google.code.gson:gson:2.2.4'
}
View
54
app/src/main/java/com/example/cosmosclient/app/Cosmos.java
package com.example.cosmosclient.app;
 
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;
import java.util.Collection;
private String uId=null;
private HashMap<Integer, AreaInformation> areaInfo = new HashMap<>();
private ArrayList<Integer> areaInfoKey= new ArrayList<>();
private String TAG = Cosmos.class.getSimpleName();
private Area theWorld = new Area();
 
public Cosmos() {
super();
Log.d(TAG, " Constructor");
 
//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<Integer> 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;
}
 
}
View
16
app/src/main/java/com/example/cosmosclient/entities/Area.java 0 → 100644
package com.example.cosmosclient.entities;
 
import java.util.HashMap;
 
public class Area {
private HashMap<Integer, AreaInformation> areaInfo = new HashMap<>();
 
public void setAreaInfo(HashMap<Integer, AreaInformation> areaInfo) {
this.areaInfo = areaInfo;
}
 
public HashMap<Integer, AreaInformation> getAreaInfo() {
return areaInfo;
}
}
View
6
app/src/main/java/com/example/cosmosclient/entities/AreaInformation.java
 
public void setFeature(HashMap<Integer, ArrayList<Feature>> feature) {
this.feature = feature;
}
public void setFeature(int code,ArrayList<Feature> feature){
this.feature.put(code,feature);
if(this.location.getLatitude() == 0){
setLocation(feature.get(0).getLocation());
}
}
public HashMap<Integer, ArrayList<Feature>> getFeature() {
return feature;
}
 
View
21
app/src/main/java/com/example/cosmosclient/entities/CachePref.java 0 → 100644
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();
}
}
View
19
app/src/main/java/com/example/cosmosclient/entities/ObjectStorage.java 0 → 100644
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> T get(String key, Class<T> klazz) {
String jsonStr = new CachePref().get(key, "");
if (jsonStr.equals("")) {
return null;
}
return new Gson().fromJson(jsonStr, klazz);
}
}
View
80
app/src/main/java/com/example/cosmosclient/services/CosmosBackgroundService.java
import android.widget.Toast;
 
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;
import com.example.cosmosclient.entities.GroupListResponse;
import java.io.FileDescriptor;
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;
import java.util.TimerTask;
private List<Notification> notifications =null;
private CosmosLocation LOC = new CosmosLocation();
private double leftLat,leftLon,rightLat,rightLon; //ここに計算予定
private int count = 0;
 
 
private int areacount = 0;
private ArrayList<Feature> Electrical = new ArrayList<>();
private ArrayList<Feature> Home = new ArrayList<>();
private ArrayList<Feature> Phone = new ArrayList<>();
private ArrayList<Feature> PC = new ArrayList<>();
private ArrayList<Feature> Convenience = new ArrayList<>();
private ArrayList<Feature> Supermarket = new ArrayList<>();
private AreaInformation areaInfo = new AreaInformation();
 
public CosmosBackgroundService() {
super("CosmosBackgroundService");
Log.d(TAG, "Constructor");
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
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) {
//retrofitの処理
@Override
public void onResponse(Call<ArrayList<Feature>> call, Response<ArrayList<Feature>> response) {
if (response.isSuccessful()) {
ArrayList<Feature> 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();
}
}
 
//失敗時
View
app/src/main/java/com/example/cosmosclient/views/SigninActivity.java