• 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 static com.android.settings.slices.SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX;
20 
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.net.Uri;
25 import android.os.Binder;
26 import android.text.TextUtils;
27 import android.util.Pair;
28 
29 import androidx.slice.Slice;
30 
31 import com.android.settings.overlay.FeatureFactory;
32 import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /**
38  * Class used to map a {@link Uri} from {@link SettingsSliceProvider} to a Slice.
39  */
40 public class SlicesDatabaseAccessor {
41 
42     public static final String[] SELECT_COLUMNS_ALL = {
43             IndexColumns.KEY,
44             IndexColumns.TITLE,
45             IndexColumns.SUMMARY,
46             IndexColumns.SCREENTITLE,
47             IndexColumns.KEYWORDS,
48             IndexColumns.ICON_RESOURCE,
49             IndexColumns.FRAGMENT,
50             IndexColumns.CONTROLLER,
51             IndexColumns.SLICE_TYPE,
52             IndexColumns.UNAVAILABLE_SLICE_SUBTITLE,
53             IndexColumns.HIGHLIGHT_MENU_RESOURCE,
54     };
55 
56     private final Context mContext;
57     private final SlicesDatabaseHelper mHelper;
58 
SlicesDatabaseAccessor(Context context)59     public SlicesDatabaseAccessor(Context context) {
60         mContext = context;
61         mHelper = SlicesDatabaseHelper.getInstance(mContext);
62     }
63 
64     /**
65      * Query the slices database and return a {@link SliceData} object corresponding to the row
66      * matching the key provided by the {@param uri}. Additionally adds the {@param uri} to the
67      * {@link SliceData} object so the {@link Slice} can bind to the {@link Uri}.
68      * Used when building a {@link Slice}.
69      */
getSliceDataFromUri(Uri uri)70     public SliceData getSliceDataFromUri(Uri uri) {
71         Pair<Boolean, String> pathData = SliceBuilderUtils.getPathData(uri);
72         if (pathData == null) {
73             throw new IllegalStateException("Invalid Slices uri: " + uri);
74         }
75         try (Cursor cursor = getIndexedSliceData(pathData.second /* key */)) {
76             return buildSliceData(cursor, uri, pathData.first /* isIntentOnly */);
77         }
78     }
79 
80     /**
81      * Query the slices database and return a {@link SliceData} object corresponding to the row
82      * matching the {@param key}.
83      * Used when handling the action of the {@link Slice}.
84      */
getSliceDataFromKey(String key)85     public SliceData getSliceDataFromKey(String key) {
86         try (Cursor cursor = getIndexedSliceData(key)) {
87             return buildSliceData(cursor, null /* uri */, false /* isIntentOnly */);
88         }
89     }
90 
91     /**
92      * @return a list of Slice {@link Uri}s based on their visibility {@param isPublicSlice } and
93      * {@param authority}.
94      */
getSliceUris(String authority, boolean isPublicSlice)95     public List<Uri> getSliceUris(String authority, boolean isPublicSlice) {
96         verifyIndexing();
97         final List<Uri> uris = new ArrayList<>();
98         final String whereClause = IndexColumns.PUBLIC_SLICE + (isPublicSlice ? "=1" : "=0");
99         final SQLiteDatabase database = mHelper.getReadableDatabase();
100         final String[] columns = new String[]{IndexColumns.SLICE_URI};
101         try (Cursor resultCursor = database.query(TABLE_SLICES_INDEX, columns,
102                 whereClause /* where */, null /* selection */, null /* groupBy */,
103                 null /* having */, null /* orderBy */)) {
104             if (!resultCursor.moveToFirst()) {
105                 return uris;
106             }
107 
108             do {
109                 final Uri uri = Uri.parse(resultCursor.getString(0 /* SLICE_URI */));
110                 if (TextUtils.isEmpty(authority)
111                         || TextUtils.equals(authority, uri.getAuthority())) {
112                     uris.add(uri);
113                 }
114             } while (resultCursor.moveToNext());
115         }
116         return uris;
117     }
118 
getIndexedSliceData(String path)119     private Cursor getIndexedSliceData(String path) {
120         verifyIndexing();
121 
122         final String whereClause = buildKeyMatchWhereClause();
123         final SQLiteDatabase database = mHelper.getReadableDatabase();
124         final String[] selection = new String[]{path};
125         final Cursor resultCursor = database.query(TABLE_SLICES_INDEX, SELECT_COLUMNS_ALL,
126                 whereClause, selection, null /* groupBy */, null /* having */, null /* orderBy */);
127 
128         int numResults = resultCursor.getCount();
129 
130         if (numResults == 0) {
131             resultCursor.close();
132             throw new IllegalStateException("Invalid Slices key from path: " + path);
133         }
134 
135         if (numResults > 1) {
136             resultCursor.close();
137             throw new IllegalStateException(
138                     "Should not match more than 1 slice with path: " + path);
139         }
140 
141         resultCursor.moveToFirst();
142         return resultCursor;
143     }
144 
buildKeyMatchWhereClause()145     private String buildKeyMatchWhereClause() {
146         return new StringBuilder(IndexColumns.KEY)
147                 .append(" = ?")
148                 .toString();
149     }
150 
buildSliceData(Cursor cursor, Uri uri, boolean isIntentOnly)151     private static SliceData buildSliceData(Cursor cursor, Uri uri, boolean isIntentOnly) {
152         final String key = cursor.getString(cursor.getColumnIndex(IndexColumns.KEY));
153         final String title = cursor.getString(cursor.getColumnIndex(IndexColumns.TITLE));
154         final String summary = cursor.getString(cursor.getColumnIndex(IndexColumns.SUMMARY));
155         final String screenTitle = cursor.getString(
156                 cursor.getColumnIndex(IndexColumns.SCREENTITLE));
157         final String keywords = cursor.getString(cursor.getColumnIndex(IndexColumns.KEYWORDS));
158         final int iconResource = cursor.getInt(cursor.getColumnIndex(IndexColumns.ICON_RESOURCE));
159         final String fragmentClassName = cursor.getString(
160                 cursor.getColumnIndex(IndexColumns.FRAGMENT));
161         final String controllerClassName = cursor.getString(
162                 cursor.getColumnIndex(IndexColumns.CONTROLLER));
163         int sliceType = cursor.getInt(
164                 cursor.getColumnIndex(IndexColumns.SLICE_TYPE));
165         final String unavailableSliceSubtitle = cursor.getString(
166                 cursor.getColumnIndex(IndexColumns.UNAVAILABLE_SLICE_SUBTITLE));
167         final int highlightMenuRes = cursor.getInt(
168                 cursor.getColumnIndex(IndexColumns.HIGHLIGHT_MENU_RESOURCE));
169 
170         if (isIntentOnly) {
171             sliceType = SliceData.SliceType.INTENT;
172         }
173 
174         return new SliceData.Builder()
175                 .setKey(key)
176                 .setTitle(title)
177                 .setSummary(summary)
178                 .setScreenTitle(screenTitle)
179                 .setKeywords(keywords)
180                 .setIcon(iconResource)
181                 .setFragmentName(fragmentClassName)
182                 .setPreferenceControllerClassName(controllerClassName)
183                 .setUri(uri)
184                 .setSliceType(sliceType)
185                 .setUnavailableSliceSubtitle(unavailableSliceSubtitle)
186                 .setHighlightMenuRes(highlightMenuRes)
187                 .build();
188     }
189 
verifyIndexing()190     private void verifyIndexing() {
191         final long uidToken = Binder.clearCallingIdentity();
192         try {
193             FeatureFactory.getFactory(
194                     mContext).getSlicesFeatureProvider().indexSliceData(mContext);
195         } finally {
196             Binder.restoreCallingIdentity(uidToken);
197         }
198     }
199 }