/*
 * Copyright 2013 wada811<at.wada811@gmail.com>
 *
 * 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 at.wada811.android.library.demos.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import at.wada811.android.library.demos.R;
import at.wada811.utils.LogUtils;
import at.wada811.view.Tooltip;

public class ExecutorActivity extends FragmentActivity {

    final ExecutorActivity self = this;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_executor);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                execute();
                LogUtils.d();
            }
        });

    }

    public void execute(){
        LogUtils.d();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable1());
        executorService.submit(new Runnable2());
        executorService.submit(new Runnable1());
        executorService.submit(new Runnable2());
        LogUtils.d("Terminated: " + executorService.isTerminated());
    }

    public class Runnable1 implements Runnable {
        @Override
        public void run(){
            LogUtils.d("Runnable1: " + System.currentTimeMillis());
            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    LogUtils.i("Runnable1: " + System.currentTimeMillis());
                    TextView mTextView = new TextView(self);
                    mTextView.setBackgroundColor(Color.GREEN);
                    mTextView.setText("Runnable1: " + System.currentTimeMillis());
                    Tooltip mTooltip = new Tooltip(self);
                    mTooltip.setContentView(mTextView);
                    mTooltip.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                    mTooltip.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                    mTooltip.setAnimationStyle(R.style.tooltipAnimation);
                    mTooltip.setDuration(3000);
                    mTooltip.showAtLocation(Gravity.CENTER, 0, 0);
                    LogUtils.i("Runnable1: " + System.currentTimeMillis());
                }
            });
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            LogUtils.d("Runnable1: " + System.currentTimeMillis());
        }
    }

    public class Runnable2 implements Runnable {
        @Override
        public void run(){
            LogUtils.d("Runnable2: " + System.currentTimeMillis());
            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    LogUtils.i("Runnable2: " + System.currentTimeMillis());
                    TextView mTextView = new TextView(self);
                    mTextView.setBackgroundColor(Color.YELLOW);
                    mTextView.setText("Runnable2: " + System.currentTimeMillis());
                    Tooltip mTooltip = new Tooltip(self);
                    mTooltip.setContentView(mTextView);
                    mTooltip.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                    mTooltip.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                    mTooltip.setAnimationStyle(R.style.tooltipAnimation);
                    mTooltip.setDuration(3000);
                    mTooltip.showAtLocation(Gravity.CENTER, 0, 30);
                    LogUtils.i("Runnable2: " + System.currentTimeMillis());
                }
            });
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            LogUtils.d("Runnable2: " + System.currentTimeMillis());
        }
    }
}

@@ -755,30 +755,37 @@
 ort 
-android.graphics.Color
+java.util.concurrent.TimeUnit
 ;%0Aim
@@ -860,37 +860,8 @@
 ty;%0A
-import android.view.Gravity;%0A
 impo
@@ -943,88 +943,21 @@
 oid.
-view.WindowManager;%0Aimport android.widget.Button;%0Aimport android.widget.TextView
+widget.Button
 ;%0Aim
@@ -1034,40 +1034,8 @@
 ils;
-%0Aimport at.wada811.view.Tooltip;
 %0A%0Apu
@@ -2101,1051 +2101,74 @@
 e1: 
-%22 + System.currentTimeMillis());%0A            runOnUiThread(new Runnable()%7B%0A                @Override%0A                public void run()%7B%0A                    LogUtils.i(%22Runnable1: %22 + System.currentTimeMillis());%0A                    TextView mTextView = new TextView(self);%0A                    mTextView.setBackgroundColor(Color.GREEN);%0A                    mTextView.setText(%22Runnable1: %22 + System.currentTimeMillis());%0A                    Tooltip mTooltip = new Tooltip(self);%0A                    mTooltip.setContentView(mTextView);%0A                    mTooltip.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);%0A                    mTooltip.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);%0A                    mTooltip.setAnimationStyle(R.style.tooltipAnimation);%0A                    mTooltip.setDuration(3000);%0A                    mTooltip.showAtLocation(Gravity.CENTER, 0, 0);%0A                    LogUtils.i(%22Runnable1: %22 + System.currentTimeMillis());%0A                %7D%0A            %7D);%0A            try%7B%0A                Thread
+start%22);%0A            try%7B%0A                TimeUnit.SECONDS
 .sleep(
-3000
+1
 );%0A 
@@ -2300,38 +2300,12 @@
 e1: 
-%22 + System.currentTimeMillis()
+end%22
 );%0A 
@@ -2453,1053 +2453,74 @@
 e2: 
-%22 + System.currentTimeMillis());%0A            runOnUiThread(new Runnable()%7B%0A                @Override%0A                public void run()%7B%0A                    LogUtils.i(%22Runnable2: %22 + System.currentTimeMillis());%0A                    TextView mTextView = new TextView(self);%0A                    mTextView.setBackgroundColor(Color.YELLOW);%0A                    mTextView.setText(%22Runnable2: %22 + System.currentTimeMillis());%0A                    Tooltip mTooltip = new Tooltip(self);%0A                    mTooltip.setContentView(mTextView);%0A                    mTooltip.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);%0A                    mTooltip.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);%0A                    mTooltip.setAnimationStyle(R.style.tooltipAnimation);%0A                    mTooltip.setDuration(3000);%0A                    mTooltip.showAtLocation(Gravity.CENTER, 0, 30);%0A                    LogUtils.i(%22Runnable2: %22 + System.currentTimeMillis());%0A                %7D%0A            %7D);%0A            try%7B%0A                Thread
+start%22);%0A            try%7B%0A                TimeUnit.SECONDS
 .sleep(
-3000
+1
 );%0A 
@@ -2652,38 +2652,12 @@
 e2: 
-%22 + System.currentTimeMillis()
+end%22
 );%0A 

delete tooltip and runnable body