1 /*
2  * Copyright (C) 2018 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.settings.homepage.contextualcards;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.database.sqlite.SQLiteOpenHelper;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 /**
27  * Defines the schema for the Homepage Cards database.
28  */
29 public class CardDatabaseHelper extends SQLiteOpenHelper {
30     private static final String TAG = "CardDatabaseHelper";
31     private static final String DATABASE_NAME = "homepage_cards.db";
32     private static final int DATABASE_VERSION = 7;
33 
34     public static final String CARD_TABLE = "cards";
35 
36     public interface CardColumns {
37         /**
38          * Primary key. Name of the card.
39          */
40         String NAME = "name";
41 
42         /**
43          * Type of the card.
44          */
45         String TYPE = "type";
46 
47         /**
48          * Score of the card. Higher numbers have higher priorities.
49          */
50         String SCORE = "score";
51 
52         /**
53          * URI of the slice card.
54          */
55         String SLICE_URI = "slice_uri";
56 
57         /**
58          * Category of the card.
59          */
60         String CATEGORY = "category";
61 
62         /**
63          * Package name for all card candidates.
64          */
65         String PACKAGE_NAME = "package_name";
66 
67         /**
68          * Application version of the package.
69          */
70         String APP_VERSION = "app_version";
71 
72         /**
73          * Timestamp of card being dismissed.
74          */
75         String DISMISSED_TIMESTAMP = "dismissed_timestamp";
76     }
77 
78     private static final String CREATE_CARD_TABLE =
79             "CREATE TABLE "
80                     + CARD_TABLE
81                     + "("
82                     + CardColumns.NAME
83                     + " TEXT NOT NULL PRIMARY KEY, "
84                     + CardColumns.TYPE
85                     + " INTEGER NOT NULL, "
86                     + CardColumns.SCORE
87                     + " DOUBLE NOT NULL, "
88                     + CardColumns.SLICE_URI
89                     + " TEXT, "
90                     + CardColumns.CATEGORY
91                     + " INTEGER DEFAULT 0, "
92                     + CardColumns.PACKAGE_NAME
93                     + " TEXT NOT NULL, "
94                     + CardColumns.APP_VERSION
95                     + " INTEGER NOT NULL, "
96                     + CardColumns.DISMISSED_TIMESTAMP
97                     + " INTEGER"
98                     + ");";
99 
CardDatabaseHelper(Context context)100     public CardDatabaseHelper(Context context) {
101         super(context, DATABASE_NAME, null, DATABASE_VERSION);
102     }
103 
104     @Override
onCreate(SQLiteDatabase db)105     public void onCreate(SQLiteDatabase db) {
106         db.execSQL(CREATE_CARD_TABLE);
107     }
108 
109     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)110     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
111         if (oldVersion < newVersion) {
112             Log.d(TAG, "Reconstructing DB from " + oldVersion + " to " + newVersion);
113             db.execSQL("DROP TABLE IF EXISTS " + CARD_TABLE);
114             onCreate(db);
115         }
116     }
117 
118     @VisibleForTesting
119     static CardDatabaseHelper sCardDatabaseHelper;
120 
getInstance(Context context)121     public static synchronized CardDatabaseHelper getInstance(Context context) {
122         if (sCardDatabaseHelper == null) {
123             sCardDatabaseHelper = new CardDatabaseHelper(context.getApplicationContext());
124         }
125         return sCardDatabaseHelper;
126     }
127 }
128