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
17package com.android.gallery3d.data;
18
19import com.android.gallery3d.app.GalleryApp;
20
21import android.net.Uri;
22
23import java.net.URLDecoder;
24import java.net.URLEncoder;
25
26class UriSource extends MediaSource {
27    @SuppressWarnings("unused")
28    private static final String TAG = "UriSource";
29
30    private GalleryApp mApplication;
31
32    public UriSource(GalleryApp context) {
33        super("uri");
34        mApplication = context;
35    }
36
37    @Override
38    public MediaObject createMediaObject(Path path) {
39        String segment[] = path.split();
40        if (segment.length != 2) {
41            throw new RuntimeException("bad path: " + path);
42        }
43
44        String decoded = URLDecoder.decode(segment[1]);
45        return new UriImage(mApplication, path, Uri.parse(decoded));
46    }
47
48    @Override
49    public Path findPathByUri(Uri uri) {
50        String type = mApplication.getContentResolver().getType(uri);
51        // Assume the type is image if the type cannot be resolved
52        // This could happen for "http" URI.
53        if (type == null || type.startsWith("image/")) {
54            return Path.fromString("/uri/" + URLEncoder.encode(uri.toString()));
55        }
56        return null;
57    }
58}
59