/*
 * Copyright (C) 2010-2015 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.widgets;

import com.anpmech.mpd.MPD;
import com.anpmech.mpd.subsystem.status.MPDStatusMap;
import com.namelessdev.mpdroid.MPDApplication;
import com.namelessdev.mpdroid.helpers.MPDControl;

import android.app.IntentService;
import android.content.Intent;

public class WidgetHelperService extends IntentService {

    public static final String CMD_UPDATE_WIDGET = "UPDATE_WIDGET";

    static final String TAG = "MPDroidWidgetHelperService";

    private final MPDApplication mApp = MPDApplication.getInstance();

    private boolean mPlaying = false;

    public WidgetHelperService() {
        super(TAG);
    }

    public boolean isPlaying() {
        return mPlaying;
    }

    @Override
    protected void onHandleIntent(final Intent intent) {
        // get MPD connection
        mApp.addConnectionLock(this);

        // prepare values for runnable
        final MPD mpd = mApp.getMPD();
        final String action = intent.getAction();

        // schedule real work
        mApp.getAsyncHelper().execAsync(new Runnable() {
            @Override
            public void run() {
                processIntent(action, mpd);
            }
        });

        // clean up
        mApp.removeConnectionLock(this);
    }

    void processIntent(final String action, final MPD mpd) {
        switch (action) {
            case CMD_UPDATE_WIDGET:
                mPlaying = mpd.getStatus().isState(MPDStatusMap.STATE_PLAYING);
                SimpleWidgetProvider.getInstance().notifyChange(this);
                break;
            default:
                MPDControl.run(action);
        }
    }
}

@@ -672,19 +672,42 @@
 ech.mpd.
-MPD
+subsystem.status.MPDStatus
 ;%0Aimport
@@ -919,16 +919,55 @@
 ntent;%0A%0A
+import java.util.concurrent.TimeUnit;%0A%0A
 public c
@@ -1249,16 +1249,8 @@
 ying
- = false
 ;%0A%0A 
@@ -1458,29 +1458,49 @@
     
-// get MPD conne
+final String action = intent.getA
 ction
+();
 %0A   
@@ -1508,176 +1508,202 @@
     
-mApp.addConnectionLock(this);%0A%0A        // prepare values for runnable%0A        final MPD mpd = mApp.getMPD();%0A        final String action = intent.getAction(
+final MPDStatus status = mApp.getMPD().getStatus();%0A%0A        switch (action) %7B%0A            case CMD_UPDATE_WIDGET:%0A                mPlaying = status.isState(MPDStatusMap.STATE_PLAYING
 );%0A
-%0A
         
 // s
@@ -1702,87 +1702,75 @@
     
-// schedule real work%0A        mApp.getAsyncHelper().execAsync(new Runnable() %7B%0A
+        SimpleWidgetProvider.getInstance().notifyChange(this);%0A    
     
@@ -1773,33 +1773,30 @@
             
-@Override
+break;
 %0A           
@@ -1800,28 +1800,61 @@
     
-public void run() %7B%0A
+default:%0A                if (status.isValid()) %7B%0A    
     
@@ -1869,33 +1869,29 @@
     
-processIntent
+MPDControl.run
 (action
-, mpd
 );%0A 
@@ -1905,448 +1905,299 @@
     
+ 
-%7D%0A        %7D);%0A%0A        // clean up%0A        mApp.removeConnectionLock(this
+   %7D else %7B%0A                    final Object token = MPDControl.setupConnection(5L, TimeUnit.SECONDS
 );%0A
+%0A
     
-%7D%0A%0A
     
-void processIntent(final String action, final MPD mpd) %7B%0A        switch (action) %7B%0A            case CMD_UPDATE_WIDGET:%0A                mPlaying = mpd.getStatus().isState(MPDStatusMap.STATE_PLAYING);%0A                SimpleWidgetProvider.getInstance().notifyChange(this);%0A                break;%0A            default:%0A                MPDControl.run(action);
+            if (token != null) %7B%0A                        MPDControl.run(action);%0A                        mApp.removeConnectionLock(token);%0A                    %7D%0A                %7D
 %0A   

Fix for connection delay.