/*
 * File Chooser Class for VX ConnectBot
 * Copyright 2012 Martin Matuska
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sk.vx.connectbot.util;

import java.io.File;
import java.net.URI;

import org.openintents.intents.FileManagerIntents;

import sk.vx.connectbot.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.lamerman.FileDialog;
import com.lamerman.SelectionMode;

public class FileChooser {
	public final static String TAG = "ConnectBot.FileChooser";

	public static final int REQUEST_CODE_SELECT_FILE = 1;

	// Constants for AndExplorer's file picking intent
	private static final String ANDEXPLORER_TITLE = "explorer_title";
	private static final String MIME_TYPE_ANDEXPLORER_FILE = "vnd.android.cursor.dir/lysesoft.andexplorer.file";

	public static void selectFile(Activity source, FileChooserCallback callback, int requestcode) {
		selectFile(source, callback, requestcode, null);
	}

	public static void selectFile(Activity source, FileChooserCallback callback, int requestcode, String title) {
		final File sdcard = Environment.getExternalStorageDirectory();
		if (title == null)
			title = source.getString(R.string.file_chooser_select_file);
		int mode = SelectionMode.MODE_OPEN;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(source);
		Intent intent;
		String filedialog;

		if (prefs == null)
			return;
		filedialog = prefs.getString(PreferenceConstants.FILE_DIALOG, "built-in");

		if (filedialog.equals("OI")) {
			intent = new Intent(FileManagerIntents.ACTION_PICK_FILE);
			intent.setData(Uri.fromFile(sdcard));
			intent.putExtra(FileManagerIntents.EXTRA_TITLE, title);
			intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, source.getString(android.R.string.ok));

			try {
				source.startActivityForResult(intent, requestcode);
				return;
			} catch (ActivityNotFoundException e1) {
				Toast.makeText(source,
						source.getString(R.string.error_starting_app,"OpenIntents File Manager"),
						Toast.LENGTH_LONG).show();
			}
		} else if (filedialog.equals("AE")) {
			intent = new Intent(Intent.ACTION_PICK);
			intent.setDataAndType(Uri.fromFile(sdcard), MIME_TYPE_ANDEXPLORER_FILE);
			intent.putExtra(ANDEXPLORER_TITLE, title);

			try {
				source.startActivityForResult(intent, requestcode);
				return;
			} catch (ActivityNotFoundException e1) {
				Toast.makeText(source,
						source.getString(R.string.error_starting_app,"AndExplorer"),
						Toast.LENGTH_LONG).show();
			}
		}
		intent = new Intent(source.getBaseContext(), FileDialog.class);
		intent.putExtra(FileDialog.START_PATH, sdcard.toString());
		intent.putExtra(FileDialog.TITLE, title);
		intent.putExtra(FileDialog.SELECTION_MODE, mode);
		source.startActivityForResult(intent, requestcode);
	}

	public static File getSelectedFile(Intent intent) {
		File file = null;

		if (intent == null)
			return null;

		Uri uri = intent.getData();
		try {
			if (uri != null) {
				file = new File(URI.create(uri.toString()));
			} else {
				String filename = intent.getDataString();
				if (filename != null)
					file = new File(URI.create(filename));
			}
		} catch (IllegalArgumentException e) {
			Log.e(TAG, "Couldn't read selected file", e);
			return null;
		}

		return file;
	}
}
@@ -2083,18 +2083,25 @@
 t intent
+ = null
 ;%0A
-
 %09%09String
@@ -2112,16 +2112,43 @@
 edialog;
+%0A%09%09String appString = null;
 %0A%0A%09%09if (
@@ -2280,24 +2280,67 @@
 ls(%22OI%22)) %7B%0A
+%09%09%09appString = %22OpenIntents File Manager%22;%0A
 %09%09%09intent = 
@@ -2588,275 +2588,8 @@
 k));
-%0A%0A%09%09%09try %7B%0A%09%09%09%09source.startActivityForResult(intent, requestcode);%0A%09%09%09%09return;%0A%09%09%09%7D catch (ActivityNotFoundException e1) %7B%0A%09%09%09%09Toast.makeText(source,%0A%09%09%09%09%09%09source.getString(R.string.error_starting_app,%22OpenIntents File Manager%22),%0A%09%09%09%09%09%09Toast.LENGTH_LONG).show();%0A%09%09%09%7D
 %0A%09%09%7D
@@ -2625,16 +2625,46 @@
 AE%22)) %7B%0A
+%09%09%09appString = %22AndExplorer%22;%0A
 %09%09%09inten
@@ -2821,17 +2821,65 @@
 title);%0A
+%09%09%7D%0A%09%09if (intent != null && appString != null) %7B
 %0A
-
 %09%09%09try %7B
@@ -3069,29 +3069,26 @@
 ing_app,
-%22AndExplorer%22
+ appString
 ),%0A%09%09%09%09%09

Simplify code in FileChooser.java