diff --git a/app/src/main/java/com/example/cosmosclient/entities/Group.java b/app/src/main/java/com/example/cosmosclient/entities/Group.java index 1ee3c6d..9e8b5c8 100644 --- a/app/src/main/java/com/example/cosmosclient/entities/Group.java +++ b/app/src/main/java/com/example/cosmosclient/entities/Group.java @@ -8,6 +8,7 @@ public class Group { private String gId; private String uri, name; + private long requestHash; /* Userどうするか private ArrayList members; */ @@ -26,6 +27,7 @@ public Group(GroupJson groupJson){ setgId(groupJson.getgId()); setUri(groupJson.getUri()); + clearRequestHash(); setName(groupJson.getName()); } @@ -61,6 +63,22 @@ return name; } + public long getRequestHash() { + return requestHash; + } + + public void setRequestHash(long requestHash) { + this.requestHash = requestHash; + } + + public void clearRequestHash(){ + setRequestHash(0); + } + + public void updateRequestHash(){ + setRequestHash(getRequestHash()+1); + } + public RequestList getRequestList(){ return requestList; } diff --git a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java index 8397fe0..b301dd4 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java @@ -25,11 +25,17 @@ import com.example.cosmosclient.R; import com.example.cosmosclient.app.Cosmos; import com.example.cosmosclient.entities.AreaInformation; -import com.example.cosmosclient.entities.Feature; -import com.example.cosmosclient.entities.SignupResponse; +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 com.example.cosmosclient.views.GroupListActivity; -import com.example.cosmosclient.views.SignupActivity; + +import java.io.IOException; +import java.util.TimeZone; +import java.util.Timer; +import java.util.TimerTask; import retrofit2.Call; import retrofit2.Callback; @@ -103,6 +109,15 @@ } startGPS(); + Timer timer = new Timer(); + TimerTask task = new TimerTask() { + @Override + public void run() { + startUpdateRequest(); + } + }; + //timer.scheduleAtFixedRate(定期的に実行したいタスク,初回のタスク実行までの時間(ms),実行するタスクの間隔(ms)); + timer.scheduleAtFixedRate(task, 10000, 300000); return START_NOT_STICKY; } @@ -114,7 +129,7 @@ } protected void startGPS() { -// Log.d("debug", "startGPS"); + Log.d("debug", "startGPS"); final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); @@ -184,10 +199,12 @@ //app/Cosmosに情報保存 Cosmos app = (Cosmos) getApplication(); + app.setAreaInfo(result.getLocation().hashCode(),result); } else { //onFailureでキャッチできないエラー用 - + Toast.makeText(CosomosBackgroundService.this, + "通信エラー", Toast.LENGTH_LONG).show(); } } @@ -195,7 +212,8 @@ @Override public void onFailure(Call call, Throwable t) { t.printStackTrace(); - + Toast.makeText(CosomosBackgroundService.this, + "区画情報の更新に失敗しました", Toast.LENGTH_SHORT).show(); } }); } @@ -216,8 +234,95 @@ } + private void startUpdateRequest() { + final Cosmos app = (Cosmos) getApplication(); + if(app.getuId() != null) { + final String uId, token; + uId = app.getuId(); + token = app.getToken(); + + //retrofitの処理 + final Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/rest/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + //interfaceから実装を取得 + final GroupsRest getGroupsService = retrofit.create(GroupsRest.class); + final GroupsRest getRequestsService = retrofit.create(GroupsRest.class); + + //API呼び出しのための値入力 + final Call call = getGroupsService.getGroups(uId, token); + + //サーバからデータ受け取り + call.enqueue(new Callback() { + //成功時 + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + final GroupListResponse groupListresult = response.body(); + Log.d("debug", "GroupListResponseグループ情報通信取得しました"); + + new Thread(new Runnable() { + public void run() { + for (GroupJson groupJson : groupListresult.getGroups()) { + app.setGroup(new Group(groupJson)); + if (groupJson.getRequestHash() != app.getGroup(groupJson.getgId()).getRequestHash()) { + //RequestList取得するための必要な情報 + final String gId = groupJson.getgId(); + final boolean detail = true; + int quantity = 20; + + final Call requestsListByGidCall = getRequestsService.getRequestsListByGid(gId, token, detail, quantity); + Response requestListResponse; + try { + requestListResponse = requestsListByGidCall.execute(); + if (requestListResponse.isSuccessful()) { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo")); + RequestList requestList = requestListResponse.body(); + app.getGroup(groupJson.getgId()).setRequestList(requestList); + app.getGroup(groupJson.getgId()).setRequestHash(groupJson.getRequestHash()); + Log.d("debug", groupJson.getName() + "のRequestListを取得しました"); + } else { + // onFailure + try { + System.out.println(requestListResponse.errorBody().string()); + } catch (IOException e) { + e.printStackTrace(); + } + + //onFailureでキャッチできないエラーの処理 + Log.d("debug", groupJson.getName() + "の通信エラー"); + } + } catch (IOException e) { + e.printStackTrace(); + Log.d("debug", groupJson.getName() + "のRequestListの取得失敗しました"); + } + } + } + } + }).start(); + } else { + //onFailureでキャッチできないエラー用 + Log.d("debug", "GroupListResponseグループ情報通信エラー"); + } + } + + //失敗時 + @Override + public void onFailure(Call call, Throwable t) { + t.printStackTrace(); + Log.d("debug", "GroupListResponseグループ情報取得失敗"); + } + }); + } else { + Log.d("debug", "app.getuId()がnull"); + } + + } + private void enableLocationSettings() { Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(settingsIntent); } diff --git a/app/src/main/java/com/example/cosmosclient/views/GroupListActivity.java b/app/src/main/java/com/example/cosmosclient/views/GroupListActivity.java index 08b6688..0ff3b5b 100644 --- a/app/src/main/java/com/example/cosmosclient/views/GroupListActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/GroupListActivity.java @@ -1,6 +1,7 @@ package com.example.cosmosclient.views; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.nfc.Tag; @@ -345,6 +346,11 @@ Intent intent=new Intent(GroupListActivity.this, com.example.cosmosclient.views.JoinGroupActionsActivity.class); startActivity(intent); } else if (id == R.id.signOutButton){ + //トークン削除 + SharedPreferences prefData = getSharedPreferences("pref_data", MODE_PRIVATE); + SharedPreferences.Editor editor = prefData.edit(); + editor.remove("token"); + editor.commit(); gridView.stopEditMode(); Intent intent=new Intent(GroupListActivity.this, com.example.cosmosclient.views.SigninActivity.class); ActivityCompat.finishAffinity(GroupListActivity.this); 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 f1cd4e9..400555d 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -133,6 +133,13 @@ app.setToken(result.token); app.setuId(UserIdText.getText().toString()); + //SharedPreferencesに情報保存 + SharedPreferences prefData = getSharedPreferences("pref_data", MODE_PRIVATE); + SharedPreferences.Editor editor = prefData.edit(); + editor.putString("account", UserIdText.getText().toString()); + editor.putString("token", result.token); + editor.commit(); + //画面遷移 Intent intent = new Intent(getApplication(), GroupListActivity.class); startActivity(intent); diff --git a/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java b/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java index 32a05c0..afa76b9 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java @@ -112,6 +112,7 @@ Cosmos app = (Cosmos) getApplication(); app.setToken(result.token); app.setuId(result.uId); + //データをprerefarenceに保存 // 「pref_data」という設定データファイルを読み込み SharedPreferences prefData = getSharedPreferences("pref_data", MODE_PRIVATE); @@ -119,6 +120,7 @@ // 入力されたログインIDとログインパスワード editor.putString("account", result.uId); + editor.putString("token", result.token); // 保存 editor.commit();