| 1 | package org.openintents.filemanager;\r |
| 2 | \r |
| 3 | import java.io.File;\r |
| 4 | import java.io.FileNotFoundException;\r |
| 5 | import java.io.IOException;\r |
| 6 | \r |
| 7 | import org.openintents.filemanager.util.MimeTypeParser;\r |
| 8 | import org.openintents.filemanager.util.MimeTypes;\r |
| 9 | import org.xmlpull.v1.XmlPullParserException;\r |
| 10 | \r |
| 11 | import android.content.ContentProvider;\r |
| 12 | import android.content.ContentValues;\r |
| 13 | import android.content.Context;\r |
| 14 | import android.content.pm.PackageManager.NameNotFoundException;\r |
| 15 | import android.content.res.XmlResourceParser;\r |
| 16 | import android.database.Cursor;\r |
| 17 | import android.database.MatrixCursor;\r |
| 18 | import android.net.Uri;\r |
| 19 | import android.os.ParcelFileDescriptor;\r |
| 20 | import android.provider.MediaStore;\r |
| 21 | import android.util.Log;\r |
| 22 | \r |
| 23 | public class FileManagerProvider extends ContentProvider {\r |
| 24 | \r |
| 25 | static final String FILE_PROVIDER_PREFIX = "content://org.openintents.filemanager";\r |
| 26 | private static final String TAG = "FileManagerProvider";\r |
| 27 | public static final String AUTHORITY = "org.openintents.filemanager";\r |
| 28 | private MimeTypes mMimeTypes;\r |
| 29 | \r |
| 30 | @Override\r |
| 31 | public boolean onCreate() {\r |
| 32 | getMimeTypes();\r |
| 33 | return true;\r |
| 34 | }\r |
| 35 | \r |
| 36 | private void getMimeTypes() {\r |
| 37 | Context ctx = getContext();\r |
| 38 | MimeTypeParser mtp = null;\r |
| 39 | try {\r |
| 40 | mtp = new MimeTypeParser(ctx, ctx.getPackageName());\r |
| 41 | } catch (NameNotFoundException e) {\r |
| 42 | //Should never get here\r |
| 43 | }\r |
| 44 | XmlResourceParser in = \r |
| 45 | getContext().getResources().getXml(R.xml.mimetypes);\r |
| 46 | \r |
| 47 | try {\r |
| 48 | mMimeTypes = mtp.fromXmlResource(in);\r |
| 49 | } catch (XmlPullParserException e) {\r |
| 50 | Log.e(TAG, "PreselectedChannelsActivity: XmlPullParserException", e);\r |
| 51 | throw new RuntimeException(\r |
| 52 | "PreselectedChannelsActivity: XmlPullParserException");\r |
| 53 | } catch (IOException e) {\r |
| 54 | Log.e(TAG, "PreselectedChannelsActivity: IOException", e);\r |
| 55 | throw new RuntimeException(\r |
| 56 | "PreselectedChannelsActivity: IOException");\r |
| 57 | }\r |
| 58 | }\r |
| 59 | \r |
| 60 | @Override\r |
| 61 | public int delete(Uri uri, String s, String[] as) {\r |
| 62 | // not supported\r |
| 63 | return 0;\r |
| 64 | }\r |
| 65 | \r |
| 66 | @Override\r |
| 67 | public String getType(Uri uri) {\r |
| 68 | // return file extension (uri.lastIndexOf("."))\r |
| 69 | return mMimeTypes.getMimeType(uri.toString());\r |
| 70 | }\r |
| 71 | \r |
| 72 | @Override\r |
| 73 | public Uri insert(Uri uri, ContentValues contentvalues) {\r |
| 74 | // not supported\r |
| 75 | return null;\r |
| 76 | }\r |
| 77 | \r |
| 78 | @Override\r |
| 79 | public Cursor query(Uri uri, String[] projection, String s, \r |
| 80 | String[] as1, String s1) {\r |
| 81 | if (uri.toString().startsWith(FILE_PROVIDER_PREFIX)) {\r |
| 82 | if (projection == null || projection.length == 0) {\r |
| 83 | // Standard projection including all supported rows\r |
| 84 | projection = new String [] {\r |
| 85 | MediaStore.MediaColumns.DATA,\r |
| 86 | MediaStore.MediaColumns.MIME_TYPE,\r |
| 87 | MediaStore.MediaColumns.DISPLAY_NAME,\r |
| 88 | MediaStore.MediaColumns.SIZE};\r |
| 89 | } \r |
| 90 | \r |
| 91 | MatrixCursor c = new MatrixCursor(projection);\r |
| 92 | MatrixCursor.RowBuilder row = c.newRow();\r |
| 93 | \r |
| 94 | // data = absolute path to file\r |
| 95 | String data = uri.getPath();\r |
| 96 | \r |
| 97 | int fromIndex = data.lastIndexOf(File.separatorChar) + 1;\r |
| 98 | if (fromIndex >= data.length()) {\r |
| 99 | // Last character was '/' or data is empty, so no file name \r |
| 100 | // was specified and we don't want to raise an \r |
| 101 | // IndexOutOfBoundsException\r |
| 102 | throw new RuntimeException("No file name specified: ".concat(data));\r |
| 103 | }\r |
| 104 | \r |
| 105 | // According to Android docs, DISPLAY_NAME should be\r |
| 106 | // the last segment of Uri\r |
| 107 | String displayName = \r |
| 108 | (fromIndex > 0) ? data.substring(fromIndex) : data;\r |
| 109 | \r |
| 110 | String mimeType = mMimeTypes.getMimeType(data);\r |
| 111 | \r |
| 112 | long size = -1;\r |
| 113 | File file = new File(data); \r |
| 114 | if (file.exists() && file.isFile()) {\r |
| 115 | size = file.length();\r |
| 116 | }\r |
| 117 | \r |
| 118 | for (String col : projection) {\r |
| 119 | if (col.equals(MediaStore.MediaColumns.DATA)) {\r |
| 120 | row.add(data);\r |
| 121 | } else if (col.equals(MediaStore.MediaColumns.MIME_TYPE)) {\r |
| 122 | row.add(mimeType);\r |
| 123 | } else if (col.equals(MediaStore.MediaColumns.DISPLAY_NAME)) {\r |
| 124 | row.add(displayName);\r |
| 125 | } else if (col.equals(MediaStore.MediaColumns.SIZE)) {\r |
| 126 | if (size >= 0)\r |
| 127 | row.add(size);\r |
| 128 | else {\r |
| 129 | // According to Android docs for unknown size.\r |
| 130 | // Standard getLong() won't throw exception and\r |
| 131 | // value will be 0.\r |
| 132 | row.add(null);\r |
| 133 | }\r |
| 134 | } else {\r |
| 135 | // Unsupported or unknown columns are filled up with null\r |
| 136 | row.add(null);\r |
| 137 | }\r |
| 138 | }\r |
| 139 | \r |
| 140 | return c;\r |
| 141 | } else {\r |
| 142 | throw new RuntimeException("Unsupported uri");\r |
| 143 | }\r |
| 144 | }\r |
| 145 | \r |
| 146 | @Override\r |
| 147 | public ParcelFileDescriptor openFile(Uri uri, String mode)\r |
| 148 | throws FileNotFoundException {\r |
| 149 | if (uri.toString().startsWith(FILE_PROVIDER_PREFIX)) {\r |
| 150 | int m = ParcelFileDescriptor.MODE_READ_ONLY;\r |
| 151 | if (mode.equalsIgnoreCase("rw"))\r |
| 152 | m = ParcelFileDescriptor.MODE_READ_WRITE;\r |
| 153 | File f = new File(uri.getPath());\r |
| 154 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, m);\r |
| 155 | return pfd;\r |
| 156 | } else {\r |
| 157 | throw new FileNotFoundException ("Unsupported uri: " + uri.toString());\r |
| 158 | }\r |
| 159 | }\r |
| 160 | \r |
| 161 | @Override\r |
| 162 | public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {\r |
| 163 | // not supported\r |
| 164 | return 0;\r |
| 165 | }\r |
| 166 | \r |
| 167 | }\r |