package com.github.timnew.androidinfrared;

import android.content.Context;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IrdaManager {

    public static IrdaManager getIrdaManager(Context applicationContext) {
        Object irdaService = applicationContext.getSystemService("irda");

        if (irdaService == null)
            return null;

        try {
            return new IrdaManager(irdaService);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }

    private final Object irdaService;
    private final Method writeIrSendMethod;

    private final Method readIrSendMethod;

    private IrdaManager(Object irdaService) throws NoSuchMethodException {
        this.irdaService = irdaService;

        Class<?> irdaServiceClass = irdaService.getClass();

        writeIrSendMethod = irdaServiceClass.getMethod("write_irsend", String.class);
        readIrSendMethod = irdaServiceClass.getMethod("read_irsend");
    }

    public IrdaManager sendSequence(String sequence) {
        Log.d("IRCode", sequence);
        rawWrite(sequence);
        return this;
    }

    private void rawWrite(String code) {
        try {
            writeIrSendMethod.invoke(irdaService, code);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private String rawRead() {
        try {
            return (String) readIrSendMethod.invoke(irdaService);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }

}

@@ -209,16 +209,72 @@
 ager %7B%0A%0A
+    public static final String IRDA_SERVICE = %22irda%22;%0A%0A%0A
     publ
@@ -405,22 +405,28 @@
 Service(
-%22irda%22
+IRDA_SERVICE
 );%0A%0A    
@@ -749,52 +749,8 @@
 d;%0A%0A
-    private final Method readIrSendMethod;%0A%0A
     
@@ -1012,78 +1012,8 @@
 s);%0A
-        readIrSendMethod = irdaServiceClass.getMethod(%22read_irsend%22);%0A
     
@@ -1424,32 +1424,32 @@
 ntStackTrace();%0A
+
         %7D%0A    %7D%0A
@@ -1453,315 +1453,6 @@
  %7D%0A%0A
-    private String rawRead() %7B%0A        try %7B%0A            return (String) readIrSendMethod.invoke(irdaService);%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D catch (InvocationTargetException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A
 %7D%0A

Remove unused code