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 */ 9 package org.libreoffice.ui; 10 11 import java.util.Map; 12 import java.util.HashMap; 13 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; 19 20 public 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 mExtnMap.put(".vstx", DRAWING); 74 75 // OOXML 76 mExtnMap.put(".docx", DOC); 77 mExtnMap.put(".pptx", IMPRESS); 78 mExtnMap.put(".ppsx", IMPRESS); 79 mExtnMap.put(".xlsx", CALC); 80 81 // OOXML templates 82 mExtnMap.put(".dotx", DOC); 83 mExtnMap.put(".potx", IMPRESS); 84 mExtnMap.put(".xltx", CALC); 85 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); 96 } 97 getExtension(String filename)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); 105 } 106 lookupExtension(String filename)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); 112 } 113 getType(String filename)114 static int getType(String filename) { 115 int type = lookupExtension (filename); 116 Log.d(LOGTAG, "extn : " + filename + " -> " + type); 117 return type; 118 } 119 120 /** 121 * Returns whether the passed MIME type is one for a document template. 122 */ isTemplateMimeType(final String mimeType)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"); 126 } 127 stripExtensionFromFileName(final String fileName)128 public static String stripExtensionFromFileName(final String fileName) 129 { 130 return fileName.split("\\.[A-Za-z0-9]*$")[0]; 131 } 132 133 /** 134 * Tries to retrieve the display (which should be the document name) 135 * for the given URI using the given resolver. 136 */ retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri)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)); 146 } 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(); 153 } 154 } 155 return displayName; 156 } 157 } 158 159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 160
