Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
odakyuToiletSignage
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kddi
odakyuToiletSignage
Commits
31dd4c13
Commit
31dd4c13
authored
Jan 25, 2018
by
Kim Jinsung
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'kddi/odakyuToiletSignage_1.0.0_leej' into 'master'
#28062 端末の日付がリセットされる問題対応 See merge request
!1
parents
95cef713
35a6c1c4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
144 additions
and
4 deletions
+144
-4
app/src/main/java/jp/odakyu/toiletsignage/connection/NtpServerConnection.java
+68
-0
app/src/main/java/jp/odakyu/toiletsignage/task/UpdateToiletInfoTask.java
+71
-4
app/src/main/java/jp/odakyu/toiletsignage/util/DateTimeFormat.java
+5
-0
No files found.
app/src/main/java/jp/odakyu/toiletsignage/connection/NtpServerConnection.java
0 → 100644
View file @
31dd4c13
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
();
}
}
}
}
app/src/main/java/jp/odakyu/toiletsignage/task/UpdateToiletInfoTask.java
View file @
31dd4c13
...
...
@@ -5,23 +5,36 @@ import android.content.Intent;
import
android.net.ConnectivityManager
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
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
;
import
jp.odakyu.toiletsignage.connection.ToiletServerConnection
;
import
jp.odakyu.toiletsignage.constants.ToiletConstants
;
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.DateTimeFormat
;
import
jp.odakyu.toiletsignage.util.DateTimeUtil
;
/**
* Created by agentec on 2017/12/11.
*/
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
;
...
...
@@ -42,11 +55,17 @@ public class UpdateToiletInfoTask extends TimerTask {
if
(
isNetworkAvailable
(
applicationContext
))
{
ArrayList
<
USBToiletDefaultJson
>
toiletInfoArray
=
toiletInfo
.
getToiletDefaultJsonArray
();
boolean
isError
=
false
;
for
(
USBToiletDefaultJson
toiletDefaultJson:
toiletInfoArray
)
{
boolean
isNtpServerCheck
=
false
;
for
(
USBToiletDefaultJson
toiletDefaultJson
:
toiletInfoArray
)
{
try
{
if
(!
isNtpServerCheck
)
{
synchronizingNtpServerDate
();
isNtpServerCheck
=
true
;
}
String
jsonString
=
ToiletServerConnection
.
connectionToiletServer
(
toiletDefaultJson
.
getToiletId
());
if
(
jsonString
==
null
)
{
Logger
.
w
(
TAG
,
"json object null jsonString"
);
Logger
.
w
(
TAG
,
"json object null jsonString"
);
isError
=
true
;
break
;
}
...
...
@@ -55,8 +74,8 @@ public class UpdateToiletInfoTask extends TimerTask {
ArrayList
<
ResultToiletJson
>
resultToiletJsonArray
=
new
ArrayList
<>();
JSONArray
jsonArray
=
jsonObject
.
getJSONArray
(
ResultToiletJson
.
KEY_RESULT
);
Logger
.
d
(
TAG
,
"jsonArray.length = "
+
jsonArray
.
length
());
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++){
Logger
.
d
(
TAG
,
"jsonArray.length = "
+
jsonArray
.
length
());
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++)
{
ResultToiletJson
resultToiletJson
=
new
ResultToiletJson
((
JSONObject
)
jsonArray
.
get
(
i
));
resultToiletJsonArray
.
add
(
resultToiletJson
);
}
...
...
@@ -97,6 +116,54 @@ public class UpdateToiletInfoTask extends TimerTask {
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」に影響があるが確認が取れてないので未使用
private boolean isInternetAvailable() {
...
...
app/src/main/java/jp/odakyu/toiletsignage/util/DateTimeFormat.java
View file @
31dd4c13
...
...
@@ -264,4 +264,9 @@ public class DateTimeFormat {
* @since 1.0.0
*/
public
static
final
String
HHmmssSSS_colon
=
"HH:mm:ss.SSS"
;
/**
* 時間を示します。"MMDDHHmmyyyy.ss"
* @since 1.0.0
*/
public
static
final
String
MMDDHHmmyyyyss__colon
=
"MMDDHHmmyyyy.ss"
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment