Home | History | Annotate | Download | only in verifier
      1 /*
      2  * Copyright (C) 2010 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 android.app.backup.BackupManager;
     20 import android.content.ContentProvider;
     21 import android.content.ContentResolver;
     22 import android.content.ContentValues;
     23 import android.content.Context;
     24 import android.content.UriMatcher;
     25 import android.database.Cursor;
     26 import android.database.sqlite.SQLiteDatabase;
     27 import android.database.sqlite.SQLiteOpenHelper;
     28 import android.database.sqlite.SQLiteQueryBuilder;
     29 import android.net.Uri;
     30 
     31 /** {@link ContentProvider} that provides read and write access to the test results. */
     32 public class TestResultsProvider extends ContentProvider {
     33 
     34     private static final String RESULTS_PATH = "results";
     35 
     36     public static final String AUTHORITY = "com.android.cts.verifier.testresultsprovider";
     37     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
     38     public static final Uri RESULTS_CONTENT_URI =
     39             Uri.withAppendedPath(CONTENT_URI, RESULTS_PATH);
     40 
     41     public static Uri getTestNameUri(String testName) {
     42         return Uri.withAppendedPath(RESULTS_CONTENT_URI, testName);
     43     }
     44 
     45     static final String _ID = "_id";
     46 
     47     /** String name of the test like "com.android.cts.verifier.foo.FooTestActivity" */
     48     static final String COLUMN_TEST_NAME = "testname";
     49 
     50     /** Integer test result corresponding to constants in {@link TestResult}. */
     51     static final String COLUMN_TEST_RESULT = "testresult";
     52 
     53     /** Boolean indicating whether the test info has been seen. */
     54     static final String COLUMN_TEST_INFO_SEEN = "testinfoseen";
     55 
     56     /** String containing the test's details. */
     57     static final String COLUMN_TEST_DETAILS = "testdetails";
     58 
     59     private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
     60     private static final int RESULTS_ALL = 1;
     61     private static final int RESULTS_ID = 2;
     62     private static final int RESULTS_TEST_NAME = 3;
     63     static {
     64         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH, RESULTS_ALL);
     65         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH + "/#", RESULTS_ID);
     66         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH + "/*", RESULTS_TEST_NAME);
     67     }
     68 
     69     private static final String TABLE_NAME = "results";
     70 
     71     private SQLiteOpenHelper mOpenHelper;
     72 
     73     private BackupManager mBackupManager;
     74 
     75     @Override
     76     public boolean onCreate() {
     77         mOpenHelper = new TestResultsOpenHelper(getContext());
     78         mBackupManager = new BackupManager(getContext());
     79         return false;
     80     }
     81 
     82     private static class TestResultsOpenHelper extends SQLiteOpenHelper {
     83 
     84         private static final String DATABASE_NAME = "results.db";
     85 
     86         private static final int DATABASE_VERSION = 6;
     87 
     88         TestResultsOpenHelper(Context context) {
     89             super(context, DATABASE_NAME, null, DATABASE_VERSION);
     90         }
     91 
     92         @Override
     93         public void onCreate(SQLiteDatabase db) {
     94             db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
     95                     + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
     96                     + COLUMN_TEST_NAME + " TEXT, "
     97                     + COLUMN_TEST_RESULT + " INTEGER,"
     98                     + COLUMN_TEST_INFO_SEEN + " INTEGER DEFAULT 0,"
     99                     + COLUMN_TEST_DETAILS + " TEXT);");
    100         }
    101 
    102         @Override
    103         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    104             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    105             onCreate(db);
    106         }
    107     }
    108 
    109     @Override
    110     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
    111             String sortOrder) {
    112         SQLiteQueryBuilder query = new SQLiteQueryBuilder();
    113         query.setTables(TABLE_NAME);
    114 
    115         int match = URI_MATCHER.match(uri);
    116         switch (match) {
    117             case RESULTS_ALL:
    118                 break;
    119 
    120             case RESULTS_ID:
    121                 query.appendWhere(_ID);
    122                 query.appendWhere("=");
    123                 query.appendWhere(uri.getPathSegments().get(1));
    124                 break;
    125 
    126             case RESULTS_TEST_NAME:
    127                 query.appendWhere(COLUMN_TEST_NAME);
    128                 query.appendWhere("=");
    129                 query.appendWhere("\"" + uri.getPathSegments().get(1) + "\"");
    130                 break;
    131 
    132             default:
    133                 throw new IllegalArgumentException("Unknown URI: " + uri);
    134         }
    135 
    136         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    137         return query.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    138     }
    139 
    140     @Override
    141     public Uri insert(Uri uri, ContentValues values) {
    142         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    143         long id = db.insert(TABLE_NAME, null, values);
    144         getContext().getContentResolver().notifyChange(uri, null);
    145         mBackupManager.dataChanged();
    146         return Uri.withAppendedPath(RESULTS_CONTENT_URI, "" + id);
    147     }
    148 
    149     @Override
    150     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    151         int match = URI_MATCHER.match(uri);
    152         switch (match) {
    153             case RESULTS_ALL:
    154                 break;
    155 
    156             case RESULTS_ID:
    157                 String idSelection = _ID + "=" + uri.getPathSegments().get(1);
    158                 if (selection != null && selection.length() > 0) {
    159                     selection = idSelection + " AND " + selection;
    160                 } else {
    161                     selection = idSelection;
    162                 }
    163                 break;
    164 
    165             case RESULTS_TEST_NAME:
    166                 String testNameSelection = COLUMN_TEST_NAME + "=\""
    167                         + uri.getPathSegments().get(1) + "\"";
    168                 if (selection != null && selection.length() > 0) {
    169                     selection = testNameSelection + " AND " + selection;
    170                 } else {
    171                     selection = testNameSelection;
    172                 }
    173                 break;
    174 
    175             default:
    176                 throw new IllegalArgumentException("Unknown URI: " + uri);
    177         }
    178 
    179         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    180         int numUpdated = db.update(TABLE_NAME, values, selection, selectionArgs);
    181         if (numUpdated > 0) {
    182             getContext().getContentResolver().notifyChange(uri, null);
    183             mBackupManager.dataChanged();
    184         }
    185         return numUpdated;
    186     }
    187 
    188     @Override
    189     public int delete(Uri uri, String selection, String[] selectionArgs) {
    190         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    191         int numDeleted = db.delete(TABLE_NAME, selection, selectionArgs);
    192         if (numDeleted > 0) {
    193             getContext().getContentResolver().notifyChange(uri, null);
    194             mBackupManager.dataChanged();
    195         }
    196         return numDeleted;
    197     }
    198 
    199     @Override
    200     public String getType(Uri uri) {
    201         return null;
    202     }
    203 
    204     static void setTestResult(Context context, String testName, int testResult,
    205             String testDetails) {
    206         ContentValues values = new ContentValues(2);
    207         values.put(TestResultsProvider.COLUMN_TEST_RESULT, testResult);
    208         values.put(TestResultsProvider.COLUMN_TEST_NAME, testName);
    209         values.put(TestResultsProvider.COLUMN_TEST_DETAILS, testDetails);
    210 
    211         ContentResolver resolver = context.getContentResolver();
    212         int numUpdated = resolver.update(TestResultsProvider.RESULTS_CONTENT_URI, values,
    213                 TestResultsProvider.COLUMN_TEST_NAME + " = ?",
    214                 new String[] {testName});
    215 
    216         if (numUpdated == 0) {
    217             resolver.insert(TestResultsProvider.RESULTS_CONTENT_URI, values);
    218         }
    219     }
    220 
    221 }
    222