package com.github.bhaak;

import java.io.IOException;
import java.net.URI;

import com.amazon.kindle.booklet.AbstractBooklet;
import com.amazon.kindle.booklet.BookletContext;
import com.amazon.kindle.booklet.ChromeException;
import com.amazon.kindle.booklet.ChromeHeaderRequest;
import com.amazon.ebook.util.log.Log;

/**
 * A Booklet for starting HackedUpReader.
 *  
 * @author Patric Mueller &lt;bhaak@gmx.net&gt;
 */
public class HackedUpReaderBooklet extends AbstractBooklet {

	private static final Log logger = Log.getInstance("HackedUpReaderBooklet");
	private static final String hackedupreader = "/mnt/us/hackedupreader/bin/cr3";
	
	public HackedUpReaderBooklet() {
		logger.info("HackedUpReaderBooklet");
	}
	public void start(URI contentURI) {
		logger.info("start("+contentURI+")");
		try {
			Runtime.getRuntime().exec(hackedupreader);
		} catch (IOException e) {
			logger.error(e.toString(), e);
		}
		super.start(contentURI);
	}

	public void create(BookletContext bookletContext) {
		logger.info("create("+bookletContext+")");
		super.create(bookletContext);
	}

	public void destroy() {
		logger.info("destroy()");
		super.destroy();
	}

	public String getName() {
		logger.info("getName()");
		return super.getName();
	}

	public void stop() {
		logger.info("stop()");
		super.stop();
	}
	public ChromeHeaderRequest getChromeHeaderRequest() {
		logger.info("getChromeHeaderRequest()");
		return super.getChromeHeaderRequest();
	}
	public void setChromeHeaderRequest(ChromeHeaderRequest chromeHeaderRequest) {
		logger.info("setChromeHeaderRequest("+chromeHeaderRequest+")");
		super.setChromeHeaderRequest(chromeHeaderRequest);
	}
	public void syncHeaderRequest() throws ChromeException {
		logger.info("syncHeaderRequest()");
		super.syncHeaderRequest();
	}
}

@@ -44,24 +44,56 @@
 OException;%0A
+import java.lang.reflect.Field;%0A
 import java.
@@ -668,16 +668,59 @@
 cr3%22;%0A%09%0A
+%09private Process hackedupreaderProcess; %0A%09%0A
 %09public 
@@ -876,16 +876,40 @@
 ry %7B%0A%09%09%09
+hackedupreaderProcess = 
 Runtime.
@@ -1036,24 +1036,25 @@
 ntURI);%0A%09%7D%0A%0A
+%0A
 %09public void
@@ -1058,232 +1058,166 @@
 oid 
-create(BookletContext bookletContext) %7B%0A%09%09logger.info(%22create(%22+bookletContext+%22)%22);%0A%09%09super.create(bookletContext);%0A%09%7D%0A%0A%09public void destroy() %7B%0A%09%09logger.info(%22destroy()%22);%0A%09%09super.destroy();%0A%09%7D%0A%0A%09public String getName(
+stop() %7B%0A%09%09logger.info(%22stop()%22);%0A%09%09%0A%09%09if (hackedupreaderProcess != null) %7B%0A%09%09%09try %7B%0A%09%09%09%09killQuitProcess(hackedupreaderProcess);%0A%09%09%09%7D catch (Exception e
 ) %7B%0A
+%09%09
 %09%09lo
@@ -1225,590 +1225,921 @@
 ger.
-info(%22getName()%22);%0A%09%09return super.getName();%0A%09%7D%0A%0A%09public void stop() %7B%0A%09%09logger.info(%22stop()%22);%0A%09%09super.stop();%0A%09%7D%0A%09public ChromeHeaderRequest getChromeHeaderRequest() %7B%0A%09%09logger.info(%22getChromeHeaderRequest()%22);%0A%09%09return super.getChromeHeaderRequest();%0A%09%7D%0A%09public void setChromeHeaderRequest(ChromeHeaderRequest chromeHeaderRequest) %7B%0A%09%09logger.info(%22setChromeHeaderRequest(%22+chromeHeaderRequest+%22)%22);%0A%09%09super.setChromeHeaderRequest(chromeHeaderRequest);%0A%09%7D%0A%09public void syncHeaderRequest() throws ChromeException %7B%0A%09%09logger.info(%22syncHeaderRequest()%22);%0A%09%09super.syncHeaderRequest();
+error(e.toString(), e);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09super.stop();%0A%09%7D%0A%09%0A%09/** %0A%09 * Send a QUIT signal to a process.%0A%09 * %0A%09 * See http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte#answer-2951193%0A%09 */%0A%09private void killQuitProcess(Process process)%0A%09%09%09throws InterruptedException, IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException%0A%09%7B%0A%09    logger.info(process.getClass().getName());%0A%09    if (process.getClass().getName().equals(%22java.lang.UNIXProcess%22)) %7B%0A%09        Class cl = process.getClass();%0A%09        Field field = cl.getDeclaredField(%22pid%22);%0A%09        field.setAccessible(true);%0A%09        Object pidObject = field.get(process);%0A%09        %0A%09        Runtime.getRuntime().exec(%22kill -QUIT %22 + pidObject).waitFor();%0A%09    %7D else %7B%0A%09        throw new IllegalArgumentException(%22Needs to be a UNIXProcess%22);%0A%09    %7D
 %0A%09%7D%0A

Stop HackedUpReader when Booklet is stopped