Commit 45fe2719 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Remove directories management form settings

parent ae0da86c
......@@ -2,10 +2,6 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/general_prefs_category" >
<PreferenceScreen
android:key="directories"
android:summary="@string/directories_summary"
android:title="@string/directories" />
<ListPreference
android:defaultValue="-1"
......
/*****************************************************************************
* BrowserActivity.java
*****************************************************************************
* Copyright © 2011-2012 VLC authors and VideoLAN
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.vlc.gui;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.vlc.MediaDatabase;
import org.videolan.vlc.MediaLibrary;
import org.videolan.vlc.R;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.CustomDirectories;
import org.videolan.vlc.util.Util;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Stack;
public class BrowserActivity extends ListActivity {
public final static String TAG = "VLC/BrowserActivity";
/**
* TODO:
*/
private MediaBrowserAdapter mAdapter;
private File mCurrentDir;
private final Stack<ScrollState> mScrollStates = new Stack<ScrollState>();
private String mRoots[];
private static class ScrollState {
public ScrollState(int index, int top) {
this.index = index;
this.top = top;
}
int index;
int top;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
applyTheme();
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
mAdapter = new MediaBrowserAdapter(this);
setListAdapter(mAdapter);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addDataScheme("file");
registerReceiver(messageReceiver, filter);
refreshRoots();
openStorageDevices(mRoots);
registerForContextMenu(getListView());
}
private void applyTheme() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
boolean enableBlackTheme = pref.getBoolean("enable_black_theme", false);
if (enableBlackTheme) {
setTheme(R.style.Theme_VLC_Black);
}
}
private void refreshRoots() {
ArrayList<String> list = new ArrayList<String>();
list.addAll(AndroidDevices.getStorageDirectories());
list.addAll(Arrays.asList(CustomDirectories.getCustomDirectories()));
mRoots = list.toArray(new String[list.size()]);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(messageReceiver);
mAdapter.clear();
mScrollStates.clear();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
int position = ((AdapterContextMenuInfo)menuInfo).position;
final File item = mAdapter.getItem(position);
if (mCurrentDir != null
|| item.getPath().equals(MediaBrowserAdapter.ADD_ITEM_PATH)
|| AndroidDevices.getStorageDirectories().contains(
item.getPath())) {
return;
}
MenuItem delete = menu.add(R.string.remove_custom_path);
delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem arg0) {
// remove any checkmarks of the custom item
MediaDatabase.getInstance().recursiveRemoveDir(item.getPath());
CustomDirectories.removeCustomDirectory(item.getPath());
refresh();
return true;
}
});
}
private void openStorageDevices(String roots[]) {
mCurrentDir = null;
mAdapter.clear();
for (String s : roots) {
File f = new File(s);
if (f.exists())
mAdapter.add(f);
}
mAdapter.add(new File(MediaBrowserAdapter.ADD_ITEM_PATH));
mAdapter.sort();
// set scroll position to top
getListView().setSelection(0);
}
private void openDir(File file) {
if(file == null || !file.exists() || file.getPath() == null
|| file.getPath().equals(MediaBrowserAdapter.ADD_ITEM_PATH))
return;
mAdapter.clear();
mCurrentDir = file;
File[] files = file.listFiles(new DirFilter());
/* If no sub-directories or I/O error don't crash */
if(files == null || files.length < 1) {
Util.toaster(this, R.string.nosubdirectory);
this.finish();
return;
}
for (int i = 0; i < files.length; i++) {
mAdapter.add(files[i]);
}
mAdapter.sort();
// set scroll position to top
getListView().setSelection(0);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = mAdapter.getItem(position);
if(file.getPath().equals(MediaBrowserAdapter.ADD_ITEM_PATH)) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
if (!LibVlcUtil.isHoneycombOrLater()) {
input.setTextColor(getResources().getColor(R.color.grey50));
}
input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
b.setTitle(R.string.add_custom_path);
b.setMessage(R.string.add_custom_path_description);
b.setView(input);
b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface x, int y) {return;}
});
b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String path = input.getText().toString().trim();
File f = new File(path);
if(!f.exists() || !f.isDirectory()) {
Toast.makeText(BrowserActivity.this, getString(R.string.directorynotfound, path), Toast.LENGTH_SHORT).show();
return;
}
CustomDirectories.addCustomDirectory(f.getAbsolutePath());
refresh();
}
});
b.show();
return;
}
File[] files = file.listFiles(new DirFilter());
if (files != null && files.length > 0) {
// store scroll state
int index = l.getFirstVisiblePosition();
int top = l.getChildAt(0).getTop();
mScrollStates.push(new ScrollState(index, top));
openDir(file);
} else {
Util.toaster(this, R.string.nosubdirectory);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mCurrentDir == null) {
// We're on the list of storage devices
return super.onKeyDown(keyCode, event);
}
// Check if we are on one of the root
boolean isRoot = false;
for (String root: mRoots) {
if (mCurrentDir.getPath().equals(root)) {
isRoot = true;
break;
}
}
if (isRoot) {
openStorageDevices(mRoots);
return true;
} else {
openDir(mCurrentDir.getParentFile());
// restore scroll state
if (mScrollStates.size() > 0) {
ScrollState ss = mScrollStates.pop();
getListView().setSelectionFromTop(ss.index, ss.top);
return true;
}
}
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
CheckBox cb = (CheckBox) getListView().getSelectedView().findViewById(R.id.browser_item_selected);
if (cb != null)
cb.toggle();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void refresh() {
if (mCurrentDir == null) {
refreshRoots();
openStorageDevices(mRoots);
} else {
openDir(mCurrentDir);
}
mAdapter.notifyDataSetChanged();
}
private final BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED) ||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED) ||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED) ||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) {
refresh();
}
}
};
/**
* Filter: accept only directories
*/
static private class DirFilter implements FileFilter {
@Override
public boolean accept(File f) {
return f.isDirectory() && !MediaLibrary.FOLDER_BLACKLIST.contains(f.getPath().toLowerCase(Locale.ENGLISH));
}
}
}
/*****************************************************************************
* MediaBrowserAdapter.java
*****************************************************************************
* Copyright © 2011-2015 VLC authors and VideoLAN
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.vlc.gui;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import org.videolan.vlc.MediaDatabase;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import java.io.File;
import java.util.Comparator;
import java.util.List;
public class MediaBrowserAdapter extends ArrayAdapter<File>
implements Comparator<File> {
public final static String TAG = "VLC/BrowserAdapter";
public final static String ADD_ITEM_PATH = "/add/a/path";
public MediaBrowserAdapter(Context context) {
super(context, 0);
}
@Override
public synchronized void add(File object) {
super.add(object);
}
/**
* Display the view of a file browser item.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.browser_item, parent, false);
holder = new ViewHolder();
holder.layout = view.findViewById(R.id.layout_item);
holder.check = (CheckBox) view.findViewById(R.id.browser_item_selected);
holder.text = (TextView) view.findViewById(R.id.browser_item_dir);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
final File item = getItem(position);
final MediaDatabase dbManager = MediaDatabase.getInstance();
if(item != null) {
if(item.getPath().equals(ADD_ITEM_PATH)) {
holder.text.setText(R.string.add_custom_path);
holder.check.setVisibility(View.GONE);
} else if(item.getName() != null) {
holder.text.setText(getVisibleName(item));
holder.check.setVisibility(View.VISIBLE);
holder.check.setOnCheckedChangeListener(null);
holder.check.setTag(item);
holder.check.setEnabled(true);
holder.check.setChecked(false);
List<File> dirs = dbManager.getMediaDirs();
for (File dir : dirs) {
if (dir.getPath().equals(item.getPath())) {
holder.check.setEnabled(true);
holder.check.setChecked(true);
break;
} else if (dir.getPath().startsWith(item.getPath()+"/")) {
Log.i(TAG, dir.getPath() + " startWith " + item.getPath());
holder.check.setEnabled(false);
holder.check.setChecked(true);
break;
}
}
holder.check.setOnCheckedChangeListener(onCheckedChangeListener);
}
}
return view;
}
private final OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final MediaDatabase dbManager = MediaDatabase.getInstance();
File item = (File) buttonView.getTag();
if (item == null)
return;
if (buttonView.isEnabled() && isChecked) {
dbManager.addDir(item.getPath());
File tmpFile = item.getParentFile();
while (tmpFile != null && !tmpFile.getPath().equals("/")) {
dbManager.removeDir(tmpFile.getPath());
tmpFile = tmpFile.getParentFile();
}
} else {
dbManager.removeDir(item.getPath());
}
}
};
public void sort() {
super.sort(this);
}
@Override
public int compare(File file1, File file2) {
// float the add item to the bottom
if(file1.getPath().equals(ADD_ITEM_PATH))
return 1;
else if(file2.getPath().equals(ADD_ITEM_PATH))
return -1;
return String.CASE_INSENSITIVE_ORDER.compare(file1.getName(), file2.getName());
}
private String getVisibleName(File file) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Show "sdcard" for the user's folder when running in multi-user
if (file.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getPath())) {
return VLCApplication.getAppContext().getString(R.string.internal_memory);
}
}
return file.getName();
}
static class ViewHolder {
View layout;
CheckBox check;
TextView text;
}
}
......@@ -92,20 +92,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
findPreference("ui_category").setEnabled(false);
}
// Directories
Preference directoriesPref = findPreference("directories");
directoriesPref.setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(getApplicationContext(), BrowserActivity.class);
startActivity(intent);
setResult(RESULT_RESCAN);
return true;
}
});
// Screen orientation
ListPreference screenOrientationPref = (ListPreference) findPreference("screen_orientation");
screenOrientationPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment