1 /*
2  * Copyright (C) 2008 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 #ifndef MEDIASCANNER_H
18 #define MEDIASCANNER_H
19 
20 #include <utils/Log.h>
21 #include <utils/threads.h>
22 #include <utils/List.h>
23 #include <utils/Errors.h>
24 #include <utils/String8.h>
25 #include <pthread.h>
26 
27 struct dirent;
28 
29 namespace android {
30 
31 class MediaScannerClient;
32 class StringArray;
33 class CharacterEncodingDetector;
34 
35 enum MediaScanResult {
36     // This file or directory was scanned successfully.
37     MEDIA_SCAN_RESULT_OK,
38     // This file or directory was skipped because it was not found, could
39     // not be opened, was of an unsupported type, or was malfored in some way.
40     MEDIA_SCAN_RESULT_SKIPPED,
41     // The scan should be aborted due to a fatal error such as out of memory
42     // or an exception.
43     MEDIA_SCAN_RESULT_ERROR,
44 };
45 
46 struct MediaAlbumArt {
47 public:
48     static MediaAlbumArt *fromData(int32_t size, const void* data);
49 
50     static void init(MediaAlbumArt* instance, int32_t size, const void* data);
51 
52     MediaAlbumArt *clone();
53 
dataMediaAlbumArt54     const char *data() {
55         return &mData[0];
56     }
57 
sizeMediaAlbumArt58     int32_t size() {
59         return mSize;
60     }
61 
62 private:
63     int32_t mSize;
64     char mData[];
65 
66     // You can't construct instances of this class directly because this is a
67     // variable-sized object passed through the binder.
68     MediaAlbumArt();
69 } __packed;
70 
71 struct MediaScanner {
72     MediaScanner();
73     virtual ~MediaScanner();
74 
75     virtual MediaScanResult processFile(
76             const char *path, const char *mimeType, MediaScannerClient &client) = 0;
77 
78     virtual MediaScanResult processDirectory(
79             const char *path, MediaScannerClient &client);
80 
81     void setLocale(const char *locale);
82 
83     virtual MediaAlbumArt *extractAlbumArt(int fd) = 0;
84 
85 protected:
86     const char *locale() const;
87 
88 private:
89     // current locale (like "ja_JP"), created/destroyed with strdup()/free()
90     char *mLocale;
91     char *mSkipList;
92     int *mSkipIndex;
93 
94     MediaScanResult doProcessDirectory(
95             char *path, int pathRemaining, MediaScannerClient &client, bool noMedia);
96     MediaScanResult doProcessDirectoryEntry(
97             char *path, int pathRemaining, MediaScannerClient &client, bool noMedia,
98             struct dirent* entry, char* fileSpot);
99     void loadSkipList();
100     bool shouldSkipDirectory(char *path);
101 
102 
103     MediaScanner(const MediaScanner &);
104     MediaScanner &operator=(const MediaScanner &);
105 };
106 
107 class MediaScannerClient
108 {
109 public:
110     MediaScannerClient();
111     virtual ~MediaScannerClient();
112     void setLocale(const char* locale);
113     void beginFile();
114     status_t addStringTag(const char* name, const char* value);
115     void endFile();
116 
117     virtual status_t scanFile(const char* path, long long lastModified,
118             long long fileSize, bool isDirectory, bool noMedia) = 0;
119     virtual status_t handleStringTag(const char* name, const char* value) = 0;
120     virtual status_t setMimeType(const char* mimeType) = 0;
121 
122 protected:
123     // default encoding from MediaScanner::mLocale
124     String8 mLocale;
125 };
126 
127 }; // namespace android
128 
129 #endif // MEDIASCANNER_H
130