<commit_before>/**
 * 
 */
package edu.vu.isis.ammo.core;

import java.util.Calendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import edu.vu.isis.ammo.api.AmmoDispatcher;
import edu.vu.isis.ammo.core.provider.DistributorSchema.SubscriptionTableSchema;

/**
 * This activity provides the operator a direct way of subscribing to content of interest.
 * He enters the subscription fields directly.
 * 
 * @author phreed
 *
 */
public class Subscribe extends Activity implements OnClickListener {
	// ===========================================================
	// Constants
	// ===========================================================
	public static final String LAUNCH = "edu.vu.isis.ammo.core.Subscribe.LAUNCH";
	public static final String MIME_OBJECT = "application/vnd.edu.vu.isis.ammo.map.object";
	
	// ===========================================================
	// Fields
	// ===========================================================
	private Spinner interestSpinner;
	private AmmoDispatcher ad = null;
	private MyOnItemSelectedListener selectionListener = null;
	private Button btnSubscribe;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.subscribe);
	    ad = AmmoDispatcher.getInstance(this);

	    interestSpinner = (Spinner) findViewById(R.id.subscribe_uri);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.content_of_interest_array, android.R.layout.simple_spinner_item);
	    
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    interestSpinner.setAdapter(adapter);
	    
	    selectionListener = new MyOnItemSelectedListener();
	    	
	    interestSpinner.setOnItemSelectedListener(selectionListener);
	    
	    btnSubscribe = (Button) findViewById(R.id.submit_content);
        btnSubscribe.setOnClickListener(this);
	}  
	    
	@Override
	public void onClick(View v) {
		if (v.equals(btnSubscribe)) {
			Uri selectedUri = selectionListener.getLastSelectedUri();
			String selectedMime = selectionListener.getMime();
        	if (selectedUri == null) {
        		Toast.makeText(Subscribe.this, "No content selected", Toast.LENGTH_SHORT).show();
        		return;
        	}
        	
            // Add item to subscription content provider.
        	ContentResolver cr = this.getContentResolver();
        	if (entryDoesNotExist(cr, selectedUri)) {
        		ContentValues values = new ContentValues();
            	values.put(SubscriptionTableSchema.URI, selectedUri.toString());
            	values.put(SubscriptionTableSchema.MIME, selectedMime);
            	cr.insert(SubscriptionTableSchema.CONTENT_URI, values);
            	
            	Toast.makeText(Subscribe.this, "Subscribed to content " + selectedUri.toString(), Toast.LENGTH_SHORT).show();
            	ad.pull(selectedUri, selectedMime, Calendar.MINUTE, 500, 10.0, ":event");	
        	} else {
        		Toast.makeText(Subscribe.this, "Already subscribed to this content", Toast.LENGTH_SHORT).show();
        	}
		}
	}
	
	private boolean entryDoesNotExist(ContentResolver cr, Uri selectedUri) {
		String[] projection = {SubscriptionTableSchema.URI, SubscriptionTableSchema._ID};
		String selection = SubscriptionTableSchema.URI + " LIKE \"%" + selectedUri.toString() + "%\"";
		Cursor c = cr.query(SubscriptionTableSchema.CONTENT_URI, projection, selection, null, null);
		return (c.getCount() == 0);
	}
	
	private class MyOnItemSelectedListener implements OnItemSelectedListener {
	    public Uri uri = null;
    	public String mime = null;
    	
    	
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        	uri = Uri.parse(parent.getItemAtPosition(pos).toString());
        	mime = MIME_OBJECT;
	        Toast.makeText(parent.getContext(), 
	    		  "The content uri is " + uri, 
	    		  Toast.LENGTH_SHORT).show();
	    }

	    public void onNothingSelected(AdapterView parent) {
	        uri = null;
	    }
	    
	    public Uri getLastSelectedUri() {<commit_msg>Modified QuickReport's content provider to hold destination group name and type. QuickReport now subscribes to its own mime types on startup. QuickReport incident posting explicitly defines mime type based on callsign specification (or lack thereof). Fixed submit button not disappearing bug.<commit_after>/**
 * 
 */
package edu.vu.isis.ammo.core;

import java.util.Calendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import edu.vu.isis.ammo.AmmoPrefKeys;
import edu.vu.isis.ammo.api.AmmoDispatcher;
import edu.vu.isis.ammo.collector.provider.IncidentSchema;
import edu.vu.isis.ammo.collector.provider.IncidentSchema.EventTableSchema;
import edu.vu.isis.ammo.core.provider.DistributorSchema.SubscriptionTableSchema;

/**
 * This activity provides the operator a direct way of subscribing to content of interest.
 * He enters the subscription fields directly.
 * 
 * @author phreed
 *
 */
public class Subscribe extends Activity implements OnClickListener {
	// ===========================================================
	// Constants
	// ===========================================================
	public static final String LAUNCH = "edu.vu.isis.ammo.core.Subscribe.LAUNCH";
	public static final String MIME_OBJECT = "application/vnd.edu.vu.isis.ammo.map.object";
	
	// ===========================================================
	// Fields
	// ===========================================================
	private Spinner interestSpinner;
	private AmmoDispatcher ad = null;
	private MyOnItemSelectedListener selectionListener = null;
	private Button btnSubscribe;
	private String uid;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.subscribe);
	    ad = AmmoDispatcher.getInstance(this);

	    interestSpinner = (Spinner) findViewById(R.id.subscribe_uri);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.content_of_interest_array, android.R.layout.simple_spinner_item);
	    
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    interestSpinner.setAdapter(adapter);
	    
	    selectionListener = new MyOnItemSelectedListener();
	    	
	    interestSpinner.setOnItemSelectedListener(selectionListener);
	    
	    btnSubscribe = (Button) findViewById(R.id.submit_content);
        btnSubscribe.setOnClickListener(this);
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        this.uid = prefs.getString(AmmoPrefKeys.PREF_OPERATOR_ID, "foo");
	}  
	    
	@Override
	public void onClick(View v) {
		if (v.equals(btnSubscribe)) {
			Uri selectedUri = selectionListener.getLastSelectedUri();
			String selectedMime = selectionListener.getMime();
        	if (selectedUri == null) {
        		Toast.makeText(Subscribe.this, "No content selected", Toast.LENGTH_SHORT).show();
        		return;
        	}
        	
            // Add item to subscription content provider.
        	ContentResolver cr = this.getContentResolver();
        	if (entryDoesNotExist(cr, selectedUri)) {
        		ContentValues values = new ContentValues();
            	values.put(SubscriptionTableSchema.URI, selectedUri.toString());
            	values.put(SubscriptionTableSchema.MIME, selectedMime);
            	values.put(SubscriptionTableSchema.DISPOSITION, SubscriptionTableSchema.DISPOSITION_PENDING);
            	cr.insert(SubscriptionTableSchema.CONTENT_URI, values);
            	
            	Toast.makeText(Subscribe.this, "Subscribed to content " + selectedUri.toString(), Toast.LENGTH_SHORT).show();
            	ad.subscribe(selectedUri, selectedMime, Calendar.MINUTE, 500, 10.0, ":event");	
        	} else {
        		Toast.makeText(Subscribe.this, "Already subscribed to this content", Toast.LENGTH_SHORT).show();
        	}
		}
	}
	
	private boolean entryDoesNotExist(ContentResolver cr, Uri selectedUri) {
		String[] projection = {SubscriptionTableSchema.URI, SubscriptionTableSchema._ID};
		String selection = SubscriptionTableSchema.URI + " LIKE \"" + selectedUri.toString() + "\"";
		Cursor c = cr.query(SubscriptionTableSchema.CONTENT_URI, projection, selection, null, null);
		return (c.getCount() == 0);
	}
	
	private class MyOnItemSelectedListener implements OnItemSelectedListener {
	    public Uri uri = null;
    	public String mime = null;
    	
    	
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        	uri = Uri.parse(parent.getItemAtPosition(pos).toString());
        	
        	if (uri.equals(IncidentSchema.EventTableSchema.CONTENT_URI)) {
        		mime = EventTableSchema.CONTENT_TOPIC + "_" + uid;
        	} else {
        		mime = MIME_OBJECT + "_" + uid;
        	}
	        Toast.makeText(parent.getContext(), 
	    		  "The content uri is " + uri, 
	    		  Toast.LENGTH_SHORT).show();
	    }

	    public void onNothingSelected(AdapterView parent) {
	        uri = null;
	    }
	    
	    public Uri getLastSelectedUri() {
{'fraction_non_alphanumeric': 0.14042764793635007, 'fraction_numerical': 0.00139234211834908, 'mean_word_length': 3.790852787041448, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '3219371', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2723, 'n_words': 576}