Kaydet (Commit) 2ac02d60 authored tarafından Jacobo Aragunde Pérez's avatar Jacobo Aragunde Pérez

Android: Abstract file management from LibreOfficeUIActivity.

This is the first step towards implementing support for different
cloud providers.

The use of File objects is replaced with the interface IFile, which
has similar operations. A LocalFile implementation is provided to
deal with local contents in the same way it was being done before.

Some system-wide storage operations are abstracted in the interface
IDocumentProvider. A LocalDocumentsProvider implementation is
provided.

Known issues in this patch:

* Dummy filesystem code has been removed. This should be provided as
  an implementation of IDocumentProvider in case we need it, but that
  will probably not happen.

* Sorting is not implemented in LocalDocumentsProvider yet. It seemed
  not to be working anyway.

* Code to get directory thumbnails has been commented out. This code
  will probably belong to the IDocumentProvider/IFile implementations.
  We might want to have a different approach instead of reading
  hidden thumbnail files, something similar to what the native
  Start Center does.

Change-Id: Ib0882c69a0b24e28d2346bbe0154db01d1102b06
üst 4bb76ebf
/* -*- 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.storage;
import java.net.URI;
/**
* Represents a Document Provider, an object able to provide documents from a
* certain source (e.g. local documents, DropBox, Google Docs).
*/
public interface IDocumentProvider {
/**
* Provides the content root element for the Document Provider.
*
* @return Content root element.
*/
IFile getRootDirectory();
/**
* Transforms some URI into the IFile object that represents that content.
*
* @param uri
* URI pointing to some content object that has been previously
* retrieved with IFile.getUri().
* @return IFile object pointing to the content represented by uri.
*/
IFile createFromUri(URI uri);
}
/* -*- 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.storage;
import java.io.File;
import java.io.FileFilter;
import java.net.URI;
import java.util.Date;
import java.util.List;
/**
* An abstraction of the File class, intended to be implemented by different
* Document Providers.
*
* It represents a file or a directory in the context of a certain Document
* Provider. It wraps the file-related operations and provides access to the
* final document as a local File, downloading it if necessary.
*/
public interface IFile {
/**
* Provides a URI that represents this IFile object.
*
* @return URI that represents this IFile object in the context of the
* Document Provider that created it. The URI can be transformed
* back into an IFile object with IDocumentProvider.createFromUri().
*/
URI getUri();
/**
* Returns the name of the file or directory represented by this file.
*
* @return This file's name.
*/
String getName();
/**
* Indicates if this file represents a directory in the context of the
* Document Provider which originated it.
*
* @return true if this file is a directory, false otherwise.
*/
boolean isDirectory();
/**
* Returns the file size in bytes.
*
* @return file size in bytes, 0 if the file does not exist.
*/
long getSize();
/**
* Returns the time when this file was last modified, measured in
* milliseconds since January 1st, 1970, midnight.
*
* @return time when this file was last modified, or 0 if the file does not
* exist.
*/
Date getLastModified();
/**
* Returns a list containing the files in the directory represented by this
* file.
*
* @return list of files contained by this directory, or an empty list if
* this is not a directory.
*/
List<IFile> listFiles();
/**
* Gets the list of files contained in the directory represented by this
* file, and filters it through some FilenameFilter.
*
* @param filter
* the filter to match names against.
* @return filtered list of files contained by this directory, or an empty
* list if this is not a directory.
*/
List<IFile> listFiles(FileFilter filter);
/**
* Returns the pparent of this file.
*
* @return this file's parent or null if it does not have it.
*/
IFile getParent();
/**
* Returns the document wrapped by this IFile as a local file. The result
* for a directory is not defined.
*
* @return local file containing the document wrapped by this object.
*/
File getDocument();
}
/* -*- 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.storage.local;
import java.net.URI;
import org.libreoffice.storage.IDocumentProvider;
import org.libreoffice.storage.IFile;
import android.os.Environment;
/**
* Implementation of IDocumentProvider for the local file system.
*/
public class LocalDocumentsProvider implements IDocumentProvider {
@Override
public IFile getRootDirectory() {
return new LocalFile(Environment.getExternalStorageDirectory());
}
@Override
public IFile createFromUri(URI uri) {
return new LocalFile(uri);
}
}
/* -*- 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.storage.local;
import java.io.File;
import java.io.FileFilter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.libreoffice.storage.IFile;
/**
* Implementation of IFile for the local file system.
*/
public class LocalFile implements IFile {
private File file;
public LocalFile(File file) {
this.file = file;
}
public LocalFile(URI uri) {
this.file = new File(uri);
}
public URI getUri() {
return file.toURI();
}
public String getName() {
return file.getName();
}
@Override
public boolean isDirectory() {
return file.isDirectory();
}
@Override
public long getSize() {
return file.length();
}
@Override
public Date getLastModified() {
return new Date(file.lastModified());
}
@Override
public List<IFile> listFiles() {
List<IFile> children = new ArrayList<IFile>();
for (File child : file.listFiles()) {
children.add(new LocalFile(child));
}
return children;
}
@Override
public List<IFile> listFiles(FileFilter filter) {
List<IFile> children = new ArrayList<IFile>();
for (File child : file.listFiles(filter)) {
children.add(new LocalFile(child));
}
return children;
}
@Override
public IFile getParent() {
return new LocalFile(file.getParentFile());
}
@Override
public File getDocument() {
return file;
}
}
...@@ -10,11 +10,13 @@ ...@@ -10,11 +10,13 @@
package org.libreoffice.ui; package org.libreoffice.ui;
import org.libreoffice.R; import org.libreoffice.R;
import org.libreoffice.storage.IFile;
import java.io.File; import java.io.File;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.List;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
...@@ -33,25 +35,26 @@ import android.graphics.Color; ...@@ -33,25 +35,26 @@ import android.graphics.Color;
public class GridItemAdapter extends BaseAdapter { public class GridItemAdapter extends BaseAdapter {
Context mContext; Context mContext;
File[] filePaths; List<IFile> filePaths;
File currentDirectory; IFile currentDirectory;
String TAG = "GridItemAdapter"; String TAG = "GridItemAdapter";
public GridItemAdapter(Context mContext, File[] filePaths) { public GridItemAdapter(Context mContext, List<IFile> filePaths) {
this.mContext = mContext; this.mContext = mContext;
this.filePaths = filePaths; this.filePaths = filePaths;
for(File fn : filePaths){ for (IFile fn : filePaths) {
Log.d(TAG, fn.getName()); Log.d(TAG, fn.getName());
} }
} }
public GridItemAdapter(Context mContext, File currentDirectory) { public GridItemAdapter(Context mContext, IFile 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, IFile currentDirectory,
List<IFile> filteredFiles)
{ {
this.mContext = mContext; this.mContext = mContext;
this.currentDirectory = currentDirectory; this.currentDirectory = currentDirectory;
...@@ -59,7 +62,7 @@ public class GridItemAdapter extends BaseAdapter { ...@@ -59,7 +62,7 @@ public class GridItemAdapter extends BaseAdapter {
} }
public int getCount() { public int getCount() {
return filePaths.length; return filePaths.size();
} }
public Object getItem(int position) { public Object getItem(int position) {
...@@ -90,11 +93,11 @@ public class GridItemAdapter extends BaseAdapter { ...@@ -90,11 +93,11 @@ public class GridItemAdapter extends BaseAdapter {
// 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);
textView.setText(filePaths[position].getName()); textView.setText(filePaths.get(position).getName());
// 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].isDirectory() ) // Is a folder if (filePaths.get(position).isDirectory()) // Is a folder
{ {
// Default view is a generic folder icon. // Default view is a generic folder icon.
imageView.setImageResource(R.drawable.folder); imageView.setImageResource(R.drawable.folder);
...@@ -102,13 +105,14 @@ public class GridItemAdapter extends BaseAdapter { ...@@ -102,13 +105,14 @@ public class GridItemAdapter extends BaseAdapter {
gridView = inflater.inflate(R.layout.file_explorer_folder_icon, null); gridView = inflater.inflate(R.layout.file_explorer_folder_icon, null);
org.libreoffice.ui.FolderIconView icon = org.libreoffice.ui.FolderIconView icon =
(org.libreoffice.ui.FolderIconView)gridView.findViewById(R.id.folder_icon); (org.libreoffice.ui.FolderIconView)gridView.findViewById(R.id.folder_icon);
icon.setDir( filePaths[position]); // icon.setDir( filePaths[position]);
textView = (TextView) gridView.findViewById(R.id.grid_item_label); textView = (TextView) gridView.findViewById(R.id.grid_item_label);
textView.setText(filePaths[position].getName()); textView.setText(filePaths.get(position).getName());
return gridView; return gridView;
} }
else else
{ {
/*
File thumbnailFile = new File( filePaths[position].getParent() , "." File thumbnailFile = new File( filePaths[position].getParent() , "."
+ filePaths[position].getName().split("[.]")[0] + ".png"); + filePaths[position].getName().split("[.]")[0] + ".png");
BitmapFactory factory = new BitmapFactory(); BitmapFactory factory = new BitmapFactory();
...@@ -118,13 +122,16 @@ public class GridItemAdapter extends BaseAdapter { ...@@ -118,13 +122,16 @@ public class GridItemAdapter extends BaseAdapter {
}else{ }else{
Log.i( "GRID" , thumbnailFile.getAbsolutePath() ); Log.i( "GRID" , thumbnailFile.getAbsolutePath() );
} }
switch (FileUtilities.getType(filePaths[position].getName())) */
switch (FileUtilities.getType(filePaths.get(position).getName()))
{ {
case FileUtilities.DOC: case FileUtilities.DOC:
/*
if( thumb != null){ if( thumb != null){
imageView.setImageBitmap( thumb ); imageView.setImageBitmap( thumb );
break; break;
} }
*/
imageView.setImageResource(R.drawable.writer); imageView.setImageResource(R.drawable.writer);
break; break;
/*case FileUtilities.CALC: /*case FileUtilities.CALC:
......
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