mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-12-01 12:31:45 +00:00
Added FileObserver
This commit is contained in:
@ -7,6 +7,7 @@ import java.util.Collections;
|
|||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
import android.os.FileObserver;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
@ -16,11 +17,13 @@ import android.widget.ArrayAdapter;
|
|||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
import android.widget.SlidingDrawer;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.actionbarsherlock.app.SherlockActivity;
|
import com.actionbarsherlock.app.SherlockActivity;
|
||||||
import com.actionbarsherlock.view.Menu;
|
import com.actionbarsherlock.view.Menu;
|
||||||
import com.actionbarsherlock.view.MenuInflater;
|
import com.actionbarsherlock.view.MenuInflater;
|
||||||
|
import com.actionbarsherlock.view.MenuItem;
|
||||||
|
|
||||||
import de.danoeh.antennapod.AppConfig;
|
import de.danoeh.antennapod.AppConfig;
|
||||||
import de.danoeh.antennapod.PodcastApp;
|
import de.danoeh.antennapod.PodcastApp;
|
||||||
@ -41,10 +44,13 @@ public class DirectoryChooserActivity extends SherlockActivity {
|
|||||||
private File selectedDir;
|
private File selectedDir;
|
||||||
private File[] filesInDir;
|
private File[] filesInDir;
|
||||||
|
|
||||||
|
private FileObserver fileObserver;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
setTheme(PodcastApp.getThemeResourceId());
|
setTheme(PodcastApp.getThemeResourceId());
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
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);
|
||||||
@ -102,6 +108,22 @@ public class DirectoryChooserActivity extends SherlockActivity {
|
|||||||
changeDirectory(Environment.getExternalStorageDirectory());
|
changeDirectory(Environment.getExternalStorageDirectory());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
if (fileObserver != null) {
|
||||||
|
fileObserver.stopWatching();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
if (fileObserver != null) {
|
||||||
|
fileObserver.startWatching();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
||||||
@ -126,10 +148,14 @@ public class DirectoryChooserActivity extends SherlockActivity {
|
|||||||
selectedDir = dir;
|
selectedDir = dir;
|
||||||
txtvSelectedFolder.setText(dir.getAbsolutePath());
|
txtvSelectedFolder.setText(dir.getAbsolutePath());
|
||||||
listDirectoriesAdapter.notifyDataSetChanged();
|
listDirectoriesAdapter.notifyDataSetChanged();
|
||||||
|
fileObserver = createFileObserver(dir.getAbsolutePath());
|
||||||
|
fileObserver.startWatching();
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Changed directory to " + dir.getAbsolutePath());
|
Log.d(TAG, "Changed directory to " + dir.getAbsolutePath());
|
||||||
} else {
|
} else {
|
||||||
if (AppConfig.DEBUG) Log.d(TAG, "Could not change folder: contents of dir were null");
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG,
|
||||||
|
"Could not change folder: contents of dir were null");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (dir == null) {
|
if (dir == null) {
|
||||||
@ -142,10 +168,54 @@ public class DirectoryChooserActivity extends SherlockActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void refreshDirectory() {
|
||||||
|
if (selectedDir != null) {
|
||||||
|
changeDirectory(selectedDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileObserver createFileObserver(String path) {
|
||||||
|
return new FileObserver(path, FileObserver.CREATE | FileObserver.DELETE
|
||||||
|
| FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEvent(int event, String path) {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "FileObserver received event " + event);
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
refreshDirectory();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
MenuInflater inflater = new MenuInflater(this);
|
MenuInflater inflater = new MenuInflater(this);
|
||||||
inflater.inflate(R.menu.directory_chooser, menu);
|
inflater.inflate(R.menu.directory_chooser, menu);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case android.R.id.home:
|
||||||
|
finish();
|
||||||
|
return true;
|
||||||
|
case R.id.new_folder_item:
|
||||||
|
openNewFolderDialog();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openNewFolderDialog() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user