Cache function add basic cache structure

Test: unit test test save and retrieve info it works correctly
Bug: 202335820
Change-Id: Ic4a316bf265b89a8bc134335f2d06ef117cf0c80
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairController.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairController.java
index 85f9ee4..f39849c 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/FastPairController.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairController.java
@@ -116,10 +116,8 @@
                             item = mFastPairCacheManager.getDiscoveryItem(itemId);
                         } else if (discoveryItem != null) {
                             try {
-                                item =
-                                        new DiscoveryItem(
-                                                mContext,
-                                                Cache.StoredDiscoveryItem.parseFrom(discoveryItem));
+                                item = new DiscoveryItem(mContext,
+                                        Cache.StoredDiscoveryItem.parseFrom(discoveryItem));
                             } catch (InvalidProtocolBufferException e) {
                                 Log.w("FastPairController",
                                         "Error parsing serialized discovery item with size "
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
index d406678..ff4bce2 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
@@ -75,9 +75,6 @@
         public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                 Log.d("FastPairService", " screen on");
-                // STOPSHIP(b/202335820): Remove this logic in prod.
-                Locator.getFromContextWrapper(mLocatorContextWrapper, FastPairCacheManager.class)
-                        .printLog();
             } else {
                 Log.d("FastPairService", " screen off");
             }
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemContract.java b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemContract.java
new file mode 100644
index 0000000..61ca3fd
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemContract.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.fastpair.cache;
+
+import android.provider.BaseColumns;
+
+/**
+ * Defines DiscoveryItem database schema.
+ */
+public class DiscoveryItemContract {
+    private DiscoveryItemContract() {}
+
+    /**
+     * Discovery item entry related info.
+     */
+    public static class DiscoveryItemEntry implements BaseColumns {
+        public static final String TABLE_NAME = "SCAN_RESULT";
+        public static final String COLUMN_MODEL_ID = "MODEL_ID";
+        public static final String COLUMN_SCAN_BYTE = "SCAN_RESULT_BYTE";
+    }
+}
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemDbHelper.java b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemDbHelper.java
new file mode 100644
index 0000000..e171462
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/fastpair/cache/DiscoveryItemDbHelper.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.fastpair.cache;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+/**
+ * Discovery db helper
+ */
+public class DiscoveryItemDbHelper extends SQLiteOpenHelper {
+
+    public static final int DATABASE_VERSION = 1;
+    public static final String DATABASE_NAME = "ScanResult.db";
+    private static final String SQL_CREATE_DB =
+            "CREATE TABLE " + DiscoveryItemContract.DiscoveryItemEntry.TABLE_NAME
+                    + " (" + DiscoveryItemContract.DiscoveryItemEntry._ID
+                    + "INTEGER PRIMARY KEY,"
+                    + DiscoveryItemContract.DiscoveryItemEntry.COLUMN_MODEL_ID
+                    + " TEXT," + DiscoveryItemContract.DiscoveryItemEntry.COLUMN_SCAN_BYTE
+                    + " BLOB)";
+    private static final String SQL_DELETE_DB =
+            "DROP TABLE IF EXISTS " + DiscoveryItemContract.DiscoveryItemEntry.TABLE_NAME;
+
+    public DiscoveryItemDbHelper(Context context) {
+        super(context, DATABASE_NAME, null, DATABASE_VERSION);
+    }
+
+    @Override
+    public void onCreate(SQLiteDatabase db) {
+        db.execSQL(SQL_CREATE_DB);
+    }
+
+    @Override
+    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+        // Since the outdated data has no value so just remove the data.
+        db.execSQL(SQL_DELETE_DB);
+        onCreate(db);
+    }
+
+    @Override
+    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+        super.onDowngrade(db, oldVersion, newVersion);
+    }
+}
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java b/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java
index 0c3461d..1b35913 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java
@@ -17,12 +17,16 @@
 package com.android.server.nearby.fastpair.cache;
 
 import android.bluetooth.le.ScanResult;
+import android.content.ContentValues;
 import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
 
-
 import com.android.server.nearby.common.eventloop.Annotations;
 
+import com.google.protobuf.InvalidProtocolBufferException;
+
 import service.proto.Cache;
 import service.proto.Rpcs;
 
@@ -32,15 +36,25 @@
  */
 public class FastPairCacheManager {
     private final Context mContext;
+    private final DiscoveryItemDbHelper mDiscoveryItemDbHelper;
 
     public FastPairCacheManager(Context context) {
         mContext = context;
+        mDiscoveryItemDbHelper = new DiscoveryItemDbHelper(context);
+    }
+
+    /**
+     * Clean up function to release db
+     */
+    public void cleanUp() {
+        mDiscoveryItemDbHelper.close();
     }
 
     /**
      * Saves the response to the db
      */
-    private void saveDevice() {}
+    private void saveDevice() {
+    }
 
     Cache.ServerResponseDbItem getDeviceFromScanResult(ScanResult scanResult) {
         return Cache.ServerResponseDbItem.newBuilder().build();
@@ -60,9 +74,16 @@
      * Save discovery item into database.
      */
     public boolean saveDiscoveryItem(DiscoveryItem item) {
+        SQLiteDatabase db = mDiscoveryItemDbHelper.getWritableDatabase();
+        ContentValues values = new ContentValues();
+        values.put(DiscoveryItemContract.DiscoveryItemEntry.COLUMN_MODEL_ID, item.getTriggerId());
+        values.put(DiscoveryItemContract.DiscoveryItemEntry.COLUMN_SCAN_BYTE,
+                item.getCopyOfStoredItem().toByteArray());
+        db.insert(DiscoveryItemContract.DiscoveryItemEntry.TABLE_NAME, null, values);
         return true;
     }
 
+
     @Annotations.EventThread
     private Rpcs.GetObservedDeviceResponse getObservedDeviceInfo(ScanResult scanResult) {
         return Rpcs.GetObservedDeviceResponse.getDefaultInstance();
@@ -72,7 +93,43 @@
      * Get discovery item from item id.
      */
     public DiscoveryItem getDiscoveryItem(String itemId) {
-        return new DiscoveryItem(mContext, Cache.StoredDiscoveryItem.getDefaultInstance());
+        return new DiscoveryItem(mContext, getStoredDiscoveryItem(itemId));
+    }
+
+    /**
+     * Get discovery item from item id.
+     */
+    public Cache.StoredDiscoveryItem getStoredDiscoveryItem(String itemId) {
+
+        SQLiteDatabase db = mDiscoveryItemDbHelper.getReadableDatabase();
+        String[] projection = {
+                DiscoveryItemContract.DiscoveryItemEntry.COLUMN_MODEL_ID,
+                DiscoveryItemContract.DiscoveryItemEntry.COLUMN_SCAN_BYTE
+        };
+        String selection = DiscoveryItemContract.DiscoveryItemEntry.COLUMN_MODEL_ID + " =? ";
+        String[] selectionArgs = {itemId};
+        Cursor cursor = db.query(
+                DiscoveryItemContract.DiscoveryItemEntry.TABLE_NAME,
+                projection,
+                selection,
+                selectionArgs,
+                null,
+                null,
+                null
+        );
+
+        if (cursor.moveToNext()) {
+            byte[] res = cursor.getBlob(cursor.getColumnIndexOrThrow(
+                    DiscoveryItemContract.DiscoveryItemEntry.COLUMN_SCAN_BYTE));
+            try {
+                Cache.StoredDiscoveryItem item = Cache.StoredDiscoveryItem.parseFrom(res);
+                return item;
+            } catch (InvalidProtocolBufferException e) {
+                Log.e("FastPairCacheManager", "storediscovery has error");
+            }
+        }
+        cursor.close();
+        return Cache.StoredDiscoveryItem.getDefaultInstance();
     }
 
     /**
@@ -82,11 +139,4 @@
         return Cache.StoredScanResult.getDefaultInstance();
     }
 
-    /**
-     * Test function to verify FastPairCacheManager setup.
-     */
-    public void printLog() {
-        Log.d("FastPairCacheManager", "print log");
-    }
-
 }
diff --git a/nearby/tests/Android.bp b/nearby/tests/Android.bp
index f74228a..fc0a700 100644
--- a/nearby/tests/Android.bp
+++ b/nearby/tests/Android.bp
@@ -34,6 +34,11 @@
 
     static_libs: [
         "androidx.test.rules",
+        "compatibility-device-util-axt",
+        "service-appsearch",
+        "truth-prebuilt",
+        "androidx.test.ext.junit",
+        "junit",
         "framework-nearby-pre-jarjar",
         "guava",
         "robolectric_android-all-stub",
diff --git a/nearby/tests/src/com/android/server/nearby/fastpair/cache/FastPairCacheManagerTest.java b/nearby/tests/src/com/android/server/nearby/fastpair/cache/FastPairCacheManagerTest.java
new file mode 100644
index 0000000..adbb12a
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/fastpair/cache/FastPairCacheManagerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.fastpair.cache;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import service.proto.Cache;
+
+public class FastPairCacheManagerTest {
+
+    private static final String MODEL_ID = "001";
+    private static final String APP_NAME = "APP_NAME";
+    @Mock
+    DiscoveryItem mDiscoveryItem;
+    Cache.StoredDiscoveryItem mStoredDiscoveryItem = Cache.StoredDiscoveryItem.newBuilder()
+            .setTriggerId(MODEL_ID)
+            .setAppName(APP_NAME).build();
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void notSaveRetrieveInfo() {
+        Context mContext = ApplicationProvider.getApplicationContext();
+        when(mDiscoveryItem.getCopyOfStoredItem()).thenReturn(mStoredDiscoveryItem);
+        when(mDiscoveryItem.getTriggerId()).thenReturn(MODEL_ID);
+
+        FastPairCacheManager fastPairCacheManager = new FastPairCacheManager(mContext);
+
+        assertThat(fastPairCacheManager.getStoredDiscoveryItem(MODEL_ID).getAppName())
+                .isNotEqualTo(APP_NAME);
+    }
+
+    @Test
+    public void saveRetrieveInfo() {
+        Context mContext = ApplicationProvider.getApplicationContext();
+        when(mDiscoveryItem.getCopyOfStoredItem()).thenReturn(mStoredDiscoveryItem);
+        when(mDiscoveryItem.getTriggerId()).thenReturn(MODEL_ID);
+
+        FastPairCacheManager fastPairCacheManager = new FastPairCacheManager(mContext);
+        fastPairCacheManager.saveDiscoveryItem(mDiscoveryItem);
+        assertThat(fastPairCacheManager.getStoredDiscoveryItem(MODEL_ID).getAppName())
+                .isEqualTo(APP_NAME);
+    }
+}