summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/util/ProviderUtils.java
blob: c2aeecf29565fc7b3d64cb59fadf448d6ef780e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.ThemesContract;
import android.provider.ThemesContract.MixnMatchColumns;
import android.provider.ThemesContract.ThemesColumns;
import org.cyanogenmod.themes.provider.ThemesOpenHelper;

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) {
        if (context == null || pkgName == null) return ThemesColumns.InstallState.UNKNOWN;

        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;
    }

    public static String getCurrentThemeForComponent(Context context, String selection,
            String[] selectionArgs) {
        if (context == null || selection == null || selectionArgs == null) {
            return null;
        }

        String[] projection = new String[] {MixnMatchColumns.COL_VALUE};
        Cursor c = context.getContentResolver().query(MixnMatchColumns.CONTENT_URI,
                projection, selection, selectionArgs, null);

        String themePkgName = null;
        if (c != null) {
            if (c.moveToFirst()) {
                themePkgName = c.getString(c.getColumnIndex(MixnMatchColumns.COL_VALUE));
            }
            c.close();
        }
        return themePkgName;
    }

    /**
     * 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);
    }
}