1 package com.android.launcher3.graphics; 2 3 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; 4 5 import android.content.ContentProvider; 6 import android.content.ContentValues; 7 import android.content.res.XmlResourceParser; 8 import android.database.Cursor; 9 import android.database.MatrixCursor; 10 import android.graphics.Bitmap; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.os.ParcelFileDescriptor; 14 import android.os.ParcelFileDescriptor.AutoCloseOutputStream; 15 import android.text.TextUtils; 16 import android.util.Log; 17 import android.util.Xml; 18 19 import com.android.launcher3.InvariantDeviceProfile; 20 import com.android.launcher3.InvariantDeviceProfile.GridOption; 21 import com.android.launcher3.R; 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 26 import java.io.FileNotFoundException; 27 import java.io.IOException; 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.concurrent.Future; 32 33 /** 34 * Exposes various launcher grid options and allows the caller to change them. 35 * APIs: 36 * /list_options: List the various available grip options, has following columns 37 * name: name of the grid 38 * rows: number of rows in the grid 39 * cols: number of columns in the grid 40 * preview_count: number of previews available for this grid option. The preview uri 41 * looks like /preview/<grid-name>/<preview index starting with 0> 42 * is_default: true if this grid is currently active 43 * 44 * /preview: Opens a file stream for the grid preview 45 * 46 * /default_grid: Call update to set the current grid, with values 47 * name: name of the grid to apply 48 */ 49 public class GridOptionsProvider extends ContentProvider { 50 51 private static final String TAG = "GridOptionsProvider"; 52 53 private static final String KEY_NAME = "name"; 54 private static final String KEY_ROWS = "rows"; 55 private static final String KEY_COLS = "cols"; 56 private static final String KEY_PREVIEW_COUNT = "preview_count"; 57 private static final String KEY_IS_DEFAULT = "is_default"; 58 59 private static final String KEY_LIST_OPTIONS = "/list_options"; 60 private static final String KEY_DEFAULT_GRID = "/default_grid"; 61 62 private static final String KEY_PREVIEW = "preview"; 63 private static final String MIME_TYPE_PNG = "image/png"; 64 65 public static final PipeDataWriter<Future<Bitmap>> BITMAP_WRITER = 66 new PipeDataWriter<Future<Bitmap>>() { 67 @Override 68 public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String s, 69 Bundle bundle, Future<Bitmap> bitmap) { 70 try (AutoCloseOutputStream os = new AutoCloseOutputStream(output)) { 71 bitmap.get().compress(Bitmap.CompressFormat.PNG, 100, os); 72 } catch (Exception e) { 73 Log.w(TAG, "fail to write to pipe", e); 74 } 75 } 76 }; 77 78 @Override onCreate()79 public boolean onCreate() { 80 return true; 81 } 82 83 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)84 public Cursor query(Uri uri, String[] projection, String selection, 85 String[] selectionArgs, String sortOrder) { 86 if (!KEY_LIST_OPTIONS.equals(uri.getPath())) { 87 return null; 88 } 89 MatrixCursor cursor = new MatrixCursor(new String[] { 90 KEY_NAME, KEY_ROWS, KEY_COLS, KEY_PREVIEW_COUNT, KEY_IS_DEFAULT}); 91 InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext()); 92 for (GridOption gridOption : parseAllGridOptions()) { 93 cursor.newRow() 94 .add(KEY_NAME, gridOption.name) 95 .add(KEY_ROWS, gridOption.numRows) 96 .add(KEY_COLS, gridOption.numColumns) 97 .add(KEY_PREVIEW_COUNT, 1) 98 .add(KEY_IS_DEFAULT, idp.numColumns == gridOption.numColumns 99 && idp.numRows == gridOption.numRows); 100 } 101 return cursor; 102 } 103 parseAllGridOptions()104 private List<GridOption> parseAllGridOptions() { 105 List<GridOption> result = new ArrayList<>(); 106 try (XmlResourceParser parser = getContext().getResources().getXml(R.xml.device_profiles)) { 107 final int depth = parser.getDepth(); 108 int type; 109 while (((type = parser.next()) != XmlPullParser.END_TAG || 110 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 111 if ((type == XmlPullParser.START_TAG) 112 && GridOption.TAG_NAME.equals(parser.getName())) { 113 result.add(new GridOption(getContext(), Xml.asAttributeSet(parser))); 114 } 115 } 116 } catch (IOException | XmlPullParserException e) { 117 Log.e(TAG, "Error parsing device profile", e); 118 return Collections.emptyList(); 119 } 120 return result; 121 } 122 123 @Override getType(Uri uri)124 public String getType(Uri uri) { 125 List<String> segments = uri.getPathSegments(); 126 if (segments.size() > 0 && KEY_PREVIEW.equals(segments.get(0))) { 127 return MIME_TYPE_PNG; 128 } 129 return "vnd.android.cursor.dir/launcher_grid"; 130 } 131 132 @Override insert(Uri uri, ContentValues initialValues)133 public Uri insert(Uri uri, ContentValues initialValues) { 134 return null; 135 } 136 137 @Override delete(Uri uri, String selection, String[] selectionArgs)138 public int delete(Uri uri, String selection, String[] selectionArgs) { 139 return 0; 140 } 141 142 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)143 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 144 if (!KEY_DEFAULT_GRID.equals(uri.getPath())) { 145 return 0; 146 } 147 148 String gridName = values.getAsString(KEY_NAME); 149 // Verify that this is a valid grid option 150 GridOption match = null; 151 for (GridOption option : parseAllGridOptions()) { 152 if (option.name.equals(gridName)) { 153 match = option; 154 break; 155 } 156 } 157 if (match == null) { 158 return 0; 159 } 160 161 InvariantDeviceProfile.INSTANCE.get(getContext()).setCurrentGrid(getContext(), gridName); 162 return 1; 163 } 164 165 @Override openFile(Uri uri, String mode)166 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 167 List<String> segments = uri.getPathSegments(); 168 if (segments.size() < 2 || !KEY_PREVIEW.equals(segments.get(0))) { 169 throw new FileNotFoundException("Invalid preview url"); 170 } 171 String profileName = segments.get(1); 172 if (TextUtils.isEmpty(profileName)) { 173 throw new FileNotFoundException("Invalid preview url"); 174 } 175 176 InvariantDeviceProfile idp; 177 try { 178 idp = new InvariantDeviceProfile(getContext(), profileName); 179 } catch (Exception e) { 180 throw new FileNotFoundException(e.getMessage()); 181 } 182 183 try { 184 return openPipeHelper(uri, MIME_TYPE_PNG, null, 185 UI_HELPER_EXECUTOR.submit(new LauncherPreviewRenderer(getContext(), idp)), 186 BITMAP_WRITER); 187 } catch (Exception e) { 188 throw new FileNotFoundException(e.getMessage()); 189 } 190 } 191 } 192