1 /* 2 * Copyright (C) 2022 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.ondevicepersonalization.services.data.events; 18 19 import android.annotation.NonNull; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.database.sqlite.SQLiteException; 24 import android.util.Log; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 import com.android.ondevicepersonalization.services.data.OnDevicePersonalizationDbHelper; 28 29 /** 30 * Dao used to manage access to Events and Queries tables 31 */ 32 public class EventsDao { 33 private static final String TAG = "EventsDao"; 34 35 private static EventsDao sSingleton; 36 37 private final OnDevicePersonalizationDbHelper mDbHelper; 38 EventsDao(@onNull OnDevicePersonalizationDbHelper dbHelper)39 public EventsDao(@NonNull OnDevicePersonalizationDbHelper dbHelper) { 40 this.mDbHelper = dbHelper; 41 } 42 43 /** Returns an instance of the EventsDao given a context. */ getInstance(@onNull Context context)44 public static EventsDao getInstance(@NonNull Context context) { 45 synchronized (EventsDao.class) { 46 if (sSingleton == null) { 47 OnDevicePersonalizationDbHelper dbHelper = 48 OnDevicePersonalizationDbHelper.getInstance(context); 49 sSingleton = new EventsDao(dbHelper); 50 } 51 return sSingleton; 52 } 53 } 54 55 /** 56 * Returns an instance of the EventsDao given a context. This is used 57 * for testing only. 58 */ 59 @VisibleForTesting getInstanceForTest(@onNull Context context)60 public static EventsDao getInstanceForTest(@NonNull Context context) { 61 synchronized (EventsDao.class) { 62 if (sSingleton == null) { 63 OnDevicePersonalizationDbHelper dbHelper = 64 OnDevicePersonalizationDbHelper.getInstanceForTest(context); 65 sSingleton = new EventsDao(dbHelper); 66 } 67 return sSingleton; 68 } 69 } 70 71 /** 72 * Inserts the Event into the Events table. 73 * 74 * @return The row id of the newly inserted row if successful, -1 otherwise 75 */ insertEvent(@onNull Event event)76 public long insertEvent(@NonNull Event event) { 77 try { 78 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 79 ContentValues values = new ContentValues(); 80 values.put(EventsContract.EventsEntry.QUERY_ID, event.getQueryId()); 81 values.put(EventsContract.EventsEntry.SLOT_INDEX, event.getSlotIndex()); 82 values.put(EventsContract.EventsEntry.TIME_MILLIS, event.getTimeMillis()); 83 values.put(EventsContract.EventsEntry.SLOT_ID, event.getSlotId()); 84 values.put(EventsContract.EventsEntry.BID_ID, event.getBidId()); 85 values.put(EventsContract.EventsEntry.SERVICE_PACKAGE_NAME, 86 event.getServicePackageName()); 87 values.put(EventsContract.EventsEntry.SLOT_POSITION, event.getSlotPosition()); 88 values.put(EventsContract.EventsEntry.TYPE, event.getType()); 89 values.put(EventsContract.EventsEntry.EVENT_DATA, event.getEventData()); 90 return db.insert(EventsContract.EventsEntry.TABLE_NAME, null, 91 values); 92 } catch (SQLiteException e) { 93 Log.e(TAG, "Failed to insert event", e); 94 } 95 return -1; 96 } 97 98 /** 99 * Inserts the Query into the Queries table. 100 * 101 * @return The row id of the newly inserted row if successful, -1 otherwise 102 */ insertQuery(@onNull Query query)103 public long insertQuery(@NonNull Query query) { 104 try { 105 SQLiteDatabase db = mDbHelper.getWritableDatabase(); 106 ContentValues values = new ContentValues(); 107 values.put(QueriesContract.QueriesEntry.TIME_MILLIS, query.getTimeMillis()); 108 values.put(QueriesContract.QueriesEntry.SERVICE_PACKAGE_NAME, 109 query.getServicePackageName()); 110 values.put(QueriesContract.QueriesEntry.QUERY_DATA, query.getQueryData()); 111 return db.insert(QueriesContract.QueriesEntry.TABLE_NAME, null, 112 values); 113 } catch (SQLiteException e) { 114 Log.e(TAG, "Failed to insert query", e); 115 } 116 return -1; 117 } 118 } 119