Commit 95473f47 by Kim Jinsung

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

parent 74123084
......@@ -4,6 +4,7 @@ import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import java.io.File;
import java.util.List;
......@@ -11,7 +12,9 @@ import java.util.Timer;
import jp.odakyu.toiletsignage.log.Logger;
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;
......@@ -32,6 +35,8 @@ public class ToiletApplication extends Application {
String logPath = downloadFilePath.getPath() + "/" + APP_LOG_PATH;
logger.setLogPathFormat(logPath);
Logger.i(TAG, "onCreate");
taskManagerThread();
}
public synchronized void stopTimer() {
......@@ -73,4 +78,21 @@ public class ToiletApplication extends Application {
}
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;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -10,12 +9,9 @@ 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.Date;
import java.util.TimerTask;
import jp.odakyu.toiletsignage.connection.NtpServerConnection;
......@@ -25,6 +21,7 @@ import jp.odakyu.toiletsignage.info.ToiletInfo;
import jp.odakyu.toiletsignage.json.ResultToiletJson;
import jp.odakyu.toiletsignage.json.USBToiletDefaultJson;
import jp.odakyu.toiletsignage.log.Logger;
import jp.odakyu.toiletsignage.util.CommonUtil;
import jp.odakyu.toiletsignage.util.DateTimeFormat;
import jp.odakyu.toiletsignage.util.DateTimeUtil;
......@@ -34,7 +31,6 @@ import jp.odakyu.toiletsignage.util.DateTimeUtil;
public class UpdateToiletInfoTask extends TimerTask {
private static final String TAG = "UpdateToiletInfoTask";
private static final int TWO_THOUSAND_YEARS = 2000;
private int errorCount = 0;
private Context applicationContext;
private ToiletInfo toiletInfo;
......@@ -52,7 +48,7 @@ public class UpdateToiletInfoTask extends TimerTask {
Logger.i(TAG, "Thread Start");
boolean isNetworkConnect = true;
//インターネット接続チェック
if (isNetworkAvailable(applicationContext)) {
if (CommonUtil.isNetworkAvailable(applicationContext)) {
ArrayList<USBToiletDefaultJson> toiletInfoArray = toiletInfo.getToiletDefaultJsonArray();
boolean isError = false;
boolean isNtpServerCheck = false;
......@@ -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サーバーの時刻を取得し、端末時刻にセット
* @throws IOException
*/
private void synchronizingNtpServerDate() throws IOException, JSONException {
if(isAbnormalYear()) {
if(DateTimeUtil.isAbnormalYear()) {
String jsonString = NtpServerConnection.connectionNtpServer();
JSONObject jsonObject = new JSONObject(jsonString);
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 {
df.setTimeZone(TimeZone.getTimeZone("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