-
Notifications
You must be signed in to change notification settings - Fork 49
/
Media.java
370 lines (317 loc) · 10.4 KB
/
Media.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package info.guardianproject.mrapp.model;
import java.util.ArrayList;
import info.guardianproject.mrapp.db.ProjectsProvider;
import info.guardianproject.mrapp.db.StoryMakerDB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class Media {
private static final String TAG = "Media";
protected Context context;
protected int id;
protected String path;
protected String mimeType;
protected String clipType; // R.arrays.cliptypes
protected int clipIndex; // which clip is this in the scene
protected int sceneId; // foreign key to the Scene which holds this media
protected int trimStart;
protected int trimEnd;
protected int duration;
public Media(Context context) {
this.context = context;
}
public Media(Context context, int id, String path, String mimeType, String clipType, int clipIndex,
int sceneId, int trimStart, int trimEnd, int duration) {
super();
this.context = context;
this.id = id;
this.path = path;
this.mimeType = mimeType;
this.clipType = clipType;
this.clipIndex = clipIndex;
this.sceneId = sceneId;
this.trimStart = trimStart;
this.trimEnd = trimEnd;
this.duration = duration;
}
public Media(Context context, Cursor cursor) {
// FIXME use column id's directly to optimize this one schema stabilizes
this(
context,
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.ID)),
cursor.getString(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_PATH)),
cursor.getString(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_MIME_TYPE)),
cursor.getString(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_CLIP_TYPE)),
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_CLIP_INDEX)),
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_SCENE_ID)),
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_TRIM_START)),
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_TRIM_END)),
cursor.getInt(cursor
.getColumnIndex(StoryMakerDB.Schema.Media.COL_DURATION)));
}
/***** Table level static methods *****/
public static Cursor getAsCursor(Context context, int id) {
String selection = StoryMakerDB.Schema.Media.ID + "=?";
String[] selectionArgs = new String[] { "" + id };
return context.getContentResolver().query(
ProjectsProvider.MEDIA_CONTENT_URI, null, selection,
selectionArgs, null);
}
public static Media get(Context context, int id) {
Cursor cursor = Media.getAsCursor(context, id);
if (cursor.moveToFirst()) {
return new Media(context, cursor);
} else {
return null;
}
}
/***** Calculated object level methods *****/
/**
* @return 0.0-1.0 percent into the clip to start play
*/
public float getTrimmedStartPercent() {
return (trimStart + 1) / 100F;
}
/**
* @return 0.0-1.0 percent into the clip to end play
*/
public float getTrimmedEndPercent() {
return (trimEnd + 1) / 100F;
}
/**
* @return milliseconds into clip trimmed clip to start playback
*/
public int getTrimmedStartTime() {
return Math.round(getTrimmedStartPercent() * duration);
}
/**
* @return milliseconds to end of trimmed clip
*/
public int getTrimmedEndTime() {
return Math.round(getTrimmedEndPercent() * duration);
}
/**
* @return milliseconds trimmed clip will last
*/
public int getTrimmedDuration() {
return getTrimmedEndTime() - getTrimmedStartTime();
}
/***** Object level methods *****/
/*
* gets media in scene at location clipIndex
*/
public static Cursor getAsCursor(Context context, int sceneId, int clipIndex) {
String selection = StoryMakerDB.Schema.Media.COL_SCENE_ID + "=? and " +
StoryMakerDB.Schema.Media.COL_CLIP_INDEX + "=?";
String[] selectionArgs = new String[] { "" + sceneId, "" + clipIndex };
return context.getContentResolver().query(
ProjectsProvider.MEDIA_CONTENT_URI, null, selection,
selectionArgs, null);
}
/*
* gets media in scene at location clipIndex
*/
public static Media get(Context context, int sceneId, int clipIndex) {
Cursor cursor = Media.getAsCursor(context, sceneId, clipIndex);
if (cursor.moveToFirst()) {
return new Media(context, cursor);
} else {
return null;
}
}
public static Cursor getAllAsCursor(Context context) {
return context.getContentResolver().query(
ProjectsProvider.MEDIA_CONTENT_URI, null, null, null, null);
}
public static ArrayList<Media> getAllAsList(Context context) {
ArrayList<Media> medias = new ArrayList<Media>();
Cursor cursor = getAllAsCursor(context);
if (cursor.moveToFirst()) {
do {
medias.add(new Media(context, cursor));
} while (cursor.moveToNext());
}
return medias;
}
/***** Object level methods *****/
public void save() {
Cursor cursor = getAsCursor(context, id);
if (cursor.getCount() == 0) {
cursor.close();
insert();
} else {
cursor.close();
update();
}
}
private ContentValues getValues() {
ContentValues values = new ContentValues();
values.put(StoryMakerDB.Schema.Media.COL_PATH, path);
values.put(StoryMakerDB.Schema.Media.COL_MIME_TYPE, mimeType);
values.put(StoryMakerDB.Schema.Media.COL_CLIP_TYPE, clipType);
values.put(StoryMakerDB.Schema.Media.COL_CLIP_INDEX, clipIndex);
values.put(StoryMakerDB.Schema.Media.COL_SCENE_ID, sceneId);
values.put(StoryMakerDB.Schema.Media.COL_TRIM_START, trimStart);
values.put(StoryMakerDB.Schema.Media.COL_TRIM_END, trimEnd);
values.put(StoryMakerDB.Schema.Media.COL_DURATION, duration);
return values;
}
private void insert() {
// There can be only one! check if a media item exists at this location already, if so purge it first.
Cursor cursorDupes = getAsCursor(context, sceneId, clipIndex);
if ((cursorDupes.getCount() > 0) && cursorDupes.moveToFirst()) {
// FIXME we should allow audio clips to remain so they can be mixed down with their buddies
do {
(new Media(context, cursorDupes)).delete();
} while (cursorDupes.moveToNext());
}
ContentValues values = getValues();
Uri uri = context.getContentResolver().insert(
ProjectsProvider.MEDIA_CONTENT_URI, values);
String lastSegment = uri.getLastPathSegment();
int newId = Integer.parseInt(lastSegment);
this.setId(newId);
cursorDupes.close();
}
private void update() {
Uri uri = ProjectsProvider.MEDIA_CONTENT_URI.buildUpon().appendPath("" + id).build();
String selection = StoryMakerDB.Schema.Media.ID + "=?";
String[] selectionArgs = new String[] { "" + id };
ContentValues values = getValues();
int count = context.getContentResolver().update(
uri, values, selection, selectionArgs);
// FIXME make sure 1 row updated
}
private void delete() {
Uri uri = ProjectsProvider.MEDIA_CONTENT_URI.buildUpon().appendPath("" + id).build();
String selection = StoryMakerDB.Schema.Media.ID + "=?";
String[] selectionArgs = new String[] { "" + id };
int count = context.getContentResolver().delete(
uri, selection, selectionArgs);
Log.d(TAG, "deleted media: " + id + ", rows deleted: " + count);
// FIXME make sure 1 row updated
}
/***** getters and setters *****/
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the path
*/
public String getPath() {
return path;
}
/**
* @param path
* the path to set
*/
public void setPath(String path) {
this.path = path;
}
/**
* @return the mimeType
*/
public String getMimeType() {
return mimeType;
}
/**
* @param mimeType
* the mimeType to set
*/
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
/**
* @return the sceneId
*/
public int getSceneId() {
return sceneId;
}
/**
* @param sceneId
* the sceneId to set
*/
public void setSceneId(int sceneId) {
this.sceneId = sceneId;
}
/**
* @return the clipType
*/
public String getClipType() {
return clipType;
}
/**
* @param clipType the clipType to set
*/
public void setClipType(String clipType) {
this.clipType = clipType;
}
/**
* @return the clipIndex
*/
public int getClipIndex() {
return clipIndex;
}
/**
* @param clipIndex the clipIndex to set
*/
public void setClipIndex(int clipIndex) {
this.clipIndex = clipIndex;
}
/**
* @return the trimStart
*/
public int getTrimStart() {
return trimStart;
}
/**
* @param trimStart the trimStart to set
*/
public void setTrimStart(int trimStart) {
this.trimStart = trimStart;
}
/**
* @return the trimEnd
*/
public int getTrimEnd() {
return trimEnd;
}
/**
* @param trimEnd the trimEnd to set
*/
public void setTrimEnd(int trimEnd) {
this.trimEnd = trimEnd;
}
/**
* @return the duration
*/
public int getDuration() {
return duration;
}
/**
* @param duration the duration to set
*/
public void setDuration(int duration) {
this.duration = duration;
}
}