• 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.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.Handler;
26 
27 import androidx.annotation.Nullable;
28 import androidx.annotation.WorkerThread;
29 import androidx.lifecycle.LiveData;
30 import androidx.lifecycle.MutableLiveData;
31 
32 import com.android.customization.model.ResourceConstants;
33 import com.android.themepicker.R;
34 import com.android.wallpaper.config.BaseFlags;
35 import com.android.wallpaper.util.PreviewUtils;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /**
41  * Abstracts the logic to retrieve available grid options from the current Launcher.
42  */
43 public class LauncherGridOptionsProvider {
44 
45     private static final String LIST_OPTIONS = "list_options";
46     private static final String PREVIEW = "preview";
47     private static final String DEFAULT_GRID = "default_grid";
48 
49     private static final String COL_NAME = "name";
50     private static final String COL_GRID_TITLE = "grid_title";
51     private static final String COL_GRID_ICON_ID = "grid_icon_id";
52     private static final String COL_ROWS = "rows";
53     private static final String COL_COLS = "cols";
54     private static final String COL_PREVIEW_COUNT = "preview_count";
55     private static final String COL_IS_DEFAULT = "is_default";
56 
57     private static final String METADATA_KEY_PREVIEW_VERSION = "preview_version";
58 
59     private final Context mContext;
60     private final PreviewUtils mPreviewUtils;
61     private final boolean mIsGridApplyButtonEnabled;
62     private List<GridOption> mOptions;
63     private OptionChangeLiveData mLiveData;
64 
LauncherGridOptionsProvider(Context context, String authorityMetadataKey)65     public LauncherGridOptionsProvider(Context context, String authorityMetadataKey) {
66         mPreviewUtils = new PreviewUtils(context, authorityMetadataKey);
67         mContext = context;
68         mIsGridApplyButtonEnabled = BaseFlags.get().isGridApplyButtonEnabled(context);
69     }
70 
areGridsAvailable()71     boolean areGridsAvailable() {
72         return mPreviewUtils.supportsPreview();
73     }
74 
75     /**
76      * Retrieve the available grids.
77      * @param reload whether to reload grid options if they're cached.
78      */
79     @WorkerThread
80     @Nullable
fetch(boolean reload)81     List<GridOption> fetch(boolean reload) {
82         if (!areGridsAvailable()) {
83             return null;
84         }
85         if (mOptions != null && !reload) {
86             return mOptions;
87         }
88         ContentResolver resolver = mContext.getContentResolver();
89         String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier(
90                 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE));
91         try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null,
92                 null)) {
93             mOptions = new ArrayList<>();
94             while(c.moveToNext()) {
95                 String name = c.getString(c.getColumnIndex(COL_NAME));
96                 String title = c.getString(c.getColumnIndex(COL_GRID_TITLE));
97                 int gridIconId = c.getInt(c.getColumnIndex(COL_GRID_ICON_ID));
98                 int rows = c.getInt(c.getColumnIndex(COL_ROWS));
99                 int cols = c.getInt(c.getColumnIndex(COL_COLS));
100                 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT));
101                 boolean isSet = Boolean.parseBoolean(c.getString(c.getColumnIndex(COL_IS_DEFAULT)));
102                 if (title == null) {
103                     title = mContext.getString(R.string.grid_title_pattern, cols, rows);
104                 }
105                 mOptions.add(new GridOption(title, name, isSet, rows, cols,
106                         mPreviewUtils.getUri(PREVIEW), previewCount, iconPath, gridIconId));
107             }
108         } catch (Exception e) {
109             mOptions = null;
110         }
111         return mOptions;
112     }
113 
updateView()114     void updateView() {
115         mLiveData.postValue(new Object());
116     }
117 
applyGrid(String name)118     int applyGrid(String name) {
119         ContentValues values = new ContentValues();
120         values.put("name", name);
121         values.put("enable_apply_button", mIsGridApplyButtonEnabled);
122         return mContext.getContentResolver().update(mPreviewUtils.getUri(DEFAULT_GRID), values,
123                 null, null);
124     }
125 
126     /**
127      * Returns an observable that receives a new value each time that the grid options are changed.
128      * Do not call if {@link #areGridsAvailable()} returns false
129      */
getOptionChangeObservable( @ullable Handler handler)130     public LiveData<Object> getOptionChangeObservable(
131             @Nullable Handler handler) {
132         if (mLiveData == null) {
133             mLiveData = new OptionChangeLiveData(
134                     mContext, mPreviewUtils.getUri(DEFAULT_GRID), handler);
135         }
136 
137         return mLiveData;
138     }
139 
140     private static class OptionChangeLiveData extends MutableLiveData<Object> {
141 
142         private final ContentResolver mContentResolver;
143         private final Uri mUri;
144         private final ContentObserver mContentObserver;
145 
OptionChangeLiveData( Context context, Uri uri, @Nullable Handler handler)146         OptionChangeLiveData(
147                 Context context,
148                 Uri uri,
149                 @Nullable Handler handler) {
150             mContentResolver = context.getContentResolver();
151             mUri = uri;
152             mContentObserver = new ContentObserver(handler) {
153                 @Override
154                 public void onChange(boolean selfChange) {
155                     // If grid apply button is enabled, user has previewed the grid before applying
156                     // the grid change. Thus there is no need to preview again (which will cause a
157                     // blank preview as launcher's is loader thread is busy reloading workspace)
158                     // after applying grid change. Thus we should ignore ContentObserver#onChange
159                     // from launcher
160                     if (BaseFlags.get().isGridApplyButtonEnabled(context.getApplicationContext())) {
161                         return;
162                     }
163                     postValue(new Object());
164                 }
165             };
166         }
167 
168         @Override
onActive()169         protected void onActive() {
170             mContentResolver.registerContentObserver(
171                     mUri,
172                     /* notifyForDescendants= */ true,
173                     mContentObserver);
174         }
175 
176         @Override
onInactive()177         protected void onInactive() {
178             mContentResolver.unregisterContentObserver(mContentObserver);
179         }
180     }
181 }
182