<commit_before>package io.magnum.antimalware;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import io.magnum.antimalware.features.*;

import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;

/**
 * This collects the vectors
 * and periodically writes them to a file (if training
 * mode is enabled).
 * 
 * @author <NAME>
 * @version 2012.08.10
 */
public class MalwareService extends Service {
	private static final String TAG = "MalwareService";
	
	private final IBinder _mBinder = new MalwareBinder();
	private Time _time;
	private ScheduledExecutorService _scheduledExecutor;
	
	// All features are contained in _normalFeatures,
	// but some need to be tracked to set certain information
	private BatteryFeatures _battFeatures;
	private PermissionFeatures _permissionFeatures;
	private ArrayList<IFeatures> _normalFeatures;

	private WekaHelper _wh;
	private ArrayList<Attribute> _normalAttributes;
	private ArrayList<Attribute> _classAttributes;
	private ArrayList<Attribute> _attributes;
	private Instances _instances;
	
	@Override
	public void onCreate() {
		_time = new Time();

		_normalFeatures = new ArrayList<IFeatures>(6);
		
		_battFeatures = new BatteryFeatures();
		_normalFeatures.add(_battFeatures);
		
		_normalFeatures.add(new BinderFeatures());
		_normalFeatures.add(new CPUFeatures());
		_normalFeatures.add(new MemoryFeatures());
		_normalFeatures.add(new NetworkFeatures());
		
		_permissionFeatures = new PermissionFeatures(
				getPackageManager());
		_normalFeatures.add(_permissionFeatures);
		
		_wh = WekaHelper.getInstance();
		
		initWeka();
		initExecutor();
	}
	
	@SuppressWarnings("unchecked")
	private void initWeka() {
		_normalAttributes = new ArrayList<Attribute>();
		for (IFeatures feature : _normalFeatures ) {
			_normalAttributes.addAll(feature.getAttributes());
		}
		
		_classAttributes = new ArrayList<Attribute>();

		List<String> classVal = new ArrayList<String>(2);
		classVal.add("positive");
		classVal.add("negative");
		Attribute classAttribute = new Attribute("classification", classVal);
		_classAttributes.add(classAttribute);
		
		_attributes = _wh.addLists(_normalAttributes, _classAttributes);
		_instances = new Instances("relation", _attributes, 0);
	}

	/**
	 * Sets up retrieval at a fixed interval.
	 */
	private void initExecutor() {
		_scheduledExecutor = Executors.newScheduledThreadPool(1);
		
		_scheduledExecutor.scheduleAtFixedRate(new Runnable() {
			public void run() {
				fetchSnapshot();
			}
		}, 1, 5, TimeUnit.SECONDS); 
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return _mBinder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		return true;
	}
	
	/**
	 * Gets the data from all of the features and puts them in 
	 * a format for Weka to read.
	 */
	public void fetchSnapshot() {
		Log.d(TAG, "fetchSnapshot!");
		
		_time.setToNow();
		
		IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		_battFeatures.setIntent(registerReceiver(null, filter));
		
		ActivityManager am = (ActivityManager) getSystemService(
				Context.ACTIVITY_SERVICE );
		_permissionFeatures.setProcesses(am.getRunningAppProcesses());
		
		ArrayList<Instance> normalInstanceList = new ArrayList<Instance>(_normalAttributes.size());
		for (IFeatures features : _normalFeatures) {
			normalInstanceList.add(features.getInstance());
		}
		_wh.resetAttributes(_normalAttributes);
		Instance normalInstances = _wh.mergeInstances(normalInstanceList);
		
		_wh.resetAttributes(_classAttributes);
		Instance classInstance = new DenseInstance(1);
		classInstance.setValue(
				_classAttributes.get(0), _wh.getClassification() );
		
		_wh.resetAttributes(_attributes);
		_instances.add(_wh.mergeInstances(normalInstances, classInstance));
		_instances.setClassIndex(_attributes.size()-1);
		
		_wh.writeArff(_instances.toString());
	}
	
	public class MalwareBinder extends Binder {
		MalwareService getService() {
			return MalwareService.this;
		}
	}
}<commit_msg>Update the service to offload work to a thread so we don't block. Also, don't write to disk after every feature vector is collected.<commit_after>package io.magnum.antimalware;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import io.magnum.antimalware.features.*;

import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;

/**
 * This collects the vectors
 * and periodically writes them to a file (if training
 * mode is enabled).
 * 
 * @author <NAME>
 * @version 2012.08.10
 */
public class MalwareService extends Service {
	private static final String TAG = "MalwareService";
	
	private final IBinder _mBinder = new MalwareBinder();
  private Time _time;
  private ScheduledExecutorService _scheduledExecutor;

  // All features are contained in _normalFeatures,
  // but some need to be tracked to set certain information
  private BatteryFeatures _battFeatures;
  private PermissionFeatures _permissionFeatures;
  private ArrayList<IFeatures> _normalFeatures;

  private WekaHelper _wh;
  private ArrayList<Attribute> _normalAttributes;
  private ArrayList<Attribute> _classAttributes;
  private ArrayList<Attribute> _attributes;
  private Instances _instances;

  private int numVectors = 0;
	
	@Override
	public void onCreate() {
		_time = new Time();

		_normalFeatures = new ArrayList<IFeatures>(6);
		
		_battFeatures = new BatteryFeatures();
		_normalFeatures.add(_battFeatures);
		
		_normalFeatures.add(new BinderFeatures());
		_normalFeatures.add(new CPUFeatures());
		_normalFeatures.add(new MemoryFeatures());
		_normalFeatures.add(new NetworkFeatures());
		
		_permissionFeatures = new PermissionFeatures(
				getPackageManager());
		_normalFeatures.add(_permissionFeatures);
		
		_wh = WekaHelper.getInstance();
		
		initWeka();
		initExecutor();
	}
	
	@SuppressWarnings("unchecked")
	private void initWeka() {
		_normalAttributes = new ArrayList<Attribute>();
		for (IFeatures feature : _normalFeatures ) {
			_normalAttributes.addAll(feature.getAttributes());
		}
		
		_classAttributes = new ArrayList<Attribute>();

		List<String> classVal = new ArrayList<String>(2);
		classVal.add("positive");
		classVal.add("negative");
		Attribute classAttribute = new Attribute("classification", classVal);
		_classAttributes.add(classAttribute);
		
		_attributes = _wh.addLists(_normalAttributes, _classAttributes);
		_instances = new Instances("relation", _attributes, 0);
    _wh.writeArff(_instances.toString());
	}

	/**
	 * Sets up retrieval at a fixed interval.
	 */
	private void initExecutor() {
		_scheduledExecutor = Executors.newScheduledThreadPool(1);
		
		_scheduledExecutor.scheduleAtFixedRate(new Runnable() {
			public void run() {
				fetchSnapshot();
			}
		}, 1, 5, TimeUnit.SECONDS); 
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return _mBinder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		return true;
	}
	
	/**
	 * Gets the data from all of the features and puts them in 
	 * a format for Weka to read.
	 */
	public void fetchSnapshot() {
		Log.d(TAG, "enter fetchSnapshot!");
    numVectors++;

    Thread t = new Thread() {
      public void run() {
        // TODO: No idea what I'm doing...
        synchronized (this) {
          _time.setToNow();

          IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
          _battFeatures.setIntent(registerReceiver(null, filter));

          ActivityManager am = (ActivityManager) getSystemService(
              Context.ACTIVITY_SERVICE );
          _permissionFeatures.setProcesses(am.getRunningAppProcesses());

          ArrayList<Instance> normalInstanceList = new ArrayList<Instance>(_normalAttributes.size());
          for (IFeatures features : _normalFeatures) {
            normalInstanceList.add(features.getInstance());
          }
          _wh.resetAttributes(_normalAttributes);
          Instance normalInstances = _wh.mergeInstances(normalInstanceList);

          _wh.resetAttributes(_classAttributes);
          Instance classInstance = new DenseInstance(1);
          classInstance.setValue(
              _classAttributes.get(0), _wh.getClassification() );

          _wh.resetAttributes(_attributes);
          _instances.add(_wh.mergeInstances(normalInstances, classInstance));
          _instances.setClassIndex(_attributes.size()-1);

          if (numVectors % 5 == 0) {
            _wh.writeArff(_instances.toString());
          }
        }
      }
    };
    t.start();
    Log.d(TAG, "exit fetchSnapshot!");
	}
	
	public class MalwareBinder extends Binder {
		MalwareService getService() {
			return MalwareService.this;
		}
	}
}
{'fraction_non_alphanumeric': 0.10121116377040548, 'fraction_numerical': 0.003896787783043707, 'mean_word_length': 4.404667046101309, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '3405250', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2656, 'n_words': 643}