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.content.ContentProvider;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.content.UriMatcher;
     23 import android.database.Cursor;
     24 import android.database.sqlite.SQLiteDatabase;
     25 import android.database.sqlite.SQLiteOpenHelper;
     26 import android.database.sqlite.SQLiteQueryBuilder;
     27 import android.net.Uri;
     28 
     29 /** {@link ContentProvider} that provides read and write access to the test results. */
     30 public class TestResultsProvider extends ContentProvider {
     31 
     32     private static final String RESULTS_PATH = "results";
     33 
     34     public static final String AUTHORITY = "com.android.cts.verifier.testresultsprovider";
     35     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
     36     public static final Uri RESULTS_CONTENT_URI =
     37             Uri.withAppendedPath(CONTENT_URI, RESULTS_PATH);
     38 
     39     public static Uri getTestNameUri(String testName) {
     40         return Uri.withAppendedPath(RESULTS_CONTENT_URI, testName);
     41     }
     42 
     43     public static final String _ID = "_id";
     44 
     45     /** String name of the test like "com.android.cts.verifier.foo.FooTestActivity" */
     46     public static final String COLUMN_TEST_NAME = "testname";
     47 
     48     /** Integer test result corresponding to constants in {@link TestResult}. */
     49     public static final String COLUMN_TEST_RESULT = "testresult";
     50 
     51     /** Boolean indicating whether the test info has been seen. */
     52     public static final String COLUMN_TEST_INFO_SEEN = "testinfoseen";
     53 
     54     public static final String[] ALL_COLUMNS = {
     55         _ID,
     56         COLUMN_TEST_NAME,
     57         COLUMN_TEST_RESULT,
     58         COLUMN_TEST_INFO_SEEN,
     59     };
     60 
     61     private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
     62     private static final int RESULTS_ALL = 1;
     63     private static final int RESULTS_ID = 2;
     64     private static final int RESULTS_TEST_NAME = 3;
     65     static {
     66         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH, RESULTS_ALL);
     67         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH + "/#", RESULTS_ID);
     68         URI_MATCHER.addURI(AUTHORITY, RESULTS_PATH + "/*", RESULTS_TEST_NAME);
     69     }
     70 
     71     private static final String TABLE_NAME = "results";
     72 
     73     private SQLiteOpenHelper mOpenHelper;
     74 
     75     @Override
     76     public boolean onCreate() {
     77         mOpenHelper = new TestResultsOpenHelper(getContext());
     78         return false;
     79     }
     80 
     81     private static class TestResultsOpenHelper extends SQLiteOpenHelper {
     82 
     83         private static final String DATABASE_NAME = "results.db";
     84 
     85         private static final int DATABASE_VERSION = 5;
     86 
     87         TestResultsOpenHelper(Context context) {
     88             super(context, DATABASE_NAME, null, DATABASE_VERSION);
     89         }
     90 
     91         @Override
     92         public void onCreate(SQLiteDatabase db) {
     93             db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
     94                     + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
     95                     + COLUMN_TEST_NAME + " TEXT, "
     96                     + COLUMN_TEST_RESULT + " INTEGER,"
     97                     + COLUMN_TEST_INFO_SEEN + " INTEGER DEFAULT 0);");
     98         }
     99 
    100         @Override
    101         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    102             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    103             onCreate(db);
    104         }
    105     }
    106 
    107     @Override
    108     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
    109             String sortOrder) {
    110         SQLiteQueryBuilder query = new SQLiteQueryBuilder();
    111         query.setTables(TABLE_NAME);
    112 
    113         int match = URI_MATCHER.match(uri);
    114         switch (match) {
    115             case RESULTS_ALL:
    116                 break;
    117 
    118             case RESULTS_ID:
    119                 query.appendWhere(_ID);
    120                 query.appendWhere("=");
    121                 query.appendWhere(uri.getPathSegments().get(1));
    122                 break;
    123 
    124             case RESULTS_TEST_NAME:
    125                 query.appendWhere(COLUMN_TEST_NAME);
    126                 query.appendWhere("=");
    127                 query.appendWhere("\"" + uri.getPathSegments().get(1) + "\"");
    128                 break;
    129 
    130             default:
    131                 throw new IllegalArgumentException("Unknown URI: " + uri);
    132         }
    133 
    134         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    135         return query.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    136     }
    137 
    138     @Override
    139     public Uri insert(Uri uri, ContentValues values) {
    140         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    141         long id = db.insert(TABLE_NAME, null, values);
    142         getContext().getContentResolver().notifyChange(uri, null);
    143         return Uri.withAppendedPath(RESULTS_CONTENT_URI, "" + id);
    144 
    145     }
    146 
    147     @Override
    148     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    149         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    150 
    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         int numUpdated = db.update(TABLE_NAME, values, selection, selectionArgs);
    180         if (numUpdated > 0) {
    181             getContext().getContentResolver().notifyChange(uri, null);
    182         }
    183         return numUpdated;
    184     }
    185 
    186     @Override
    187     public int delete(Uri uri, String selection, String[] selectionArgs) {
    188         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    189         int numDeleted = db.delete(TABLE_NAME, selection, selectionArgs);
    190         if (numDeleted > 0) {
    191             getContext().getContentResolver().notifyChange(uri, null);
    192         }
    193         return numDeleted;
    194     }
    195 
    196     @Override
    197     public String getType(Uri uri) {
    198         return null;
    199     }
    200 }
    201