/*
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namelessdev.mpdroid.locale;

import com.namelessdev.mpdroid.MPDApplication;
import com.namelessdev.mpdroid.NotificationService;
import com.namelessdev.mpdroid.helpers.MPDControl;

import org.a0z.mpd.MPD;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class ActionFireReceiver extends BroadcastReceiver {

    private static final String TAG = "MPDroid Locale Plugin";

    private final MPDApplication app = MPDApplication.getInstance();

    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getBundleExtra(LocaleConstants.EXTRA_BUNDLE);
        if (bundle == null) {
            return;
        }
        final String action = bundle.getString(EditActivity.BUNDLE_ACTION_STRING);

        switch (action) {
            case NotificationService.ACTION_OPEN_NOTIFICATION:
                final Intent i = new Intent(context, NotificationService.class);
                i.setAction(NotificationService.ACTION_OPEN_NOTIFICATION);
                context.startService(i);
                break;
            case NotificationService.ACTION_CLOSE_NOTIFICATION:
                final Intent i2 = new Intent(context, NotificationService.class);
                i2.setAction(NotificationService.ACTION_CLOSE_NOTIFICATION);
                context.startService(i2);
                break;
            default:
                final MPD mpd = connectToMPD(context);
                int volume = MPDControl.INVALID_INT;

                if (MPDControl.ACTION_SET_VOLUME.equals(action)) {
                    final String volumeString = bundle
                            .getString(EditActivity.BUNDLE_ACTION_EXTRA);
                    if (volumeString != null) {
                        try {
                            volume = Integer.parseInt(volumeString);
                        } catch (final NumberFormatException e) {
                            Log.e(TAG, "Invalid volume string : " + volumeString, e);
                        }
                    }
                }
                MPDControl.run(mpd, action, volume);
                closeMPDConnection();
                break;
        }
    }

    private MPD connectToMPD(Context context) {
        app.init(context);
        app.addConnectionLock(this);
        app.connect();
        return app.oMPDAsyncHelper.oMPD;
    }

    private void closeMPDConnection() {
        app.removeConnectionLock(this);
        // Seems required since I can't exit the dedicated process properly yet
        System.exit(0);
    }
}

@@ -679,30 +679,35 @@
 mpdroid.
-MPDApplication
+NotificationService
 ;%0Aimport
@@ -731,35 +731,37 @@
 mpdroid.
-NotificationService
+RemoteControlReceiver
 ;%0Aimport
@@ -810,33 +810,8 @@
 l;%0A%0A
-import org.a0z.mpd.MPD;%0A%0A
 impo
@@ -1092,78 +1092,8 @@
 %22;%0A%0A
-    private final MPDApplication app = MPDApplication.getInstance();%0A%0A
     
@@ -1128,16 +1128,22 @@
 Receive(
+final 
 Context 
@@ -1150,16 +1150,22 @@
 context,
+ final
  Intent 
@@ -1399,16 +1399,52 @@
 STRING);
+%0A        final Intent serviceIntent;
 %0A%0A      
@@ -1546,23 +1546,22 @@
     
-final 
+service
 Intent 
-i 
 = ne
@@ -1626,64 +1626,38 @@
     
-i.setAction(NotificationService.ACTION_OPEN_NOTIFICATION
+serviceIntent.setAction(action
 );%0A 
@@ -1692,17 +1692,29 @@
 Service(
-i
+serviceIntent
 );%0A     
@@ -1815,24 +1815,22 @@
     
-final 
+service
 Intent 
-i2 
 = ne
@@ -1847,35 +1847,37 @@
 ontext, 
-NotificationService
+RemoteControlReceiver
 .class);
@@ -1897,66 +1897,38 @@
     
-i2.setAction(NotificationService.ACTION_CLOSE_NOTIFICATION
+serviceIntent.setAction(action
 );%0A 
@@ -1967,10 +1967,21 @@
 ice(
-i2
+serviceIntent
 );%0A 
@@ -2027,63 +2027,8 @@
 lt:%0A
-                final MPD mpd = connectToMPD(context);%0A
     
@@ -2673,13 +2673,8 @@
 run(
-mpd, 
 acti
@@ -2706,202 +2706,21 @@
     
-closeMPDConnection();%0A                break;%0A        %7D%0A    %7D%0A%0A    private MPD connectToMPD(Context context) %7B%0A        app.init(context);%0A        app.addConnectionLock(this);%0A        app.connec
+System.exi
 t(
+0
 );%0A 
@@ -2730,231 +2730,32 @@
     
-return app.oMPDAsyncHelper.oMPD;%0A    %7D%0A%0A    private void closeMPDConnection() %7B%0A        app.removeConnectionLock(this);%0A        // Seems required since I can't exit the dedicated process properly yet%0A        System.exit(0);
+        break;%0A        %7D
 %0A   

Use the MPDControl generated connection.