From d56651ee27f618762440fa0efe5b16c137814854 Mon Sep 17 00:00:00 2001 From: d34d Date: Mon, 2 Mar 2015 15:31:05 -0800 Subject: Themes: Update ThemesContract with install state [2/3] This provider will be responsible for keeping track of the installed state of themes. This includes two intermedite states, INSTALLING and UPDATING. When a theme is installed from PackageManager the provider will receive this broadcast and add the theme to its DB and set the state to INSTALLING if the ThemeService is processing the theme, or INSTALLED if not and broadcast the ACTION_THEME_INSTALLED action. When a theme is updated the provider will receive another broadcast indicating that the current version of the theme is being replaced. If the ThemeService needs to do some processing, we'll update the state to UPDATING. Once the theme service is done processing a theme, the provider will receive a broadcast from the ThemeService. At this point, the themes provider will update the state to INSTALLED, if it is not already set to that state. The provider will broadcast the ACTION_THEME_INSTALLED action if the previous state was INSTALLING and the ACTION_THEME_UPDATED action if the previous state was UPDATING. Change-Id: Ie37f85463e472d96b8393801007537db1e3eefc6 --- .../themes/provider/util/ProviderUtils.java | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/org/cyanogenmod/themes/provider/util/ProviderUtils.java (limited to 'src/org/cyanogenmod/themes/provider/util/ProviderUtils.java') diff --git a/src/org/cyanogenmod/themes/provider/util/ProviderUtils.java b/src/org/cyanogenmod/themes/provider/util/ProviderUtils.java new file mode 100644 index 0000000..a07d0ba --- /dev/null +++ b/src/org/cyanogenmod/themes/provider/util/ProviderUtils.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.cyanogenmod.themes.provider.util; + +import android.Manifest; +import android.content.Context; +import android.content.Intent; +import android.content.res.ThemeManager; +import android.database.Cursor; +import android.net.Uri; +import android.provider.ThemesContract; +import android.provider.ThemesContract.ThemesColumns; + +public class ProviderUtils { + /** + * Convenience method for determining if a theme exists in the provider + * @param context + * @param pkgName + * @return True if the theme exists, false otherwise + */ + public static boolean themeExistsInProvider(Context context, String pkgName) { + boolean exists = false; + String[] projection = new String[] { ThemesColumns.PKG_NAME }; + String selection = ThemesColumns.PKG_NAME + "=?"; + String[] selectionArgs = new String[] { pkgName }; + Cursor c = context.getContentResolver().query(ThemesColumns.CONTENT_URI, + projection, selection, selectionArgs, null); + + if (c != null) { + exists = c.getCount() >= 1; + c.close(); + } + return exists; + } + + /** + * Queries the {@link android.content.res.ThemeManager} to check if the theme is currently + * being processed by {@link com.android.server.ThemeService} + * @param context + * @param pkgName + * @return True if the theme is being processed or queued up for processing + */ + public static boolean isThemeBeingProcessed(Context context, String pkgName) { + ThemeManager tm = (ThemeManager) context.getSystemService(Context.THEME_SERVICE); + return tm.isThemeBeingProcessed(pkgName); + } + + /** + * Convenience method for getting the install state of a theme in the provider + * @param context + * @param pkgName + * @return + */ + public static int getInstallStateForTheme(Context context, String pkgName) { + String[] projection = new String[] { ThemesColumns.INSTALL_STATE }; + String selection = ThemesColumns.PKG_NAME + "=?"; + String[] selectionArgs = new String[] { pkgName }; + Cursor c = context.getContentResolver().query(ThemesColumns.CONTENT_URI, + projection, selection, selectionArgs, null); + + int state = ThemesColumns.InstallState.UNKNOWN; + if (c != null) { + if (c.moveToFirst()) { + state = c.getInt(c.getColumnIndex(ThemesColumns.INSTALL_STATE)); + } + c.close(); + } + return state; + } + + /** + * Sends the {@link android.provider.ThemesContract.Intent#ACTION_THEME_INSTALLED} action + * @param context + * @param pkgName + */ + public static void sendThemeInstalledBroadcast(Context context, String pkgName) { + Intent intent = new Intent(ThemesContract.Intent.ACTION_THEME_INSTALLED, + Uri.fromParts(ThemesContract.Intent.URI_SCHEME_PACKAGE, pkgName, null)); + context.sendBroadcast(intent, Manifest.permission.READ_THEMES); + } + + /** + * Sends the {@link android.provider.ThemesContract.Intent#ACTION_THEME_UPDATED} action + * @param context + * @param pkgName + */ + public static void sendThemeUpdatedBroadcast(Context context, String pkgName) { + Intent intent = new Intent(ThemesContract.Intent.ACTION_THEME_UPDATED, + Uri.fromParts(ThemesContract.Intent.URI_SCHEME_PACKAGE, pkgName, null)); + context.sendBroadcast(intent, Manifest.permission.READ_THEMES); + } + + /** + * Sends the {@link android.provider.ThemesContract.Intent#ACTION_THEME_REMOVED} action + * @param context + * @param pkgName + */ + public static void sendThemeRemovedBroadcast(Context context, String pkgName) { + Intent intent = new Intent(ThemesContract.Intent.ACTION_THEME_REMOVED, + Uri.fromParts(ThemesContract.Intent.URI_SCHEME_PACKAGE, pkgName, null)); + context.sendBroadcast(intent, Manifest.permission.READ_THEMES); + } +} -- cgit v1.2.3