• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.launcher3.celllayout;
17 
18 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
19 
20 import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
21 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
22 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
23 import static com.android.launcher3.util.TestUtil.runOnExecutorSync;
24 
25 import android.content.Context;
26 
27 import androidx.test.uiautomator.UiDevice;
28 
29 import com.android.launcher3.LauncherAppState;
30 import com.android.launcher3.LauncherModel;
31 import com.android.launcher3.LauncherSettings;
32 import com.android.launcher3.model.BgDataModel.Callbacks;
33 import com.android.launcher3.model.ModelDbController;
34 import com.android.launcher3.model.data.FolderInfo;
35 import com.android.launcher3.model.data.ItemInfo;
36 import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
37 import com.android.launcher3.tapl.LauncherInstrumentation;
38 import com.android.launcher3.util.ContentWriter;
39 
40 import java.util.ArrayList;
41 import java.util.function.Supplier;
42 
43 public class FavoriteItemsTransaction {
44     private ArrayList<Supplier<ItemInfo>> mItemsToSubmit;
45     private Context mContext;
46 
FavoriteItemsTransaction(Context context)47     public FavoriteItemsTransaction(Context context) {
48         mItemsToSubmit = new ArrayList<>();
49         mContext = context;
50     }
51 
addItem(Supplier<ItemInfo> itemInfo)52     public FavoriteItemsTransaction addItem(Supplier<ItemInfo> itemInfo) {
53         this.mItemsToSubmit.add(itemInfo);
54         return this;
55     }
56 
57     /**
58      * Commits all the ItemInfo into the database of Favorites
59      **/
commit()60     public void commit() {
61         LauncherModel model = LauncherAppState.getInstance(mContext).getModel();
62         // Load the model once so that there is no pending migration:
63         loadModelSync(model);
64 
65         runOnExecutorSync(MODEL_EXECUTOR, () -> {
66             ModelDbController controller = model.getModelDbController();
67             // Migrate any previous data so that the DB state is correct
68             controller.tryMigrateDB();
69 
70             // Create DB again to load fresh data
71             controller.createEmptyDB();
72             controller.clearEmptyDbFlag();
73 
74             // Add new data
75             try (SQLiteTransaction transaction = controller.newTransaction()) {
76                 int count = mItemsToSubmit.size();
77                 ArrayList<ItemInfo> containerItems = new ArrayList<>();
78                 for (int i = 0; i < count; i++) {
79                     ContentWriter writer = new ContentWriter(mContext);
80                     ItemInfo item = mItemsToSubmit.get(i).get();
81 
82                     if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
83                         FolderInfo folderInfo = (FolderInfo) item;
84                         for (ItemInfo itemInfo : folderInfo.contents) {
85                             itemInfo.container = i;
86                             containerItems.add(itemInfo);
87                         }
88                     }
89 
90                     item.onAddToDatabase(writer);
91                     writer.put(LauncherSettings.Favorites._ID, i);
92                     controller.insert(TABLE_NAME, writer.getValues(mContext));
93                 }
94 
95                 for (int i = 0; i < containerItems.size(); i++) {
96                     ContentWriter writer = new ContentWriter(mContext);
97                     ItemInfo item = containerItems.get(i);
98                     item.onAddToDatabase(writer);
99                     writer.put(LauncherSettings.Favorites._ID, count + i);
100                     controller.insert(TABLE_NAME, writer.getValues(mContext));
101                 }
102                 transaction.commit();
103             }
104         });
105 
106         // Reload model
107         runOnExecutorSync(MAIN_EXECUTOR, model::forceReload);
108         loadModelSync(model);
109     }
110 
loadModelSync(LauncherModel model)111     private void loadModelSync(LauncherModel model) {
112         Callbacks mockCb = new Callbacks() { };
113         runOnExecutorSync(MAIN_EXECUTOR, () -> model.addCallbacksAndLoad(mockCb));
114         runOnExecutorSync(MODEL_EXECUTOR, () -> { });
115 
116         runOnExecutorSync(MAIN_EXECUTOR, () -> { });
117         runOnExecutorSync(MAIN_EXECUTOR, () -> model.removeCallbacks(mockCb));
118     }
119 
120     /**
121      * Commits the transaction and waits for home load
122      */
commitAndLoadHome(LauncherInstrumentation inst)123     public void commitAndLoadHome(LauncherInstrumentation inst) {
124         commit();
125 
126         // Launch the home activity
127         UiDevice.getInstance(getInstrumentation()).pressHome();
128         inst.waitForLauncherInitialized();
129     }
130 }
131