LibreOffice Module android (master) 1
FileUtilities.java
Go to the documentation of this file.
1/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9package org.libreoffice.ui;
10
11import java.util.Map;
12import java.util.HashMap;
13
14import android.content.ContentResolver;
15import android.database.Cursor;
16import android.net.Uri;
17import android.provider.OpenableColumns;
18import android.util.Log;
19
20public class FileUtilities {
21
22 private static final String LOGTAG = FileUtilities.class.getSimpleName();
23
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;
29
30 static final int UNKNOWN = 10;
31
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";
37
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
42
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);
52
53 // ODF templates
54 mExtnMap.put(".ott", DOC);
55 mExtnMap.put(".otg", DRAWING);
56 mExtnMap.put(".otp", IMPRESS);
57 mExtnMap.put(".ots", CALC);
58
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);
68
69 // MS templates
70 mExtnMap.put(".dot", DOC);
71 mExtnMap.put(".pot", IMPRESS);
72 mExtnMap.put(".xlt", CALC);
73
74 // OOXML
75 mExtnMap.put(".docx", DOC);
76 mExtnMap.put(".pptx", IMPRESS);
77 mExtnMap.put(".ppsx", IMPRESS);
78 mExtnMap.put(".xlsx", CALC);
79
80 // OOXML templates
81 mExtnMap.put(".dotx", DOC);
82 mExtnMap.put(".potx", IMPRESS);
83 mExtnMap.put(".xltx", CALC);
84
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);
95 }
96
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);
104 }
105
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);
111 }
112
113 static int getType(String filename) {
114 int type = lookupExtension (filename);
115 Log.d(LOGTAG, "extn : " + filename + " -> " + type);
116 return type;
117 }
118
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");
125 }
126
131 public static String retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri) {
132 String displayName = "";
133 // try to retrieve original file name
134 Cursor cursor = null;
135 try {
136 String[] columns = {OpenableColumns.DISPLAY_NAME};
137 cursor = resolver.query(docUri, columns, null, null, null);
138 if (cursor != null && cursor.moveToFirst()) {
139 displayName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
140 }
141 } catch (SecurityException e) {
142 // thrown e.g. when Uri has become invalid, e.g. corresponding file has been deleted
143 Log.i(LOGTAG, "SecurityException when trying to receive display name for Uri " + docUri);
144 } finally {
145 if (cursor != null) {
146 cursor.close();
147 }
148 }
149 return displayName;
150 }
151}
152
153/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static final String MIMETYPE_OPENDOCUMENT_TEXT
static boolean isTemplateMimeType(final String mimeType)
Returns whether the passed MIME type is one for a document template.
static final String MIMETYPE_PDF
static final String MIMETYPE_OPENDOCUMENT_PRESENTATION
static String retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri)
Tries to retrieve the display (which should be the document name) for the given URI using the given r...
static final Map< String, Integer > mExtnMap
static int lookupExtension(String filename)
static final String MIMETYPE_OPENDOCUMENT_GRAPHICS
static String getExtension(String filename)
static final String MIMETYPE_OPENDOCUMENT_SPREADSHEET
UNKNOWN
resolver
ResultType type