blob: 5a51bdbf41c70041d510c3c0ded62735b536b40a [file] [log] [blame]
Dirk Vogtd0b55f22016-11-01 12:30:47 +01001 package 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;
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() {
Dirk Vogtd0b55f22016-11-01 12:30:47 +010051 long startTime = 0;
52 try {
53 byte[] boardFile = getBoardDateFileInByteArray();
Dirk Vogt4b2005d2016-10-27 18:19:48 +020054
Dirk Vogtd0b55f22016-11-01 12:30:47 +010055 String strDate = bytesToHex(boardFile);
56 startTime = getTimeInMilliseconds(strDate);
57 } catch (Exception e) {
58 Log.e(TAG,"Unknown error while reading board date; using default", e);
59 }
Dirk Vogt4b2005d2016-10-27 18:19:48 +020060 return new Date(startTime);
61 }
62
63 private static byte[] getBoardDateFileInByteArray()
64 {
65 File file = new File(BOARD_DATE_FILE);
66 byte[] fileData = new byte[(int) file.length()];
67 DataInputStream dis = null;
68 try {
69 dis = new DataInputStream(new FileInputStream(file));
70 dis.read(fileData);
71 dis.close();
72 } catch (FileNotFoundException e) {
73 Log.e(TAG,"BoardDateFile not found", e);
74 } catch (IOException e) {
75 Log.e(TAG, "BoardDateFile IOException", e);
76 }
77 finally {
78 return fileData;
79 }
80 }
81
82 public static String bytesToHex(byte[] bytes) {
83 final char[] hexArray = "0123456789ABCDEF".toCharArray();
84 char[] hexChars = new char[bytes.length * 2];
85 for ( int j = 0; j < bytes.length; j++ ) {
86 int v = bytes[j] & 0xFF;
87 hexChars[j * 2] = hexArray[v >>> 4];
88 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
89 }
90 return new String(hexChars);
91 }
92
93 private static long getTimeInMilliseconds(String date)
94 {
95 Calendar cal = Calendar.getInstance();
96
97 try {
98 cal.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
99 cal.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)));
100 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6, 8)));
101 cal.set(Calendar.HOUR_OF_DAY, 0);
102 cal.set(Calendar.MINUTE, 0);
103 cal.set(Calendar.SECOND, 0);
104 }
105 catch (NumberFormatException e) {
106 Log.e(TAG, "Parse Exception", e);
107 return 0;
108 }
109 return cal.getTimeInMillis();
110 }
111}
112