1/*
2 * Copyright (C) 2012 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 */
16package com.android.gallery3d.data;
17
18import com.android.gallery3d.app.GalleryApp;
19
20public class SnailSource extends MediaSource {
21    private static final String TAG = "SnailSource";
22    private static final int SNAIL_ALBUM = 0;
23    private static final int SNAIL_ITEM = 1;
24
25    private GalleryApp mApplication;
26    private PathMatcher mMatcher;
27    private static int sNextId;
28
29    public SnailSource(GalleryApp application) {
30        super("snail");
31        mApplication = application;
32        mMatcher = new PathMatcher();
33        mMatcher.add("/snail/set/*", SNAIL_ALBUM);
34        mMatcher.add("/snail/item/*", SNAIL_ITEM);
35    }
36
37    // The only path we accept is "/snail/set/id" and "/snail/item/id"
38    @Override
39    public MediaObject createMediaObject(Path path) {
40        DataManager dataManager = mApplication.getDataManager();
41        switch (mMatcher.match(path)) {
42            case SNAIL_ALBUM:
43                String itemPath = "/snail/item/" + mMatcher.getVar(0);
44                MediaItem item =
45                        (MediaItem) dataManager.getMediaObject(itemPath);
46                return new SnailAlbum(path, item);
47            case SNAIL_ITEM: {
48                int id = mMatcher.getIntVar(0);
49                return new SnailItem(path);
50            }
51        }
52        return null;
53    }
54
55    // Registers a new SnailAlbum containing a SnailItem and returns the id of
56    // them. You can obtain the Path of the SnailAlbum and SnailItem associated
57    // with the id by getSetPath and getItemPath().
58    public static synchronized int newId() {
59        return sNextId++;
60    }
61
62    public static Path getSetPath(int id) {
63        return Path.fromString("/snail/set").getChild(id);
64    }
65
66    public static Path getItemPath(int id) {
67        return Path.fromString("/snail/item").getChild(id);
68    }
69}
70