<commit_before>package com.powerblock.timesheets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.powerblock.timesheets.fragments.EquipmentFragment;
import com.powerblock.timesheets.fragments.JobSetupFragment;
import com.powerblock.timesheets.fragments.MainFragment;
import com.powerblock.timesheets.fragments.MaterialsFragment;
import com.powerblock.timesheets.fragments.MaterialsLighting;
import com.powerblock.timesheets.fragments.MaterialsPower;
import com.powerblock.timesheets.fragments.SafetyFragment;
import com.powerblock.timesheets.fragments.TestingFragment;
import com.powerblock.timesheets.fragments.TimeFragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("InflateParams")
public class MainActivity extends ActionBarActivity {
	
	public static final String root = Environment.getExternalStorageDirectory().toString();
	public final static String workingTemplateDir = "/.workingTemplate";
	public final static String workingTemplateFileName="workingTemplate.xls";
	public final static String outputDir = root + "/Time Sheets";
	private static ExcelHandler mExcelHandler;
	private static XmlHandler mXmlHandler;
	private static Activity thisActivity;
	private File workingTemplate;
	private File timeSheets[];
	private String timeSheetNames[];
	private String openFileName = "NoFileOpen";
	private ProgressDialog mProgressDialog;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		thisActivity = this;
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new MainFragment()).commit();
		}
		File myDir = new File(root + workingTemplateDir);
		if(!myDir.exists()){
			myDir.mkdirs();
		}
		workingTemplate = new File(myDir, workingTemplateFileName);
		checkAndCreateTemplate();
		mExcelHandler = new ExcelHandler();
		mXmlHandler = new XmlHandler(this);
	}
	
	public static XmlHandler getXmlHandler(){
		if(mXmlHandler != null){
			return mXmlHandler;
		} else {
			Toast.makeText(thisActivity, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return null;
		}
	}
	
	public static ExcelHandler getExcelHandler(){
		if(mExcelHandler != null){
			return mExcelHandler;
		} else {
			Toast.makeText(thisActivity, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return null;
		}
	}
	
	public void updateCurrentStrings(){
		final DownloadTask downTask = new DownloadTask(this);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage("Updating");
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setCancelable(true);
		mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				downTask.cancel(true);
			}
		});
		downTask.execute("https://docs.google.com/uc?export=download&id=0B7ZA1yW79zKtdlYxTWpNemcwN2c");
		// Old link downTask.execute("https://docs.google.com/uc?export=download&id=0B7ZA1yW79zKtNmRHWjFTMkNwREk");
	}
	
	public void checkAndCreateTemplate(){
		InputStream in = null;
		OutputStream out = null;
		if(!workingTemplate.exists()){
			try{
				in = getResources().openRawResource(R.raw.time_sheets_template);
				out = new FileOutputStream(workingTemplate);
				
				byte[] buffer = new byte[1024];
				int read;
				while((read = in.read(buffer)) != - 1){
					out.write(buffer, 0, read);
				}
				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			} catch(Exception e){
				
			}
		}
	}
	
	public void show(int id, Fragment frag, String back){
		getSupportFragmentManager().beginTransaction().replace(id, frag).addToBackStack(back).commit();
	}
	
	public void showJobSetup(View v){
		show(R.id.container, new JobSetupFragment(),"Job Setup");
	}
	
	public void showSafety(View v){
		show(R.id.container, new SafetyFragment(),"Safety");
	}
	
	public void showTime(View v){
		show(R.id.container, new TimeFragment(),"Time");
	}
	
	public void showEquipment(View v){
		show(R.id.container, new EquipmentFragment(),"Equipment");
	}
	
	public void showMaterials(View v){
		show(R.id.container, new MaterialsFragment(),"Materials");
	}
	
	public void showTesting(View v){
		show(R.id.container, new TestingFragment(),"Testing");
	}
	
	public void showMaterialsLighting(View v){
		show(R.id.container, new MaterialsLighting(),"Materials");
	}
	
	public void showMaterialsPower(View v){
		show(R.id.container,new MaterialsPower(), "Power");
	}
	
	public void saveTimeSheet(String fileName){
		if(fileName.equalsIgnoreCase("Error")){
			Toast.makeText(this, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return;
		}
		Log.v("Test","Save called, filename = " + fileName);
		//workingTemplate.delete();
		InputStream in = null;
		OutputStream out = null;
		File outputDir = new File(MainActivity.outputDir);
		File output = new File(outputDir, fileName);
		Log.v("Test", "Output = " + output.toString());
		if(!outputDir.exists()){
			outputDir.mkdirs();
		}
		try{
			in = new FileInputStream(workingTemplate);
			out = new FileOutputStream(output);
			
			byte[] buffer = new byte[1024];
			int read;
			while((read = in.read(buffer)) != - 1){
				out.write(buffer, 0, read);
			}
			in.close();
			in = null;
			out.flush();
			out.close();
			out = null;<commit_msg>Update MainActivity.java<commit_after>package com.powerblock.timesheets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.powerblock.timesheets.fragments.*;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("InflateParams")
public class MainActivity extends ActionBarActivity {
	
	public static final String root = Environment.getExternalStorageDirectory().toString();
	public final static String workingTemplateDir = "/.workingTemplate";
	public final static String workingTemplateFileName="workingTemplate.xls";
	public final static String outputDir = root + "/Time Sheets";
	private static ExcelHandler mExcelHandler;
	private static XmlHandler mXmlHandler;
	private static Activity thisActivity;
	private File workingTemplate;
	private File timeSheets[];
	private String timeSheetNames[];
	private String openFileName = "NoFileOpen";
	private ProgressDialog mProgressDialog;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		thisActivity = this;
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new MainFragment()).commit();
		}
		File myDir = new File(root + workingTemplateDir);
		if(!myDir.exists()){
			myDir.mkdirs();
		}
		workingTemplate = new File(myDir, workingTemplateFileName);
		checkAndCreateTemplate();
		mExcelHandler = new ExcelHandler();
		mXmlHandler = new XmlHandler(this);
	}
	
	public static XmlHandler getXmlHandler(){
		if(mXmlHandler != null){
			return mXmlHandler;
		} else {
			Toast.makeText(thisActivity, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return null;
		}
	}
	
	public static ExcelHandler getExcelHandler(){
		if(mExcelHandler != null){
			return mExcelHandler;
		} else {
			Toast.makeText(thisActivity, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return null;
		}
	}
	
	public void updateCurrentStrings(){
		final DownloadTask downTask = new DownloadTask(this);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage("Updating");
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setCancelable(true);
		mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				downTask.cancel(true);
			}
		});
		downTask.execute("https://docs.google.com/uc?export=download&id=0B7ZA1yW79zKtdlYxTWpNemcwN2c");
		// Old link downTask.execute("https://docs.google.com/uc?export=download&id=0B7ZA1yW79zKtNmRHWjFTMkNwREk");
	}
	
	public void checkAndCreateTemplate(){
		InputStream in = null;
		OutputStream out = null;
		if(!workingTemplate.exists()){
			try{
				in = getResources().openRawResource(R.raw.time_sheets_template);
				out = new FileOutputStream(workingTemplate);
				
				byte[] buffer = new byte[1024];
				int read;
				while((read = in.read(buffer)) != - 1){
					out.write(buffer, 0, read);
				}
				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			} catch(Exception e){
				
			}
		}
	}
	
	public void show(int id, Fragment frag, String back){
		getSupportFragmentManager().beginTransaction().replace(id, frag).addToBackStack(back).commit();
	}
	
	public void showJobSetup(View v){
		show(R.id.container, new JobSetupFragment(),"Job Setup");
	}
	
	public void showSafety(View v){
		show(R.id.container, new SafetyFragment(),"Safety");
	}
	
	public void showTime(View v){
		show(R.id.container, new TimeFragment(),"Time");
	}
	
	public void showEquipment(View v){
		show(R.id.container, new EquipmentFragment(),"Equipment");
	}
	
	public void showMaterials(View v){
		show(R.id.container, new MaterialsFragment(),"Materials");
	}
	
	public void showTesting(View v){
		show(R.id.container, new TestingFragment(),"Testing");
	}
	
	public void showMaterialsLighting(View v){
		show(R.id.container, new MaterialsLighting(),"Materials");
	}
	
	public void showMaterialsPower(View v){
		show(R.id.container,new MaterialsPower(), "Power");
	}
	
	public void showMaterialsData(View v){
		show(R.id.container, new MaterialsData(), "Data");
	}
	
	public void showMaterialsContainment(View v){
		show(R.id.container, new MaterialsContainment(), "Containment");
	}
	
	public void saveTimeSheet(String fileName){
		if(fileName.equalsIgnoreCase("Error")){
			Toast.makeText(this, "Error, please contact developer", Toast.LENGTH_LONG).show();
			return;
		}
		Log.v("Test","Save called, filename = " + fileName);
		//workingTemplate.delete();
		InputStream in = null;
		OutputStream out = null;
		File outputDir = new File(MainActivity.outputDir);
		File output = new File(outputDir, fileName);
		Log.v("Test", "Output = " + output.toString());
		if(!outputDir.exists()){
			outputDir.mkdirs();
		}
		try{
			in = new FileInputStream(workingTemplate);
			out = new FileOutputStream(output);
			
			byte[] buffer = new byte[1024];
			int read;
			while((read = in.read(buffer)) != - 1){
				out.write(buffer, 0, read);
			}
			in.close();
			in = null;
			out.flush();
			out.close();
			out = null;
{'fraction_non_alphanumeric': 0.12217751867969455, 'fraction_numerical': 0.004105427374989736, 'mean_word_length': 5.014814814814815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306307', 'n_tokens_mistral': 4179, 'n_tokens_neox': 3654, 'n_words': 794}