Implementation of import button and FileBrowser

This commit is contained in:
Ziver Koc 2014-08-19 14:30:51 +02:00
parent 00ba6a4bf0
commit 1112dde4d8
14 changed files with 1356 additions and 3 deletions

View file

@ -20,6 +20,7 @@ import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing;
import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment;
import com.ericsson.uecontrol.gui.fragments.FileBrowserDialog;
import com.ericsson.uecontrol.gui.fragments.FileBrowserDialog.OnFileSelectionListener;
import com.ericsson.uecontrol.gui.fragments.StatusFragment;
import com.ericsson.uecontrol.gui.util.CSVWriter;
@ -31,7 +32,8 @@ import java.io.File;
import de.mindpipe.android.logging.log4j.LogConfigurator;
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener,
OnFileSelectionListener {
private static final Logger log = Logger.getLogger(MainActivity.class);
public static final String DEFAULT_LOG_PATH = "/sdcard/uecontrol/";
public static final String BEHAVIOUR_SAVE_FILE = "behaviour_list.json";
@ -203,7 +205,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
return true;
}
else if (id == R.id.action_import) {
//TODO:
FileBrowserDialog browser = FileBrowserDialog.newInstance("/sdcard");
browser.show(this.getFragmentManager(), "import");
}
else if (id == R.id.action_export) {
//TODO:
@ -214,6 +217,16 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
return super.onOptionsItemSelected(item);
}
public void onFileSelection(String tag, File file){
try {
if(tag.equals("import")) {
currentExecutor.read(file.getAbsolutePath());
}
} catch (Exception e) {
log.error("Unable to import from: "+file.getAbsolutePath(), e);
Toast.makeText(this, "Unable to import from file", Toast.LENGTH_SHORT).show();
}
}
private void updateExecutionState(){

View file

@ -0,0 +1,233 @@
package com.ericsson.uecontrol.gui.fragments;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.ericsson.uecontrol.R;
import org.apache.log4j.Logger;
/** Allow user to select destination directory and to enter filename.
*
* */
public class FileBrowserDialog extends DialogFragment
implements OnItemClickListener, DialogInterface.OnKeyListener {
public static final Logger log = Logger.getLogger(FileBrowserDialog.class);
public static final int FOLDER_ICON = R.drawable.folder;
public static final int FILE_ICON = R.drawable.file;
public static interface OnFileSelectionListener{
public void onFileSelection(String tag, File file);
}
private File root;
private File path;
private File selection;
private ListView fileListView;
private TextView pathView;
private OnFileSelectionListener callback;
public static FileBrowserDialog newInstance(String root) {
FileBrowserDialog browser = new FileBrowserDialog();
Bundle bundle = new Bundle();
bundle.putString("root", root);
browser.setArguments(bundle);
return browser;
}
@Override
public void onAttach(Activity activity) {
if(!(activity instanceof OnFileSelectionListener))
throw new ClassCastException(activity.toString() + " must implement OnFileSelectionListener");
Bundle bundle = this.getArguments();
root = new File(bundle.getString("root"));
callback = (OnFileSelectionListener) activity;
super.onAttach(activity);
}
/** Build the popup.
* */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Fix for CopyPaste bar not visible
final Context context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);
LinearLayout rootView = new LinearLayout(context);
rootView.setOrientation(LinearLayout.VERTICAL);
rootView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
int padding = getResources().getDimensionPixelSize(R.dimen.standard_padding);
rootView.setPadding(padding, padding, padding, padding);
pathView = new TextView(context);
pathView.setText(root.getAbsolutePath());
pathView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rootView.addView(pathView);
fileListView = new ListView(context);
fileListView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
fileListView.setOnItemClickListener(this);
fileListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
updateFileList(root);
rootView.addView(fileListView);
/*EditText fileNameView = new EditText(context);
fileNameView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
fileNameView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
rootView.addView(fileNameView);
*/
// Create dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.configure_behaviour);
builder.setOnKeyListener(this);
builder.setPositiveButton(R.string.action_select, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(selection != null)
callback.onFileSelection(FileBrowserDialog.this.getTag(), selection);
else
Toast.makeText(context, "No File Selected", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FileBrowserDialog.this.getDialog().cancel();
}
});
builder.setView(rootView);
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return dialog;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File file = (File)fileListView.getAdapter().getItem(position);
if(file.isDirectory()){
updateFileList(file);
}
else{
log.debug("File selected: "+ file.getAbsolutePath());
((AlertDialog)this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
selection = file;
((FileListAdapter)fileListView.getAdapter()).notifyDataSetChanged();
}
}
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
if(root.equals(path))
dialog.dismiss();
else
updateFileList(path.getParentFile());
return true;
}
return false;
}
private void updateFileList(File dir){
if(!dir.isDirectory())
dir = dir.getParentFile();
if(dir.compareTo(root) <= 0)
dir = root;
log.debug("Browsing folder: "+ dir.getAbsolutePath());
ArrayList<File> folders = new ArrayList();
ArrayList<File> files = new ArrayList();
for(File file : dir.listFiles()){
if(file.isDirectory())
folders.add(file);
else
files.add(file);
}
Collections.sort(folders);
Collections.sort(files);
folders.addAll(files);
path = dir;
pathView.setText(dir.getAbsolutePath());
fileListView.setAdapter(new FileListAdapter(this.getActivity(), folders));
// Reset selection
selection = null;
if(this.getDialog() != null)
((AlertDialog)this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
protected class FileListAdapter extends ArrayAdapter<File>{
private List<File> fileList;
public FileListAdapter(Context context, List<File> fileList) {
super(context, android.R.layout.simple_list_item_1, fileList);
this.fileList = fileList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textview = (TextView) super.getView( position, convertView, parent );
textview.setPadding(5, 10, 10, 10);
File file = fileList.get(position);
textview.setText(file.getName());
Drawable icon = getActivity().getResources().getDrawable(
( file.isDirectory() ? FOLDER_ICON : FILE_ICON )
);
textview.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null );
// Is selected?
if (position == fileList.indexOf(selection))
textview.setBackgroundResource(android.R.color.darker_gray);
else
textview.setBackgroundResource(android.R.color.transparent);
return textview;
}
}
}