package com.lukekorth.pebblelocker;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

public class LogReporting {

	private Context mContext;
	
	private ProgressDialog mLoading;
	
	public LogReporting(Context context) {
		mContext = context;
	}
	
	public void collectAndSendLogs() {
		mLoading = ProgressDialog.show(mContext, "", "Loading. Please wait...", true);
		new GenerateLogFile().execute();
	}
	
	private class GenerateLogFile extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... args) {
			String filename = "pebble-locker.log";
			StringBuilder message = new StringBuilder();
			
			Map<String,?> keys = PreferenceManager.getDefaultSharedPreferences(mContext).getAll();
			for(Map.Entry<String,?> entry : keys.entrySet()) {
				if(!entry.getKey().equals("key_password")) {
					message.append(entry.getKey() + " : " + entry.getValue().toString() + "\n");
				}
			}	
			message.append("\n\n");
			message.append(new Logger(mContext).getLog());
			
			try {
				File file = new File(mContext.getExternalFilesDir(null), filename);
				file.createNewFile();
				
				FileWriter out = new FileWriter(file);
	            out.write(message.toString());
	            out.close();
			} catch (IOException e) {
			}
			
			return filename;
		}

		@Override
		protected void onPostExecute(String filename) {
			if(mLoading != null && mLoading.isShowing())
				mLoading.cancel();
			
			Uri fileUri = Uri.fromFile(new File(mContext.getExternalFilesDir(null), filename));
			
		    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		    emailIntent.setType("text/plain");
		    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "korth.luke@gmail.com" });
		    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Pebble Locker Debug Log"); 
		    emailIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
		    mContext.startActivity(Intent.createChooser(emailIntent, "Send email via"));
		}
	}
}

@@ -227,16 +227,122 @@
 Intent;%0A
+import android.content.pm.PackageManager;%0Aimport android.content.pm.PackageManager.NameNotFoundException;%0A
 import a
@@ -357,16 +357,16 @@
 et.Uri;%0A
-
 import a
@@ -386,16 +386,41 @@
 ncTask;%0A
+import android.os.Build;%0A
 import a
@@ -950,16 +950,16 @@
 r.log%22;%0A
-
 %09%09%09Strin
@@ -1002,16 +1002,680 @@
 ();%0A%09%09%09%0A
+%09%09%09PackageManager pManager = mContext.getPackageManager();%0A%09%09%09String lockerVersion;%0A%09%09%09try %7B%0A%09%09%09%09lockerVersion = pManager.getPackageInfo(mContext.getPackageName(), 0).versionName;%0A%09%09%09%7D catch (NameNotFoundException e1) %7B%0A%09%09%09%09lockerVersion = %22not found%22;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09String pebbleVersion;%0A%09%09%09try %7B%0A%09%09%09%09pebbleVersion = pManager.getPackageInfo(%22com.getpebble.android%22, 0).versionName;%0A%09%09%09%7D catch (NameNotFoundException e1) %7B%0A%09%09%09%09pebbleVersion = %22not found%22;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09message.append(%22Android version: %22 + Build.VERSION.SDK_INT + %22%5Cn%22);%0A%09%09%09message.append(%22App version: %22 + lockerVersion  + %22%5Cn%22);%0A%09%09%09message.append(%22Pebble app version: %22 + pebbleVersion + %22%5Cn%22);%0A%09%09%09%0A
 %09%09%09Map%3CS

Add Android and app versions to debug log