cid#1607171 Data race condition
[LibreOffice.git] / android / source / src / java / org / libreoffice / ui / FileUtilities.java
blobf7fa856217fca9e529ee3f8e2b7c18c6953c1cb1
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 java.util.Map;
12 import java.util.HashMap;
14 import android.content.ContentResolver;
15 import android.database.Cursor;
16 import android.net.Uri;
17 import android.provider.OpenableColumns;
18 import android.util.Log;
20 public class FileUtilities {
22 private static final String LOGTAG = FileUtilities.class.getSimpleName();
24 // These have to be in sync with the file_view_modes resource.
25 static final int DOC = 0;
26 static final int CALC = 1;
27 static final int IMPRESS = 2;
28 static final int DRAWING = 3;
30 static final int UNKNOWN = 10;
32 public static final String MIMETYPE_OPENDOCUMENT_TEXT = "application/vnd.oasis.opendocument.text";
33 public static final String MIMETYPE_OPENDOCUMENT_SPREADSHEET = "application/vnd.oasis.opendocument.spreadsheet";
34 public static final String MIMETYPE_OPENDOCUMENT_PRESENTATION = "application/vnd.oasis.opendocument.presentation";
35 public static final String MIMETYPE_OPENDOCUMENT_GRAPHICS = "application/vnd.oasis.opendocument.graphics";
36 public static final String MIMETYPE_PDF = "application/pdf";
38 private static final Map<String, Integer> mExtnMap = new HashMap<String, Integer>();
39 static {
40 // Please keep this in sync with AndroidManifest.xml
41 // and 'SUPPORTED_MIME_TYPES' in LibreOfficeUIActivity.java
43 // ODF
44 mExtnMap.put(".odt", DOC);
45 mExtnMap.put(".odg", DRAWING);
46 mExtnMap.put(".odp", IMPRESS);
47 mExtnMap.put(".ods", CALC);
48 mExtnMap.put(".fodt", DOC);
49 mExtnMap.put(".fodg", DRAWING);
50 mExtnMap.put(".fodp", IMPRESS);
51 mExtnMap.put(".fods", CALC);
53 // ODF templates
54 mExtnMap.put(".ott", DOC);
55 mExtnMap.put(".otg", DRAWING);
56 mExtnMap.put(".otp", IMPRESS);
57 mExtnMap.put(".ots", CALC);
59 // MS
60 mExtnMap.put(".rtf", DOC);
61 mExtnMap.put(".doc", DOC);
62 mExtnMap.put(".vsd", DRAWING);
63 mExtnMap.put(".vsdx", DRAWING);
64 mExtnMap.put(".pub", DRAWING);
65 mExtnMap.put(".ppt", IMPRESS);
66 mExtnMap.put(".pps", IMPRESS);
67 mExtnMap.put(".xls", CALC);
69 // MS templates
70 mExtnMap.put(".dot", DOC);
71 mExtnMap.put(".pot", IMPRESS);
72 mExtnMap.put(".xlt", CALC);
73 mExtnMap.put(".vstx", DRAWING);
75 // OOXML
76 mExtnMap.put(".docx", DOC);
77 mExtnMap.put(".pptx", IMPRESS);
78 mExtnMap.put(".ppsx", IMPRESS);
79 mExtnMap.put(".xlsx", CALC);
81 // OOXML templates
82 mExtnMap.put(".dotx", DOC);
83 mExtnMap.put(".potx", IMPRESS);
84 mExtnMap.put(".xltx", CALC);
86 // Other
87 mExtnMap.put(".csv", CALC);
88 mExtnMap.put(".wps", DOC);
89 mExtnMap.put(".key", IMPRESS);
90 mExtnMap.put(".abw", DOC);
91 mExtnMap.put(".pmd", DRAWING);
92 mExtnMap.put(".emf", DRAWING);
93 mExtnMap.put(".svm", DRAWING);
94 mExtnMap.put(".wmf", DRAWING);
95 mExtnMap.put(".svg", DRAWING);
98 public static String getExtension(String filename) {
99 if (filename == null)
100 return "";
101 int nExt = filename.lastIndexOf('.');
102 if (nExt < 0)
103 return "";
104 return filename.substring(nExt);
107 private static int lookupExtension(String filename) {
108 String extn = getExtension(filename);
109 if (!mExtnMap.containsKey(extn))
110 return UNKNOWN;
111 return mExtnMap.get(extn);
114 static int getType(String filename) {
115 int type = lookupExtension (filename);
116 Log.d(LOGTAG, "extn : " + filename + " -> " + type);
117 return type;
121 * Returns whether the passed MIME type is one for a document template.
123 public static boolean isTemplateMimeType(final String mimeType) {
124 // this works for ODF and OOXML template MIME types
125 return mimeType != null && mimeType.endsWith("template");
128 public static String stripExtensionFromFileName(final String fileName)
130 return fileName.split("\\.[A-Za-z0-9]*$")[0];
134 * Tries to retrieve the display (which should be the document name)
135 * for the given URI using the given resolver.
137 public static String retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri) {
138 String displayName = "";
139 // try to retrieve original file name
140 Cursor cursor = null;
141 try {
142 String[] columns = {OpenableColumns.DISPLAY_NAME};
143 cursor = resolver.query(docUri, columns, null, null, null);
144 if (cursor != null && cursor.moveToFirst()) {
145 displayName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
147 } catch (SecurityException e) {
148 // thrown e.g. when Uri has become invalid, e.g. corresponding file has been deleted
149 Log.i(LOGTAG, "SecurityException when trying to receive display name for Uri " + docUri);
150 } finally {
151 if (cursor != null) {
152 cursor.close();
155 return displayName;
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */