package com.example.androidfileexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	private static final String tag = "MainActivity";
	private static final File ROOT_DIR = File.listRoots()[0];
	private File mCwd;
	private ArrayAdapter<File> mEntryAdapter;
	private List<File> mEntries;

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

		mEntries = new ArrayList<File>();
		mEntryAdapter = new DirListArrayAdapter(this, R.id.entry, mEntries);

		Button upButton = (Button) findViewById(R.id.up);
		upButton.setOnClickListener(this);
		ListView listView = (ListView) findViewById(R.id.entry_list_view);
		listView.setAdapter(mEntryAdapter);
		listView.setOnItemClickListener(this);

		mCwd = ROOT_DIR;
		updateEntriesAndViews();
	}

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

	private void updateEntriesAndViews() {
		mEntries.clear();
		File[] tmp = mCwd.listFiles(Util.onlyDirFilter());
		if (tmp != null) {
			Arrays.sort(tmp);
			mEntries.addAll(Arrays.asList(tmp));
		}
		tmp = mCwd.listFiles(Util.onlyFileFilter());
		if (tmp != null) {
			Arrays.sort(tmp);
			mEntries.addAll(Arrays.asList(tmp));
		}
		mEntryAdapter.notifyDataSetChanged();
		TextView cwdView = (TextView) findViewById(R.id.cwd);
		cwdView.setText("[" + mCwd.toString() + "]");
	}

	@Override
	public void onItemClick(AdapterView<?> listView, View view, int pos, long id) {
		File f = (File) listView.getItemAtPosition(pos);
		if (f.isDirectory()) {
			mCwd = new File(f.toString());
			updateEntriesAndViews();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.up:
			String p = mCwd.getParent();
			mCwd = (p != null ? new File(p) : ROOT_DIR);
			updateEntriesAndViews();
			break;
		}
	}
}

@@ -2373,67 +2373,191 @@
 d on
-Click(View v) %7B%0D%0A%09%09switch (v.getId()) %7B%0D%0A%09%09case R.id.up:%0D%0A%09
+BackPressed() %7B%0D%0A%09%09moveUp();%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public void onClick(View v) %7B%0D%0A%09%09switch (v.getId()) %7B%0D%0A%09%09case R.id.up:%0D%0A%09%09%09moveUp();%0D%0A%09%09%09break;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09private void moveUp() %7B%0D%0A
 %09%09St
@@ -2584,17 +2584,16 @@
 ent();%0D%0A
-%09
 %09%09mCwd =
@@ -2630,25 +2630,24 @@
 OT_DIR);%0D%0A%09%09
-%09
 updateEntrie
@@ -2660,31 +2660,15 @@
 ews();%0D%0A
-%09%09%09break;%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%7D%0D%0A

make back key go one dir up