From c16990420bc1b6377edc4523d80fc71edf32d55d Mon Sep 17 00:00:00 2001 From: Richard MacGregor Date: Mon, 4 May 2015 17:07:52 -0700 Subject: [2/3] Improve PreviewsColumns Implementation Previews columns now uses a key/value pair structure to allow for easier future support of multiple components per theme pack. Depends on: http://review.cyanogenmod.org/#/c/96836/ Change-Id: I37a44558d3c52f8fe910b4773495ee1fac71fbbd --- .../themes/provider/ThemesProvider.java | 70 +++++++++++++--------- 1 file changed, 42 insertions(+), 28 deletions(-) (limited to 'src/org/cyanogenmod/themes/provider/ThemesProvider.java') diff --git a/src/org/cyanogenmod/themes/provider/ThemesProvider.java b/src/org/cyanogenmod/themes/provider/ThemesProvider.java index 2d57f73..c9eecce 100644 --- a/src/org/cyanogenmod/themes/provider/ThemesProvider.java +++ b/src/org/cyanogenmod/themes/provider/ThemesProvider.java @@ -40,7 +40,6 @@ import android.provider.ThemesContract; import android.provider.ThemesContract.MixnMatchColumns; import android.provider.ThemesContract.PreviewColumns; import android.provider.ThemesContract.ThemesColumns; -import android.text.TextUtils; import android.util.Log; import org.cyanogenmod.themes.provider.ThemesOpenHelper.MixnMatchTable; @@ -67,6 +66,7 @@ public class ThemesProvider extends ContentProvider { private static final int PREVIEWS = 5; private static final int PREVIEWS_ID = 6; private static final int APPLIED_PREVIEWS = 7; + private static final int COMPONENTS_PREVIEWS = 8; private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); @@ -83,6 +83,7 @@ public class ThemesProvider extends ContentProvider { sUriMatcher.addURI(ThemesContract.AUTHORITY, "previews/", PREVIEWS); sUriMatcher.addURI(ThemesContract.AUTHORITY, "previews/#", PREVIEWS_ID); sUriMatcher.addURI(ThemesContract.AUTHORITY, "applied_previews/", APPLIED_PREVIEWS); + sUriMatcher.addURI(ThemesContract.AUTHORITY, "components_previews/", COMPONENTS_PREVIEWS); } public static void setActiveTheme(Context context, String pkgName) { @@ -203,7 +204,7 @@ public class ThemesProvider extends ContentProvider { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); SQLiteDatabase db = mDatabase.getReadableDatabase(); - + String groupBy = null; /* * Choose the table to query and a sort order based on the code returned for the incoming * URI. Here, too, only the statements for table 3 are shown. @@ -223,6 +224,10 @@ public class ThemesProvider extends ContentProvider { queryBuilder.setTables(THEMES_MIXNMATCH_INNER_JOIN); queryBuilder.appendWhere(MixnMatchColumns.COL_KEY + "=" + uri.getLastPathSegment()); break; + case COMPONENTS_PREVIEWS: + // COMPONENT_PREVIEWS has the same query behavior as PREVIEWS, however, it requires a + // groupBy parameter to be defined before doing so. Let it fall through after this. + groupBy = PreviewColumns.THEME_ID; case PREVIEWS: queryBuilder.setTables(THEMES_PREVIEWS_INNER_JOIN); break; @@ -236,7 +241,7 @@ public class ThemesProvider extends ContentProvider { return null; } - Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, + Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, groupBy, null, sortOrder); if (cursor != null) { cursor.setNotificationUri(getContext().getContentResolver(), uri); @@ -331,36 +336,45 @@ public class ThemesProvider extends ContentProvider { } if (id >= 0) { if (ThemesColumns.MODIFIES_STATUS_BAR.equals(component)) { - sb.append(delimeter).append("(SELECT "); - sb.append(TextUtils.join(",", - PreviewsTable.STATUS_BAR_PREVIEW_COLUMNS)); - sb.append(String.format(" FROM previews WHERE %s=%d)", - PreviewColumns.THEME_ID, id)); - delimeter = ","; + for (String previewKey : PreviewsTable.STATUS_BAR_PREVIEW_KEYS) { + sb.append(delimeter).append(String.format( + "(SELECT %s AS %s FROM previews WHERE %s=%d AND %s='%s')", + PreviewColumns.COL_VALUE, previewKey, + PreviewColumns.THEME_ID, id, PreviewColumns.COL_KEY, + previewKey)); + delimeter = ","; + } } else if (ThemesColumns.MODIFIES_ICONS.equals(component)) { - sb.append(delimeter).append("(SELECT "); - sb.append(TextUtils.join(",", PreviewsTable.ICON_PREVIEW_COLUMNS)); - sb.append(String.format(" FROM previews WHERE %s=%d)", - PreviewColumns.THEME_ID, id)); - delimeter = ","; + for (String previewKey : PreviewsTable.ICON_PREVIEW_KEYS) { + sb.append(delimeter).append(String.format( + "(SELECT %s AS %s FROM previews WHERE %s=%d AND %s='%s')", + PreviewColumns.COL_VALUE, previewKey, + PreviewColumns.THEME_ID, id, PreviewColumns.COL_KEY, + previewKey)); + delimeter = ","; + } } else if (ThemesColumns.MODIFIES_LAUNCHER.equals(component)) { - sb.append(delimeter).append("(SELECT "); - sb.append(String.format("%s", PreviewColumns.WALLPAPER_PREVIEW)); - sb.append(String.format(" FROM previews WHERE %s=%d)", - PreviewColumns.THEME_ID, id)); + String previewKey = PreviewColumns.KEY_WALLPAPER_PREVIEW; + sb.append(delimeter).append(String.format( + "(SELECT %s AS %s FROM previews WHERE %s=%d AND %s='%s')", + PreviewColumns.COL_VALUE, previewKey, PreviewColumns.THEME_ID, + id, PreviewColumns.COL_KEY, previewKey)); delimeter = ","; } else if (ThemesColumns.MODIFIES_NAVIGATION_BAR.equals(component)) { - sb.append(delimeter).append("(SELECT "); - sb.append(TextUtils.join(",", - PreviewsTable.NAVIGATION_BAR_PREVIEW_COLUMNS)); - sb.append(String.format(" FROM previews WHERE %s=%d)", - PreviewColumns.THEME_ID, id)); - delimeter = ","; + for (String previewKey : PreviewsTable.NAVIGATION_BAR_PREVIEW_KEYS) { + sb.append(delimeter).append(String.format( + "(SELECT %s AS %s FROM previews WHERE %s=%d AND %s='%s')", + PreviewColumns.COL_VALUE, previewKey, + PreviewColumns.THEME_ID, id, PreviewColumns.COL_KEY, + previewKey)); + delimeter = ","; + } } else if (ThemesColumns.MODIFIES_OVERLAYS.equals(component)) { - sb.append(delimeter).append("(SELECT "); - sb.append(PreviewColumns.STYLE_PREVIEW); - sb.append(String.format(" FROM previews WHERE %s=%d)", - PreviewColumns.THEME_ID, id)); + String previewKey = PreviewColumns.KEY_STYLE_PREVIEW; + sb.append(delimeter).append(String.format( + "(SELECT %s AS %s FROM previews WHERE %s=%d AND %s='%s')", + PreviewColumns.COL_VALUE, previewKey, PreviewColumns.THEME_ID, + id, PreviewColumns.COL_KEY, previewKey)); delimeter = ","; } } -- cgit v1.2.3