55import android .content .ContentResolver ;
66import android .content .Context ;
77import android .database .Cursor ;
8+ import android .net .Uri ;
9+ import android .os .Build ;
10+ import android .os .Bundle ;
811import android .provider .MediaStore ;
912import android .text .TextUtils ;
1013
14+ import androidx .annotation .Nullable ;
15+ import androidx .core .content .ContentResolverCompat ;
1116import androidx .lifecycle .LiveData ;
1217import androidx .lifecycle .MutableLiveData ;
1318
3136import ir .smartdevelopers .smartfilebrowser .models .GalleryModel ;
3237
3338public class Repository {
34- private ExecutorService mExecutorService ;
35- private ContentResolver mContentResolver ;
36- private WeakReference <Context > wContext ;
39+ private final ExecutorService mExecutorService ;
40+ private final ContentResolver mContentResolver ;
41+ private final WeakReference <Context > wContext ;
3742 private MutableLiveData <List <GalleryModel >> galleryList ;
3843 public Repository (Application application ) {
3944 mExecutorService = Executors .newCachedThreadPool ();
@@ -165,7 +170,7 @@ private List<GalleryModel> getGalleryModel(Cursor cursor,String[] projection){
165170
166171 /**
167172 * @param modelType is one of {@link FileBrowserModel} model types
168- * @param filesLiveData */
173+ **/
169174 public void getFirstBrowserPageList (String selection ,String [] selectionArgs , int modelType ,
170175 FileFilter fileFilter ,
171176 MutableLiveData <List <FileBrowserModel >> filesLiveData ){
@@ -184,8 +189,27 @@ public void getFirstBrowserPageList(String selection,String[] selectionArgs, int
184189 MediaStore .Files .FileColumns .DATA ,
185190 MediaStore .Files .FileColumns .MIME_TYPE };
186191
187- Cursor cursor =mContentResolver .query (MediaStore .Files .getContentUri ("external" ),projection , finalSelection ,
188- selectionArgs , MediaStore .MediaColumns .DATE_ADDED +" DESC LIMIT 30" );
192+
193+ Cursor cursor =getCursor (MediaStore .Files .getContentUri ("external" ),
194+ projection ,finalSelection ,selectionArgs ,
195+ MediaStore .MediaColumns .DATE_ADDED +" DESC" ,30 );
196+ // if (Build.VERSION.SDK_INT>=26){
197+ // Bundle args=new Bundle();
198+ //// args.putInt(ContentResolver.QUERY_ARG_LIMIT,30);
199+ //// args.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION,ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
200+ //// args.putString(ContentResolver.QUERY_ARG_SORT_COLUMNS,MediaStore.MediaColumns.DATE_ADDED);
201+ //// args.putString(ContentResolver.Que,MediaStore.MediaColumns.DATE_ADDED);
202+ // args.putString(ContentResolver.QUERY_ARG_SQL_SELECTION,finalSelection);
203+ // args.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,selectionArgs);
204+ // cursor=mContentResolver.query(MediaStore.Files.getContentUri("external"),
205+ // projection,args,null);
206+ // }else {
207+ // cursor =mContentResolver.query(MediaStore.Files.getContentUri("external"),
208+ // projection, finalSelection,
209+ // selectionArgs,
210+ // MediaStore.MediaColumns.DATE_ADDED+" DESC LIMIT 30");
211+ // }
212+
189213 int idIndex =cursor .getColumnIndex (projection [0 ]);
190214 int pathIndex =cursor .getColumnIndex (projection [1 ]);
191215// int dateIndex=cursor.getColumnIndex(projection[2]);
@@ -221,4 +245,30 @@ public void getFirstBrowserPageList(String selection,String[] selectionArgs, int
221245
222246
223247 }
248+
249+ private Cursor getCursor (Uri uri ,String [] projection ,String selection ,
250+ String [] selectionArgs ,String sortOrder ,@ Nullable Integer limit ){
251+ Cursor cursor ;
252+ if (Build .VERSION .SDK_INT >=26 ){
253+ Bundle args =new Bundle ();
254+ args .putString (ContentResolver .QUERY_ARG_SQL_SELECTION ,selection );
255+ args .putStringArray (ContentResolver .QUERY_ARG_SQL_SELECTION_ARGS ,selectionArgs );
256+ args .putString (ContentResolver .QUERY_ARG_SQL_SORT_ORDER ,sortOrder );
257+ if (limit !=null ){
258+ args .putInt (ContentResolver .QUERY_ARG_LIMIT ,limit );
259+ }
260+ cursor =mContentResolver .query (uri ,
261+ projection ,args ,null );
262+ }else {
263+ String sOrder =sortOrder ;
264+ if (limit != null ) {
265+ sOrder =sortOrder +" LIMIT " +limit ;
266+ }
267+ cursor =mContentResolver .query (uri ,
268+ projection , selection ,
269+ selectionArgs ,
270+ sOrder );
271+ }
272+ return cursor ;
273+ }
224274}
0 commit comments