• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android.settings.slices;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.support.annotation.VisibleForTesting;
23 import android.util.Log;
24 
25 import com.android.settings.dashboard.DashboardFragment;
26 
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns;
30 import com.android.settings.slices.SlicesDatabaseHelper.Tables;
31 
32 import java.util.List;
33 
34 /**
35  * Manages the conversion of {@link DashboardFragment} and {@link BasePreferenceController} to
36  * indexable data {@link SliceData} to be stored for Slices.
37  */
38 class SlicesIndexer implements Runnable {
39 
40     private static final String TAG = "SlicesIndexer";
41 
42     private Context mContext;
43 
44     private SlicesDatabaseHelper mHelper;
45 
SlicesIndexer(Context context)46     public SlicesIndexer(Context context) {
47         mContext = context;
48         mHelper = SlicesDatabaseHelper.getInstance(mContext);
49     }
50 
51     /**
52      * Asynchronously index slice data from {@link #indexSliceData()}.
53      */
54     @Override
run()55     public void run() {
56         indexSliceData();
57     }
58 
59     /**
60      * Synchronously takes data obtained from {@link SliceDataConverter} and indexes it into a
61      * SQLite database
62      */
indexSliceData()63     protected void indexSliceData() {
64         if (mHelper.isSliceDataIndexed()) {
65             Log.d(TAG, "Slices already indexed - returning.");
66             return;
67         }
68 
69         SQLiteDatabase database = mHelper.getWritableDatabase();
70 
71         try {
72             long startTime = System.currentTimeMillis();
73             database.beginTransaction();
74 
75             mHelper.reconstruct(mHelper.getWritableDatabase());
76             List<SliceData> indexData = getSliceData();
77             insertSliceData(database, indexData);
78 
79             mHelper.setIndexedState();
80 
81             // TODO (b/71503044) Log indexing time.
82             Log.d(TAG,
83                     "Indexing slices database took: " + (System.currentTimeMillis() - startTime));
84             database.setTransactionSuccessful();
85         } finally {
86             database.endTransaction();
87         }
88     }
89 
90     @VisibleForTesting
getSliceData()91     List<SliceData> getSliceData() {
92         return FeatureFactory.getFactory(mContext)
93                 .getSlicesFeatureProvider()
94                 .getSliceDataConverter(mContext)
95                 .getSliceData();
96     }
97 
98     @VisibleForTesting
insertSliceData(SQLiteDatabase database, List<SliceData> indexData)99     void insertSliceData(SQLiteDatabase database, List<SliceData> indexData) {
100         ContentValues values;
101 
102         for (SliceData dataRow : indexData) {
103             values = new ContentValues();
104             values.put(IndexColumns.KEY, dataRow.getKey());
105             values.put(IndexColumns.TITLE, dataRow.getTitle());
106             values.put(IndexColumns.SUMMARY, dataRow.getSummary());
107             values.put(IndexColumns.SCREENTITLE, dataRow.getScreenTitle().toString());
108             values.put(IndexColumns.KEYWORDS, dataRow.getKeywords());
109             values.put(IndexColumns.ICON_RESOURCE, dataRow.getIconResource());
110             values.put(IndexColumns.FRAGMENT, dataRow.getFragmentClassName());
111             values.put(IndexColumns.CONTROLLER, dataRow.getPreferenceController());
112             values.put(IndexColumns.PLATFORM_SLICE, dataRow.isPlatformDefined());
113             values.put(IndexColumns.SLICE_TYPE, dataRow.getSliceType());
114 
115             database.replaceOrThrow(Tables.TABLE_SLICES_INDEX, null /* nullColumnHack */,
116                     values);
117         }
118     }
119 }