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 46 TestResultsBackupHelper(Context context) { 47 mContext = context; 48 } 49 50 @Override 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 int metricsIndex = cursor.getColumnIndex(TestResultsProvider.COLUMN_TEST_METRICS); 63 64 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); 65 DataOutputStream dataOutput = new DataOutputStream(byteOutput); 66 67 dataOutput.writeInt(cursor.getCount()); 68 while (cursor.moveToNext()) { 69 String name = cursor.getString(nameIndex); 70 int result = cursor.getInt(resultIndex); 71 int infoSeen = cursor.getInt(infoSeenIndex); 72 String details = cursor.getString(detailsIndex); 73 byte[] metricsData = cursor.getBlob(metricsIndex); 74 75 dataOutput.writeUTF(name); 76 dataOutput.writeInt(result); 77 dataOutput.writeInt(infoSeen); 78 dataOutput.writeUTF(details != null ? details : ""); 79 dataOutput.writeInt(metricsData.length); 80 if (metricsData.length > 0) { 81 dataOutput.write(metricsData); 82 } 83 } 84 85 byte[] rawBytes = byteOutput.toByteArray(); 86 data.writeEntityHeader(DB_BACKUP_KEY, rawBytes.length); 87 data.writeEntityData(rawBytes, rawBytes.length); 88 } catch (IOException e) { 89 Log.e(TAG, "Couldn't backup test results...", e); 90 failBackupTest(); 91 } finally { 92 if (cursor != null) { 93 cursor.close(); 94 } 95 } 96 } 97 98 @Override 99 public void restoreEntity(BackupDataInputStream data) { 100 try { 101 if (DB_BACKUP_KEY.equals(data.getKey())) { 102 byte[] rawBytes = new byte[data.size()]; 103 data.read(rawBytes, 0, data.size()); 104 105 ByteArrayInputStream byteInput = new ByteArrayInputStream(rawBytes); 106 DataInputStream dataInput = new DataInputStream(byteInput); 107 108 int numRows = dataInput.readInt(); 109 ContentValues[] values = new ContentValues[numRows]; 110 for (int i = 0; i < numRows; i++) { 111 String name = dataInput.readUTF(); 112 int result = dataInput.readInt(); 113 int infoSeen = dataInput.readInt(); 114 String details = dataInput.readUTF(); 115 int metricsDataSize = dataInput.readInt(); 116 117 values[i] = new ContentValues(); 118 values[i].put(TestResultsProvider.COLUMN_TEST_NAME, name); 119 values[i].put(TestResultsProvider.COLUMN_TEST_RESULT, result); 120 values[i].put(TestResultsProvider.COLUMN_TEST_INFO_SEEN, infoSeen); 121 values[i].put(TestResultsProvider.COLUMN_TEST_DETAILS, details); 122 123 if (metricsDataSize > 0) { 124 byte[] metrics = new byte[metricsDataSize]; 125 dataInput.readFully(metrics); 126 values[i].put(TestResultsProvider.COLUMN_TEST_METRICS, metrics); 127 } 128 } 129 130 ContentResolver resolver = mContext.getContentResolver(); 131 resolver.bulkInsert(TestResultsProvider.RESULTS_CONTENT_URI, values); 132 } else { 133 Log.e(TAG, "Skipping key: " + data.getKey()); 134 } 135 } catch (IOException e) { 136 Log.e(TAG, "Couldn't restore test results...", e); 137 failBackupTest(); 138 } 139 } 140 141 private void failBackupTest() { 142 TestResultsProvider.setTestResult(mContext, BackupTestActivity.class.getName(), 143 TestResult.TEST_RESULT_FAILED, null /*testDetails*/, null /*testMetrics*/); 144 } 145 146 @Override 147 public void writeNewStateDescription(ParcelFileDescriptor newState) { 148 } 149 } 150