1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/ServiceNotificationClient.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import android.content.ComponentName; 1.12 +import android.content.Context; 1.13 +import android.content.Intent; 1.14 +import android.content.ServiceConnection; 1.15 +import android.os.IBinder; 1.16 +import android.util.Log; 1.17 + 1.18 +/** 1.19 + * Client for posting notifications through the NotificationService. 1.20 + */ 1.21 +public class ServiceNotificationClient extends NotificationClient { 1.22 + private static final String LOGTAG = "GeckoServiceNotificationClient"; 1.23 + 1.24 + private final ServiceConnection mConnection = new NotificationServiceConnection(); 1.25 + private boolean mBound; 1.26 + private final Context mContext; 1.27 + 1.28 + public ServiceNotificationClient(Context context) { 1.29 + mContext = context; 1.30 + } 1.31 + 1.32 + @Override 1.33 + protected void bind() { 1.34 + super.bind(); 1.35 + final Intent intent = new Intent(mContext, NotificationService.class); 1.36 + mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 1.37 + } 1.38 + 1.39 + @Override 1.40 + protected void unbind() { 1.41 + // If there are no more tasks and no notifications being 1.42 + // displayed, the service is disconnected. Unfortunately, 1.43 + // since completed download notifications are shown by 1.44 + // removing the progress notification and creating a new 1.45 + // static one, this will cause the service to be unbound 1.46 + // and immediately rebound when a download completes. 1.47 + super.unbind(); 1.48 + 1.49 + if (mBound) { 1.50 + mBound = false; 1.51 + mContext.unbindService(mConnection); 1.52 + } 1.53 + } 1.54 + 1.55 + class NotificationServiceConnection implements ServiceConnection { 1.56 + @Override 1.57 + public void onServiceConnected(ComponentName className, IBinder service) { 1.58 + final NotificationService.NotificationBinder binder = 1.59 + (NotificationService.NotificationBinder) service; 1.60 + connectHandler(binder.getService().getNotificationHandler()); 1.61 + mBound = true; 1.62 + } 1.63 + 1.64 + @Override 1.65 + public void onServiceDisconnected(ComponentName componentName) { 1.66 + // This is called when the connection with the service has been 1.67 + // unexpectedly disconnected -- that is, its process crashed. 1.68 + // Because it is running in our same process, we should never 1.69 + // see this happen, and the correctness of this class relies on 1.70 + // this never happening. 1.71 + Log.e(LOGTAG, "Notification service disconnected", new Exception()); 1.72 + } 1.73 + } 1.74 +}