Home | History | Annotate | Download | only in db
      1 /*
      2  * Copyright (C) 2017 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.example.android.autofill.service.data.source.local.db;
     18 
     19 import android.arch.persistence.db.SupportSQLiteDatabase;
     20 import android.arch.persistence.room.Database;
     21 import android.arch.persistence.room.Room;
     22 import android.arch.persistence.room.RoomDatabase;
     23 import android.arch.persistence.room.TypeConverters;
     24 import android.content.Context;
     25 import android.support.annotation.NonNull;
     26 
     27 import com.example.android.autofill.service.data.source.DefaultFieldTypesSource;
     28 import com.example.android.autofill.service.data.source.local.dao.AutofillDao;
     29 import com.example.android.autofill.service.model.AutofillDataset;
     30 import com.example.android.autofill.service.model.AutofillHint;
     31 import com.example.android.autofill.service.model.DefaultFieldTypeWithHints;
     32 import com.example.android.autofill.service.model.FakeData;
     33 import com.example.android.autofill.service.model.FieldType;
     34 import com.example.android.autofill.service.model.FilledAutofillField;
     35 import com.example.android.autofill.service.model.ResourceIdHeuristic;
     36 import com.example.android.autofill.service.util.AppExecutors;
     37 
     38 import java.util.ArrayList;
     39 import java.util.List;
     40 
     41 import static com.example.android.autofill.service.data.source.local.db.Converters.IntList;
     42 import static java.util.stream.Collectors.toList;
     43 
     44 @Database(entities = {
     45         FilledAutofillField.class,
     46         AutofillDataset.class,
     47         FieldType.class,
     48         AutofillHint.class,
     49         ResourceIdHeuristic.class
     50 }, version = 1)
     51 @TypeConverters({Converters.class})
     52 public abstract class AutofillDatabase extends RoomDatabase {
     53 
     54     private static final Object sLock = new Object();
     55     private static AutofillDatabase sInstance;
     56 
     57     public static AutofillDatabase getInstance(Context context,
     58             DefaultFieldTypesSource defaultFieldTypesSource,
     59             AppExecutors appExecutors) {
     60         if (sInstance == null) {
     61             synchronized (sLock) {
     62                 if (sInstance == null) {
     63                     sInstance = Room.databaseBuilder(context.getApplicationContext(),
     64                             AutofillDatabase.class, "AutofillSample.db")
     65                             .addCallback(new RoomDatabase.Callback() {
     66                                 @Override
     67                                 public void onCreate(@NonNull SupportSQLiteDatabase db) {
     68                                     appExecutors.diskIO().execute(() -> {
     69                                         List<DefaultFieldTypeWithHints> fieldTypes =
     70                                                 defaultFieldTypesSource.getDefaultFieldTypes();
     71                                         AutofillDatabase autofillDatabase =
     72                                                 getInstance(context, defaultFieldTypesSource,
     73                                                         appExecutors);
     74                                         autofillDatabase.saveDefaultFieldTypes(fieldTypes);
     75                                     });
     76                                 }
     77 
     78                                 @Override
     79                                 public void onOpen(@NonNull SupportSQLiteDatabase db) {
     80                                     super.onOpen(db);
     81                                 }
     82                             })
     83                             .build();
     84                 }
     85             }
     86         }
     87         return sInstance;
     88     }
     89 
     90     private void saveDefaultFieldTypes(List<DefaultFieldTypeWithHints> defaultFieldTypes) {
     91         List<FieldType> storedFieldTypes = new ArrayList<>();
     92         List<AutofillHint> storedAutofillHints = new ArrayList<>();
     93         for (DefaultFieldTypeWithHints defaultType : defaultFieldTypes) {
     94             DefaultFieldTypeWithHints.DefaultFieldType defaultFieldType = defaultType.fieldType;
     95             List<String> autofillHints = defaultType.autofillHints;
     96             IntList autofillTypes = new IntList(defaultFieldType.autofillTypes);
     97             DefaultFieldTypeWithHints.DefaultFakeData defaultFakeData = defaultType.fieldType.fakeData;
     98             FakeData fakeData = new FakeData(new Converters.StringList(
     99                     defaultFakeData.strictExampleSet), defaultFakeData.textTemplate,
    100                     defaultFakeData.dateTemplate);
    101             FieldType storedFieldType = new FieldType(defaultFieldType.typeName, autofillTypes,
    102                     defaultFieldType.saveInfo, defaultFieldType.partition, fakeData);
    103             storedFieldTypes.add(storedFieldType);
    104             storedAutofillHints.addAll(autofillHints.stream()
    105                     .map((autofillHint) -> new AutofillHint(autofillHint,
    106                             storedFieldType.getTypeName())).collect(toList()));
    107         }
    108         AutofillDao autofillDao = autofillDao();
    109         autofillDao.insertFieldTypes(storedFieldTypes);
    110         autofillDao.insertAutofillHints(storedAutofillHints);
    111     }
    112 
    113     public abstract AutofillDao autofillDao();
    114 }