<commit_before>package com.brogrammers.agora.views;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.brogrammers.agora.Agora;
import com.brogrammers.agora.R;
import com.brogrammers.agora.R.id;
import com.brogrammers.agora.R.layout;
import com.brogrammers.agora.R.menu;
import com.brogrammers.agora.data.QuestionController;
import com.brogrammers.agora.helper.ImageGetter;
import com.brogrammers.agora.helper.ImageResizer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Activity for posting a question to the app. Displays two text boxes for title
 * and body input.
 * 
 * Todo: implement picturehandler to post pictures to a question.
 * 
 * @author Group02
 * 
 */
public class AuthorQuestionActivity extends Activity {

	protected Uri imageUri = null;
	protected byte[] image = null;

	/**
	 * Retrieves button layouts and activity author question layout.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_author_question);

		Button addQuestion = (Button) findViewById(R.id.authorQuestionAddQuestionButton);
		Button addPicture = (Button) findViewById(R.id.authorQuestionAddPictureButton);

		addQuestion.setOnClickListener(questionHandler);
		addPicture.setOnClickListener(pictureHandler);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.author_question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Retrieves text from title and body editTexts and adds question via
	 * controller.
	 */
	View.OnClickListener questionHandler = new View.OnClickListener() {
		public void onClick(View v) {
			// add question
			EditText titleText = (EditText) findViewById(R.id.authorQuestionEditText);
			EditText bodyText = (EditText) findViewById(R.id.authorQuestionBodyEditText);

			String title = titleText.getText().toString();
			String body = bodyText.getText().toString();
			if (title.isEmpty() || body.isEmpty()) {
				Toast.makeText(Agora.getContext(), "Please include a title and a description", 1).show();
				return;
			} else {
				Toast.makeText(Agora.getContext(), "Adding Question!", Toast.LENGTH_SHORT).show();
			}

			QuestionController.getController().addQuestion(title, body, image);
			finish(); // ends activity
		}
	};
	
	View.OnClickListener pictureHandler = new View.OnClickListener() {
		public void onClick(View v) {
			ImageGetter imageGetter = new ImageGetter(AuthorQuestionActivity.this);
			imageUri = imageGetter.getUri();
			imageGetter.getImage(imageUri);
		}
	};

	// ImageGetter camera activity callback
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ImageGetter.CAMERA_ACTIVITY_REQUEST_CODE
				&& resultCode == RESULT_OK) {
			image = ImageResizer.resize(imageUri);
			(new File(imageUri.getPath())).delete(); // delete the original file
			ImageView iv = (ImageView) findViewById(R.id.AuthorQuestionImage);

			Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(
					BitmapFactory.decodeStream(new ByteArrayInputStream(image)), 480, 360);
			iv.setImageBitmap(ThumbImage);
			
		} else if (resultCode != RESULT_CANCELED) {
			Toast.makeText(this, "Error when adding image.", 0).show();
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
	   super.onSaveInstanceState(outState);
	   outState.putString("uri", imageUri.getPath());
	   Log.e("ONSAVEDINSTANCESTATE", "AQA ONSAVE");
	}
	
	@Override
	 protected void onRestoreInstanceState(Bundle savedInstanceState) {
	     super.onRestoreInstanceState(savedInstanceState);
	     imageUri = Uri.parse(savedInstanceState.getString("uri"));
	     Log.e("ONRESTOREINSTANCESTATE", "AQA onRestoreInstanceState()");
	 }
	
	<commit_msg>Added test in author question activity for location - does not work<commit_after>package com.brogrammers.agora.views;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.brogrammers.agora.Agora;
import com.brogrammers.agora.R;
import com.brogrammers.agora.R.id;
import com.brogrammers.agora.R.layout;
import com.brogrammers.agora.R.menu;
import com.brogrammers.agora.data.QuestionController;
import com.brogrammers.agora.helper.ImageGetter;
import com.brogrammers.agora.helper.ImageResizer;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Activity for posting a question to the app. Displays two text boxes for title
 * and body input.
 * 
 * Todo: implement picturehandler to post pictures to a question.
 * 
 * @author Group02
 * 
 */
public class AuthorQuestionActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {

	protected Uri imageUri = null;
	protected byte[] image = null;
	private LocationClient mLocationClient;
	private Location mCurrentLocation;

	/**
	 * Retrieves button layouts and activity author question layout.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_author_question);
		mLocationClient = new LocationClient(this, this, this);

		Button addQuestion = (Button) findViewById(R.id.authorQuestionAddQuestionButton);
		Button addPicture = (Button) findViewById(R.id.authorQuestionAddPictureButton);

		addQuestion.setOnClickListener(questionHandler);
		addPicture.setOnClickListener(pictureHandler);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.author_question, menu);
		return true;
	}
	
	   @Override
	    protected void onStart() {
	        super.onStart();
	        // Connect the client.
	        mLocationClient.connect();
	    }
	   
	    @Override
	    protected void onStop() {
	        // Disconnecting the client invalidates it.
	        mLocationClient.disconnect();
	        super.onStop();
	    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			toastLocationTest();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void toastLocationTest() {
		Toast.makeText(Agora.getContext(), "Location Test!", Toast.LENGTH_SHORT).show();
		mLocationClient.getLastLocation();

	}

	/**
	 * Retrieves text from title and body editTexts and adds question via
	 * controller.
	 */
	View.OnClickListener questionHandler = new View.OnClickListener() {
		public void onClick(View v) {
			// add question
			EditText titleText = (EditText) findViewById(R.id.authorQuestionEditText);
			EditText bodyText = (EditText) findViewById(R.id.authorQuestionBodyEditText);

			String title = titleText.getText().toString();
			String body = bodyText.getText().toString();
			if (title.isEmpty() || body.isEmpty()) {
				Toast.makeText(Agora.getContext(), "Please include a title and a description", 1).show();
				return;
			} else {
				Toast.makeText(Agora.getContext(), "Adding Question!", Toast.LENGTH_SHORT).show();
			}

			QuestionController.getController().addQuestion(title, body, image);
			finish(); // ends activity
		}
	};
	
	View.OnClickListener pictureHandler = new View.OnClickListener() {
		public void onClick(View v) {
			ImageGetter imageGetter = new ImageGetter(AuthorQuestionActivity.this);
			imageUri = imageGetter.getUri();
			imageGetter.getImage(imageUri);
		}
	};

	// ImageGetter camera activity callback
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ImageGetter.CAMERA_ACTIVITY_REQUEST_CODE
				&& resultCode == RESULT_OK) {
			image = ImageResizer.resize(imageUri);
			(new File(imageUri.getPath())).delete(); // delete the original file
			ImageView iv = (ImageView) findViewById(R.id.AuthorQuestionImage);

			Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(
					BitmapFactory.decodeStream(new ByteArrayInputStream(image)), 480, 360);
			iv.setImageBitmap(ThumbImage);
			
		} else if (resultCode != RESULT_CANCELED) {
			Toast.makeText(this, "Error when adding image.", 0).show();
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
	   super.onSaveInstanceState(outState);
	   outState.putString("uri", imageUri.getPath());
	   Log.e("ONSAVEDINSTANCESTATE", "AQA ONSAVE");
	}
	
	@Override
	 protected void onRestoreInstanceState(Bundle savedInstanceState) {
	     super.onRestoreInstanceState(savedInstanceState);
	     imageUri = Uri.parse(savedInstanceState.getString("uri"));
	     Log.e("ONRESTOREINSTANCESTATE", "AQA onRestoreInstanceState()");
	 }

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		
	}
	
	
{'fraction_non_alphanumeric': 0.10411449016100179, 'fraction_numerical': 0.002325581395348837, 'mean_word_length': 4.701682814890362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6345715', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3187, 'n_words': 777}