OSDN Git Service

Make the bind/unbind utility methods work for activities in an activitygroup too.
authorMarco Nelissen <marcone@google.com>
Thu, 28 Jan 2010 02:44:52 +0000 (18:44 -0800)
committerMarco Nelissen <marcone@google.com>
Thu, 28 Jan 2010 02:44:52 +0000 (18:44 -0800)
src/com/android/music/MusicUtils.java

index d1314bf..1b72e58 100644 (file)
@@ -159,22 +159,31 @@ public class MusicUtils {
         }
     }
 
-    public static ServiceToken bindToService(Context context) {
+    public static ServiceToken bindToService(Activity context) {
         return bindToService(context, null);
     }
 
-    public static ServiceToken bindToService(Context context, ServiceConnection callback) {
-        ContextWrapper cw = new ContextWrapper(context);
+    public static ServiceToken bindToService(Activity context, ServiceConnection callback) {
+        Activity realActivity = context.getParent();
+        if (realActivity == null) {
+            realActivity = context;
+        }
+        ContextWrapper cw = new ContextWrapper(realActivity);
         cw.startService(new Intent(cw, MediaPlaybackService.class));
         ServiceBinder sb = new ServiceBinder(callback);
         if (cw.bindService((new Intent()).setClass(cw, MediaPlaybackService.class), sb, 0)) {
             sConnectionMap.put(cw, sb);
             return new ServiceToken(cw);
         }
+        Log.e("Music", "Failed to bind to service");
         return null;
     }
 
     public static void unbindFromService(ServiceToken token) {
+        if (token == null) {
+            Log.e("MusicUtils", "Trying to unbind with null token");
+            return;
+        }
         ContextWrapper cw = token.mWrappedContext;
         ServiceBinder sb = sConnectionMap.remove(cw);
         if (sb == null) {