Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +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; |
Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 10 | import java.text.SimpleDateFormat; |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 11 | import java.util.Date; |
Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 12 | import java.util.Locale; |
| 13 | import java.util.TimeZone; |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 14 | |
| 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 | |
| 31 | public 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 Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 40 | 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 Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 43 | |
| 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 Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 56 | Date boardDate; |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 57 | |
Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 58 | 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 Vogt | d0b55f2 | 2016-11-01 12:30:47 +0100 | [diff] [blame] | 66 | } catch (Exception e) { |
Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 67 | Log.e(TAG, "Unknown error while reading board date; using default", e); |
| 68 | boardDate = new Date(DEFAULT_BOARD_DATE_TIMESTAMP); |
Dirk Vogt | d0b55f2 | 2016-11-01 12:30:47 +0100 | [diff] [blame] | 69 | } |
Dirk Vogt | 142d778 | 2016-11-23 16:52:52 +0100 | [diff] [blame] | 70 | |
| 71 | return boardDate; |
Dirk Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 72 | } |
| 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 Vogt | 4b2005d | 2016-10-27 18:19:48 +0200 | [diff] [blame] | 103 | } |
| 104 | |