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.text.TextUtils;
25 import android.util.Pair;
26 import android.view.SurfaceView;
27 
28 import androidx.annotation.Nullable;
29 import androidx.annotation.WorkerThread;
30 
31 import com.android.customization.model.ResourceConstants;
32 import com.android.systemui.shared.system.SurfaceViewRequestUtils;
33 import com.android.wallpaper.R;
34 import com.android.wallpaper.util.PreviewUtils;
35 
36 import com.bumptech.glide.Glide;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * Abstracts the logic to retrieve available grid options from the current Launcher.
43  */
44 public class LauncherGridOptionsProvider {
45 
46     private static final String LIST_OPTIONS = "list_options";
47     private static final String PREVIEW = "preview";
48     private static final String DEFAULT_GRID = "default_grid";
49 
50     private static final String COL_NAME = "name";
51     private static final String COL_ROWS = "rows";
52     private static final String COL_COLS = "cols";
53     private static final String COL_PREVIEW_COUNT = "preview_count";
54     private static final String COL_IS_DEFAULT = "is_default";
55 
56     private static final String METADATA_KEY_PREVIEW_VERSION = "preview_version";
57 
58     private final Context mContext;
59     private final PreviewUtils mPreviewUtils;
60     private List<GridOption> mOptions;
61     private String mVersion;
62 
LauncherGridOptionsProvider(Context context, String authorityMetadataKey)63     public LauncherGridOptionsProvider(Context context, String authorityMetadataKey) {
64         mPreviewUtils = new PreviewUtils(context, authorityMetadataKey);
65         mContext = context;
66     }
67 
areGridsAvailable()68     boolean areGridsAvailable() {
69         return mPreviewUtils.supportsPreview();
70     }
71 
usesSurfaceView()72     boolean usesSurfaceView() {
73         // If no version code is returned, fall back to V1.
74         return TextUtils.equals(mVersion, "V2");
75     }
76 
77     /**
78      * Retrieve the available grids.
79      * @param reload whether to reload grid options if they're cached.
80      */
81     @WorkerThread
82     @Nullable
fetch(boolean reload)83     Pair<List<GridOption>, String> fetch(boolean reload) {
84         if (!areGridsAvailable()) {
85             return null;
86         }
87         if (mOptions != null && !reload) {
88             return Pair.create(mOptions, mVersion);
89         }
90         ContentResolver resolver = mContext.getContentResolver();
91         String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier(
92                 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE));
93         try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null,
94                 null)) {
95             mVersion = c.getExtras().getString(METADATA_KEY_PREVIEW_VERSION);
96             mOptions = new ArrayList<>();
97             while(c.moveToNext()) {
98                 String name = c.getString(c.getColumnIndex(COL_NAME));
99                 int rows = c.getInt(c.getColumnIndex(COL_ROWS));
100                 int cols = c.getInt(c.getColumnIndex(COL_COLS));
101                 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT));
102                 boolean isSet = Boolean.valueOf(c.getString(c.getColumnIndex(COL_IS_DEFAULT)));
103                 String title = mContext.getString(R.string.grid_title_pattern, cols, rows);
104                 mOptions.add(new GridOption(title, name, isSet, rows, cols,
105                         mPreviewUtils.getUri(PREVIEW), previewCount, iconPath));
106             }
107             Glide.get(mContext).clearDiskCache();
108         } catch (Exception e) {
109             mOptions = null;
110             mVersion = null;
111         }
112         return Pair.create(mOptions, mVersion);
113     }
114 
115     /**
116      * Request rendering of home screen preview via Launcher to Wallpaper using SurfaceView
117      * @param name      the grid option name
118      * @param bundle    surface view request bundle generated from
119      *                  {@link SurfaceViewRequestUtils#createSurfaceBundle(SurfaceView)}.
120      */
renderPreview(String name, Bundle bundle)121     void renderPreview(String name, Bundle bundle) {
122         bundle.putString("name", name);
123         mPreviewUtils.renderPreview(bundle);
124     }
125 
applyGrid(String name)126     int applyGrid(String name) {
127         ContentValues values = new ContentValues();
128         values.put("name", name);
129         return mContext.getContentResolver().update(mPreviewUtils.getUri(DEFAULT_GRID), values,
130                 null, null);
131     }
132 }
133