mobile/android/base/ServiceNotificationClient.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial