• 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.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.dashboard.DashboardFragment;
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         final SQLiteDatabase database = mHelper.getWritableDatabase();
70 
71         long startTime = System.currentTimeMillis();
72         database.beginTransaction();
73         try {
74             mHelper.reconstruct(database);
75             List<SliceData> indexData = getSliceData();
76             insertSliceData(database, indexData);
77 
78             mHelper.setIndexedState();
79 
80             // TODO (b/71503044) Log indexing time.
81             Log.d(TAG,
82                     "Indexing slices database took: " + (System.currentTimeMillis() - startTime));
83             database.setTransactionSuccessful();
84         } finally {
85             database.endTransaction();
86         }
87     }
88 
89     @VisibleForTesting
getSliceData()90     List<SliceData> getSliceData() {
91         return FeatureFactory.getFactory(mContext)
92                 .getSlicesFeatureProvider()
93                 .getSliceDataConverter(mContext)
94                 .getSliceData();
95     }
96 
97     @VisibleForTesting
insertSliceData(SQLiteDatabase database, List<SliceData> indexData)98     void insertSliceData(SQLiteDatabase database, List<SliceData> indexData) {
99         ContentValues values;
100 
101         for (SliceData dataRow : indexData) {
102             values = new ContentValues();
103             values.put(IndexColumns.KEY, dataRow.getKey());
104             values.put(IndexColumns.TITLE, dataRow.getTitle());
105             values.put(IndexColumns.SUMMARY, dataRow.getSummary());
106             values.put(IndexColumns.SCREENTITLE, dataRow.getScreenTitle().toString());
107             values.put(IndexColumns.KEYWORDS, dataRow.getKeywords());
108             values.put(IndexColumns.ICON_RESOURCE, dataRow.getIconResource());
109             values.put(IndexColumns.FRAGMENT, dataRow.getFragmentClassName());
110             values.put(IndexColumns.CONTROLLER, dataRow.getPreferenceController());
111             values.put(IndexColumns.PLATFORM_SLICE, dataRow.isPlatformDefined());
112             values.put(IndexColumns.SLICE_TYPE, dataRow.getSliceType());
113             values.put(IndexColumns.UNAVAILABLE_SLICE_SUBTITLE,
114                     dataRow.getUnavailableSliceSubtitle());
115 
116             database.replaceOrThrow(Tables.TABLE_SLICES_INDEX, null /* nullColumnHack */,
117                     values);
118         }
119     }
120 }