• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.customization.model.grid;
17 
18 import android.content.ContentResolver;
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.Cursor;
23 import android.os.Bundle;
24 import android.view.SurfaceView;
25 
26 import androidx.annotation.Nullable;
27 import androidx.annotation.WorkerThread;
28 
29 import com.android.customization.model.ResourceConstants;
30 import com.android.wallpaper.R;
31 import com.android.wallpaper.util.PreviewUtils;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * Abstracts the logic to retrieve available grid options from the current Launcher.
38  */
39 public class LauncherGridOptionsProvider {
40 
41     private static final String LIST_OPTIONS = "list_options";
42     private static final String PREVIEW = "preview";
43     private static final String DEFAULT_GRID = "default_grid";
44 
45     private static final String COL_NAME = "name";
46     private static final String COL_ROWS = "rows";
47     private static final String COL_COLS = "cols";
48     private static final String COL_PREVIEW_COUNT = "preview_count";
49     private static final String COL_IS_DEFAULT = "is_default";
50 
51     private static final String METADATA_KEY_PREVIEW_VERSION = "preview_version";
52 
53     private final Context mContext;
54     private final PreviewUtils mPreviewUtils;
55     private List<GridOption> mOptions;
56 
LauncherGridOptionsProvider(Context context, String authorityMetadataKey)57     public LauncherGridOptionsProvider(Context context, String authorityMetadataKey) {
58         mPreviewUtils = new PreviewUtils(context, authorityMetadataKey);
59         mContext = context;
60     }
61 
areGridsAvailable()62     boolean areGridsAvailable() {
63         return mPreviewUtils.supportsPreview();
64     }
65 
66     /**
67      * Retrieve the available grids.
68      * @param reload whether to reload grid options if they're cached.
69      */
70     @WorkerThread
71     @Nullable
fetch(boolean reload)72     List<GridOption> fetch(boolean reload) {
73         if (!areGridsAvailable()) {
74             return null;
75         }
76         if (mOptions != null && !reload) {
77             return mOptions;
78         }
79         ContentResolver resolver = mContext.getContentResolver();
80         String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier(
81                 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE));
82         try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null,
83                 null)) {
84             mOptions = new ArrayList<>();
85             while(c.moveToNext()) {
86                 String name = c.getString(c.getColumnIndex(COL_NAME));
87                 int rows = c.getInt(c.getColumnIndex(COL_ROWS));
88                 int cols = c.getInt(c.getColumnIndex(COL_COLS));
89                 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT));
90                 boolean isSet = Boolean.parseBoolean(c.getString(c.getColumnIndex(COL_IS_DEFAULT)));
91                 String title = mContext.getString(R.string.grid_title_pattern, cols, rows);
92                 mOptions.add(new GridOption(title, name, isSet, rows, cols,
93                         mPreviewUtils.getUri(PREVIEW), previewCount, iconPath));
94             }
95         } catch (Exception e) {
96             mOptions = null;
97         }
98         return mOptions;
99     }
100 
101     /**
102      * Request rendering of home screen preview via Launcher to Wallpaper using SurfaceView
103      * @param name      the grid option name
104      * @param bundle    surface view request bundle generated from
105      *    {@link com.android.wallpaper.util.SurfaceViewUtils#createSurfaceViewRequest(SurfaceView)}.
106      * @param callback To receive the result (will be called on the main thread)
107      */
renderPreview(String name, Bundle bundle, PreviewUtils.WorkspacePreviewCallback callback)108     void renderPreview(String name, Bundle bundle,
109             PreviewUtils.WorkspacePreviewCallback callback) {
110         bundle.putString("name", name);
111         mPreviewUtils.renderPreview(bundle, callback);
112     }
113 
applyGrid(String name)114     int applyGrid(String name) {
115         ContentValues values = new ContentValues();
116         values.put("name", name);
117         return mContext.getContentResolver().update(mPreviewUtils.getUri(DEFAULT_GRID), values,
118                 null, null);
119     }
120 }
121