Commit 31dd4c13 by Kim Jinsung

Merge branch 'kddi/odakyuToiletSignage_1.0.0_leej' into 'master'

#28062 端末の日付がリセットされる問題対応

See merge request !1
parents 95cef713 35a6c1c4
package jp.odakyu.toiletsignage.connection;
import android.util.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by agentec on 2018/01/23.
*/
public class NtpServerConnection {
private static final String NTP_SERVER_URL = "http://ntp-a1.nict.go.jp/cgi-bin/json";
private static final int CONNECTION_TIMEOUT = 5 * 1000;
private static final int READ_TIMEOUT = 5 * 1000;
public static String connectionNtpServer() throws IOException {
HttpURLConnection conn = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
InputStreamReader inReader =null;
try {
URL url = new URL(NTP_SERVER_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setRequestMethod("GET");
conn.connect();
int statusCode = conn.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
StringBuffer result = new StringBuffer();
//responseの読み込み
inputStream = conn.getInputStream();
String encoding = conn.getContentEncoding();
if (null == encoding) {
encoding = "UTF-8";
}
inReader = new InputStreamReader(inputStream, encoding);
bufferedReader = new BufferedReader(inReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
return null;
} finally {
if(bufferedReader != null) {
bufferedReader.close();
}
if(inReader != null) {
inReader.close();
}
if(inputStream != null) {
inputStream.close();
}
if(conn != null) {
conn.disconnect();
}
}
}
}
...@@ -5,23 +5,36 @@ import android.content.Intent; ...@@ -5,23 +5,36 @@ import android.content.Intent;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.DataOutputStream;
import java.io.IOException;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.TimerTask; import java.util.TimerTask;
import jp.odakyu.toiletsignage.connection.NtpServerConnection;
import jp.odakyu.toiletsignage.connection.ToiletServerConnection; import jp.odakyu.toiletsignage.connection.ToiletServerConnection;
import jp.odakyu.toiletsignage.constants.ToiletConstants; import jp.odakyu.toiletsignage.constants.ToiletConstants;
import jp.odakyu.toiletsignage.info.ToiletInfo; import jp.odakyu.toiletsignage.info.ToiletInfo;
import jp.odakyu.toiletsignage.json.ResultToiletJson; import jp.odakyu.toiletsignage.json.ResultToiletJson;
import jp.odakyu.toiletsignage.json.USBToiletDefaultJson; import jp.odakyu.toiletsignage.json.USBToiletDefaultJson;
import jp.odakyu.toiletsignage.log.Logger; import jp.odakyu.toiletsignage.log.Logger;
import jp.odakyu.toiletsignage.util.DateTimeFormat;
import jp.odakyu.toiletsignage.util.DateTimeUtil;
/** /**
* Created by agentec on 2017/12/11. * Created by agentec on 2017/12/11.
*/ */
public class UpdateToiletInfoTask extends TimerTask { public class UpdateToiletInfoTask extends TimerTask {
private static final String TAG = "UpdateToiletInfoTask"; private static final String TAG = "UpdateToiletInfoTask";
private static final int TWO_THOUSAND_YEARS = 2000;
private int errorCount = 0; private int errorCount = 0;
private Context applicationContext; private Context applicationContext;
private ToiletInfo toiletInfo; private ToiletInfo toiletInfo;
...@@ -42,11 +55,17 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -42,11 +55,17 @@ public class UpdateToiletInfoTask extends TimerTask {
if (isNetworkAvailable(applicationContext)) { if (isNetworkAvailable(applicationContext)) {
ArrayList<USBToiletDefaultJson> toiletInfoArray = toiletInfo.getToiletDefaultJsonArray(); ArrayList<USBToiletDefaultJson> toiletInfoArray = toiletInfo.getToiletDefaultJsonArray();
boolean isError = false; boolean isError = false;
for (USBToiletDefaultJson toiletDefaultJson:toiletInfoArray) { boolean isNtpServerCheck = false;
for (USBToiletDefaultJson toiletDefaultJson : toiletInfoArray) {
try { try {
if(!isNtpServerCheck) {
synchronizingNtpServerDate();
isNtpServerCheck = true;
}
String jsonString = ToiletServerConnection.connectionToiletServer(toiletDefaultJson.getToiletId()); String jsonString = ToiletServerConnection.connectionToiletServer(toiletDefaultJson.getToiletId());
if (jsonString == null) { if (jsonString == null) {
Logger.w(TAG,"json object null jsonString"); Logger.w(TAG, "json object null jsonString");
isError = true; isError = true;
break; break;
} }
...@@ -55,8 +74,8 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -55,8 +74,8 @@ public class UpdateToiletInfoTask extends TimerTask {
ArrayList<ResultToiletJson> resultToiletJsonArray = new ArrayList<>(); ArrayList<ResultToiletJson> resultToiletJsonArray = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray(ResultToiletJson.KEY_RESULT); JSONArray jsonArray = jsonObject.getJSONArray(ResultToiletJson.KEY_RESULT);
Logger.d(TAG,"jsonArray.length = " + jsonArray.length()); Logger.d(TAG, "jsonArray.length = " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++){ for (int i = 0; i < jsonArray.length(); i++) {
ResultToiletJson resultToiletJson = new ResultToiletJson((JSONObject) jsonArray.get(i)); ResultToiletJson resultToiletJson = new ResultToiletJson((JSONObject) jsonArray.get(i));
resultToiletJsonArray.add(resultToiletJson); resultToiletJsonArray.add(resultToiletJson);
} }
...@@ -97,6 +116,54 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -97,6 +116,54 @@ public class UpdateToiletInfoTask extends TimerTask {
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected(); return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
} }
/**
* 端末時刻が異常かチェック(2000年未満)
* @return
*/
private boolean isAbnormalYear() {
int currentYear = Integer.parseInt(DateTimeUtil.toStringInTimeZone(new Date(), DateTimeFormat.yyyy, "UTC"));
Logger.i(TAG,"Current year : " + currentYear);
return currentYear < TWO_THOUSAND_YEARS;
}
/**
* NTPサーバーの時刻を取得し、端末時刻にセット
* @throws IOException
*/
private void synchronizingNtpServerDate() throws IOException, JSONException {
if(isAbnormalYear()) {
String jsonString = NtpServerConnection.connectionNtpServer();
JSONObject jsonObject = new JSONObject(jsonString);
Long ntpTimeMillis = (long) ((Double) jsonObject.get("st") * 1000);
Logger.d(TAG, "NTP Server ntpTimeMillis : " + ntpTimeMillis);
//ntpサーバから取得時間の結果が秒(xxxxxxxxxx.xxx)なのでミリ秒に変換のため、小数点を削除
String cmd = "date " + DateTimeUtil.toString(new Timestamp(ntpTimeMillis), DateTimeFormat.MMDDHHmmyyyyss__colon);
Logger.d(TAG, "Command : " + cmd);
runAsRoot(cmd);
}
}
/**
* Root化端末のコマンド実行
*
* @param cmd
* @throws IOException
*/
private void runAsRoot(String cmd) {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream dataOutputStream = new DataOutputStream(process.getOutputStream());
dataOutputStream.writeBytes(cmd+"\n");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
} catch (IOException e) {
Logger.w(TAG, "runAsRoot error.");
//何も処理しない
}
}
/* /*
//実際「google.com」に影響があるが確認が取れてないので未使用 //実際「google.com」に影響があるが確認が取れてないので未使用
private boolean isInternetAvailable() { private boolean isInternetAvailable() {
......
...@@ -264,4 +264,9 @@ public class DateTimeFormat { ...@@ -264,4 +264,9 @@ public class DateTimeFormat {
* @since 1.0.0 * @since 1.0.0
*/ */
public static final String HHmmssSSS_colon = "HH:mm:ss.SSS"; public static final String HHmmssSSS_colon = "HH:mm:ss.SSS";
/**
* 時間を示します。"MMDDHHmmyyyy.ss"
* @since 1.0.0
*/
public static final String MMDDHHmmyyyyss__colon = "MMDDHHmmyyyy.ss";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment