1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.data;
18 
19 import android.content.ContentResolver;
20 import android.net.Uri;
21 import android.webkit.MimeTypeMap;
22 
23 import com.android.gallery3d.app.GalleryApp;
24 
25 import java.io.UnsupportedEncodingException;
26 import java.net.URLDecoder;
27 import java.net.URLEncoder;
28 
29 class UriSource extends MediaSource {
30     @SuppressWarnings("unused")
31     private static final String TAG = "UriSource";
32     private static final String IMAGE_TYPE_PREFIX = "image/";
33     private static final String IMAGE_TYPE_ANY = "image/*";
34     private static final String CHARSET_UTF_8 = "utf-8";
35 
36     private GalleryApp mApplication;
37 
UriSource(GalleryApp context)38     public UriSource(GalleryApp context) {
39         super("uri");
40         mApplication = context;
41     }
42 
43     @Override
createMediaObject(Path path)44     public MediaObject createMediaObject(Path path) {
45         String segment[] = path.split();
46         if (segment.length != 3) {
47             throw new RuntimeException("bad path: " + path);
48         }
49         try {
50             String uri = URLDecoder.decode(segment[1], CHARSET_UTF_8);
51             String type = URLDecoder.decode(segment[2], CHARSET_UTF_8);
52             return new UriImage(mApplication, path, Uri.parse(uri), type);
53         } catch (UnsupportedEncodingException e) {
54             throw new AssertionError(e);
55         }
56     }
57 
getMimeType(Uri uri)58     private String getMimeType(Uri uri) {
59         if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
60             String extension =
61                     MimeTypeMap.getFileExtensionFromUrl(uri.toString());
62             String type = MimeTypeMap.getSingleton()
63                     .getMimeTypeFromExtension(extension.toLowerCase());
64             if (type != null) return type;
65         }
66         // Assume the type is image if the type cannot be resolved
67         // This could happen for "http" URI.
68         String type = mApplication.getContentResolver().getType(uri);
69         if (type == null) type = "image/*";
70         return type;
71     }
72 
73     @Override
findPathByUri(Uri uri, String type)74     public Path findPathByUri(Uri uri, String type) {
75         String mimeType = getMimeType(uri);
76 
77         // Try to find a most specific type but it has to be started with "image/"
78         if ((type == null) || (IMAGE_TYPE_ANY.equals(type)
79                 && mimeType.startsWith(IMAGE_TYPE_PREFIX))) {
80             type = mimeType;
81         }
82 
83         if (type.startsWith(IMAGE_TYPE_PREFIX)) {
84             try {
85                 return Path.fromString("/uri/"
86                         + URLEncoder.encode(uri.toString(), CHARSET_UTF_8)
87                         + "/" +URLEncoder.encode(type, CHARSET_UTF_8));
88             } catch (UnsupportedEncodingException e) {
89                 throw new AssertionError(e);
90             }
91         }
92         // We have no clues that it is an image
93         return null;
94     }
95 }
96