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.Context; 19 import android.os.AsyncTask; 20 import android.os.Bundle; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.VisibleForTesting; 25 26 import com.android.customization.model.CustomizationManager; 27 import com.android.customization.module.CustomizationInjector; 28 import com.android.customization.module.ThemesUserEventLogger; 29 import com.android.wallpaper.R; 30 import com.android.wallpaper.module.InjectorProvider; 31 import com.android.wallpaper.util.PreviewUtils; 32 33 import java.util.List; 34 35 /** 36 * {@link CustomizationManager} for interfacing with the launcher to handle {@link GridOption}s. 37 */ 38 public class GridOptionsManager implements CustomizationManager<GridOption> { 39 40 private static GridOptionsManager sGridOptionsManager; 41 42 private final LauncherGridOptionsProvider mProvider; 43 private final ThemesUserEventLogger mEventLogger; 44 45 /** Returns the {@link GridOptionsManager} instance. */ getInstance(Context context)46 public static GridOptionsManager getInstance(Context context) { 47 if (sGridOptionsManager == null) { 48 Context appContext = context.getApplicationContext(); 49 CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector(); 50 ThemesUserEventLogger eventLogger = (ThemesUserEventLogger) injector.getUserEventLogger( 51 appContext); 52 sGridOptionsManager = new GridOptionsManager( 53 new LauncherGridOptionsProvider(appContext, 54 appContext.getString(R.string.grid_control_metadata_name)), 55 eventLogger); 56 } 57 return sGridOptionsManager; 58 } 59 60 @VisibleForTesting GridOptionsManager(LauncherGridOptionsProvider provider, ThemesUserEventLogger logger)61 GridOptionsManager(LauncherGridOptionsProvider provider, ThemesUserEventLogger logger) { 62 mProvider = provider; 63 mEventLogger = logger; 64 } 65 66 @Override isAvailable()67 public boolean isAvailable() { 68 return mProvider.areGridsAvailable(); 69 } 70 71 @Override apply(GridOption option, Callback callback)72 public void apply(GridOption option, Callback callback) { 73 int updated = mProvider.applyGrid(option.name); 74 if (updated == 1) { 75 mEventLogger.logGridApplied(option); 76 callback.onSuccess(); 77 } else { 78 callback.onError(null); 79 } 80 } 81 82 @Override fetchOptions(OptionsFetchedListener<GridOption> callback, boolean reload)83 public void fetchOptions(OptionsFetchedListener<GridOption> callback, boolean reload) { 84 new FetchTask(mProvider, callback, reload).execute(); 85 } 86 87 /** Call through content provider API to render preview */ renderPreview(Bundle bundle, String gridName, PreviewUtils.WorkspacePreviewCallback callback)88 public void renderPreview(Bundle bundle, String gridName, 89 PreviewUtils.WorkspacePreviewCallback callback) { 90 mProvider.renderPreview(gridName, bundle, callback); 91 } 92 93 private static class FetchTask extends AsyncTask<Void, Void, List<GridOption>> { 94 private final LauncherGridOptionsProvider mProvider; 95 @Nullable private final OptionsFetchedListener<GridOption> mCallback; 96 private final boolean mReload; 97 FetchTask(@onNull LauncherGridOptionsProvider provider, @Nullable OptionsFetchedListener<GridOption> callback, boolean reload)98 private FetchTask(@NonNull LauncherGridOptionsProvider provider, 99 @Nullable OptionsFetchedListener<GridOption> callback, boolean reload) { 100 mCallback = callback; 101 mProvider = provider; 102 mReload = reload; 103 } 104 105 @Override doInBackground(Void[] params)106 protected List<GridOption> doInBackground(Void[] params) { 107 return mProvider.fetch(mReload); 108 } 109 110 @Override onPostExecute(List<GridOption> gridOptions)111 protected void onPostExecute(List<GridOption> gridOptions) { 112 if (mCallback != null) { 113 if (gridOptions != null && !gridOptions.isEmpty()) { 114 mCallback.onOptionsLoaded(gridOptions); 115 } else { 116 mCallback.onError(null); 117 } 118 } 119 } 120 121 @Override onCancelled()122 protected void onCancelled() { 123 super.onCancelled(); 124 if (mCallback != null) { 125 mCallback.onError(null); 126 } 127 } 128 } 129 } 130