25
25
import android .database .Cursor ;
26
26
import android .database .DatabaseUtils ;
27
27
import android .net .Uri ;
28
+ import android .os .Build ;
29
+ import android .os .CancellationSignal ;
28
30
import android .util .Log ;
29
31
30
32
import java .util .ArrayList ;
39
41
* Static Implementation of Context.getContentResolver()-ContentProvider based media api
40
42
*/
41
43
public class ContentProviderMediaImpl {
44
+ private static final String MODUL_NAME = ContentProviderMediaImpl .class .getName ();
45
+
42
46
public static Cursor createCursorForQuery (
43
47
StringBuilder out_debugMessage , String dbgContext , final Context context ,
44
- QueryParameter parameters , VISIBILITY visibility ) {
48
+ QueryParameter parameters , VISIBILITY visibility , CancellationSignal cancellationSignal ) {
45
49
if (visibility != null ) FotoSql .setWhereVisibility (parameters , visibility );
46
50
return createCursorForQuery (out_debugMessage , dbgContext , context , parameters .toFrom (),
47
51
parameters .toAndroidWhere (),
48
52
parameters .toAndroidParameters (), parameters .toOrderBy (),
49
- parameters .toColumns ()
53
+ cancellationSignal , parameters .toColumns ()
50
54
);
51
55
}
52
56
53
57
/**
54
58
* every cursor query should go through this. adds logging if enabled
55
59
*/
56
- static Cursor createCursorForQuery (StringBuilder out_debugMessage , String dbgContext , final Context context , final String from , final String sqlWhereStatement ,
57
- final String [] sqlWhereParameters , final String sqlSortOrder ,
58
- final String ... sqlSelectColums ) {
60
+ static Cursor createCursorForQuery (
61
+ StringBuilder out_debugMessage , String dbgContext , final Context context ,
62
+ final String from , final String sqlWhereStatement ,
63
+ final String [] sqlWhereParameters , final String sqlSortOrder ,
64
+ CancellationSignal cancellationSignal , final String ... sqlSelectColums ) {
59
65
ContentResolver resolver = context .getContentResolver ();
60
66
Cursor query = null ;
61
67
62
68
Exception excpetion = null ;
63
69
try {
64
- query = resolver .query (Uri .parse (from ), sqlSelectColums , sqlWhereStatement , sqlWhereParameters , sqlSortOrder );
70
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
71
+ query = resolver .query (Uri .parse (from ), sqlSelectColums , sqlWhereStatement , sqlWhereParameters , sqlSortOrder , cancellationSignal );
72
+ } else {
73
+ query = resolver .query (Uri .parse (from ), sqlSelectColums , sqlWhereStatement , sqlWhereParameters , sqlSortOrder );
74
+ }
65
75
} catch (Exception ex ) {
66
76
excpetion = ex ;
67
77
} finally {
68
78
if ((excpetion != null ) || Global .debugEnabledSql || (out_debugMessage != null )) {
69
79
StringBuilder message = StringUtils .appendMessage (out_debugMessage , excpetion ,
70
- dbgContext , "FotoSql.createCursorForQuery:\n " ,
80
+ dbgContext , MODUL_NAME +
81
+ ".createCursorForQuery:\n " ,
71
82
QueryParameter .toString (sqlSelectColums , null , from , sqlWhereStatement ,
72
83
sqlWhereParameters , sqlSortOrder , query .getCount ()));
73
84
if (out_debugMessage == null ) {
@@ -79,10 +90,6 @@ static Cursor createCursorForQuery(StringBuilder out_debugMessage, String dbgCon
79
90
return query ;
80
91
}
81
92
82
- public static int execUpdate (String dbgContext , Context context , long id , ContentValues values ) {
83
- return exexUpdateImpl (dbgContext , context , values , FotoSql .FILTER_COL_PK , new String []{Long .toString (id )});
84
- }
85
-
86
93
public static int execUpdate (String dbgContext , Context context , String path , ContentValues values , VISIBILITY visibility ) {
87
94
return exexUpdateImpl (dbgContext , context , values , FotoSql .getFilterExprPathLikeWithVisibility (visibility ), new String []{path });
88
95
}
@@ -101,7 +108,9 @@ public static int exexUpdateImpl(String dbgContext, Context context, ContentValu
101
108
excpetion = ex ;
102
109
} finally {
103
110
if ((excpetion != null ) || ((dbgContext != null ) && (Global .debugEnabledSql || LibGlobal .debugEnabledJpg ))) {
104
- Log .i (Global .LOG_CONTEXT , dbgContext + ":FotoSql.exexUpdate " + excpetion + "\n " +
111
+ Log .i (Global .LOG_CONTEXT , dbgContext + ":" +
112
+ MODUL_NAME +
113
+ ".exexUpdate " + excpetion + "\n " +
105
114
QueryParameter .toString (null , values .toString (), FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME ,
106
115
sqlWhere , selectionArgs , null , result ), excpetion );
107
116
}
@@ -146,7 +155,9 @@ public static Uri execInsert(String dbgContext, Context context, ContentValues v
146
155
excpetion = ex ;
147
156
} finally {
148
157
if ((excpetion != null ) || Global .debugEnabledSql || LibGlobal .debugEnabledJpg ) {
149
- Log .i (Global .LOG_CONTEXT , dbgContext + ":FotoSql.execInsert " + excpetion + " " +
158
+ Log .i (Global .LOG_CONTEXT , dbgContext + ":" +
159
+ MODUL_NAME +
160
+ ".execInsert " + excpetion + " " +
150
161
values .toString () + " => " + result + " " + excpetion , excpetion );
151
162
}
152
163
}
@@ -166,31 +177,39 @@ public static int deleteMedia(String dbgContext, Context context, String where,
166
177
ContentValues values = new ContentValues ();
167
178
values .put (FotoSql .SQL_COL_PATH , FotoSql .DELETED_FILE_MARKER );
168
179
values .put (FotoSql .SQL_COL_EXT_MEDIA_TYPE , 0 ); // so it will not be shown as image any more
169
- exexUpdateImpl (dbgContext + "-a: FotoSql.deleteMedia: " ,
180
+ exexUpdateImpl (dbgContext + "-a: " +
181
+ MODUL_NAME +
182
+ ".deleteMedia: " ,
170
183
context , values , lastUsedWhereClause , lastSelectionArgs );
171
184
172
185
lastUsedWhereClause = FotoSql .SQL_COL_PATH + " is null" ;
173
186
lastSelectionArgs = null ;
174
187
delCount = context .getContentResolver ()
175
188
.delete (FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE , lastUsedWhereClause , lastSelectionArgs );
176
189
if (Global .debugEnabledSql || LibGlobal .debugEnabledJpg ) {
177
- Log .i (Global .LOG_CONTEXT , dbgContext + "-b: FotoSql.deleteMedia delete\n " +
190
+ Log .i (Global .LOG_CONTEXT , dbgContext + "-b: " +
191
+ MODUL_NAME +
192
+ ".deleteMedia delete\n " +
178
193
QueryParameter .toString (null , null , FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME ,
179
194
lastUsedWhereClause , lastSelectionArgs , null , delCount ));
180
195
}
181
196
} else {
182
197
delCount = context .getContentResolver ()
183
198
.delete (FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE , lastUsedWhereClause , lastSelectionArgs );
184
199
if (Global .debugEnabledSql || LibGlobal .debugEnabledJpg ) {
185
- Log .i (Global .LOG_CONTEXT , dbgContext + ": FotoSql.deleteMedia\n delete " +
200
+ Log .i (Global .LOG_CONTEXT , dbgContext + ": " +
201
+ MODUL_NAME +
202
+ ".deleteMedia\n delete " +
186
203
QueryParameter .toString (null , null ,
187
204
FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME ,
188
205
lastUsedWhereClause , lastSelectionArgs , null , delCount ));
189
206
}
190
207
}
191
208
} catch (Exception ex ) {
192
209
// null pointer exception when delete matches not items??
193
- final String msg = dbgContext + ": Exception in FotoSql.deleteMedia:\n " +
210
+ final String msg = dbgContext + ": Exception in " +
211
+ MODUL_NAME +
212
+ ".deleteMedia:\n " +
194
213
QueryParameter .toString (null , null , FotoSqlBase .SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME ,
195
214
lastUsedWhereClause , lastSelectionArgs , null , -1 )
196
215
+ " : " + ex .getMessage ();
@@ -207,7 +226,8 @@ public static int deleteMedia(String dbgContext, Context context, String where,
207
226
* @return number of updated items
208
227
*/
209
228
private static int _del_execRenameFolder_batch_not_working (Context context , String pathOld , String pathNew ) {
210
- final String dbgContext = "FotoSql.execRenameFolder('" +
229
+ final String dbgContext = MODUL_NAME +
230
+ ".execRenameFolder('" +
211
231
pathOld + "' => '" + pathNew + "')" ;
212
232
// sql update file set path = newBegin + substing(path, begin+len) where path like newBegin+'%'
213
233
// public static final String SQL_EXPR_FOLDER = "substr(" + SQL_COL_PATH + ",1,length(" + SQL_COL_PATH + ") - length(" + MediaStore.Images.Media.DISPLAY_NAME + "))";
@@ -229,7 +249,7 @@ private static int _del_execRenameFolder_batch_not_working(Context context, Stri
229
249
230
250
Cursor c = null ;
231
251
try {
232
- c = createCursorForQuery (null , dbgContext , context , queryAffectedFiles , null );
252
+ c = createCursorForQuery (null , dbgContext , context , queryAffectedFiles , null , null );
233
253
int pkColNo = c .getColumnIndex (FotoSql .SQL_COL_PK );
234
254
int pathColNo = c .getColumnIndex (sqlColNewPathAlias );
235
255
@@ -271,7 +291,8 @@ public static ContentValues getDbContent(Context context, final long id) {
271
291
return values ;
272
292
}
273
293
} catch (Exception ex ) {
274
- Log .e (Global .LOG_CONTEXT , "FotoSql.getDbContent(id=" + id + ") failed" , ex );
294
+ Log .e (Global .LOG_CONTEXT , MODUL_NAME +
295
+ ".getDbContent(id=" + id + ") failed" , ex );
275
296
} finally {
276
297
if (c != null ) c .close ();
277
298
}
0 commit comments