|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import android.content.ComponentName; |
|
9 import android.content.Context; |
|
10 import android.content.Intent; |
|
11 import android.content.ServiceConnection; |
|
12 import android.os.IBinder; |
|
13 import android.util.Log; |
|
14 |
|
15 /** |
|
16 * Client for posting notifications through the NotificationService. |
|
17 */ |
|
18 public class ServiceNotificationClient extends NotificationClient { |
|
19 private static final String LOGTAG = "GeckoServiceNotificationClient"; |
|
20 |
|
21 private final ServiceConnection mConnection = new NotificationServiceConnection(); |
|
22 private boolean mBound; |
|
23 private final Context mContext; |
|
24 |
|
25 public ServiceNotificationClient(Context context) { |
|
26 mContext = context; |
|
27 } |
|
28 |
|
29 @Override |
|
30 protected void bind() { |
|
31 super.bind(); |
|
32 final Intent intent = new Intent(mContext, NotificationService.class); |
|
33 mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
|
34 } |
|
35 |
|
36 @Override |
|
37 protected void unbind() { |
|
38 // If there are no more tasks and no notifications being |
|
39 // displayed, the service is disconnected. Unfortunately, |
|
40 // since completed download notifications are shown by |
|
41 // removing the progress notification and creating a new |
|
42 // static one, this will cause the service to be unbound |
|
43 // and immediately rebound when a download completes. |
|
44 super.unbind(); |
|
45 |
|
46 if (mBound) { |
|
47 mBound = false; |
|
48 mContext.unbindService(mConnection); |
|
49 } |
|
50 } |
|
51 |
|
52 class NotificationServiceConnection implements ServiceConnection { |
|
53 @Override |
|
54 public void onServiceConnected(ComponentName className, IBinder service) { |
|
55 final NotificationService.NotificationBinder binder = |
|
56 (NotificationService.NotificationBinder) service; |
|
57 connectHandler(binder.getService().getNotificationHandler()); |
|
58 mBound = true; |
|
59 } |
|
60 |
|
61 @Override |
|
62 public void onServiceDisconnected(ComponentName componentName) { |
|
63 // This is called when the connection with the service has been |
|
64 // unexpectedly disconnected -- that is, its process crashed. |
|
65 // Because it is running in our same process, we should never |
|
66 // see this happen, and the correctness of this class relies on |
|
67 // this never happening. |
|
68 Log.e(LOGTAG, "Notification service disconnected", new Exception()); |
|
69 } |
|
70 } |
|
71 } |