Dirk Vogt | d0b55f2 | 2016-11-01 12:30:47 +0100 | [diff] [blame^] | 1 | package com.fairphone.hiccup.app; |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 2 | |
| 3 | import android.util.Log; |
| 4 | |
| 5 | import java.io.DataInputStream; |
| 6 | import java.io.File; |
| 7 | import java.io.FileInputStream; |
| 8 | import java.io.FileNotFoundException; |
| 9 | import java.io.IOException; |
| 10 | import java.util.Calendar; |
| 11 | import 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 | |
| 29 | public 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 Vogt | d0b55f2 | 2016-11-01 12:30:47 +0100 | [diff] [blame^] | 51 | long startTime = 0; |
| 52 | try { |
| 53 | byte[] boardFile = getBoardDateFileInByteArray(); |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 54 | |
Dirk Vogt | d0b55f2 | 2016-11-01 12:30:47 +0100 | [diff] [blame^] | 55 | 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 Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 60 | 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 | |