Commit 3025b63d by leej

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

parent 95cef713
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/ntp";
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;
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の読み込み
final InputStream in = conn.getInputStream();
String encoding = conn.getContentEncoding();
if (null == encoding) {
encoding = "UTF-8";
}
final InputStreamReader inReader = new InputStreamReader(in, encoding);
final BufferedReader bufferedReader = new BufferedReader(inReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.indexOf("<") == -1 && line.indexOf(">") == -1) {
result.append(line);
}
}
bufferedReader.close();
inReader.close();
in.close();
return result.toString();
}
return null;
} catch(IOException e) {
throw e;
} finally {
if(conn != null) {
conn.disconnect();
}
}
}
}
...@@ -7,21 +7,31 @@ import android.net.ConnectivityManager; ...@@ -7,21 +7,31 @@ import android.net.ConnectivityManager;
import org.json.JSONArray; import org.json.JSONArray;
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.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 +52,17 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -42,11 +52,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 +71,8 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -55,8 +71,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 +113,85 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -97,6 +113,85 @@ 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 {
if(isAbnormalYear() && rootCheck()) {
Logger.i(TAG, "Synchronizing NTP Server Time");
String result = NtpServerConnection.connectionNtpServer();
Logger.i(TAG, "NTP Server Time : " + result);
//ntpサーバから取得時間の結果が秒(xxxxxxxxxx.xxx)なのでミリ秒に変換のため、小数点を削除
String ntpTimeMillis = result.replace(".", "");
String cmd = "date " + DateTimeUtil.convertNtpTimeToStringTime(Long.parseLong(ntpTimeMillis), DateTimeFormat.MMDDHHmmyyyyss__colon);
Logger.i(TAG, "Command : " + cmd);
RunAsRoot(cmd);
}
}
/**
* Root化端末のコマンド実行
*
* @param cmds
* @throws IOException
*/
private void RunAsRoot(String cmds) throws IOException {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(cmds+"\n");
os.writeBytes("exit\n");
os.flush();
}
//Root化端末チェック
private boolean rootCheck() {
boolean isRoot = false;
if (suCommandCheck() || superUserCheck()) {
isRoot = true;
}
Logger.i(TAG, "isRoot : " + isRoot);
return isRoot;
}
//suコマンドチェック
private boolean suCommandCheck() {
boolean suCommand = true;
try {
Process process = Runtime.getRuntime().exec("su");
process.destroy();
} catch (IOException e) {
//suコマンドが実行されない場合
suCommand = false;
}
return suCommand;
}
//superUserアプリチェック
private boolean superUserCheck() {
boolean superUser = true;
try {
applicationContext.getPackageManager().getApplicationInfo("com.noshufou.android.su", 0);
} catch (NameNotFoundException e) {
superUser = false;
}
return superUser;
}
/* /*
//実際「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";
} }
...@@ -538,4 +538,12 @@ public class DateTimeUtil { ...@@ -538,4 +538,12 @@ public class DateTimeUtil {
df.setTimeZone(TimeZone.getTimeZone("GMT")); df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(date)+ " GMT"; return df.format(date)+ " GMT";
} }
public static String convertNtpTimeToStringTime (long ntpTime, String format) {
//NTP時刻とPOSIX時刻のオフセット(ミリ秒)
long offset = 2208988800L * 1000;
long result = ntpTime - offset;
return new SimpleDateFormat(format).format(new Timestamp(result));
}
} }
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