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
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.photos.data;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.android.photos.data.PhotoProvider.Albums;
import com.android.photos.data.PhotoProvider.Metadata;
import com.android.photos.data.PhotoProvider.Photos;
import junit.framework.AssertionFailedError;
public class PhotoDatabaseUtils {
public static String[] PROJECTION_ALBUMS = {
Albums._ID,
Albums.PARENT_ID,
Albums.VISIBILITY,
Albums.NAME,
Albums.SERVER_ID,
};
public static String[] PROJECTION_METADATA = {
Metadata.PHOTO_ID,
Metadata.KEY,
Metadata.VALUE,
};
public static String[] PROJECTION_PHOTOS = {
Photos._ID,
Photos.SERVER_ID,
Photos.WIDTH,
Photos.HEIGHT,
Photos.DATE_TAKEN,
Photos.ALBUM_ID,
Photos.MIME_TYPE,
};
private static String SELECTION_ALBUM_SERVER_ID = Albums.SERVER_ID + " = ?";
private static String SELECTION_PHOTO_SERVER_ID = Photos.SERVER_ID + " = ?";
public static long queryAlbumIdFromServerId(SQLiteDatabase db, long serverId) {
return queryId(db, Albums.TABLE, PROJECTION_ALBUMS, SELECTION_ALBUM_SERVER_ID, serverId);
}
public static long queryPhotoIdFromServerId(SQLiteDatabase db, long serverId) {
return queryId(db, Photos.TABLE, PROJECTION_PHOTOS, SELECTION_PHOTO_SERVER_ID, serverId);
}
public static long queryId(SQLiteDatabase db, String table, String[] projection,
String selection, Object parameter) {
String paramString = parameter == null ? null : parameter.toString();
String[] selectionArgs = {
paramString,
};
Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
try {
if (cursor.getCount() != 1 || !cursor.moveToNext()) {
throw new AssertionFailedError("Couldn't find item in table");
}
long id = cursor.getLong(0);
return id;
} finally {
cursor.close();
}
}
public static boolean insertPhoto(SQLiteDatabase db, Long serverId, Integer width,
Integer height, Long dateTaken, Long albumId, String mimeType) {
ContentValues values = new ContentValues();
values.put(Photos.SERVER_ID, serverId);
values.put(Photos.WIDTH, width);
values.put(Photos.HEIGHT, height);
values.put(Photos.DATE_TAKEN, dateTaken);
values.put(Photos.ALBUM_ID, albumId);
values.put(Photos.MIME_TYPE, mimeType);
return db.insert(Photos.TABLE, null, values) != -1;
}
public static boolean insertAlbum(SQLiteDatabase db, Long parentId, String name,
Integer privacy, Long serverId) {
ContentValues values = new ContentValues();
values.put(Albums.PARENT_ID, parentId);
values.put(Albums.NAME, name);
values.put(Albums.VISIBILITY, privacy);
values.put(Albums.SERVER_ID, serverId);
return db.insert(Albums.TABLE, null, values) != -1;
}
public static boolean insertMetadata(SQLiteDatabase db, Long photosId, String key, String value) {
ContentValues values = new ContentValues();
values.put(Metadata.PHOTO_ID, photosId);
values.put(Metadata.KEY, key);
values.put(Metadata.VALUE, value);
return db.insert(Metadata.TABLE, null, values) != -1;
}
public static void deleteAllContent(SQLiteDatabase db) {
db.delete(Metadata.TABLE, null, null);
db.delete(Photos.TABLE, null, null);
db.delete(Albums.TABLE, null, null);
}
}
|