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 com.bumptech.glide.Glide; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Abstracts the logic to retrieve available grid options from the current Launcher. 40 */ 41 public class LauncherGridOptionsProvider { 42 43 private static final String LIST_OPTIONS = "list_options"; 44 private static final String PREVIEW = "preview"; 45 private static final String DEFAULT_GRID = "default_grid"; 46 47 private static final String COL_NAME = "name"; 48 private static final String COL_ROWS = "rows"; 49 private static final String COL_COLS = "cols"; 50 private static final String COL_PREVIEW_COUNT = "preview_count"; 51 private static final String COL_IS_DEFAULT = "is_default"; 52 53 // Normal gird size name 54 private static final String GRID_NAME_NORMAL = "normal"; 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 LauncherGridOptionsProvider(Context context, String authorityMetadataKey)62 public LauncherGridOptionsProvider(Context context, String authorityMetadataKey) { 63 mPreviewUtils = new PreviewUtils(context, authorityMetadataKey); 64 mContext = context; 65 } 66 areGridsAvailable()67 boolean areGridsAvailable() { 68 return mPreviewUtils.supportsPreview(); 69 } 70 71 /** 72 * Retrieve the available grids. 73 * @param reload whether to reload grid options if they're cached. 74 */ 75 @WorkerThread 76 @Nullable fetch(boolean reload)77 List<GridOption> fetch(boolean reload) { 78 if (!areGridsAvailable()) { 79 return null; 80 } 81 if (mOptions != null && !reload) { 82 return mOptions; 83 } 84 ContentResolver resolver = mContext.getContentResolver(); 85 String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier( 86 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE)); 87 try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null, 88 null)) { 89 mOptions = new ArrayList<>(); 90 while(c.moveToNext()) { 91 String name = c.getString(c.getColumnIndex(COL_NAME)); 92 int rows = c.getInt(c.getColumnIndex(COL_ROWS)); 93 int cols = c.getInt(c.getColumnIndex(COL_COLS)); 94 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT)); 95 boolean isSet = Boolean.valueOf(c.getString(c.getColumnIndex(COL_IS_DEFAULT))); 96 String title = GRID_NAME_NORMAL.equals(name) 97 ? mContext.getString(R.string.default_theme_title) 98 : mContext.getString(R.string.grid_title_pattern, cols, rows); 99 mOptions.add(new GridOption(title, name, isSet, rows, cols, 100 mPreviewUtils.getUri(PREVIEW), previewCount, iconPath)); 101 } 102 Glide.get(mContext).clearDiskCache(); 103 } catch (Exception e) { 104 mOptions = null; 105 } 106 return mOptions; 107 } 108 109 /** 110 * Request rendering of home screen preview via Launcher to Wallpaper using SurfaceView 111 * @param name the grid option name 112 * @param bundle surface view request bundle generated from 113 * {@link com.android.wallpaper.util.SurfaceViewUtils#createSurfaceViewRequest(SurfaceView)}. 114 */ renderPreview(String name, Bundle bundle)115 Bundle renderPreview(String name, Bundle bundle) { 116 bundle.putString("name", name); 117 return mPreviewUtils.renderPreview(bundle); 118 } 119 applyGrid(String name)120 int applyGrid(String name) { 121 ContentValues values = new ContentValues(); 122 values.put("name", name); 123 return mContext.getContentResolver().update(mPreviewUtils.getUri(DEFAULT_GRID), values, 124 null, null); 125 } 126 } 127