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.dao;
18 
19 import android.arch.persistence.room.Dao;
20 import android.arch.persistence.room.Insert;
21 import android.arch.persistence.room.OnConflictStrategy;
22 import android.arch.persistence.room.Query;
23 
24 import com.example.android.autofill.service.model.AutofillDataset;
25 import com.example.android.autofill.service.model.AutofillHint;
26 import com.example.android.autofill.service.model.DatasetWithFilledAutofillFields;
27 import com.example.android.autofill.service.model.FieldType;
28 import com.example.android.autofill.service.model.FieldTypeWithHeuristics;
29 import com.example.android.autofill.service.model.FilledAutofillField;
30 import com.example.android.autofill.service.model.ResourceIdHeuristic;
31 
32 import java.util.Collection;
33 import java.util.List;
34 
35 @Dao
36 public interface AutofillDao {
37     /**
38      * Fetches a list of datasets associated to autofill fields on the page.
39      *
40      * @param allAutofillHints Filtering parameter; represents all of the hints associated with
41      *                         all of the views on the page.
42      */
43     @Query("SELECT DISTINCT id, datasetName FROM FilledAutofillField, AutofillDataset" +
44             " WHERE AutofillDataset.id = FilledAutofillField.datasetId" +
45             " AND FilledAutofillField.fieldTypeName IN (:allAutofillHints)")
getDatasets(List<String> allAutofillHints)46     List<DatasetWithFilledAutofillFields> getDatasets(List<String> allAutofillHints);
47 
48     @Query("SELECT DISTINCT id, datasetName FROM FilledAutofillField, AutofillDataset" +
49             " WHERE AutofillDataset.id = FilledAutofillField.datasetId")
getAllDatasets()50     List<DatasetWithFilledAutofillFields> getAllDatasets();
51 
52     /**
53      * Fetches a list of datasets associated to autofill fields. It should only return a dataset
54      * if that dataset has an autofill field associate with the view the user is focused on, and
55      * if that dataset's name matches the name passed in.
56      *
57      * @param fieldTypes Filtering parameter; represents all of the field types associated with
58      *                         all of the views on the page.
59      * @param datasetName      Filtering parameter; only return datasets with this name.
60      */
61     @Query("SELECT DISTINCT id, datasetName FROM FilledAutofillField, AutofillDataset" +
62             " WHERE AutofillDataset.id = FilledAutofillField.datasetId" +
63             " AND AutofillDataset.datasetName = (:datasetName)" +
64             " AND FilledAutofillField.fieldTypeName IN (:fieldTypes)")
getDatasetsWithName( List<String> fieldTypes, String datasetName)65     List<DatasetWithFilledAutofillFields> getDatasetsWithName(
66             List<String> fieldTypes, String datasetName);
67 
68     @Query("SELECT DISTINCT typeName, autofillTypes, saveInfo, partition, strictExampleSet, " +
69             "textTemplate, dateTemplate" +
70             " FROM FieldType, AutofillHint" +
71             " WHERE FieldType.typeName = AutofillHint.fieldTypeName" +
72             " UNION " +
73             "SELECT DISTINCT typeName, autofillTypes, saveInfo, partition, strictExampleSet, " +
74             "textTemplate, dateTemplate" +
75             " FROM FieldType, ResourceIdHeuristic" +
76             " WHERE FieldType.typeName = ResourceIdHeuristic.fieldTypeName")
getFieldTypesWithHints()77     List<FieldTypeWithHeuristics> getFieldTypesWithHints();
78 
79     @Query("SELECT DISTINCT typeName, autofillTypes, saveInfo, partition, strictExampleSet, " +
80             "textTemplate, dateTemplate" +
81             " FROM FieldType, AutofillHint" +
82             " WHERE FieldType.typeName = AutofillHint.fieldTypeName" +
83             " AND AutofillHint.autofillHint IN (:autofillHints)" +
84             " UNION " +
85             "SELECT DISTINCT typeName, autofillTypes, saveInfo, partition, strictExampleSet, " +
86             "textTemplate, dateTemplate" +
87             " FROM FieldType, ResourceIdHeuristic" +
88             " WHERE FieldType.typeName = ResourceIdHeuristic.fieldTypeName")
getFieldTypesForAutofillHints(List<String> autofillHints)89     List<FieldTypeWithHeuristics> getFieldTypesForAutofillHints(List<String> autofillHints);
90 
91     @Query("SELECT DISTINCT id, datasetName FROM FilledAutofillField, AutofillDataset" +
92             " WHERE AutofillDataset.id = FilledAutofillField.datasetId" +
93             " AND AutofillDataset.id = (:datasetId)")
getAutofillDatasetWithId(String datasetId)94     DatasetWithFilledAutofillFields getAutofillDatasetWithId(String datasetId);
95 
96     @Query("SELECT * FROM FilledAutofillField" +
97             " WHERE FilledAutofillField.datasetId = (:datasetId)" +
98             " AND FilledAutofillField.fieldTypeName = (:fieldTypeName)")
getFilledAutofillField(String datasetId, String fieldTypeName)99     FilledAutofillField getFilledAutofillField(String datasetId, String fieldTypeName);
100 
101     @Query("SELECT * FROM FieldType" +
102             " WHERE FieldType.typeName = (:fieldTypeName)")
getFieldType(String fieldTypeName)103     FieldType getFieldType(String fieldTypeName);
104 
105     /**
106      * @param autofillFields Collection of autofill fields to be saved to the db.
107      */
108     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertFilledAutofillFields(Collection<FilledAutofillField> autofillFields)109     void insertFilledAutofillFields(Collection<FilledAutofillField> autofillFields);
110 
111     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAutofillDataset(AutofillDataset datasets)112     void insertAutofillDataset(AutofillDataset datasets);
113 
114     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAutofillHints(List<AutofillHint> autofillHints)115     void insertAutofillHints(List<AutofillHint> autofillHints);
116 
117     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertResourceIdHeuristic(ResourceIdHeuristic resourceIdHeuristic)118     void insertResourceIdHeuristic(ResourceIdHeuristic resourceIdHeuristic);
119 
120     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertFieldTypes(List<FieldType> fieldTypes)121     void insertFieldTypes(List<FieldType> fieldTypes);
122 
123 
124     @Query("DELETE FROM AutofillDataset")
clearAll()125     void clearAll();
126 }