• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.retaildemo;
18 
19 import android.content.Context;
20 import android.util.Log;
21 import libcore.io.IoUtils;
22 
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 
30 class DataReaderWriter {
31     private static final String TAG = "DataReaderWriter";
32     private static final boolean DEBUG = false;
33 
34     private static final String FILE_NAME = "last_download_info.txt";
35 
setElapsedRealTime(Context context, long elapsedRealTime)36     public static void setElapsedRealTime(Context context, long elapsedRealTime) {
37         FileOutputStream fileOutputStream = null;
38         DataOutputStream out = null;
39         try {
40             final String filePath = context.getObbDir() + File.separator + FILE_NAME;
41             final File file = new File(filePath);
42             if (!file.exists()) {
43                 file.createNewFile();
44             }
45             fileOutputStream = new FileOutputStream(filePath);
46             out = new DataOutputStream(fileOutputStream);
47             if (DEBUG) Log.d(TAG, "Writing value elapsedRealTime=" + elapsedRealTime);
48             out.writeLong(elapsedRealTime);
49         } catch (IOException e) {
50             Log.e(TAG, "Error writing elapsedRealTime=" + elapsedRealTime + " to file", e);
51         } finally {
52             IoUtils.closeQuietly(out);
53             IoUtils.closeQuietly(fileOutputStream);
54         }
55     }
56 
getElapsedRealTime(Context context)57     public static long getElapsedRealTime(Context context) {
58         long elapsedRealTime = 0;
59         final String filePath = context.getObbDir() + File.separator + FILE_NAME;
60         if (!new File(filePath).exists()) {
61             return elapsedRealTime;
62         }
63         FileInputStream fileInputStream = null;
64         DataInputStream in = null;
65         try {
66             fileInputStream = new FileInputStream(filePath);
67             in = new DataInputStream(fileInputStream);
68             elapsedRealTime = in.readLong();
69             if (DEBUG) Log.d(TAG, "Read value elapsedRealTime=" + elapsedRealTime);
70         } catch (IOException e) {
71             Log.e(TAG, "Error reading elapsedRealTime value from file", e);
72         } finally {
73             IoUtils.closeQuietly(in);
74             IoUtils.closeQuietly(fileInputStream);
75         }
76         return elapsedRealTime;
77     }
78 }