• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 package com.android.gallery3d.data;
18 
19 import java.util.ArrayList;
20 
21 // FilterDeleteSet filters a base MediaSet to remove some deletion items (we
22 // expect the number to be small). The user can use the following methods to
23 // add/remove deletion items:
24 //
25 // void addDeletion(Path path, int index);
26 // void removeDelection(Path path);
27 // void clearDeletion();
28 // int getNumberOfDeletions();
29 //
30 public class FilterDeleteSet extends MediaSet implements ContentListener {
31     private static final String TAG = "FilterDeleteSet";
32 
33     private static final int REQUEST_ADD = 1;
34     private static final int REQUEST_REMOVE = 2;
35     private static final int REQUEST_CLEAR = 3;
36 
37     private static class Request {
38         int type;  // one of the REQUEST_* constants
39         Path path;
40         int indexHint;
Request(int type, Path path, int indexHint)41         public Request(int type, Path path, int indexHint) {
42             this.type = type;
43             this.path = path;
44             this.indexHint = indexHint;
45         }
46     }
47 
48     private static class Deletion {
49         Path path;
50         int index;
Deletion(Path path, int index)51         public Deletion(Path path, int index) {
52             this.path = path;
53             this.index = index;
54         }
55     }
56 
57     // The underlying MediaSet
58     private final MediaSet mBaseSet;
59 
60     // Pending Requests
61     private ArrayList<Request> mRequests = new ArrayList<Request>();
62 
63     // Deletions currently in effect, ordered by index
64     private ArrayList<Deletion> mCurrent = new ArrayList<Deletion>();
65     private int mMediaItemCount;
66 
FilterDeleteSet(Path path, MediaSet baseSet)67     public FilterDeleteSet(Path path, MediaSet baseSet) {
68         super(path, INVALID_DATA_VERSION);
69         mBaseSet = baseSet;
70         mBaseSet.addContentListener(this);
71     }
72 
73     @Override
getName()74     public String getName() {
75         return mBaseSet.getName();
76     }
77 
78     @Override
getMediaItemCount()79     public int getMediaItemCount() {
80         return mMediaItemCount;
81     }
82 
83     // Gets the MediaItems whose (post-deletion) index are in the range [start,
84     // start + count). Because we remove some of the MediaItems, the index need
85     // to be adjusted.
86     //
87     // For example, if there are 12 items in total. The deleted items are 3, 5,
88     // 10, and the the requested range is [3, 7]:
89     //
90     // The original index:   0 1 2 3 4 5 6 7 8 9 A B C
91     // The deleted items:          X   X         X
92     // The new index:        0 1 2   3   4 5 6 7   8 9
93     // Requested:                    *   * * * *
94     //
95     // We need to figure out the [3, 7] actually maps to the original index 4,
96     // 6, 7, 8, 9.
97     //
98     // We can break the MediaItems into segments, each segment other than the
99     // last one ends in a deleted item. The difference between the new index and
100     // the original index increases with each segment:
101     //
102     // 0 1 2 X     (new index = old index)
103     // 4 X         (new index = old index - 1)
104     // 6 7 8 9 X   (new index = old index - 2)
105     // B C         (new index = old index - 3)
106     //
107     @Override
getMediaItem(int start, int count)108     public ArrayList<MediaItem> getMediaItem(int start, int count) {
109         if (count <= 0) return new ArrayList<MediaItem>();
110 
111         int end = start + count - 1;
112         int n = mCurrent.size();
113         // Find the segment that "start" falls into. Count the number of items
114         // not yet deleted until it reaches "start".
115         int i = 0;
116         for (i = 0; i < n; i++) {
117             Deletion d = mCurrent.get(i);
118             if (d.index - i > start) break;
119         }
120         // Find the segment that "end" falls into.
121         int j = i;
122         for (; j < n; j++) {
123             Deletion d = mCurrent.get(j);
124             if (d.index - j > end) break;
125         }
126 
127         // Now get enough to cover deleted items in [start, end]
128         ArrayList<MediaItem> base = mBaseSet.getMediaItem(start + i, count + (j - i));
129 
130         // Remove the deleted items.
131         for (int m = j - 1; m >= i; m--) {
132             Deletion d = mCurrent.get(m);
133             int k = d.index - (start + i);
134             base.remove(k);
135         }
136         return base;
137     }
138 
139     // We apply the pending requests in the mRequests to construct mCurrent in reload().
140     @Override
reload()141     public long reload() {
142         boolean newData = mBaseSet.reload() > mDataVersion;
143         synchronized (mRequests) {
144             if (!newData && mRequests.isEmpty()) {
145                 return mDataVersion;
146             }
147             for (int i = 0; i < mRequests.size(); i++) {
148                 Request r = mRequests.get(i);
149                 switch (r.type) {
150                     case REQUEST_ADD: {
151                         // Add the path into mCurrent if there is no duplicate.
152                         int n = mCurrent.size();
153                         int j;
154                         for (j = 0; j < n; j++) {
155                             if (mCurrent.get(j).path == r.path) break;
156                         }
157                         if (j == n) {
158                             mCurrent.add(new Deletion(r.path, r.indexHint));
159                         }
160                         break;
161                     }
162                     case REQUEST_REMOVE: {
163                         // Remove the path from mCurrent.
164                         int n = mCurrent.size();
165                         for (int j = 0; j < n; j++) {
166                             if (mCurrent.get(j).path == r.path) {
167                                 mCurrent.remove(j);
168                                 break;
169                             }
170                         }
171                         break;
172                     }
173                     case REQUEST_CLEAR: {
174                         mCurrent.clear();
175                         break;
176                     }
177                 }
178             }
179             mRequests.clear();
180         }
181 
182         if (!mCurrent.isEmpty()) {
183             // See if the elements in mCurrent can be found in the MediaSet. We
184             // don't want to search the whole mBaseSet, so we just search a
185             // small window that contains the index hints (plus some margin).
186             int minIndex = mCurrent.get(0).index;
187             int maxIndex = minIndex;
188             for (int i = 1; i < mCurrent.size(); i++) {
189                 Deletion d = mCurrent.get(i);
190                 minIndex = Math.min(d.index, minIndex);
191                 maxIndex = Math.max(d.index, maxIndex);
192             }
193 
194             int n = mBaseSet.getMediaItemCount();
195             int from = Math.max(minIndex - 5, 0);
196             int to = Math.min(maxIndex + 5, n);
197             ArrayList<MediaItem> items = mBaseSet.getMediaItem(from, to - from);
198             ArrayList<Deletion> result = new ArrayList<Deletion>();
199             for (int i = 0; i < items.size(); i++) {
200                 MediaItem item = items.get(i);
201                 if (item == null) continue;
202                 Path p = item.getPath();
203                 // Find the matching path in mCurrent, if found move it to result
204                 for (int j = 0; j < mCurrent.size(); j++) {
205                     Deletion d = mCurrent.get(j);
206                     if (d.path == p) {
207                         d.index = from + i;
208                         result.add(d);
209                         mCurrent.remove(j);
210                         break;
211                     }
212                 }
213             }
214             mCurrent = result;
215         }
216 
217         mMediaItemCount = mBaseSet.getMediaItemCount() - mCurrent.size();
218         mDataVersion = nextVersionNumber();
219         return mDataVersion;
220     }
221 
sendRequest(int type, Path path, int indexHint)222     private void sendRequest(int type, Path path, int indexHint) {
223         Request r = new Request(type, path, indexHint);
224         synchronized (mRequests) {
225             mRequests.add(r);
226         }
227         notifyContentChanged();
228     }
229 
230     @Override
onContentDirty()231     public void onContentDirty() {
232         notifyContentChanged();
233     }
234 
addDeletion(Path path, int indexHint)235     public void addDeletion(Path path, int indexHint) {
236         sendRequest(REQUEST_ADD, path, indexHint);
237     }
238 
removeDeletion(Path path)239     public void removeDeletion(Path path) {
240         sendRequest(REQUEST_REMOVE, path, 0 /* unused */);
241     }
242 
clearDeletion()243     public void clearDeletion() {
244         sendRequest(REQUEST_CLEAR, null /* unused */ , 0 /* unused */);
245     }
246 
247     // Returns number of deletions _in effect_ (the number will only gets
248     // updated after a reload()).
getNumberOfDeletions()249     public int getNumberOfDeletions() {
250         return mCurrent.size();
251     }
252 }
253