michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import android.content.ComponentName; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.content.ServiceConnection; michael@0: import android.os.IBinder; michael@0: import android.util.Log; michael@0: michael@0: /** michael@0: * Client for posting notifications through the NotificationService. michael@0: */ michael@0: public class ServiceNotificationClient extends NotificationClient { michael@0: private static final String LOGTAG = "GeckoServiceNotificationClient"; michael@0: michael@0: private final ServiceConnection mConnection = new NotificationServiceConnection(); michael@0: private boolean mBound; michael@0: private final Context mContext; michael@0: michael@0: public ServiceNotificationClient(Context context) { michael@0: mContext = context; michael@0: } michael@0: michael@0: @Override michael@0: protected void bind() { michael@0: super.bind(); michael@0: final Intent intent = new Intent(mContext, NotificationService.class); michael@0: mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); michael@0: } michael@0: michael@0: @Override michael@0: protected void unbind() { michael@0: // If there are no more tasks and no notifications being michael@0: // displayed, the service is disconnected. Unfortunately, michael@0: // since completed download notifications are shown by michael@0: // removing the progress notification and creating a new michael@0: // static one, this will cause the service to be unbound michael@0: // and immediately rebound when a download completes. michael@0: super.unbind(); michael@0: michael@0: if (mBound) { michael@0: mBound = false; michael@0: mContext.unbindService(mConnection); michael@0: } michael@0: } michael@0: michael@0: class NotificationServiceConnection implements ServiceConnection { michael@0: @Override michael@0: public void onServiceConnected(ComponentName className, IBinder service) { michael@0: final NotificationService.NotificationBinder binder = michael@0: (NotificationService.NotificationBinder) service; michael@0: connectHandler(binder.getService().getNotificationHandler()); michael@0: mBound = true; michael@0: } michael@0: michael@0: @Override michael@0: public void onServiceDisconnected(ComponentName componentName) { michael@0: // This is called when the connection with the service has been michael@0: // unexpectedly disconnected -- that is, its process crashed. michael@0: // Because it is running in our same process, we should never michael@0: // see this happen, and the correctness of this class relies on michael@0: // this never happening. michael@0: Log.e(LOGTAG, "Notification service disconnected", new Exception()); michael@0: } michael@0: } michael@0: }