• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.net.URLDecoder;
26 import java.net.URLEncoder;
27 
28 class UriSource extends MediaSource {
29     @SuppressWarnings("unused")
30     private static final String TAG = "UriSource";
31     private static final String IMAGE_TYPE_PREFIX = "image/";
32     private static final String IMAGE_TYPE_ANY = "image/*";
33 
34     private GalleryApp mApplication;
35 
UriSource(GalleryApp context)36     public UriSource(GalleryApp context) {
37         super("uri");
38         mApplication = context;
39     }
40 
41     @Override
createMediaObject(Path path)42     public MediaObject createMediaObject(Path path) {
43         String segment[] = path.split();
44         if (segment.length != 3) {
45             throw new RuntimeException("bad path: " + path);
46         }
47         String uri = URLDecoder.decode(segment[1]);
48         String type = URLDecoder.decode(segment[2]);
49         return new UriImage(mApplication, path, Uri.parse(uri), type);
50     }
51 
getMimeType(Uri uri)52     private String getMimeType(Uri uri) {
53         if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
54             String extension =
55                     MimeTypeMap.getFileExtensionFromUrl(uri.toString());
56             String type = MimeTypeMap.getSingleton()
57                     .getMimeTypeFromExtension(extension.toLowerCase());
58             if (type != null) return type;
59         }
60         // Assume the type is image if the type cannot be resolved
61         // This could happen for "http" URI.
62         String type = mApplication.getContentResolver().getType(uri);
63         if (type == null) type = "image/*";
64         return type;
65     }
66 
67     @Override
findPathByUri(Uri uri, String type)68     public Path findPathByUri(Uri uri, String type) {
69         String mimeType = getMimeType(uri);
70 
71         // Try to find a most specific type but it has to be started with "image/"
72         if ((type == null) || (IMAGE_TYPE_ANY.equals(type)
73                 && mimeType.startsWith(IMAGE_TYPE_PREFIX))) {
74             type = mimeType;
75         }
76 
77         if (type.startsWith(IMAGE_TYPE_PREFIX)) {
78             return Path.fromString("/uri/" + URLEncoder.encode(uri.toString())
79                     + "/" +URLEncoder.encode(type));
80         }
81         // We have no clues that it is an image
82         return null;
83     }
84 }
85