Make filebrowser code a little bit nicer.
This commit is contained in:
parent
4cb857371f
commit
1b88ebdc84
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2016 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
* Copyright (C) 2017 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -17,131 +17,32 @@
|
||||
|
||||
package ch.blinkenlights.android.vanilla;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.io.File;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Menu;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FilebrowserStartActivity extends FolderPickerActivity {
|
||||
|
||||
public class FilebrowserStartActivity extends Activity
|
||||
implements AdapterView.OnItemClickListener
|
||||
{
|
||||
|
||||
private ListView mListView;
|
||||
private TextView mPathDisplay;
|
||||
private Button mSaveButton;
|
||||
private FilebrowserStartAdapter mListAdapter;
|
||||
private String mCurrentPath;
|
||||
private SharedPreferences.Editor mPrefEditor;
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
ThemeHelper.setTheme(this, R.style.BackActionBar);
|
||||
super.onCreate(savedInstanceState);
|
||||
mPrefEditor = PlaybackService.getSettings(this).edit();
|
||||
|
||||
setTitle(R.string.filebrowser_start);
|
||||
setContentView(R.layout.filebrowser_content);
|
||||
mCurrentPath = (String)FileUtils.getFilesystemBrowseStart(this).getAbsolutePath();
|
||||
mPrefEditor = PlaybackService.getSettings(this).edit();
|
||||
mListAdapter = new FilebrowserStartAdapter(this, 0);
|
||||
mPathDisplay = (TextView) findViewById(R.id.path_display);
|
||||
mListView = (ListView) findViewById(R.id.list);
|
||||
mSaveButton = (Button) findViewById(R.id.save_button);
|
||||
|
||||
mListView.setAdapter(mListAdapter);
|
||||
mListView.setOnItemClickListener(this);
|
||||
|
||||
mSaveButton.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
mPrefEditor.putString(PrefKeys.FILESYSTEM_BROWSE_START, mCurrentPath);
|
||||
mPrefEditor.commit();
|
||||
finish();
|
||||
}});
|
||||
// Make sure that we display the current selection
|
||||
File startPath = FileUtils.getFilesystemBrowseStart(this);
|
||||
setCurrentDirectory(startPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when we are displayed (again)
|
||||
* This will always refresh the whole song list
|
||||
*/
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
refreshDirectoryList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a bare-bones actionbar
|
||||
*/
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item)
|
||||
{
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
finish();
|
||||
return true;
|
||||
} else {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
public void onFolderSelected(File directory) {
|
||||
mPrefEditor.putString(PrefKeys.FILESYSTEM_BROWSE_START, directory.getAbsolutePath());
|
||||
mPrefEditor.commit();
|
||||
finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called if user taps a row
|
||||
*/
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
ViewHolder holder = (ViewHolder)view.getTag();
|
||||
int pos = (int)holder.id;
|
||||
String dirent = mListAdapter.getItem(pos);
|
||||
|
||||
if(pos == 0) {
|
||||
mCurrentPath = (new File(mCurrentPath)).getParent();
|
||||
}
|
||||
else {
|
||||
mCurrentPath += "/" + dirent;
|
||||
}
|
||||
|
||||
/* let java fixup any strange paths */
|
||||
mCurrentPath = (new File(mCurrentPath == null ? "/" : mCurrentPath)).getAbsolutePath();
|
||||
|
||||
refreshDirectoryList();
|
||||
}
|
||||
|
||||
/**
|
||||
* display mCurrentPath in the dialog
|
||||
*/
|
||||
private void refreshDirectoryList() {
|
||||
File path = new File(mCurrentPath);
|
||||
File[]dirs = path.listFiles();
|
||||
|
||||
mListAdapter.clear();
|
||||
mListAdapter.add("../");
|
||||
|
||||
if(dirs != null) {
|
||||
Arrays.sort(dirs);
|
||||
for(File fentry: dirs) {
|
||||
if(fentry.isDirectory()) {
|
||||
mListAdapter.add(fentry.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Toast.makeText(this, "Failed to display "+mCurrentPath, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
mPathDisplay.setText(mCurrentPath);
|
||||
mListView.setSelectionFromTop(0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
174
src/ch/blinkenlights/android/vanilla/FolderPickerActivity.java
Normal file
174
src/ch/blinkenlights/android/vanilla/FolderPickerActivity.java
Normal file
@ -0,0 +1,174 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2016 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ch.blinkenlights.android.vanilla;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.io.File;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Menu;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
public abstract class FolderPickerActivity extends Activity
|
||||
implements AdapterView.OnItemClickListener
|
||||
{
|
||||
|
||||
/**
|
||||
* The path we should currently display
|
||||
*/
|
||||
private File mCurrentPath;
|
||||
/**
|
||||
* Our listview
|
||||
*/
|
||||
private ListView mListView;
|
||||
/**
|
||||
* View displaying the current path
|
||||
*/
|
||||
private TextView mPathDisplay;
|
||||
/**
|
||||
* Save button
|
||||
*/
|
||||
private Button mSaveButton;
|
||||
/**
|
||||
* The array adapter of our listview
|
||||
*/
|
||||
private FolderPickerAdapter mListAdapter;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
ThemeHelper.setTheme(this, R.style.BackActionBar);
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setTitle(R.string.filebrowser_start);
|
||||
setContentView(R.layout.filebrowser_content);
|
||||
|
||||
mCurrentPath = new File("/");
|
||||
mListAdapter = new FolderPickerAdapter(this, 0);
|
||||
mPathDisplay = (TextView) findViewById(R.id.path_display);
|
||||
mListView = (ListView) findViewById(R.id.list);
|
||||
mSaveButton = (Button) findViewById(R.id.save_button);
|
||||
|
||||
mListView.setAdapter(mListAdapter);
|
||||
mListView.setOnItemClickListener(this);
|
||||
|
||||
mSaveButton.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
onFolderSelected(mCurrentPath);
|
||||
}});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a folder was selected
|
||||
*
|
||||
* @param directory the selected directory
|
||||
*/
|
||||
public abstract void onFolderSelected(File directory);
|
||||
|
||||
/**
|
||||
* Jumps to given directory
|
||||
*
|
||||
* @param directory the directory to jump to
|
||||
*/
|
||||
void setCurrentDirectory(File directory) {
|
||||
mCurrentPath = directory;
|
||||
refreshDirectoryList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when we are displayed (again)
|
||||
* This will always refresh the whole song list
|
||||
*/
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
refreshDirectoryList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a bare-bones actionbar
|
||||
*/
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item)
|
||||
{
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
finish();
|
||||
return true;
|
||||
} else {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called if user taps a row
|
||||
*/
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
ViewHolder holder = (ViewHolder)view.getTag();
|
||||
int pos = (int)holder.id;
|
||||
String dirent = mListAdapter.getItem(pos);
|
||||
|
||||
File newPath = null;
|
||||
if(pos == 0) {
|
||||
newPath = mCurrentPath.getParentFile();
|
||||
}
|
||||
else {
|
||||
newPath = new File(mCurrentPath, dirent);
|
||||
}
|
||||
|
||||
if (newPath != null)
|
||||
setCurrentDirectory(newPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* display mCurrentPath in the dialog
|
||||
*/
|
||||
private void refreshDirectoryList() {
|
||||
File path = mCurrentPath;
|
||||
File[]dirs = path.listFiles();
|
||||
|
||||
mListAdapter.clear();
|
||||
mListAdapter.add("../");
|
||||
|
||||
if(dirs != null) {
|
||||
Arrays.sort(dirs);
|
||||
for(File fentry: dirs) {
|
||||
if(fentry.isDirectory()) {
|
||||
mListAdapter.add(fentry.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Toast.makeText(this, "Failed to display " + path.getAbsolutePath(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
mPathDisplay.setText(path.getAbsolutePath());
|
||||
mListView.setSelectionFromTop(0, 0);
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2016 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
* Copyright (C) 2013-2017 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -29,13 +29,13 @@ import android.widget.TextView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
public class FilebrowserStartAdapter
|
||||
public class FolderPickerAdapter
|
||||
extends ArrayAdapter<String>
|
||||
{
|
||||
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
public FilebrowserStartAdapter(Context context, int resource) {
|
||||
public FolderPickerAdapter(Context context, int resource) {
|
||||
super(context, resource);
|
||||
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user