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.ContentObserver; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.view.SurfaceView; 28 29 import androidx.annotation.Nullable; 30 import androidx.annotation.WorkerThread; 31 import androidx.lifecycle.LiveData; 32 import androidx.lifecycle.MutableLiveData; 33 34 import com.android.customization.model.ResourceConstants; 35 import com.android.wallpaper.R; 36 import com.android.wallpaper.util.PreviewUtils; 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 OptionChangeLiveData mLiveData; 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 72 /** 73 * Retrieve the available grids. 74 * @param reload whether to reload grid options if they're cached. 75 */ 76 @WorkerThread 77 @Nullable fetch(boolean reload)78 List<GridOption> fetch(boolean reload) { 79 if (!areGridsAvailable()) { 80 return null; 81 } 82 if (mOptions != null && !reload) { 83 return mOptions; 84 } 85 ContentResolver resolver = mContext.getContentResolver(); 86 String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier( 87 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE)); 88 try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null, 89 null)) { 90 mOptions = new ArrayList<>(); 91 while(c.moveToNext()) { 92 String name = c.getString(c.getColumnIndex(COL_NAME)); 93 int rows = c.getInt(c.getColumnIndex(COL_ROWS)); 94 int cols = c.getInt(c.getColumnIndex(COL_COLS)); 95 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT)); 96 boolean isSet = Boolean.parseBoolean(c.getString(c.getColumnIndex(COL_IS_DEFAULT))); 97 String title = mContext.getString(R.string.grid_title_pattern, cols, rows); 98 mOptions.add(new GridOption(title, name, isSet, rows, cols, 99 mPreviewUtils.getUri(PREVIEW), previewCount, iconPath)); 100 } 101 } catch (Exception e) { 102 mOptions = null; 103 } 104 return mOptions; 105 } 106 107 /** 108 * Request rendering of home screen preview via Launcher to Wallpaper using SurfaceView 109 * @param name the grid option name 110 * @param bundle surface view request bundle generated from 111 * {@link com.android.wallpaper.util.SurfaceViewUtils#createSurfaceViewRequest(SurfaceView)}. 112 * @param callback To receive the result (will be called on the main thread) 113 */ renderPreview(String name, Bundle bundle, PreviewUtils.WorkspacePreviewCallback callback)114 void renderPreview(String name, Bundle bundle, 115 PreviewUtils.WorkspacePreviewCallback callback) { 116 bundle.putString("name", name); 117 mPreviewUtils.renderPreview(bundle, callback); 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 /** 128 * Returns an observable that receives a new value each time that the grid options are changed. 129 * Do not call if {@link #areGridsAvailable()} returns false 130 */ getOptionChangeObservable( @ullable Handler handler)131 public LiveData<Object> getOptionChangeObservable( 132 @Nullable Handler handler) { 133 if (mLiveData == null) { 134 mLiveData = new OptionChangeLiveData( 135 mContext, mPreviewUtils.getUri(DEFAULT_GRID), handler); 136 } 137 138 return mLiveData; 139 } 140 141 private static class OptionChangeLiveData extends MutableLiveData<Object> { 142 143 private final ContentResolver mContentResolver; 144 private final Uri mUri; 145 private final ContentObserver mContentObserver; 146 OptionChangeLiveData( Context context, Uri uri, @Nullable Handler handler)147 OptionChangeLiveData( 148 Context context, 149 Uri uri, 150 @Nullable Handler handler) { 151 mContentResolver = context.getContentResolver(); 152 mUri = uri; 153 mContentObserver = new ContentObserver(handler) { 154 @Override 155 public void onChange(boolean selfChange) { 156 postValue(new Object()); 157 } 158 }; 159 } 160 161 @Override onActive()162 protected void onActive() { 163 mContentResolver.registerContentObserver( 164 mUri, 165 /* notifyForDescendants= */ true, 166 mContentObserver); 167 } 168 169 @Override onInactive()170 protected void onInactive() { 171 mContentResolver.unregisterContentObserver(mContentObserver); 172 } 173 } 174 } 175