blob: 8ec39422aeadea6b715b185456e2d595a6fd47dd [file] [log] [blame]
Dirk Vogt142d7782016-11-23 16:52:52 +01001package com.fairphone.hiccup.app;
Dirk Vogt4b2005d2016-10-27 18:19:48 +02002
3import android.util.Log;
4
5import java.io.DataInputStream;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.IOException;
Dirk Vogt142d7782016-11-23 16:52:52 +010010import java.text.SimpleDateFormat;
Dirk Vogt4b2005d2016-10-27 18:19:48 +020011import java.util.Date;
Dirk Vogt142d7782016-11-23 16:52:52 +010012import java.util.Locale;
13import java.util.TimeZone;
Dirk Vogt4b2005d2016-10-27 18:19:48 +020014
15/**
16 * Copyright 2016 Fairphone B.V.
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31public class BoardDate {
32
33 /* The board date is stored in an ingenious format:
34 * Read the binary data of the file, convert it to hex
35 * and you get the decimal representation of the date.
36 * Speechless.
37 */
38
39 private static final String BOARD_DATE_FILE = "/persist/board_date.bin";
Dirk Vogt142d7782016-11-23 16:52:52 +010040 private static final String BOARD_DATE_FORMAT = "yyyyMMddHH";
41 private static final String BOARD_DATE_TIMEZONE = "Asia/Shanghai";
42 private static final long DEFAULT_BOARD_DATE_TIMESTAMP = 0L;
Dirk Vogt4b2005d2016-10-27 18:19:48 +020043
44 private static Date boardDate = null;
45
46 private static final String TAG = "HICCUP";
47
48 public static Date getBoardDate() {
49 if (boardDate== null) {
50 boardDate = readBoardDate();
51 }
52 return boardDate;
53 }
54
55 private static Date readBoardDate() {
Dirk Vogt142d7782016-11-23 16:52:52 +010056 Date boardDate;
Dirk Vogt4b2005d2016-10-27 18:19:48 +020057
Dirk Vogt142d7782016-11-23 16:52:52 +010058 try {
59 final byte[] boardFile = getBoardDateFileInByteArray();
60 final String strDate = bytesToHex(boardFile);
61 final SimpleDateFormat format = new SimpleDateFormat(BOARD_DATE_FORMAT, Locale.US);
62 format.setTimeZone(TimeZone.getTimeZone(BOARD_DATE_TIMEZONE));
63
64 // we want to ignore the time for anonymity
65 boardDate = format.parse(strDate.substring(0,10));
Dirk Vogtd0b55f22016-11-01 12:30:47 +010066 } catch (Exception e) {
Dirk Vogt142d7782016-11-23 16:52:52 +010067 Log.e(TAG, "Unknown error while reading board date; using default", e);
68 boardDate = new Date(DEFAULT_BOARD_DATE_TIMESTAMP);
Dirk Vogtd0b55f22016-11-01 12:30:47 +010069 }
Dirk Vogt142d7782016-11-23 16:52:52 +010070
71 return boardDate;
Dirk Vogt4b2005d2016-10-27 18:19:48 +020072 }
73
74 private static byte[] getBoardDateFileInByteArray()
75 {
76 File file = new File(BOARD_DATE_FILE);
77 byte[] fileData = new byte[(int) file.length()];
78 DataInputStream dis = null;
79 try {
80 dis = new DataInputStream(new FileInputStream(file));
81 dis.read(fileData);
82 dis.close();
83 } catch (FileNotFoundException e) {
84 Log.e(TAG,"BoardDateFile not found", e);
85 } catch (IOException e) {
86 Log.e(TAG, "BoardDateFile IOException", e);
87 }
88 finally {
89 return fileData;
90 }
91 }
92
93 public static String bytesToHex(byte[] bytes) {
94 final char[] hexArray = "0123456789ABCDEF".toCharArray();
95 char[] hexChars = new char[bytes.length * 2];
96 for ( int j = 0; j < bytes.length; j++ ) {
97 int v = bytes[j] & 0xFF;
98 hexChars[j * 2] = hexArray[v >>> 4];
99 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
100 }
101 return new String(hexChars);
102 }
Dirk Vogt4b2005d2016-10-27 18:19:48 +0200103}
104