1 package com.android.launcher3.model; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.database.sqlite.SQLiteDatabase; 7 8 import com.android.launcher3.LauncherProvider; 9 import com.android.launcher3.LauncherSettings; 10 import com.android.launcher3.util.TestLauncherProvider; 11 12 import org.junit.Before; 13 import org.robolectric.Robolectric; 14 import org.robolectric.RuntimeEnvironment; 15 import org.robolectric.shadows.ShadowContentResolver; 16 import org.robolectric.shadows.ShadowLog; 17 18 public abstract class BaseGridChangesTestCase { 19 20 21 public static final int DESKTOP = LauncherSettings.Favorites.CONTAINER_DESKTOP; 22 public static final int HOTSEAT = LauncherSettings.Favorites.CONTAINER_HOTSEAT; 23 24 public static final int APP_ICON = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; 25 public static final int SHORTCUT = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; 26 public static final int NO__ICON = -1; 27 28 public static final String TEST_PACKAGE = "com.android.launcher3.validpackage"; 29 30 public Context mContext; 31 public TestLauncherProvider mProvider; 32 public SQLiteDatabase mDb; 33 34 @Before setUpBaseCase()35 public void setUpBaseCase() { 36 ShadowLog.stream = System.out; 37 38 mContext = RuntimeEnvironment.application; 39 mProvider = Robolectric.setupContentProvider(TestLauncherProvider.class); 40 ShadowContentResolver.registerProviderInternal(LauncherProvider.AUTHORITY, mProvider); 41 mDb = mProvider.getDb(); 42 } 43 44 /** 45 * Adds a dummy item in the DB. 46 * @param type {@link #APP_ICON} or {@link #SHORTCUT} or >= 2 for 47 * folder (where the type represents the number of items in the folder). 48 */ addItem(int type, int screen, int container, int x, int y)49 public int addItem(int type, int screen, int container, int x, int y) { 50 int id = LauncherSettings.Settings.call(mContext.getContentResolver(), 51 LauncherSettings.Settings.METHOD_NEW_ITEM_ID) 52 .getInt(LauncherSettings.Settings.EXTRA_VALUE); 53 54 ContentValues values = new ContentValues(); 55 values.put(LauncherSettings.Favorites._ID, id); 56 values.put(LauncherSettings.Favorites.CONTAINER, container); 57 values.put(LauncherSettings.Favorites.SCREEN, screen); 58 values.put(LauncherSettings.Favorites.CELLX, x); 59 values.put(LauncherSettings.Favorites.CELLY, y); 60 values.put(LauncherSettings.Favorites.SPANX, 1); 61 values.put(LauncherSettings.Favorites.SPANY, 1); 62 63 if (type == APP_ICON || type == SHORTCUT) { 64 values.put(LauncherSettings.Favorites.ITEM_TYPE, type); 65 values.put(LauncherSettings.Favorites.INTENT, 66 new Intent(Intent.ACTION_MAIN).setPackage(TEST_PACKAGE).toUri(0)); 67 } else { 68 values.put(LauncherSettings.Favorites.ITEM_TYPE, 69 LauncherSettings.Favorites.ITEM_TYPE_FOLDER); 70 // Add folder items. 71 for (int i = 0; i < type; i++) { 72 addItem(APP_ICON, 0, id, 0, 0); 73 } 74 } 75 76 mContext.getContentResolver().insert(LauncherSettings.Favorites.CONTENT_URI, values); 77 return id; 78 } 79 createGrid(int[][][] typeArray)80 public int[][][] createGrid(int[][][] typeArray) { 81 return createGrid(typeArray, 1); 82 } 83 84 /** 85 * Initializes the DB with dummy elements to represent the provided grid structure. 86 * @param typeArray A 3d array of item types. {@see #addItem(int, long, long, int, int)} for 87 * type definitions. The first dimension represents the screens and the next 88 * two represent the workspace grid. 89 * @param startScreen First screen id from where the icons will be added. 90 * @return the same grid representation where each entry is the corresponding item id. 91 */ createGrid(int[][][] typeArray, int startScreen)92 public int[][][] createGrid(int[][][] typeArray, int startScreen) { 93 LauncherSettings.Settings.call(mContext.getContentResolver(), 94 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); 95 int[][][] ids = new int[typeArray.length][][]; 96 97 for (int i = 0; i < typeArray.length; i++) { 98 // Add screen to DB 99 int screenId = startScreen + i; 100 101 // Keep the screen id counter up to date 102 LauncherSettings.Settings.call(mContext.getContentResolver(), 103 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID); 104 105 ids[i] = new int[typeArray[i].length][]; 106 for (int y = 0; y < typeArray[i].length; y++) { 107 ids[i][y] = new int[typeArray[i][y].length]; 108 for (int x = 0; x < typeArray[i][y].length; x++) { 109 if (typeArray[i][y][x] < 0) { 110 // Empty cell 111 ids[i][y][x] = -1; 112 } else { 113 ids[i][y][x] = addItem(typeArray[i][y][x], screenId, DESKTOP, x, y); 114 } 115 } 116 } 117 } 118 119 return ids; 120 } 121 } 122