<commit_before>package edu.uw.bladedroid.blade;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class ActionReplay extends AbstractBlade{
    
    private final String LOG_TAG = "ACTION_REPLAY";
    private final Mode MODE = Mode.RECORD;
//    private final Mode MODE = Mode.REPLAY;
    
    private TouchingEventSaver saver;
 
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        Log.wtf(LOG_TAG, "in " + MODE + " mode");
        saver = TouchingEventSaver.getInstance(activity);
        if(MODE == Mode.RECORD){
            Toast.makeText(activity, "RECORDING NOW...", Toast.LENGTH_SHORT).show();
            monitorAllButtonsApproache(activity);
        }else if (MODE == Mode.REPLAY){
            Toast.makeText(activity, "REPLAY ACTIONS", Toast.LENGTH_SHORT).show();
            new ButtonClickAsync().execute(activity);
        }
    }
    
    private void monitorAllButtonsApproache(Activity activity) {
        ViewGroup rootView = (ViewGroup)activity.findViewById(android.R.id.content);  
        resetOnClickListenerOfAllButtons(rootView);
    }

    private void resetOnClickListenerOfAllButtons(ViewGroup rootView) {
        for(int i = 0; i < rootView.getChildCount(); i++){
            View v = rootView.getChildAt(i);
                final View.OnClickListener listener = getOnClickListener(v);
                if(listener != null){
                    v.setOnClickListener(new ClickMonitListener(listener, v.getId()));
                }
            if(v instanceof ViewGroup){
                resetOnClickListenerOfAllButtons((ViewGroup)v);
            }
        }
    }
    
    private class ClickMonitListener implements View.OnClickListener{
        
        private View.OnClickListener listener;
        private int viewID;
        
        public ClickMonitListener(View.OnClickListener listener, int viewID){
            this.listener = listener;
            this.viewID = viewID;
        }

        @Override
        public void onClick(View v) {
            try {
                saver.save(viewID, System.currentTimeMillis());
            } catch (IOException e) {
                Log.wtf(LOG_TAG, "failed to save");
                e.printStackTrace();
            }
            Log.wtf(LOG_TAG, "click action saved");
            listener.onClick(v);
        }
    }
    
//    public void onResume(Activity activity){
//        if(MODE == Mode.REPLAY){
//            Toast.makeText(activity, "REPLAY ACTIONS", Toast.LENGTH_SHORT).show();
//            new ButtonClickAsync().execute(activity);
//        }
//    }
    
    private class ButtonClickAsync extends AsyncTask<Activity, Void, Void>{

        @Override
        protected Void doInBackground(Activity... arg0) {
            Activity activity = arg0[0];
            try {
                //give sometime to render UI
                List<ViewIDTouchTime> idTimes = saver.load();
                if(idTimes.isEmpty()) return null;
                Thread.sleep(1500);
                
                long lastStartTime = 0;
                for(int i = 0; i < idTimes.size(); i++){
                    ViewIDTouchTime idTime = idTimes.get(i);
                    if(i > 0){<commit_msg>capitalize Blade and BladeDroid, change some minor typos<commit_after>package edu.uw.bladedroid.blade;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class ActionReplay extends AbstractBlade{
    
    private final String LOG_TAG = "ACTION_REPLAY";
    private final Mode MODE = Mode.RECORD;
//    private final Mode MODE = Mode.REPLAY;
    private TouchingEventSaver saver;
 
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        Log.wtf(LOG_TAG, "in " + MODE + " mode");
        saver = TouchingEventSaver.getInstance(activity);
        if(MODE == Mode.RECORD){
            Toast.makeText(activity, "RECORDING NOW...", Toast.LENGTH_SHORT).show();
            monitorAllButtonsApproache(activity);
        }else if (MODE == Mode.REPLAY){
            Toast.makeText(activity, "REPLAY ACTIONS", Toast.LENGTH_SHORT).show();
            new ButtonClickAsync().execute(activity);
        }
    }
    
    private void monitorAllButtonsApproache(Activity activity) {
        ViewGroup rootView = (ViewGroup)activity.findViewById(android.R.id.content);  
        resetOnClickListenerOfAllButtons(rootView);
    }

    private void resetOnClickListenerOfAllButtons(ViewGroup rootView) {
        for(int i = 0; i < rootView.getChildCount(); i++){
            View v = rootView.getChildAt(i);
                final View.OnClickListener listener = getOnClickListener(v);
                if(listener != null){
                    v.setOnClickListener(new ClickMonitListener(listener, v.getId()));
                }
            if(v instanceof ViewGroup){
                resetOnClickListenerOfAllButtons((ViewGroup)v);
            }
        }
    }
    
    private class ClickMonitListener implements View.OnClickListener{
        
        private View.OnClickListener listener;
        private int viewID;
        
        public ClickMonitListener(View.OnClickListener listener, int viewID){
            this.listener = listener;
            this.viewID = viewID;
        }

        @Override
        public void onClick(View v) {
            try {
                saver.save(viewID, System.currentTimeMillis());
            } catch (IOException e) {
                Log.wtf(LOG_TAG, "failed to save");
                e.printStackTrace();
            }
            Log.wtf(LOG_TAG, "click action saved");
            listener.onClick(v);
        }
    }
    
    private class ButtonClickAsync extends AsyncTask<Activity, Void, Void>{

        @Override
        protected Void doInBackground(Activity... arg0) {
            Activity activity = arg0[0];
            try {
                //give sometime to render UI
                List<ViewIDTouchTime> idTimes = saver.load();
                if(idTimes.isEmpty()) return null;
                Thread.sleep(1500);
                
                long lastStartTime = 0;
                for(int i = 0; i < idTimes.size(); i++){
                    ViewIDTouchTime idTime = idTimes.get(i);
                    if(i > 0){
{'fraction_non_alphanumeric': 0.09899374490073429, 'fraction_numerical': 0.002991569214033179, 'mean_word_length': 2.2805530776092775, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3833934', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1941, 'n_words': 444}