Commit 95473f47 by Kim Jinsung

端末の時間が初期化された場合、実行中のタスクを再作成するスレッド追加

parent 74123084
...@@ -4,6 +4,7 @@ import android.app.ActivityManager; ...@@ -4,6 +4,7 @@ import android.app.ActivityManager;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
...@@ -11,7 +12,9 @@ import java.util.Timer; ...@@ -11,7 +12,9 @@ import java.util.Timer;
import jp.odakyu.toiletsignage.log.Logger; import jp.odakyu.toiletsignage.log.Logger;
import jp.odakyu.toiletsignage.task.UpdateToiletInfoTask; import jp.odakyu.toiletsignage.task.UpdateToiletInfoTask;
import jp.odakyu.toiletsignage.util.FileUtil; import jp.odakyu.toiletsignage.util.CommonUtil;
import jp.odakyu.toiletsignage.util.DateTimeFormat;
import jp.odakyu.toiletsignage.util.DateTimeUtil;
import static jp.odakyu.toiletsignage.constants.ToiletConstants.APP_LOG_PATH; import static jp.odakyu.toiletsignage.constants.ToiletConstants.APP_LOG_PATH;
...@@ -32,6 +35,8 @@ public class ToiletApplication extends Application { ...@@ -32,6 +35,8 @@ public class ToiletApplication extends Application {
String logPath = downloadFilePath.getPath() + "/" + APP_LOG_PATH; String logPath = downloadFilePath.getPath() + "/" + APP_LOG_PATH;
logger.setLogPathFormat(logPath); logger.setLogPathFormat(logPath);
Logger.i(TAG, "onCreate"); Logger.i(TAG, "onCreate");
taskManagerThread();
} }
public synchronized void stopTimer() { public synchronized void stopTimer() {
...@@ -73,4 +78,21 @@ public class ToiletApplication extends Application { ...@@ -73,4 +78,21 @@ public class ToiletApplication extends Application {
} }
return isForeground; return isForeground;
} }
private void taskManagerThread() {
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
String currentDateString = DateTimeUtil.toString(DateTimeUtil.getCurrentDate(),DateTimeFormat.yyyyMMddHHmmss_hyphen);
Logger.d(TAG, "*** currentDate = "+ currentDateString);
if (DateTimeUtil.isAbnormalYear() && CommonUtil.isNetworkAvailable(getApplicationContext())) {
stopTimer();
startTimer();
Logger.d(TAG, "*** ReStartTime currentDate = "+ currentDateString);
}
handler.postDelayed(this, 10000);//10秒後に再度実行
}
});
}
} }
...@@ -2,7 +2,6 @@ package jp.odakyu.toiletsignage.task; ...@@ -2,7 +2,6 @@ package jp.odakyu.toiletsignage.task;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -10,12 +9,9 @@ import org.json.JSONObject; ...@@ -10,12 +9,9 @@ import org.json.JSONObject;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import java.sql.Timestamp; 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.NtpServerConnection;
...@@ -25,6 +21,7 @@ import jp.odakyu.toiletsignage.info.ToiletInfo; ...@@ -25,6 +21,7 @@ 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.CommonUtil;
import jp.odakyu.toiletsignage.util.DateTimeFormat; import jp.odakyu.toiletsignage.util.DateTimeFormat;
import jp.odakyu.toiletsignage.util.DateTimeUtil; import jp.odakyu.toiletsignage.util.DateTimeUtil;
...@@ -34,7 +31,6 @@ import jp.odakyu.toiletsignage.util.DateTimeUtil; ...@@ -34,7 +31,6 @@ import jp.odakyu.toiletsignage.util.DateTimeUtil;
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;
...@@ -52,7 +48,7 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -52,7 +48,7 @@ public class UpdateToiletInfoTask extends TimerTask {
Logger.i(TAG, "Thread Start"); Logger.i(TAG, "Thread Start");
boolean isNetworkConnect = true; boolean isNetworkConnect = true;
//インターネット接続チェック //インターネット接続チェック
if (isNetworkAvailable(applicationContext)) { if (CommonUtil.isNetworkAvailable(applicationContext)) {
ArrayList<USBToiletDefaultJson> toiletInfoArray = toiletInfo.getToiletDefaultJsonArray(); ArrayList<USBToiletDefaultJson> toiletInfoArray = toiletInfo.getToiletDefaultJsonArray();
boolean isError = false; boolean isError = false;
boolean isNtpServerCheck = false; boolean isNtpServerCheck = false;
...@@ -107,30 +103,11 @@ public class UpdateToiletInfoTask extends TimerTask { ...@@ -107,30 +103,11 @@ public class UpdateToiletInfoTask extends TimerTask {
} }
/** /**
* インターネット(Wifi)チェック
* @param context Application Context
* @return internet check
*/
private boolean isNetworkAvailable(Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
/**
* 端末時刻が異常かチェック(2000年未満)
* @return
*/
private boolean isAbnormalYear() {
int currentYear = Integer.parseInt(DateTimeUtil.toStringInTimeZone(new Date(), DateTimeFormat.yyyy, "UTC"));
return currentYear < TWO_THOUSAND_YEARS;
}
/**
* NTPサーバーの時刻を取得し、端末時刻にセット * NTPサーバーの時刻を取得し、端末時刻にセット
* @throws IOException * @throws IOException
*/ */
private void synchronizingNtpServerDate() throws IOException, JSONException { private void synchronizingNtpServerDate() throws IOException, JSONException {
if(isAbnormalYear()) { if(DateTimeUtil.isAbnormalYear()) {
String jsonString = NtpServerConnection.connectionNtpServer(); String jsonString = NtpServerConnection.connectionNtpServer();
JSONObject jsonObject = new JSONObject(jsonString); JSONObject jsonObject = new JSONObject(jsonString);
Long ntpTimeMillis = (long) ((Double) jsonObject.get("st") * 1000); Long ntpTimeMillis = (long) ((Double) jsonObject.get("st") * 1000);
......
package jp.odakyu.toiletsignage.util;
import android.content.Context;
import android.net.ConnectivityManager;
import java.util.Date;
/**
* Created by agentec on 2018/01/26.
*/
public class CommonUtil {
/**
* インターネット(Wifi)チェック
* @param context Application Context
* @return internet check
*/
public static boolean isNetworkAvailable(Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}
...@@ -538,4 +538,13 @@ public class DateTimeUtil { ...@@ -538,4 +538,13 @@ public class DateTimeUtil {
df.setTimeZone(TimeZone.getTimeZone("GMT")); df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(date)+ " GMT"; return df.format(date)+ " GMT";
} }
/**
* 端末時刻が異常かチェック(2000年未満)
* @return
*/
public static boolean isAbnormalYear() {
int currentYear = Integer.parseInt(DateTimeUtil.toStringInTimeZone(new Date(), DateTimeFormat.yyyy, "UTC"));
return currentYear < 2000;
}
} }
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