blob: b00c1ee0a8c551b80ebab80d4966c2b41999a840 [file] [log] [blame]
Dirk Vogt4b2005d2016-10-27 18:19:48 +02001package com.fairphone.hiccup.app;
2
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;
10import java.util.Calendar;
11import java.util.Date;
12
13/**
14 * Copyright 2016 Fairphone B.V.
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29public class BoardDate {
30
31 /* The board date is stored in an ingenious format:
32 * Read the binary data of the file, convert it to hex
33 * and you get the decimal representation of the date.
34 * Speechless.
35 */
36
37 private static final String BOARD_DATE_FILE = "/persist/board_date.bin";
38
39 private static Date boardDate = null;
40
41 private static final String TAG = "HICCUP";
42
43 public static Date getBoardDate() {
44 if (boardDate== null) {
45 boardDate = readBoardDate();
46 }
47 return boardDate;
48 }
49
50 private static Date readBoardDate() {
51 long startTime;
52
53 byte[] boardFile = getBoardDateFileInByteArray();
54
55 String strDate = bytesToHex(boardFile);
56 startTime = getTimeInMilliseconds(strDate);
57
58 return new Date(startTime);
59 }
60
61 private static byte[] getBoardDateFileInByteArray()
62 {
63 File file = new File(BOARD_DATE_FILE);
64 byte[] fileData = new byte[(int) file.length()];
65 DataInputStream dis = null;
66 try {
67 dis = new DataInputStream(new FileInputStream(file));
68 dis.read(fileData);
69 dis.close();
70 } catch (FileNotFoundException e) {
71 Log.e(TAG,"BoardDateFile not found", e);
72 } catch (IOException e) {
73 Log.e(TAG, "BoardDateFile IOException", e);
74 }
75 finally {
76 return fileData;
77 }
78 }
79
80 public static String bytesToHex(byte[] bytes) {
81 final char[] hexArray = "0123456789ABCDEF".toCharArray();
82 char[] hexChars = new char[bytes.length * 2];
83 for ( int j = 0; j < bytes.length; j++ ) {
84 int v = bytes[j] & 0xFF;
85 hexChars[j * 2] = hexArray[v >>> 4];
86 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
87 }
88 return new String(hexChars);
89 }
90
91 private static long getTimeInMilliseconds(String date)
92 {
93 Calendar cal = Calendar.getInstance();
94
95 try {
96 cal.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
97 cal.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)));
98 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6, 8)));
99 cal.set(Calendar.HOUR_OF_DAY, 0);
100 cal.set(Calendar.MINUTE, 0);
101 cal.set(Calendar.SECOND, 0);
102 }
103 catch (NumberFormatException e) {
104 Log.e(TAG, "Parse Exception", e);
105 return 0;
106 }
107 return cal.getTimeInMillis();
108 }
109}
110