aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/app/CMStatusBarManager.java
blob: ecd310612c26fc19d3ec28dc4ea253fc11f50501 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
 * 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 cyanogenmod.app;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;
import android.util.Slog;

import cyanogenmod.app.ICMStatusBarManager;

/**
 * The CMStatusBarManager allows you to publish and remove CustomTiles within the
 * Quick Settings Panel.
 *
 * <p>
 * Each of the publish methods takes an int id parameter and optionally a
 * {@link String} tag parameter, which may be {@code null}.  These parameters
 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
 * unspecified.  This pair identifies this custom tile from your app to the
 * system, so that pair should be unique within your app.  If you call one
 * of the publish methods with a (tag, id) pair that is currently active and
 * a new set of custom tile parameters, it will be updated.  For example,
 * if you pass a new custom tile icon, the old icon in the panel will
 * be replaced with the new one.  This is also the same tag and id you pass
 * to the {@link #removeTile(int)} or {@link #removeTile(String, int)} method to clear
 * this custom tile.
 *
 * <p>
 * To get the instance of this class, utilize CMStatusBarManager#getInstance(Context context)
 *
 * @see cyanogenmod.app.CustomTile
 */
public class CMStatusBarManager {
    private static final String TAG = "CMStatusBarManager";
    private static boolean localLOGV = false;

    private Context mContext;

    private static ICMStatusBarManager sService;

    private static CMStatusBarManager sCMStatusBarManagerInstance;
    private CMStatusBarManager(Context context) {
        Context appContext = context.getApplicationContext();
        if (appContext != null) {
            mContext = appContext;
        } else {
            mContext = context;
        }
        sService = getService();

        if (context.getPackageManager().hasSystemFeature(
                cyanogenmod.app.CMContextConstants.Features.STATUSBAR) && sService == null) {
            Log.wtf(TAG, "Unable to get CMStatusBarService. The service either" +
                    " crashed, was not started, or the interface has been called to early in" +
                    " SystemServer init");
        }
    }

    /**
     * Get or create an instance of the {@link cyanogenmod.app.CMStatusBarManager}
     * @param context
     * @return {@link cyanogenmod.app.CMStatusBarManager}
     */
    public static CMStatusBarManager getInstance(Context context) {
        if (sCMStatusBarManagerInstance == null) {
            sCMStatusBarManagerInstance = new CMStatusBarManager(context);
        }
        return sCMStatusBarManagerInstance;
    }

    /**
     * Post a custom tile to be shown in the status bar panel. If a custom tile with
     * the same id has already been posted by your application and has not yet been removed, it
     * will be replaced by the updated information.
     *
     * You will need the cyanogenmod.permission.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param id An identifier for this customTile unique within your
     *        application.
     * @param customTile A {@link CustomTile} object describing what to show the user.
     *                   Must not be null.
     */
    public void publishTile(int id, CustomTile customTile) {
        publishTile(null, id, customTile);
    }

    /**
     * Post a custom tile to be shown in the status bar panel. If a custom tile with
     * the same tag and id has already been posted by your application and has not yet been
     * removed, it will be replaced by the updated information.
     *
     * You will need the cyanogenmod.permission.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param tag A string identifier for this custom tile.  May be {@code null}.
     * @param id An identifier for this custom tile.  The pair (tag, id) must be unique
     *        within your application.
     * @param customTile A {@link cyanogenmod.app.CustomTile} object describing what to
     *        show the user. Must not be null.
     */
    public void publishTile(String tag, int id, CustomTile customTile) {
        if (sService == null) {
            Log.w(TAG, "not connected to CMStatusBarManagerService");
            return;
        }

        int[] idOut = new int[1];
        String pkg = mContext.getPackageName();
        if (localLOGV) Log.v(TAG, pkg + ": create(" + id + ", " + customTile + ")");
        try {
            sService.createCustomTileWithTag(pkg, mContext.getOpPackageName(), tag, id,
                    customTile, idOut, UserHandle.myUserId());
            if (id != idOut[0]) {
                Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
            }
        } catch (RemoteException e) {
            Slog.w("CMStatusBarManager", "warning: no cm status bar service");
        }
    }

    /**
     * Similar to {@link cyanogenmod.app.CMStatusBarManager#publishTile(int id, cyanogenmod.app.CustomTile)},
     * however lets you specify a {@link android.os.UserHandle}
     *
     * You will need the cyanogenmod.permission.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param tag A string identifier for this custom tile.  May be {@code null}.
     * @param id An identifier for this custom tile.  The pair (tag, id) must be unique
     *        within your application.
     * @param customTile A {@link cyanogenmod.app.CustomTile} object describing what to
     *        show the user. Must not be null.
     * @param user A user handle to publish the tile as.
     */
    public void publishTileAsUser(String tag, int id, CustomTile customTile, UserHandle user) {
        if (sService == null) {
            Log.w(TAG, "not connected to CMStatusBarManagerService");
            return;
        }

        int[] idOut = new int[1];
        String pkg = mContext.getPackageName();
        if (localLOGV) Log.v(TAG, pkg + ": create(" + id + ", " + customTile + ")");
        try {
            sService.createCustomTileWithTag(pkg, mContext.getOpPackageName(), tag, id,
                    customTile, idOut, user.getIdentifier());
            if (id != idOut[0]) {
                Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
            }
        } catch (RemoteException e) {
            Slog.w("CMStatusBarManager", "warning: no cm status bar service");
        }
    }

    /**
     * Remove a custom tile that's currently published to the StatusBarPanel.
     *
     * You will need the cyanogenmod.permission.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param id The identifier for the custom tile to be removed.
     */
    public void removeTile(int id) {
        removeTile(null, id);
    }

    /**
     * Remove a custom tile that's currently published to the StatusBarPanel.
     *
     * You will need the cyanogenmod.platform.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param tag The string identifier for the custom tile to be removed.
     * @param id The identifier for the custom tile to be removed.
     */
    public void removeTile(String tag, int id) {
        if (sService == null) {
            Log.w(TAG, "not connected to CMStatusBarManagerService");
            return;
        }

        String pkg = mContext.getPackageName();
        if (localLOGV) Log.v(TAG, pkg + ": remove(" + id + ")");
        try {
            sService.removeCustomTileWithTag(pkg, tag, id, UserHandle.myUserId());
        } catch (RemoteException e) {
            Slog.w("CMStatusBarManager", "warning: no cm status bar service");
        }
    }

    /**
     * Similar to {@link cyanogenmod.app.CMStatusBarManager#removeTile(String tag, int id)}
     * however lets you specific a {@link android.os.UserHandle}
     *
     * You will need the cyanogenmod.platform.PUBLISH_CUSTOM_TILE
     * to utilize this functionality.
     *
     * @param tag The string identifier for the custom tile to be removed.
     * @param id The identifier for the custom tile to be removed.
     * @param user The user handle to remove the tile from.
     */
    public void removeTileAsUser(String tag, int id, UserHandle user) {
        if (sService == null) {
            Log.w(TAG, "not connected to CMStatusBarManagerService");
            return;
        }

        String pkg = mContext.getPackageName();
        if (localLOGV) Log.v(TAG, pkg + ": remove(" + id + ")");
        try {
            sService.removeCustomTileWithTag(pkg, tag, id, user.getIdentifier());
        } catch (RemoteException e) {
        }
    }

    /** @hide */
    public ICMStatusBarManager getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService(CMContextConstants.CM_STATUS_BAR_SERVICE);
        if (b != null) {
            sService = ICMStatusBarManager.Stub.asInterface(b);
            return sService;
        }
        return null;
    }
}