SnailSource.java revision b29a27f475a2c449abdda8d4e03d30914feed8c6
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 android.util.SparseArray;
19import com.android.gallery3d.app.GalleryApp;
20import com.android.gallery3d.ui.ScreenNail;
21
22public class SnailSource extends MediaSource {
23    private static final String TAG = "SnailSource";
24    private static final int SNAIL_ALBUM = 0;
25    private static final int SNAIL_ITEM = 1;
26
27    private GalleryApp mApplication;
28    private PathMatcher mMatcher;
29    private static int sNextId;
30    private static SparseArray<ScreenNail> sRegistry = new SparseArray<ScreenNail>();
31
32    public SnailSource(GalleryApp application) {
33        super("snail");
34        mApplication = application;
35        mMatcher = new PathMatcher();
36        mMatcher.add("/snail/set/*", SNAIL_ALBUM);
37        mMatcher.add("/snail/item/*", SNAIL_ITEM);
38    }
39
40    // The only path we accept is "/snail/set/id" and "/snail/item/id"
41    @Override
42    public MediaObject createMediaObject(Path path) {
43        DataManager dataManager = mApplication.getDataManager();
44        switch (mMatcher.match(path)) {
45            case SNAIL_ALBUM:
46                String itemPath = "/snail/item/" + mMatcher.getVar(0);
47                MediaItem item =
48                        (MediaItem) dataManager.getMediaObject(itemPath);
49                return new SnailAlbum(path, item);
50            case SNAIL_ITEM: {
51                int id = mMatcher.getIntVar(0);
52                return new SnailItem(path, lookupScreenNail(id));
53            }
54        }
55        return null;
56    }
57
58    // Registers a ScreenNail and returns the id of it. You can obtain the Path
59    // of the MediaItem associated with the ScreenNail by getItemPath(), and the
60    // Path of the MediaSet containing that MediaItem by getSetPath().
61    public static synchronized int registerScreenNail(ScreenNail s) {
62        int id = sNextId++;
63        sRegistry.put(id, s);
64        return id;
65    }
66
67    public static Path getSetPath(int id) {
68        return Path.fromString("/snail/set").getChild(id);
69    }
70
71    public static Path getItemPath(int id) {
72        return Path.fromString("/snail/item").getChild(id);
73    }
74
75    public static synchronized void unregisterScreenNail(ScreenNail s) {
76        int index = sRegistry.indexOfValue(s);
77        sRegistry.removeAt(index);
78    }
79
80    private static synchronized ScreenNail lookupScreenNail(int id) {
81        return sRegistry.get(id);
82    }
83}
84