package com.octtoplus.proj.recorda;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

/**
 * Created by muangsiriworamet on 9/21/16.
 */

public class RecordAccessibilityService extends AccessibilityService {

    private final AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    private static final String TAG = "RecService";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        final int eventType = accessibilityEvent.getEventType();
        String eventText = null;
        
        switch(eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                eventText = "Clicked: ";
                break;
            case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
                eventText = "Focused: ";
                break;
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
                eventText = "Scrolled: ";
                break;
            default:
                eventText = "Other: ";
                break;
        }
        
        eventText = eventText + accessibilityEvent.getContentDescription();
        Log.i(TAG, "Event: " + eventText);
        Log.i(TAG, "a: "+accessibilityEvent.toString());
        toast("Event:"+ eventText);
        logToSdCard(accessibilityEvent, true);

        
    }

    @Override
    protected void onServiceConnected() {
        Log.i(TAG, "onServiceConnected: start");
        //logToSdCard("Start recorda session");

        info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED | AccessibilityEvent.TYPE_VIEW_FOCUSED |
        AccessibilityEvent.TYPE_VIEW_SCROLLED | AccessibilityEvent.TYPE_VIEW_SELECTED | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED |
        AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | AccessibilityEvent.TYPE_WINDOWS_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_HAPTIC;
        info.packageNames = new String[]{"com.octtoplus.proj.recorda"};
        info.notificationTimeout = 100;

        this.setServiceInfo(info);
    }

    @Override
    public void onInterrupt() {

    }

    public void toast(CharSequence text) {
        Context context = getApplicationContext();

        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

    public void logToSdCard(AccessibilityEvent event, boolean isVerbose) {
        try {
            File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/recorda_log.txt");
            toast(Environment.getExternalStorageDirectory().getAbsolutePath());
            if (!myFile.exists()) {
                toast("CREATE NEW FILE");
                myFile.createNewFile();
            }
            FileOutputStream fOut = new FileOutputStream(myFile, true);
            OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

            if (isVerbose) {
                outWriter.append("[class]:"+event.getClassName());
                outWriter.append("\n");
                outWriter.append("[source]:"+event.getSource());
                outWriter.append("\n");
                outWriter.append("[type]:"+AccessibilityEvent.eventTypeToString(event.getEventType()));
                outWriter.append("\n");
                outWriter.append("[desc]:"+event.getContentDescription());
                outWriter.append("\n");
                outWriter.append(event.toString());
                outWriter.append("\n");
                outWriter.append("---------------------");
            } else {
                outWriter.append(event.toString());
            }

            outWriter.append("\n");
            outWriter.close();
            fOut.close();
        } catch (Exception e) {
            toast(e.getMessage());
        }
    }
}

@@ -1859,33 +1859,42 @@
 E_VIEW_CLICKED %7C
- 
+%0A//%09%09%09%09%09%09%09
 AccessibilityEve
@@ -1910,12 +1910,15 @@
 IEW_
-LONG
+CONTEXT
 _CLI
@@ -1919,24 +1919,40 @@
 XT_CLICKED %7C
+%0A               
  Accessibili
@@ -1973,18 +1973,31 @@
 IEW_
-FOCUS
+LONG_CLICK
 ED %7C%0A
+        
     
@@ -2034,17 +2034,81 @@
 EW_S
-CROLLED %7C
+ELECTED %7C%0A//%09%09%09%09%09%09%09AccessibilityEvent.TYPE_VIEW_FOCUSED %7C%0A               
  Acc
@@ -2137,19 +2137,32 @@
 IEW_
-SELECTED %7C 
+TEXT_CHANGED %7C%0A//%09%09%09%09%09%09%09
 Acce
@@ -2195,26 +2195,88 @@
 EXT_
-CHANGED %7C%0A
+SELECTION_CHANGED %7C%0A%09%09%09%09%09%09%09AccessibilityEvent.TYPE_VIEW_SCROLLED %7C%0A     
         
 Acce
@@ -2263,32 +2263,35 @@
  %7C%0A             
+   
 AccessibilityEve
@@ -2320,16 +2320,32 @@
 HANGED %7C
+%0A               
  Accessi
@@ -2378,16 +2378,17 @@
 HANGED;%0A
+%0A
         
@@ -2445,12 +2445,13 @@
 ACK_
-HAPT
+GENER
 IC;%0A

Update eventType