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.support.annotation.VisibleForTesting; 21 import android.util.Log; 22 import libcore.io.IoUtils; 23 24 import java.io.DataInputStream; 25 import java.io.DataOutputStream; 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileOutputStream; 29 import java.io.IOException; 30 31 class DataReaderWriter { 32 private static final String TAG = "DataReaderWriter"; 33 private static final boolean DEBUG = false; 34 35 private static final String FILE_NAME = "last_download_info.txt"; 36 writeLastBootCount(Context context, int bootCount)37 public static void writeLastBootCount(Context context, int bootCount) { 38 FileOutputStream fileOutputStream = null; 39 DataOutputStream out = null; 40 try { 41 final String filePath = getFilePath(context); 42 final File file = new File(filePath); 43 if (!file.exists()) { 44 file.createNewFile(); 45 } 46 fileOutputStream = new FileOutputStream(filePath); 47 out = new DataOutputStream(fileOutputStream); 48 if (DEBUG) Log.d(TAG, "Writing value bootCount=" + bootCount); 49 out.writeInt(bootCount); 50 } catch (IOException e) { 51 Log.e(TAG, "Error writing bootCount=" + bootCount + " to file", e); 52 } finally { 53 IoUtils.closeQuietly(out); 54 IoUtils.closeQuietly(fileOutputStream); 55 } 56 } 57 58 /** 59 * @return 0 if reading the value for the first time, 60 * -1 if there is an error reading the saved count, 61 * last saved boot count otherwise. 62 */ readLastBootCount(Context context)63 public static int readLastBootCount(Context context) { 64 int bootCount = 0; 65 final String filePath = getFilePath(context); 66 if (!new File(filePath).exists()) { 67 return bootCount; 68 } 69 FileInputStream fileInputStream = null; 70 DataInputStream in = null; 71 try { 72 fileInputStream = new FileInputStream(filePath); 73 in = new DataInputStream(fileInputStream); 74 bootCount = in.readInt(); 75 if (DEBUG) Log.d(TAG, "Read value bootCount=" + bootCount); 76 } catch (IOException e) { 77 Log.e(TAG, "Error reading bootCount value from file", e); 78 return -1; 79 } finally { 80 IoUtils.closeQuietly(in); 81 IoUtils.closeQuietly(fileInputStream); 82 } 83 return bootCount; 84 } 85 86 @VisibleForTesting getFilePath(Context context)87 static String getFilePath(Context context) { 88 return context.getObbDir() + File.separator + FILE_NAME; 89 } 90 }