1 /* 2 * Copyright (C) 2011 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.cts.verifier; 18 19 import com.android.cts.verifier.backup.BackupTestActivity; 20 21 import android.app.backup.BackupDataInputStream; 22 import android.app.backup.BackupDataOutput; 23 import android.app.backup.BackupHelper; 24 import android.content.ContentResolver; 25 import android.content.ContentValues; 26 import android.content.Context; 27 import android.database.Cursor; 28 import android.os.ParcelFileDescriptor; 29 import android.util.Log; 30 31 import java.io.ByteArrayInputStream; 32 import java.io.ByteArrayOutputStream; 33 import java.io.DataInputStream; 34 import java.io.DataOutputStream; 35 import java.io.IOException; 36 37 /** {@link BackupHelper} for the test results database. */ 38 class TestResultsBackupHelper implements BackupHelper { 39 40 private static final String TAG = TestResultsBackupHelper.class.getSimpleName(); 41 42 private static final String DB_BACKUP_KEY = "db"; 43 44 private final Context mContext; 45 TestResultsBackupHelper(Context context)46 TestResultsBackupHelper(Context context) { 47 mContext = context; 48 } 49 50 @Override performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)51 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 52 ParcelFileDescriptor newState) { 53 ContentResolver resolver = mContext.getContentResolver(); 54 Cursor cursor = null; 55 try { 56 cursor = resolver.query(TestResultsProvider.RESULTS_CONTENT_URI, 57 null, null, null, null); 58 int nameIndex = cursor.getColumnIndex(TestResultsProvider.COLUMN_TEST_NAME); 59 int resultIndex = cursor.getColumnIndex(TestResultsProvider.COLUMN_TEST_RESULT); 60 int infoSeenIndex = cursor.getColumnIndex(TestResultsProvider.COLUMN_TEST_INFO_SEEN); 61 int detailsIndex = cursor.getColumnIndex(TestResultsProvider.COLUMN_TEST_DETAILS); 62 63 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); 64 DataOutputStream dataOutput = new DataOutputStream(byteOutput); 65 66 dataOutput.writeInt(cursor.getCount()); 67 while (cursor.moveToNext()) { 68 String name = cursor.getString(nameIndex); 69 int result = cursor.getInt(resultIndex); 70 int infoSeen = cursor.getInt(infoSeenIndex); 71 String details = cursor.getString(detailsIndex); 72 73 dataOutput.writeUTF(name); 74 dataOutput.writeInt(result); 75 dataOutput.writeInt(infoSeen); 76 dataOutput.writeUTF(details != null ? details : ""); 77 } 78 79 byte[] rawBytes = byteOutput.toByteArray(); 80 data.writeEntityHeader(DB_BACKUP_KEY, rawBytes.length); 81 data.writeEntityData(rawBytes, rawBytes.length); 82 } catch (IOException e) { 83 Log.e(TAG, "Couldn't backup test results...", e); 84 failBackupTest(); 85 } finally { 86 if (cursor != null) { 87 cursor.close(); 88 } 89 } 90 } 91 92 @Override restoreEntity(BackupDataInputStream data)93 public void restoreEntity(BackupDataInputStream data) { 94 try { 95 if (DB_BACKUP_KEY.equals(data.getKey())) { 96 byte[] rawBytes = new byte[data.size()]; 97 data.read(rawBytes, 0, data.size()); 98 99 ByteArrayInputStream byteInput = new ByteArrayInputStream(rawBytes); 100 DataInputStream dataInput = new DataInputStream(byteInput); 101 102 int numRows = dataInput.readInt(); 103 ContentValues[] values = new ContentValues[numRows]; 104 for (int i = 0; i < numRows; i++) { 105 String name = dataInput.readUTF(); 106 int result = dataInput.readInt(); 107 int infoSeen = dataInput.readInt(); 108 String details = dataInput.readUTF(); 109 110 values[i] = new ContentValues(); 111 values[i].put(TestResultsProvider.COLUMN_TEST_NAME, name); 112 values[i].put(TestResultsProvider.COLUMN_TEST_RESULT, result); 113 values[i].put(TestResultsProvider.COLUMN_TEST_INFO_SEEN, infoSeen); 114 values[i].put(TestResultsProvider.COLUMN_TEST_DETAILS, details); 115 } 116 117 ContentResolver resolver = mContext.getContentResolver(); 118 resolver.bulkInsert(TestResultsProvider.RESULTS_CONTENT_URI, values); 119 } else { 120 Log.e(TAG, "Skipping key: " + data.getKey()); 121 } 122 } catch (IOException e) { 123 Log.e(TAG, "Couldn't restore test results...", e); 124 failBackupTest(); 125 } 126 } 127 failBackupTest()128 private void failBackupTest() { 129 TestResultsProvider.setTestResult(mContext, BackupTestActivity.class.getName(), 130 TestResult.TEST_RESULT_FAILED, null); 131 } 132 133 @Override writeNewStateDescription(ParcelFileDescriptor newState)134 public void writeNewStateDescription(ParcelFileDescriptor newState) { 135 } 136 } 137