package com.michalmazur.orphanedtexts;

import java.util.ArrayList;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.AlertDialog.Builder;

public class OrphanedTextsActivity extends Activity {
	
	String uriString;
	Uri uri;
	private String output;

    public OrphanedTextsActivity() {
		super();

		uriString = "content://sms/raw";
		uri = Uri.parse(uriString);
		output = "";
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        RawSmsReader reader = new RawSmsReader(this.getApplicationContext());
//        RawSmsReader reader = new RawSmsReader();
        
        ArrayList<Orphan> orphans = reader.getOrphans();
        setContentView(R.layout.main);
        output = new CsvConverter().convert(orphans);
        ListView lv = (ListView)findViewById(R.id.listView1);
    	ArrayList<String> items = new ArrayList<String>();
    	for (Orphan o: orphans) {
    		items.add(o.getAddress() + " on " + o.getDate().toLocaleString() + ":\n" + o.getMessageBody() + "\n");
    	}
    	lv.setAdapter(new ArrayAdapter<String>(this, R.layout.lvitem, items));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.email:
                email();
            case R.id.delete_all:
                deleteAll();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public void deleteAll() {
    	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					deleteAllRecords();
					break;
				}
			}
		};
		
		Builder builder = new Builder(this);
		builder.setMessage("Are you sure you want to delete all orphaned messages?")
			.setPositiveButton("Yes", dialogClickListener)
			.setNegativeButton("No", dialogClickListener)
			.show();
    }
    

    public void deleteAllRecords()
    {
        getContentResolver().query(uri, null, null, null, null /* "_id limit 10" */);
        getContentResolver().delete(uri, null, null);
        // TODO: Refresh the list of messages to show that there are none.
    }
    
   
    public void email()
    {
    	Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    	emailIntent.setType("plain/text");
    	emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Orphaned Texts");
    	emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, output);  
    	startActivity(emailIntent);	
    }    
}

@@ -544,16 +544,44 @@
  output;
+%0A%09ArrayList%3COrphan%3E orphans;
 %0A%0A    pu
@@ -978,26 +978,8 @@
     
- ArrayList%3COrphan%3E
  orp
@@ -1111,338 +1111,26 @@
     
-ListView lv = (ListView)findViewById(R.id.listView1);%0A    %09ArrayList%3CString%3E items = new ArrayList%3CString%3E();%0A    %09for (Orphan o: orphans) %7B%0A    %09%09items.add(o.getAddress() + %22 on %22 + o.getDate().toLocaleString() + %22:%5Cn%22 + o.getMessageBody() + %22%5Cn%22);%0A    %09%7D%0A    %09lv.setAdapter(new ArrayAdapter%3CString%3E(this, R.layout.lvitem, items)
+displayOrphanList(
 );%0A 
@@ -1654,24 +1654,415 @@
 %0A    %7D%0A    %0A
+    public void displayOrphanList() %7B%0A    %09ListView lv = (ListView)findViewById(R.id.listView1);%0A    %09ArrayList%3CString%3E items = new ArrayList%3CString%3E();%0A    %09for (Orphan o: orphans) %7B%0A    %09%09items.add(o.getAddress() + %22 on %22 + o.getDate().toLocaleString() + %22:%5Cn%22 + o.getMessageBody() + %22%5Cn%22);%0A    %09%7D%0A    %09lv.setAdapter(new ArrayAdapter%3CString%3E(this, R.layout.lvitem, items));%0A    %7D    %0A    %0A
     public v
@@ -2339,16 +2339,42 @@
 ords();%0A
+%09%09%09%09%09displayOrphanList();%0A
 %09%09%09%09%09bre
@@ -2830,74 +2830,24 @@
     
-// TODO: Refresh the list of messages to show that there are none.
+orphans.clear();
 %0A   

Remove all orphaned messages from list when they are deleted.