• 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.os.AsyncTask;
19 
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 
23 import com.android.customization.model.CustomizationManager;
24 import com.android.customization.module.ThemesUserEventLogger;
25 
26 import java.util.Collections;
27 import java.util.List;
28 
29 /**
30  * {@link CustomizationManager} for interfacing with the launcher to handle {@link GridOption}s.
31  */
32 public class GridOptionsManager implements CustomizationManager<GridOption> {
33 
34     private final LauncherGridOptionsProvider mProvider;
35     private final ThemesUserEventLogger mEventLogger;
36 
GridOptionsManager(LauncherGridOptionsProvider provider, ThemesUserEventLogger logger)37     public GridOptionsManager(LauncherGridOptionsProvider provider, ThemesUserEventLogger logger) {
38         mProvider = provider;
39         mEventLogger = logger;
40     }
41 
42     @Override
isAvailable()43     public boolean isAvailable() {
44         return mProvider.areGridsAvailable();
45     }
46 
47     @Override
apply(GridOption option, Callback callback)48     public void apply(GridOption option, Callback callback) {
49         int updated = mProvider.applyGrid(option.name);
50         if (updated == 1) {
51             mEventLogger.logGridApplied(option);
52             callback.onSuccess();
53         } else {
54             callback.onError(null);
55         }
56     }
57 
58     @Override
fetchOptions(OptionsFetchedListener<GridOption> callback, boolean reload)59     public void fetchOptions(OptionsFetchedListener<GridOption> callback, boolean reload) {
60         new FetchTask(mProvider, callback).execute();
61     }
62 
63     private static class FetchTask extends AsyncTask<Void, Void, List<GridOption>> {
64         private final LauncherGridOptionsProvider mProvider;
65         @Nullable private final OptionsFetchedListener<GridOption> mCallback;
66 
FetchTask(@onNull LauncherGridOptionsProvider provider, @Nullable OptionsFetchedListener<GridOption> callback)67         private FetchTask(@NonNull LauncherGridOptionsProvider provider,
68                 @Nullable OptionsFetchedListener<GridOption> callback) {
69             mCallback = callback;
70             mProvider = provider;
71         }
72 
73         @Override
doInBackground(Void[] params)74         protected List<GridOption> doInBackground(Void[] params) {
75             return mProvider.fetch(false);
76         }
77 
78         @Override
onPostExecute(List<GridOption> gridOptions)79         protected void onPostExecute(List<GridOption> gridOptions) {
80             if (mCallback != null) {
81                 if (gridOptions != null && !gridOptions.isEmpty()) {
82                     mCallback.onOptionsLoaded(gridOptions);
83                 } else {
84                     mCallback.onError(null);
85                 }
86             }
87         }
88 
89         @Override
onCancelled()90         protected void onCancelled() {
91             super.onCancelled();
92             if (mCallback != null) {
93                 mCallback.onError(null);
94             }
95         }
96     }
97 }
98