
package net.cattaka.android.gcmadkalart;

import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.widget.Toast;

import net.cattaka.android.gcmadkalart.data.Command;
import net.cattaka.android.gcmadkalart.data.MyPacket;
import net.cattaka.android.gcmadkalart.data.MyPacketFactory;
import net.cattaka.android.gcmadkalart.data.OpCode;
import net.cattaka.android.gcmadkalart.entity.Action;
import net.cattaka.libgeppa.AdkPassiveGeppaService;
import net.cattaka.libgeppa.data.ConnectionState;
import net.cattaka.libgeppa.data.PacketWrapper;

import java.util.List;

public class GeppaServiceEx extends AdkPassiveGeppaService<MyPacket> {
    public static final String EXTRA_ACTIONS = "actions";
    private static Handler sHandler = new Handler(Looper.getMainLooper());

    private GeppaServiceEx me = this;

    private HandlerThread mHandlerThread;
    private Handler mHandler;

    private WakeLock mWakeLock;

    public GeppaServiceEx() {
        super(new MyPacketFactory());

    }

    @Override
    public void onCreate() {
        super.onCreate();

        PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);

        mHandlerThread = new HandlerThread("Actions");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }

        mHandlerThread.quit();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        {   // From GCM
            List<Action> actions = null;
            if (intent != null) {
                actions = intent.getParcelableArrayListExtra(EXTRA_ACTIONS);
            }
            if (actions != null) {
                for (Action action : actions) {
                    doAction(action);
                }
            }
        }
    }

    @Override
    protected void onReceivePacket(MyPacket packet) {
        super.onReceivePacket(packet);
        // No op
    }

    @Override
    protected void onConnectionStateChanged(ConnectionState state) {
        super.onConnectionStateChanged(state);
//        if (state == ConnectionState.CONNECTED) {
//        }
    }

    private void doAction(final Action action) {
        if (action.getCommand() == Command.LED) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    {
                        byte[] bs = new byte[]{
                                (byte) (0xFF & (action.getColor() >> 16)),
                                (byte) (0xFF & (action.getColor() >> 8)),
                                (byte) (0xFF & (action.getColor())),
                        };
                        MyPacket packet = new MyPacket(OpCode.LED_RGB, bs.length, bs);
                        sendPacket(new PacketWrapper(packet));
                    }
                    {
                        SystemClock.sleep(action.getInterval());
                    }
                    {
                        byte[] bs = new byte[]{0,0,0};
                        MyPacket packet = new MyPacket(OpCode.LED_RGB, bs.length, bs);
                        sendPacket(new PacketWrapper(packet));
                    }
                }
            });
        } else if (action.getCommand() == Command.WAIT) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    {
                        SystemClock.sleep(action.getInterval());
                    }
                }
            });
        } else if (action.getCommand() == Command.TALK) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    sHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(me, String.valueOf(action.getText()), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
        }
    }
}

@@ -280,24 +280,89 @@
 ystemClock;%0A
+import android.speech.tts.TextToSpeech;%0Aimport android.util.Log;%0A
 import andro
@@ -826,16 +826,41 @@
 il.List;
+%0Aimport java.util.Locale;
 %0A%0Apublic
@@ -1186,26 +1186,648 @@
 ate 
-WakeLock mWakeLock
+TextToSpeech mTextToSpeech;%0A%0A    private WakeLock mWakeLock;%0A%0A    TextToSpeech.OnInitListener mOnInitListener = new TextToSpeech.OnInitListener() %7B%0A        @Override%0A        public void onInit(int status) %7B%0A            if (TextToSpeech.SUCCESS == status) %7B%0A                Locale locale = Locale.ENGLISH;%0A                if (mTextToSpeech.isLanguageAvailable(locale) %3E= TextToSpeech.LANG_AVAILABLE) %7B%0A                    mTextToSpeech.setLanguage(locale);%0A                %7D else %7B%0A                    Log.d(%22%22, %22Error SetLocale%22);%0A                %7D%0A            %7D else %7B%0A                Log.d(%22%22, %22Error Init%22);%0A            %7D%0A        %7D%0A    %7D
 ;%0A%0A 
@@ -1889,25 +1889,24 @@
 Factory());%0A
-%0A
     %7D%0A%0A    @
@@ -2269,24 +2269,88 @@
 etLooper());
+%0A%0A        mTextToSpeech = new TextToSpeech(me, mOnInitListener);
 %0A    %7D%0A%0A    
@@ -2490,24 +2490,109 @@
 %0A        %7D%0A%0A
+        if (mTextToSpeech != null) %7B%0A            mTextToSpeech.shutdown();%0A        %7D%0A
         mHan
@@ -5091,32 +5091,144 @@
 ic void run() %7B%0A
+                            mTextToSpeech.speak(String.valueOf(action.getText()),TextToSpeech.QUEUE_ADD, null);%0A
                 

Add tts