Replace tabs

This commit is contained in:
Martin Fietz 2015-11-19 18:11:11 +01:00
parent 06cdd987e0
commit 87f710d381
1 changed files with 240 additions and 240 deletions

View File

@ -35,85 +35,85 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
* will be sent back to the starting activity as an activity result. * will be sent back to the starting activity as an activity result.
*/ */
public class DirectoryChooserActivity extends ActionBarActivity { public class DirectoryChooserActivity extends ActionBarActivity {
private static final String TAG = "DirectoryChooserActivit"; private static final String TAG = "DirectoryChooserActivit";
private static final String CREATE_DIRECTORY_NAME = "AntennaPod"; private static final String CREATE_DIRECTORY_NAME = "AntennaPod";
public static final String RESULT_SELECTED_DIR = "selected_dir"; public static final String RESULT_SELECTED_DIR = "selected_dir";
public static final int RESULT_CODE_DIR_SELECTED = 1; public static final int RESULT_CODE_DIR_SELECTED = 1;
private Button butConfirm; private Button butConfirm;
private Button butCancel; private Button butCancel;
private ImageButton butNavUp; private ImageButton butNavUp;
private TextView txtvSelectedFolder; private TextView txtvSelectedFolder;
private ListView listDirectories; private ListView listDirectories;
private ArrayAdapter<String> listDirectoriesAdapter; private ArrayAdapter<String> listDirectoriesAdapter;
private ArrayList<String> filenames; private ArrayList<String> filenames;
/** The directory that is currently being shown. */ /** The directory that is currently being shown. */
private File selectedDir; private File selectedDir;
private File[] filesInDir; private File[] filesInDir;
private FileObserver fileObserver; private FileObserver fileObserver;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme()); setTheme(UserPreferences.getTheme());
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.directory_chooser); setContentView(R.layout.directory_chooser);
butConfirm = (Button) findViewById(R.id.butConfirm); butConfirm = (Button) findViewById(R.id.butConfirm);
butCancel = (Button) findViewById(R.id.butCancel); butCancel = (Button) findViewById(R.id.butCancel);
butNavUp = (ImageButton) findViewById(R.id.butNavUp); butNavUp = (ImageButton) findViewById(R.id.butNavUp);
txtvSelectedFolder = (TextView) findViewById(R.id.txtvSelectedFolder); txtvSelectedFolder = (TextView) findViewById(R.id.txtvSelectedFolder);
listDirectories = (ListView) findViewById(R.id.directory_list); listDirectories = (ListView) findViewById(R.id.directory_list);
butConfirm.setOnClickListener(new OnClickListener() { butConfirm.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (isValidFile(selectedDir)) { if (isValidFile(selectedDir)) {
if (selectedDir.list().length == 0) { if (selectedDir.list().length == 0) {
returnSelectedFolder(); returnSelectedFolder();
} else { } else {
showNonEmptyDirectoryWarning(); showNonEmptyDirectoryWarning();
} }
} }
} }
private void showNonEmptyDirectoryWarning() { private void showNonEmptyDirectoryWarning() {
AlertDialog.Builder adb = new AlertDialog.Builder( AlertDialog.Builder adb = new AlertDialog.Builder(
DirectoryChooserActivity.this); DirectoryChooserActivity.this);
adb.setTitle(R.string.folder_not_empty_dialog_title); adb.setTitle(R.string.folder_not_empty_dialog_title);
adb.setMessage(R.string.folder_not_empty_dialog_msg); adb.setMessage(R.string.folder_not_empty_dialog_msg);
adb.setNegativeButton(R.string.cancel_label, adb.setNegativeButton(R.string.cancel_label,
(dialog, which) -> { (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
}); });
adb.setPositiveButton(R.string.confirm_label, adb.setPositiveButton(R.string.confirm_label,
(dialog, which) -> { (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
returnSelectedFolder(); returnSelectedFolder();
}); });
adb.create().show(); adb.create().show();
} }
}); });
butCancel.setOnClickListener(v -> { butCancel.setOnClickListener(v -> {
setResult(Activity.RESULT_CANCELED); setResult(Activity.RESULT_CANCELED);
finish(); finish();
}); });
listDirectories.setOnItemClickListener((adapter, view, position, id) -> { listDirectories.setOnItemClickListener((adapter, view, position, id) -> {
Log.d(TAG, "Selected index: " + position); Log.d(TAG, "Selected index: " + position);
if (filesInDir != null && position >= 0 if (filesInDir != null && position >= 0
&& position < filesInDir.length) { && position < filesInDir.length) {
changeDirectory(filesInDir[position]); changeDirectory(filesInDir[position]);
} }
}); });
butNavUp.setOnClickListener(v -> { butNavUp.setOnClickListener(v -> {
File parent = null; File parent = null;
if (selectedDir != null if (selectedDir != null
&& (parent = selectedDir.getParentFile()) != null) { && (parent = selectedDir.getParentFile()) != null) {
@ -121,216 +121,216 @@ public class DirectoryChooserActivity extends ActionBarActivity {
} }
}); });
filenames = new ArrayList<>(); filenames = new ArrayList<>();
listDirectoriesAdapter = new ArrayAdapter<>(this, listDirectoriesAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, filenames); android.R.layout.simple_list_item_1, filenames);
listDirectories.setAdapter(listDirectoriesAdapter); listDirectories.setAdapter(listDirectoriesAdapter);
changeDirectory(Environment.getExternalStorageDirectory()); changeDirectory(Environment.getExternalStorageDirectory());
} }
/** /**
* Finishes the activity and returns the selected folder as a result. The * Finishes the activity and returns the selected folder as a result. The
* selected folder can also be null. * selected folder can also be null.
*/ */
private void returnSelectedFolder() { private void returnSelectedFolder() {
if (selectedDir != null && BuildConfig.DEBUG) if (selectedDir != null && BuildConfig.DEBUG)
Log.d(TAG, "Returning " + selectedDir.getAbsolutePath() Log.d(TAG, "Returning " + selectedDir.getAbsolutePath()
+ " as result"); + " as result");
Intent resultData = new Intent(); Intent resultData = new Intent();
if (selectedDir != null) { if (selectedDir != null) {
resultData.putExtra(RESULT_SELECTED_DIR, resultData.putExtra(RESULT_SELECTED_DIR,
selectedDir.getAbsolutePath()); selectedDir.getAbsolutePath());
} }
setResult(Activity.RESULT_OK, resultData); setResult(Activity.RESULT_OK, resultData);
finish(); finish();
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
if (fileObserver != null) { if (fileObserver != null) {
fileObserver.stopWatching(); fileObserver.stopWatching();
} }
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (fileObserver != null) { if (fileObserver != null) {
fileObserver.startWatching(); fileObserver.startWatching();
} }
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
listDirectoriesAdapter = null; listDirectoriesAdapter = null;
fileObserver = null; fileObserver = null;
} }
/** /**
* Change the directory that is currently being displayed. * Change the directory that is currently being displayed.
* *
* @param dir * @param dir
* The file the activity should switch to. This File must be * The file the activity should switch to. This File must be
* non-null and a directory, otherwise the displayed directory * non-null and a directory, otherwise the displayed directory
* will not be changed * will not be changed
*/ */
private void changeDirectory(File dir) { private void changeDirectory(File dir) {
if (dir != null && dir.isDirectory()) { if (dir != null && dir.isDirectory()) {
File[] contents = dir.listFiles(); File[] contents = dir.listFiles();
if (contents != null) { if (contents != null) {
int numDirectories = 0; int numDirectories = 0;
for (File f : contents) { for (File f : contents) {
if (f.isDirectory()) { if (f.isDirectory()) {
numDirectories++; numDirectories++;
} }
} }
filesInDir = new File[numDirectories]; filesInDir = new File[numDirectories];
filenames.clear(); filenames.clear();
for (int i = 0, counter = 0; i < numDirectories; counter++) { for (int i = 0, counter = 0; i < numDirectories; counter++) {
if (contents[counter].isDirectory()) { if (contents[counter].isDirectory()) {
filesInDir[i] = contents[counter]; filesInDir[i] = contents[counter];
filenames.add(contents[counter].getName()); filenames.add(contents[counter].getName());
i++; i++;
} }
} }
Arrays.sort(filesInDir); Arrays.sort(filesInDir);
Collections.sort(filenames); Collections.sort(filenames);
selectedDir = dir; selectedDir = dir;
txtvSelectedFolder.setText(dir.getAbsolutePath()); txtvSelectedFolder.setText(dir.getAbsolutePath());
listDirectoriesAdapter.notifyDataSetChanged(); listDirectoriesAdapter.notifyDataSetChanged();
fileObserver = createFileObserver(dir.getAbsolutePath()); fileObserver = createFileObserver(dir.getAbsolutePath());
fileObserver.startWatching(); fileObserver.startWatching();
Log.d(TAG, "Changed directory to " + dir.getAbsolutePath()); Log.d(TAG, "Changed directory to " + dir.getAbsolutePath());
} else { } else {
Log.d(TAG, "Could not change folder: contents of dir were null"); Log.d(TAG, "Could not change folder: contents of dir were null");
} }
} else { } else {
if (dir == null) { if (dir == null) {
Log.d(TAG, "Could not change folder: dir was null"); Log.d(TAG, "Could not change folder: dir was null");
} else { } else {
Log.d(TAG, "Could not change folder: dir is no directory"); Log.d(TAG, "Could not change folder: dir is no directory");
} }
} }
refreshButtonState(); refreshButtonState();
} }
/** /**
* Changes the state of the buttons depending on the currently selected file * Changes the state of the buttons depending on the currently selected file
* or folder. * or folder.
*/ */
private void refreshButtonState() { private void refreshButtonState() {
if (selectedDir != null) { if (selectedDir != null) {
butConfirm.setEnabled(isValidFile(selectedDir)); butConfirm.setEnabled(isValidFile(selectedDir));
supportInvalidateOptionsMenu(); supportInvalidateOptionsMenu();
} }
} }
/** Refresh the contents of the directory that is currently shown. */ /** Refresh the contents of the directory that is currently shown. */
private void refreshDirectory() { private void refreshDirectory() {
if (selectedDir != null) { if (selectedDir != null) {
changeDirectory(selectedDir); changeDirectory(selectedDir);
} }
} }
/** Sets up a FileObserver to watch the current directory. */ /** Sets up a FileObserver to watch the current directory. */
private FileObserver createFileObserver(String path) { private FileObserver createFileObserver(String path) {
return new FileObserver(path, FileObserver.CREATE | FileObserver.DELETE return new FileObserver(path, FileObserver.CREATE | FileObserver.DELETE
| FileObserver.MOVED_FROM | FileObserver.MOVED_TO) { | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
@Override @Override
public void onEvent(int event, String path) { public void onEvent(int event, String path) {
Log.d(TAG, "FileObserver received event " + event); Log.d(TAG, "FileObserver received event " + event);
runOnUiThread(() -> refreshDirectory()); runOnUiThread(() -> refreshDirectory());
} }
}; };
} }
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.new_folder_item) menu.findItem(R.id.new_folder_item)
.setVisible(isValidFile(selectedDir)); .setVisible(isValidFile(selectedDir));
return true; return true;
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.directory_chooser, menu); inflater.inflate(R.menu.directory_chooser, menu);
return true; return true;
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
NavUtils.navigateUpFromSameTask(this); NavUtils.navigateUpFromSameTask(this);
return true; return true;
case R.id.new_folder_item: case R.id.new_folder_item:
openNewFolderDialog(); openNewFolderDialog();
return true; return true;
case R.id.set_to_default_folder_item: case R.id.set_to_default_folder_item:
selectedDir = null; selectedDir = null;
returnSelectedFolder(); returnSelectedFolder();
return true; return true;
default: default:
return false; return false;
} }
} }
/** /**
* Shows a confirmation dialog that asks the user if he wants to create a * Shows a confirmation dialog that asks the user if he wants to create a
* new folder. * new folder.
*/ */
private void openNewFolderDialog() { private void openNewFolderDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.create_folder_label); builder.setTitle(R.string.create_folder_label);
builder.setMessage(String.format(getString(R.string.create_folder_msg), builder.setMessage(String.format(getString(R.string.create_folder_msg),
CREATE_DIRECTORY_NAME)); CREATE_DIRECTORY_NAME));
builder.setNegativeButton(R.string.cancel_label, builder.setNegativeButton(R.string.cancel_label,
(dialog, which) -> { (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
}); });
builder.setPositiveButton(R.string.confirm_label, builder.setPositiveButton(R.string.confirm_label,
(dialog, which) -> { (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
int msg = createFolder(); int msg = createFolder();
Toast t = Toast.makeText(DirectoryChooserActivity.this, Toast t = Toast.makeText(DirectoryChooserActivity.this,
msg, Toast.LENGTH_SHORT); msg, Toast.LENGTH_SHORT);
t.show(); t.show();
}); });
builder.create().show(); builder.create().show();
} }
/** /**
* Creates a new folder in the current directory with the name * Creates a new folder in the current directory with the name
* CREATE_DIRECTORY_NAME. * CREATE_DIRECTORY_NAME.
*/ */
private int createFolder() { private int createFolder() {
if (selectedDir == null) { if (selectedDir == null) {
return R.string.create_folder_error; return R.string.create_folder_error;
} else if (selectedDir.canWrite()) { } else if (selectedDir.canWrite()) {
File newDir = new File(selectedDir, CREATE_DIRECTORY_NAME); File newDir = new File(selectedDir, CREATE_DIRECTORY_NAME);
if (!newDir.exists()) { if (!newDir.exists()) {
boolean result = newDir.mkdir(); boolean result = newDir.mkdir();
if (result) { if (result) {
return R.string.create_folder_success; return R.string.create_folder_success;
} else { } else {
return R.string.create_folder_error; return R.string.create_folder_error;
} }
} else { } else {
return R.string.create_folder_error_already_exists; return R.string.create_folder_error_already_exists;
} }
} else { } else {
return R.string.create_folder_error_no_write_access; return R.string.create_folder_error_no_write_access;
} }
} }
/** Returns true if the selected file or directory would be valid selection. */ /** Returns true if the selected file or directory would be valid selection. */
private boolean isValidFile(File file) { private boolean isValidFile(File file) {
return file != null && file.isDirectory() && file.canRead() && file.canWrite(); return file != null && file.isDirectory() && file.canRead() && file.canWrite();
} }
} }