android: cleanup the file extension guessing and expand it for prettiness
This commit is contained in:
parent
2005a34318
commit
5f091e66d7
@ -1,3 +1,11 @@
|
|||||||
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||||
|
/*
|
||||||
|
* This file is part of the LibreOffice project.
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
package org.libreoffice.ui;
|
package org.libreoffice.ui;
|
||||||
|
|
||||||
import org.libreoffice.R;
|
import org.libreoffice.R;
|
||||||
@ -5,106 +13,121 @@ import org.libreoffice.R;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileFilter;
|
import java.io.FileFilter;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
public class FileUtilities {
|
public class FileUtilities {
|
||||||
|
static final int ALL = -1;
|
||||||
static final int ALL = -1;
|
static final int DOC = 0;
|
||||||
static final int DOC = 0;
|
static final int CALC = 1;
|
||||||
static final int CALC = 1;
|
static final int IMPRESS = 2;
|
||||||
static final int IMPRESS = 2;
|
static final int DRAWING = 3;
|
||||||
|
static final int UNKNOWN = 10;
|
||||||
static final int SORT_AZ = 0;
|
|
||||||
static final int SORT_ZA = 1;
|
|
||||||
/** Oldest Files First*/
|
|
||||||
static final int SORT_OLDEST = 2;
|
|
||||||
/** Newest Files First*/
|
|
||||||
static final int SORT_NEWEST = 3;
|
|
||||||
/** Largest Files First */
|
|
||||||
static final int SORT_LARGEST = 4;
|
|
||||||
/** Smallest Files First */
|
|
||||||
static final int SORT_SMALLEST = 5;
|
|
||||||
|
|
||||||
private static String[] fileExtensions = {".odt",".ods",".odp"};
|
|
||||||
|
|
||||||
static boolean isDoc(String filename){
|
|
||||||
if( filename.endsWith( fileExtensions[ DOC ] ) ){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isCalc(String filename){
|
|
||||||
if( filename.endsWith( fileExtensions[ CALC ] ) ){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isImpress(String filename){
|
|
||||||
if( filename.endsWith( fileExtensions[ IMPRESS ] ) ){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static FileFilter getFileFilter(int mode ){
|
|
||||||
if( mode != ALL){
|
|
||||||
final String ext = fileExtensions[ mode ];
|
|
||||||
return new FileFilter() {
|
|
||||||
|
|
||||||
public boolean accept(File pathname) {
|
|
||||||
if( pathname.getName().endsWith( ext ) ){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if( pathname.isDirectory() ){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}else{//return all
|
|
||||||
return new FileFilter() {
|
|
||||||
|
|
||||||
public boolean accept(File pathname) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static FilenameFilter getFilenameFilter(int mode){
|
static final int SORT_AZ = 0;
|
||||||
if( mode != ALL){
|
static final int SORT_ZA = 1;
|
||||||
final String ext = fileExtensions[ mode ];
|
/** Oldest Files First*/
|
||||||
return new FilenameFilter() {
|
static final int SORT_OLDEST = 2;
|
||||||
|
/** Newest Files First*/
|
||||||
public boolean accept(File dir, String filename) {
|
static final int SORT_NEWEST = 3;
|
||||||
if( filename.endsWith( ext ) ){
|
/** Largest Files First */
|
||||||
return true;
|
static final int SORT_LARGEST = 4;
|
||||||
}
|
/** Smallest Files First */
|
||||||
if( new File( dir , filename ).isDirectory() ){
|
static final int SORT_SMALLEST = 5;
|
||||||
return true;
|
|
||||||
}
|
private static final Map<String,Integer> mExtnMap = new HashMap<String,Integer>();
|
||||||
return false;
|
static {
|
||||||
}
|
mExtnMap.put(".odt", DOC);
|
||||||
};
|
mExtnMap.put(".sxw", DOC);
|
||||||
}else{
|
mExtnMap.put(".rtf", DOC);
|
||||||
return new FilenameFilter() {
|
mExtnMap.put(".doc", DOC);
|
||||||
|
mExtnMap.put(".docx", DOC);
|
||||||
public boolean accept(File dir, String filename) {
|
mExtnMap.put(".html", DOC);
|
||||||
return true;
|
mExtnMap.put(".txt", DOC);
|
||||||
}
|
|
||||||
};
|
mExtnMap.put(".ods", CALC);
|
||||||
}
|
mExtnMap.put(".sxc", CALC);
|
||||||
|
mExtnMap.put(".xls", CALC);
|
||||||
|
mExtnMap.put(".xlsx", CALC);
|
||||||
|
|
||||||
|
mExtnMap.put(".odp", IMPRESS);
|
||||||
|
mExtnMap.put(".sxi", IMPRESS);
|
||||||
|
mExtnMap.put(".ppt", IMPRESS);
|
||||||
|
mExtnMap.put(".pptx", IMPRESS);
|
||||||
|
|
||||||
|
mExtnMap.put(".vsd", DRAWING);
|
||||||
|
// FIXME: we need to expand this ...
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String getExtension(String filename)
|
||||||
|
{
|
||||||
|
int nExt = filename.lastIndexOf('.');
|
||||||
|
if (nExt < 0)
|
||||||
|
return "";
|
||||||
|
return filename.substring(nExt);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int lookupExtension(String filename)
|
||||||
|
{
|
||||||
|
String extn = getExtension (filename);
|
||||||
|
if (!mExtnMap.containsKey(extn))
|
||||||
|
return UNKNOWN;
|
||||||
|
return mExtnMap.get (extn);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getType(String filename)
|
||||||
|
{
|
||||||
|
int type = lookupExtension (filename);
|
||||||
|
android.util.Log.d("debug", "extn : " + filename + " -> " + type);
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by mode, and/or in future by filename/wildcard
|
||||||
|
static private boolean doAccept(String filename, int byMode, String byFilename)
|
||||||
|
{
|
||||||
|
android.util.Log.d("debug", "doAccept : " + filename + " mode " + byMode + " byFilename " + byFilename);
|
||||||
|
if (byMode == ALL && byFilename == "")
|
||||||
|
return true;
|
||||||
|
// check extension
|
||||||
|
if (byMode != ALL) {
|
||||||
|
if (mExtnMap.get (getExtension (filename)) != byMode)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
if (byFilename != "") {
|
||||||
|
// FIXME return false on a non-match
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FileFilter getFileFilter(final int mode)
|
||||||
|
{
|
||||||
|
return new FileFilter() {
|
||||||
|
public boolean accept(File pathname) {
|
||||||
|
if (pathname.isDirectory())
|
||||||
|
return true;
|
||||||
|
return doAccept(pathname.getName(), mode, "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static FilenameFilter getFilenameFilter(final int mode)
|
||||||
|
{
|
||||||
|
return new FilenameFilter() {
|
||||||
|
public boolean accept(File dir, String filename) {
|
||||||
|
if( new File( dir , filename ).isDirectory() )
|
||||||
|
return true;
|
||||||
|
return doAccept(filename, mode, "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
static void sortFiles(File[] files , int sortMode){
|
static void sortFiles(File[] files , int sortMode){
|
||||||
//Should really change all this to a switch statement...
|
// Should really change all this to a switch statement...
|
||||||
if( sortMode == SORT_AZ ){
|
if( sortMode == SORT_AZ ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return lhs.getName().compareTo( rhs.getName() );
|
return lhs.getName().compareTo( rhs.getName() );
|
||||||
}
|
}
|
||||||
@ -113,7 +136,6 @@ public class FileUtilities {
|
|||||||
}
|
}
|
||||||
if( sortMode == SORT_ZA ){
|
if( sortMode == SORT_ZA ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return rhs.getName().compareTo( lhs.getName() );
|
return rhs.getName().compareTo( lhs.getName() );
|
||||||
}
|
}
|
||||||
@ -122,7 +144,6 @@ public class FileUtilities {
|
|||||||
}
|
}
|
||||||
if( sortMode == SORT_OLDEST ){
|
if( sortMode == SORT_OLDEST ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return Long.valueOf( lhs.lastModified() ).compareTo( rhs.lastModified() );
|
return Long.valueOf( lhs.lastModified() ).compareTo( rhs.lastModified() );
|
||||||
}
|
}
|
||||||
@ -131,7 +152,6 @@ public class FileUtilities {
|
|||||||
}
|
}
|
||||||
if( sortMode == SORT_NEWEST ){
|
if( sortMode == SORT_NEWEST ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return Long.valueOf( rhs.lastModified() ).compareTo( lhs.lastModified() );
|
return Long.valueOf( rhs.lastModified() ).compareTo( lhs.lastModified() );
|
||||||
}
|
}
|
||||||
@ -140,7 +160,6 @@ public class FileUtilities {
|
|||||||
}
|
}
|
||||||
if( sortMode == SORT_LARGEST ){
|
if( sortMode == SORT_LARGEST ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return Long.valueOf( rhs.length() ).compareTo( lhs.length() );
|
return Long.valueOf( rhs.length() ).compareTo( lhs.length() );
|
||||||
}
|
}
|
||||||
@ -149,7 +168,6 @@ public class FileUtilities {
|
|||||||
}
|
}
|
||||||
if( sortMode == SORT_SMALLEST ){
|
if( sortMode == SORT_SMALLEST ){
|
||||||
Arrays.sort( files , new Comparator<File>() {
|
Arrays.sort( files , new Comparator<File>() {
|
||||||
|
|
||||||
public int compare(File lhs, File rhs) {
|
public int compare(File lhs, File rhs) {
|
||||||
return Long.valueOf( lhs.length() ).compareTo( rhs.length() );
|
return Long.valueOf( lhs.length() ).compareTo( rhs.length() );
|
||||||
}
|
}
|
||||||
@ -159,3 +177,5 @@ public class FileUtilities {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||||
|
@ -18,7 +18,7 @@ public class GridItemAdapter extends BaseAdapter{
|
|||||||
File[] filePaths;
|
File[] filePaths;
|
||||||
File currentDirectory;
|
File currentDirectory;
|
||||||
String tag = "GridItemAdapter";
|
String tag = "GridItemAdapter";
|
||||||
|
|
||||||
public GridItemAdapter(Context mContext, File[] filePaths) {
|
public GridItemAdapter(Context mContext, File[] filePaths) {
|
||||||
this.mContext = mContext;
|
this.mContext = mContext;
|
||||||
this.filePaths = filePaths;
|
this.filePaths = filePaths;
|
||||||
@ -26,14 +26,15 @@ public class GridItemAdapter extends BaseAdapter{
|
|||||||
Log.d(tag, fn.getName());
|
Log.d(tag, fn.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GridItemAdapter(Context mContext, File currentDirectory) {
|
public GridItemAdapter(Context mContext, File currentDirectory) {
|
||||||
this.mContext = mContext;
|
this.mContext = mContext;
|
||||||
this.currentDirectory = currentDirectory;
|
this.currentDirectory = currentDirectory;
|
||||||
filePaths = currentDirectory.listFiles();
|
filePaths = currentDirectory.listFiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GridItemAdapter(Context mContext, File currentDirectory, File[] filteredFiles) {
|
public GridItemAdapter(Context mContext, File currentDirectory, File[] filteredFiles)
|
||||||
|
{
|
||||||
this.mContext = mContext;
|
this.mContext = mContext;
|
||||||
this.currentDirectory = currentDirectory;
|
this.currentDirectory = currentDirectory;
|
||||||
filePaths = filteredFiles;
|
filePaths = filteredFiles;
|
||||||
@ -52,24 +53,22 @@ public class GridItemAdapter extends BaseAdapter{
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public View getView(int position, View convertView, ViewGroup parent) {
|
public View getView(int position, View convertView, ViewGroup parent)
|
||||||
|
{
|
||||||
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
|
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
|
||||||
Context.LAYOUT_INFLATER_SERVICE);
|
Context.LAYOUT_INFLATER_SERVICE);
|
||||||
|
|
||||||
View gridView;
|
View gridView;
|
||||||
|
|
||||||
if (convertView == null) {
|
if (convertView == null) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
gridView = (View) convertView;
|
gridView = (View) convertView;
|
||||||
}
|
}
|
||||||
gridView = new View(mContext);
|
gridView = new View(mContext);
|
||||||
|
|
||||||
// get layout from mobile.xml
|
// get layout from mobile.xml
|
||||||
gridView = inflater.inflate(R.layout.file_explorer_grid_item, null);
|
gridView = inflater.inflate(R.layout.file_explorer_grid_item, null);
|
||||||
|
|
||||||
// set value into textview
|
// set value into textview
|
||||||
TextView textView = (TextView) gridView
|
TextView textView = (TextView) gridView
|
||||||
.findViewById(R.id.grid_item_label);
|
.findViewById(R.id.grid_item_label);
|
||||||
@ -77,20 +76,31 @@ public class GridItemAdapter extends BaseAdapter{
|
|||||||
// set image based on selected text
|
// set image based on selected text
|
||||||
ImageView imageView = (ImageView) gridView
|
ImageView imageView = (ImageView) gridView
|
||||||
.findViewById(R.id.grid_item_image);
|
.findViewById(R.id.grid_item_image);
|
||||||
if( filePaths[position].getName().endsWith(".odt") ){
|
if( filePaths[position].isDirectory() ) // Is a folder
|
||||||
imageView.setImageResource(R.drawable.writer);
|
{
|
||||||
}
|
// Eventually have thumbnails of each sub file on a black circle
|
||||||
if( filePaths[position].getName().endsWith(".ods") ){
|
// For now just a folder icon
|
||||||
imageView.setImageResource(R.drawable.calc);
|
|
||||||
}
|
|
||||||
if( filePaths[position].getName().endsWith(".odp") ){
|
|
||||||
imageView.setImageResource(R.drawable.impress);
|
|
||||||
}
|
|
||||||
if( filePaths[position].isDirectory() ){//Is a folder
|
|
||||||
//Eventually have thumbnails of each sub file on a black circle
|
|
||||||
//For now just a folder icon
|
|
||||||
imageView.setImageResource(R.drawable.folder);
|
imageView.setImageResource(R.drawable.folder);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch (FileUtilities.getType(filePaths[position].getName()))
|
||||||
|
{
|
||||||
|
case FileUtilities.DOC:
|
||||||
|
imageView.setImageResource(R.drawable.writer);
|
||||||
|
break;
|
||||||
|
case FileUtilities.CALC:
|
||||||
|
imageView.setImageResource(R.drawable.calc);
|
||||||
|
break;
|
||||||
|
case FileUtilities.DRAW: // FIXME: only for now ...
|
||||||
|
case FileUtilities.IMPRESS:
|
||||||
|
imageView.setImageResource(R.drawable.impress);
|
||||||
|
break;
|
||||||
|
case FileUtilities.UNKNOWN:
|
||||||
|
default:
|
||||||
|
break; // FIXME something prettier ?
|
||||||
|
}
|
||||||
|
}
|
||||||
return gridView;
|
return gridView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ public class LibreOfficeUIActivity extends Activity implements OnNavigationListe
|
|||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
Log.d(tag, "onCreate");
|
Log.d(tag, "onCreate - tweaked - meeks !");
|
||||||
//Set the "home" - top level - directory.
|
//Set the "home" - top level - directory.
|
||||||
homeDirectory = new File(Environment.getExternalStorageDirectory(),"LibreOffice");
|
homeDirectory = new File(Environment.getExternalStorageDirectory(),"LibreOffice");
|
||||||
homeDirectory.mkdirs();
|
homeDirectory.mkdirs();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user