Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / android / source / src / java / org / libreoffice / ui / FileUtilities.java
blob811fc4562dc78597333f2323e1966a27e51f3cca
1 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 package org.libreoffice.ui;
11 import org.libreoffice.storage.IFile;
13 import java.io.File;
14 import java.io.FileFilter;
15 import java.io.FilenameFilter;
16 import java.util.Map;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.HashMap;
20 import java.util.Comparator;
21 import android.util.Log;
22 import android.webkit.MimeTypeMap;
24 public class FileUtilities {
26 private static String LOGTAG = FileUtilities.class.getSimpleName();
28 static final int ALL = -1;
30 // These have to be in sync with the file_view_modes resource.
31 static final int DOC = 0;
32 static final int CALC = 1;
33 static final int IMPRESS = 2;
34 static final int DRAWING = 3;
36 static final int UNKNOWN = 10;
38 static final int SORT_AZ = 0;
39 static final int SORT_ZA = 1;
40 /** Oldest Files First*/
41 static final int SORT_OLDEST = 2;
42 /** Newest Files First*/
43 static final int SORT_NEWEST = 3;
44 /** Largest Files First */
45 static final int SORT_LARGEST = 4;
46 /** Smallest Files First */
47 static final int SORT_SMALLEST = 5;
49 private static final Map<String, Integer> mExtnMap = new HashMap<String, Integer>();
50 private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();
51 static {
52 // Please keep this in sync with AndroidManifest.xml
54 // ODF
55 mExtnMap.put(".odt", DOC);
56 mExtnMap.put(".odg", DRAWING);
57 mExtnMap.put(".odp", IMPRESS);
58 mExtnMap.put(".ods", CALC);
59 mExtnMap.put(".fodt", DOC);
60 mExtnMap.put(".fodg", DRAWING);
61 mExtnMap.put(".fodp", IMPRESS);
62 mExtnMap.put(".fods", CALC);
64 // ODF templates
65 mExtnMap.put(".ott", DOC);
66 mExtnMap.put(".otg", DRAWING);
67 mExtnMap.put(".otp", IMPRESS);
68 mExtnMap.put(".ots", CALC);
70 // MS
71 mExtnMap.put(".rtf", DOC);
72 mExtnMap.put(".doc", DOC);
73 mExtnMap.put(".vsd", DRAWING);
74 mExtnMap.put(".vsdx", DRAWING);
75 mExtnMap.put(".pub", DRAWING);
76 mExtnMap.put(".ppt", IMPRESS);
77 // mExtnMap.put(".pps", IMPRESS);
78 mExtnMap.put(".xls", CALC);
80 // MS templates
81 mExtnMap.put(".dot", DOC);
82 mExtnMap.put(".pot", IMPRESS);
83 mExtnMap.put(".xlt", CALC);
85 // OOXML
86 mExtnMap.put(".docx", DOC);
87 mExtnMap.put(".pptx", IMPRESS);
88 // mExtnMap.put(".ppsx", IMPRESS);
89 mExtnMap.put(".xlsx", CALC);
91 // OOXML templates
92 mExtnMap.put(".dotx", DOC);
93 mExtnMap.put(".potx", IMPRESS);
94 mExtnMap.put(".xltx", CALC);
96 // Other
97 mExtnMap.put(".csv", CALC);
98 mExtnMap.put(".wps", DOC);
99 mExtnMap.put(".key", IMPRESS);
100 mExtnMap.put(".abw", DOC);
101 mExtnMap.put(".pmd", DRAWING);
102 mExtnMap.put(".emf", DRAWING);
103 mExtnMap.put(".svm", DRAWING);
104 mExtnMap.put(".wmf", DRAWING);
105 mExtnMap.put(".svg", DRAWING);
107 // Some basic MIME types
108 // Android's MimeTypeMap lacks some types that we need
109 extensionToMimeTypeMap.put("odb", "application/vnd.oasis.opendocument.database");
110 extensionToMimeTypeMap.put("odf", "application/vnd.oasis.opendocument.formula");
111 extensionToMimeTypeMap.put("odg", "application/vnd.oasis.opendocument.graphics");
112 extensionToMimeTypeMap.put("otg", "application/vnd.oasis.opendocument.graphics-template");
113 extensionToMimeTypeMap.put("odi", "application/vnd.oasis.opendocument.image");
114 extensionToMimeTypeMap.put("odp", "application/vnd.oasis.opendocument.presentation");
115 extensionToMimeTypeMap.put("otp", "application/vnd.oasis.opendocument.presentation-template");
116 extensionToMimeTypeMap.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
117 extensionToMimeTypeMap.put("ots", "application/vnd.oasis.opendocument.spreadsheet-template");
118 extensionToMimeTypeMap.put("odt", "application/vnd.oasis.opendocument.text");
119 extensionToMimeTypeMap.put("odm", "application/vnd.oasis.opendocument.text-master");
120 extensionToMimeTypeMap.put("ott", "application/vnd.oasis.opendocument.text-template");
121 extensionToMimeTypeMap.put("oth", "application/vnd.oasis.opendocument.text-web");
124 private static final String getExtension(String filename) {
125 if (filename == null)
126 return "";
127 int nExt = filename.lastIndexOf('.');
128 if (nExt < 0)
129 return "";
130 return filename.substring(nExt);
133 private static final int lookupExtension(String filename) {
134 String extn = getExtension(filename);
135 if (!mExtnMap.containsKey(extn))
136 return UNKNOWN;
137 return mExtnMap.get(extn);
140 static int getType(String filename) {
141 int type = lookupExtension (filename);
142 Log.d(LOGTAG, "extn : " + filename + " -> " + type);
143 return type;
146 static String getMimeType(String filename) {
147 String extension = MimeTypeMap.getFileExtensionFromUrl(filename);
148 String mime = extensionToMimeTypeMap.get(extension);
149 if (mime == null) {
150 //fallback to Android's MimeTypeMap
151 mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
152 extension);
154 return mime;
157 // Filter by mode, and/or in future by filename/wildcard
158 private static boolean doAccept(String filename, int byMode, String byFilename) {
159 Log.d(LOGTAG, "doAccept : " + filename + " mode " + byMode + " byFilename " + byFilename);
160 if (filename == null)
161 return false;
163 if (byMode == ALL && byFilename.equals("")) {
164 if (filename.startsWith(".")) {//ignore hidden files
165 return false;
167 return true;
169 // check extension
170 if (byMode != ALL) {
171 if (mExtnMap.get (getExtension (filename)) != byMode)
172 return false;
174 if (!byFilename.equals("")) {
175 // FIXME return false on a non-match
177 return true;
180 static FileFilter getFileFilter(final int mode) {
181 return new FileFilter() {
182 public boolean accept(File pathname) {
183 if (pathname.isDirectory())
184 return true;
185 if (lookupExtension(pathname.getName()) == UNKNOWN)
186 return false;
187 return doAccept(pathname.getName(), mode, "");
192 static FilenameFilter getFilenameFilter(final int mode) {
193 return new FilenameFilter() {
194 public boolean accept(File dir, String filename) {
195 if (new File(dir , filename).isDirectory())
196 return true;
197 return doAccept(filename, mode, "");
202 static void sortFiles(List<IFile> files, int sortMode) {
203 if (files == null)
204 return;
205 switch (sortMode) {
206 case SORT_AZ:
207 Collections.sort(files , new Comparator<IFile>() {
208 public int compare(IFile lhs, IFile rhs) {
209 return lhs.getName().compareTo(rhs.getName());
212 break;
213 case SORT_ZA:
214 Collections.sort(files , new Comparator<IFile>() {
215 public int compare(IFile lhs, IFile rhs) {
216 return rhs.getName().compareTo(lhs.getName());
219 break;
220 case SORT_OLDEST:
221 Collections.sort(files , new Comparator<IFile>() {
222 public int compare(IFile lhs, IFile rhs) {
223 return lhs.getLastModified().compareTo(rhs.getLastModified());
226 break;
227 case SORT_NEWEST:
228 Collections.sort(files , new Comparator<IFile>() {
229 public int compare(IFile lhs, IFile rhs) {
230 return rhs.getLastModified().compareTo(lhs.getLastModified());
233 break;
234 case SORT_LARGEST:
235 Collections.sort(files , new Comparator<IFile>() {
236 public int compare(IFile lhs, IFile rhs) {
237 return Long.valueOf(rhs.getSize()).compareTo(lhs.getSize());
240 break;
241 case SORT_SMALLEST:
242 Collections.sort(files , new Comparator<IFile>() {
243 public int compare(IFile lhs, IFile rhs) {
244 return Long.valueOf(lhs.getSize()).compareTo(rhs.getSize());
247 break;
248 default:
249 Log.e(LOGTAG, "uncatched sortMode: " + sortMode);
251 return;
254 static boolean isHidden(File file) {
255 if (file.getName().startsWith("."))
256 return true;
257 return false;
260 static boolean isThumbnail(File file) {
261 if (isHidden(file) && file.getName().endsWith(".png"))
262 return true;
263 return false;
266 static boolean hasThumbnail(File file) {
267 String filename = file.getName();
268 if (lookupExtension(filename) == DOC) // only do this for docs for now
270 // Will need another method to check if Thumb is up-to-date - or extend this one?
271 if (new File(file.getParent() , getThumbnailName(file)).isFile())
272 return true;
273 return false; // If it's a document with no thumb
275 return true;
278 static String getThumbnailName(File file) {
279 return "." + file.getName().split("[.]")[0] + ".png" ;
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */