View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.android;
17  
18  import android.app.Service;
19  import android.content.Context;
20  import android.content.Intent;
21  import android.os.IBinder;
22  import org.fourthline.cling.UpnpService;
23  import org.fourthline.cling.UpnpServiceConfiguration;
24  import org.fourthline.cling.UpnpServiceImpl;
25  import org.fourthline.cling.controlpoint.ControlPoint;
26  import org.fourthline.cling.protocol.ProtocolFactory;
27  import org.fourthline.cling.registry.Registry;
28  import org.fourthline.cling.transport.Router;
29  
30  /**
31   * Provides a UPnP stack with Android configuration as an application service component.
32   * <p>
33   * Sends a search for all UPnP devices on instantiation. See the
34   * {@link org.fourthline.cling.android.AndroidUpnpService} interface for a usage example.
35   * </p>
36   * <p/>
37   * Override the {@link #createRouter(org.fourthline.cling.UpnpServiceConfiguration, org.fourthline.cling.protocol.ProtocolFactory, android.content.Context)}
38   * and {@link #createConfiguration()} methods to customize the service.
39   *
40   * @author Christian Bauer
41   */
42  public class AndroidUpnpServiceImpl extends Service {
43  
44      protected UpnpService upnpService;
45      protected Binder binder = new Binder();
46  
47      /**
48       * Starts the UPnP service.
49       */
50      @Override
51      public void onCreate() {
52          super.onCreate();
53  
54          upnpService = new UpnpServiceImpl(createConfiguration()) {
55  
56              @Override
57              protected Router createRouter(ProtocolFactory protocolFactory, Registry registry) {
58                  return AndroidUpnpServiceImpl.this.createRouter(
59                      getConfiguration(),
60                      protocolFactory,
61                      AndroidUpnpServiceImpl.this
62                  );
63              }
64  
65              @Override
66              public synchronized void shutdown() {
67                  // First have to remove the receiver, so Android won't complain about it leaking
68                  // when the main UI thread exits.
69                  ((AndroidRouter)getRouter()).unregisterBroadcastReceiver();
70  
71                  // Now we can concurrently run the Cling shutdown code, without occupying the
72                  // Android main UI thread. This will complete probably after the main UI thread
73                  // is done.
74                  super.shutdown(true);
75              }
76          };
77      }
78  
79      protected UpnpServiceConfiguration createConfiguration() {
80          return new AndroidUpnpServiceConfiguration();
81      }
82  
83      protected AndroidRouter createRouter(UpnpServiceConfiguration configuration,
84                                                     ProtocolFactory protocolFactory,
85                                                     Context context) {
86          return new AndroidRouter(configuration, protocolFactory, context);
87      }
88  
89      @Override
90      public IBinder onBind(Intent intent) {
91          return binder;
92      }
93  
94      /**
95       * Stops the UPnP service, when the last Activity unbinds from this Service.
96       */
97      @Override
98      public void onDestroy() {
99          upnpService.shutdown();
100         super.onDestroy();
101     }
102 
103     protected class Binder extends android.os.Binder implements AndroidUpnpService {
104 
105         public UpnpService get() {
106             return upnpService;
107         }
108 
109         public UpnpServiceConfiguration getConfiguration() {
110             return upnpService.getConfiguration();
111         }
112 
113         public Registry getRegistry() {
114             return upnpService.getRegistry();
115         }
116 
117         public ControlPoint getControlPoint() {
118             return upnpService.getControlPoint();
119         }
120     }
121 
122 }