Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / libreoffice / ui / FileUtilities.java
blob7fc8c3c84eb1fa124ab716146ff880d1bafb2bee
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);
74 // OOXML
75 mExtnMap.put(".docx", DOC);
76 mExtnMap.put(".pptx", IMPRESS);
77 mExtnMap.put(".ppsx", IMPRESS);
78 mExtnMap.put(".xlsx", CALC);
80 // OOXML templates
81 mExtnMap.put(".dotx", DOC);
82 mExtnMap.put(".potx", IMPRESS);
83 mExtnMap.put(".xltx", CALC);
85 // Other
86 mExtnMap.put(".csv", CALC);
87 mExtnMap.put(".wps", DOC);
88 mExtnMap.put(".key", IMPRESS);
89 mExtnMap.put(".abw", DOC);
90 mExtnMap.put(".pmd", DRAWING);
91 mExtnMap.put(".emf", DRAWING);
92 mExtnMap.put(".svm", DRAWING);
93 mExtnMap.put(".wmf", DRAWING);
94 mExtnMap.put(".svg", DRAWING);
97 public static String getExtension(String filename) {
98 if (filename == null)
99 return "";
100 int nExt = filename.lastIndexOf('.');
101 if (nExt < 0)
102 return "";
103 return filename.substring(nExt);
106 private static int lookupExtension(String filename) {
107 String extn = getExtension(filename);
108 if (!mExtnMap.containsKey(extn))
109 return UNKNOWN;
110 return mExtnMap.get(extn);
113 static int getType(String filename) {
114 int type = lookupExtension (filename);
115 Log.d(LOGTAG, "extn : " + filename + " -> " + type);
116 return type;
120 * Returns whether the passed MIME type is one for a document template.
122 public static boolean isTemplateMimeType(final String mimeType) {
123 // this works for ODF and OOXML template MIME types
124 return mimeType != null && mimeType.endsWith("template");
127 public static String stripExtensionFromFileName(final String fileName)
129 return fileName.split("\\.[A-Za-z0-9]*$")[0];
133 * Tries to retrieve the display (which should be the document name)
134 * for the given URI using the given resolver.
136 public static String retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri) {
137 String displayName = "";
138 // try to retrieve original file name
139 Cursor cursor = null;
140 try {
141 String[] columns = {OpenableColumns.DISPLAY_NAME};
142 cursor = resolver.query(docUri, columns, null, null, null);
143 if (cursor != null && cursor.moveToFirst()) {
144 displayName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
146 } catch (SecurityException e) {
147 // thrown e.g. when Uri has become invalid, e.g. corresponding file has been deleted
148 Log.i(LOGTAG, "SecurityException when trying to receive display name for Uri " + docUri);
149 } finally {
150 if (cursor != null) {
151 cursor.close();
154 return displayName;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */