ListViewTest.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.gadget;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.IBinder;
22import android.os.Looper;
23import android.os.Message;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.widget.RemoteViews;
27
28import java.util.ArrayList;
29import java.util.HashMap;
30import java.util.List;
31
32import com.android.internal.gadget.IGadgetHost;
33import com.android.internal.gadget.IGadgetService;
34
35/**
36 * GadgetHost provides the interaction with the Gadget Service for apps,
37 * like the home screen, that want to embed gadgets in their UI.
38 */
39public class GadgetHost {
40
41    static final int HANDLE_UPDATE = 1;
42    static final int HANDLE_PROVIDER_CHANGED = 2;
43
44    static Object sServiceLock = new Object();
45    static IGadgetService sService;
46
47    Context mContext;
48    String mPackageName;
49
50    class Callbacks extends IGadgetHost.Stub {
51        public void updateGadget(int gadgetId, RemoteViews views) {
52            Message msg = mHandler.obtainMessage(HANDLE_UPDATE);
53            msg.arg1 = gadgetId;
54            msg.obj = views;
55            msg.sendToTarget();
56        }
57
58        public void providerChanged(int gadgetId, GadgetProviderInfo info) {
59            Message msg = mHandler.obtainMessage(HANDLE_PROVIDER_CHANGED);
60            msg.arg1 = gadgetId;
61            msg.obj = info;
62            msg.sendToTarget();
63        }
64    }
65
66    class UpdateHandler extends Handler {
67        public UpdateHandler(Looper looper) {
68            super(looper);
69        }
70
71        public void handleMessage(Message msg) {
72            switch (msg.what) {
73                case HANDLE_UPDATE: {
74                    updateGadgetView(msg.arg1, (RemoteViews)msg.obj);
75                    break;
76                }
77                case HANDLE_PROVIDER_CHANGED: {
78                    onProviderChanged(msg.arg1, (GadgetProviderInfo)msg.obj);
79                    break;
80                }
81            }
82        }
83    }
84
85    Handler mHandler;
86
87    int mHostId;
88    Callbacks mCallbacks = new Callbacks();
89    HashMap<Integer,GadgetHostView> mViews = new HashMap();
90
91    public GadgetHost(Context context, int hostId) {
92        mContext = context;
93        mHostId = hostId;
94        mHandler = new UpdateHandler(context.getMainLooper());
95        synchronized (sServiceLock) {
96            if (sService == null) {
97                IBinder b = ServiceManager.getService(Context.GADGET_SERVICE);
98                sService = IGadgetService.Stub.asInterface(b);
99            }
100        }
101    }
102
103    /**
104     * Start receiving onGadgetChanged calls for your gadgets.  Call this when your activity
105     * becomes visible, i.e. from onStart() in your Activity.
106     */
107    public void startListening() {
108        int[] updatedIds = null;
109        ArrayList<RemoteViews> updatedViews = new ArrayList();
110
111        try {
112            if (mPackageName == null) {
113                mPackageName = mContext.getPackageName();
114            }
115            updatedIds = sService.startListening(mCallbacks, mPackageName, mHostId, updatedViews);
116        }
117        catch (RemoteException e) {
118            throw new RuntimeException("system server dead?", e);
119        }
120
121        final int N = updatedIds.length;
122        for (int i=0; i<N; i++) {
123            updateGadgetView(updatedIds[i], updatedViews.get(i));
124        }
125    }
126
127    /**
128     * Stop receiving onGadgetChanged calls for your gadgets.  Call this when your activity is
129     * no longer visible, i.e. from onStop() in your Activity.
130     */
131    public void stopListening() {
132        try {
133            sService.stopListening(mHostId);
134        }
135        catch (RemoteException e) {
136            throw new RuntimeException("system server dead?", e);
137        }
138    }
139
140    /**
141     * Get a gadgetId for a host in the calling process.
142     *
143     * @return a gadgetId
144     */
145    public int allocateGadgetId() {
146        try {
147            if (mPackageName == null) {
148                mPackageName = mContext.getPackageName();
149            }
150            return sService.allocateGadgetId(mPackageName, mHostId);
151        }
152        catch (RemoteException e) {
153            throw new RuntimeException("system server dead?", e);
154        }
155    }
156
157    /**
158     * Stop listening to changes for this gadget.
159     */
160    public void deleteGadgetId(int gadgetId) {
161        synchronized (mViews) {
162            mViews.remove(gadgetId);
163            try {
164                sService.deleteGadgetId(gadgetId);
165            }
166            catch (RemoteException e) {
167                throw new RuntimeException("system server dead?", e);
168            }
169        }
170    }
171
172    /**
173     * Remove all records about this host from the gadget manager.
174     * <ul>
175     *   <li>Call this when initializing your database, as it might be because of a data wipe.</li>
176     *   <li>Call this to have the gadget manager release all resources associated with your
177     *   host.  Any future calls about this host will cause the records to be re-allocated.</li>
178     * </ul>
179     */
180    public void deleteHost() {
181        try {
182            sService.deleteHost(mHostId);
183        }
184        catch (RemoteException e) {
185            throw new RuntimeException("system server dead?", e);
186        }
187    }
188
189    /**
190     * Remove all records about all hosts for your package.
191     * <ul>
192     *   <li>Call this when initializing your database, as it might be because of a data wipe.</li>
193     *   <li>Call this to have the gadget manager release all resources associated with your
194     *   host.  Any future calls about this host will cause the records to be re-allocated.</li>
195     * </ul>
196     */
197    public static void deleteAllHosts() {
198        try {
199            sService.deleteAllHosts();
200        }
201        catch (RemoteException e) {
202            throw new RuntimeException("system server dead?", e);
203        }
204    }
205
206    public final GadgetHostView createView(Context context, int gadgetId,
207            GadgetProviderInfo gadget) {
208        GadgetHostView view = onCreateView(context, gadgetId, gadget);
209        view.setGadget(gadgetId, gadget);
210        synchronized (mViews) {
211            mViews.put(gadgetId, view);
212        }
213        RemoteViews views = null;
214        try {
215            views = sService.getGadgetViews(gadgetId);
216        } catch (RemoteException e) {
217            throw new RuntimeException("system server dead?", e);
218        }
219        view.updateGadget(views);
220        return view;
221    }
222
223    /**
224     * Called to create the GadgetHostView.  Override to return a custom subclass if you
225     * need it.  {@more}
226     */
227    protected GadgetHostView onCreateView(Context context, int gadgetId,
228            GadgetProviderInfo gadget) {
229        return new GadgetHostView(context);
230    }
231
232    /**
233     * Called when the gadget provider for a gadget has been upgraded to a new apk.
234     */
235    protected void onProviderChanged(int gadgetId, GadgetProviderInfo gadget) {
236    }
237
238    void updateGadgetView(int gadgetId, RemoteViews views) {
239        GadgetHostView v;
240        synchronized (mViews) {
241            v = mViews.get(gadgetId);
242        }
243        if (v != null) {
244            v.updateGadget(views);
245        }
246    }
247}
248
249
250