/** * todo: * * @param newCursor * @param oldCursor * @return */ @NonNull @SuppressWarnings("ConstantConditions") public Result processCursors(@Nullable Cursor newCursor, @Nullable Cursor oldCursor) { if (newCursor == null && oldCursor == null) return new Result(); Result result; if ((result = processCursorsTotalChange(oldCursor, newCursor)) != null) { return result; } final CursorJoiner joiner = new CursorJoiner( oldCursor, COMPARABLE_COLUMN_NAMES, newCursor, COMPARABLE_COLUMN_NAMES ); mInsertHandler.clear(); mRemoveHandler.clear(); result = new Result(); DataSetChange[] dataSetChanges; for (CursorJoiner.Result joinerResult : joiner) { switch (joinerResult) { // Row has been removed. case LEFT: this.resolvePendingChange(mInsertHandler, result); dataSetChanges = mRemoveHandler.handleChange(oldCursor.getPosition()); if (dataSetChanges != null) { if (dataSetChanges.length == 1) { result.addDataSetChange(dataSetChanges[0]); } else { result.addDataSetChange(dataSetChanges[1]); } } break; // Row has been inserted. case RIGHT: this.resolvePendingChange(mRemoveHandler, result); dataSetChanges = mInsertHandler.handleChange(newCursor.getPosition()); if (dataSetChanges != null) { if (dataSetChanges.length == 1) { result.addDataSetChange(dataSetChanges[0]); } else { result.addDataSetChange(dataSetChanges[1]); } } break; // Row has been possibly changed. case BOTH: if (mItemChangeHandler != null && mFirstVisibleItemPosition != NO_POSITION && mLastVisibleItemPosition != NO_POSITION) { final int cursorPosition = newCursor.getPosition(); if (cursorPosition >= mFirstVisibleItemPosition && cursorPosition <= mLastVisibleItemPosition && mItemChangeHandler.hasChanged(newCursor)) { this.resolvePendingChange(mInsertHandler, result); this.resolvePendingChange(mRemoveHandler, result); // Item at the current processing position is visible and its data has // changed so its UI should be re-bound with the new data. result.addDataSetChange(new ItemChanged(cursorPosition)); // todo: handle item range changes } } break; } } return result; }
@Override protected void onCreateCallback(Bundle savedInstanceState) { Cursor medias = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Images.Media._ID); String[] colunsMedia = new String[]{MediaStore.Images.Media._ID}; int idIdx = medias.getColumnIndex(MediaStore.Images.Media._ID); int displayIdx = medias.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME); int pathIdx = medias.getColumnIndex(MediaStore.Images.Media.DATA); Cursor thumbnail = getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Images.Thumbnails.IMAGE_ID); String[] columnsThumbnail = new String[]{MediaStore.Images.Thumbnails.IMAGE_ID}; int thumbIdx = thumbnail.getColumnIndex(MediaStore.Images.Thumbnails.DATA); CursorJoiner joiner = new CursorJoiner(medias, colunsMedia, thumbnail, columnsThumbnail); mList = new ArrayList<ImageEntity>(); mFilters = new ArrayList<ImageFilter>(); for (CursorJoiner.Result joinerResult : joiner) { ImageEntity entity = new ImageEntity(); switch (joinerResult) { case LEFT: entity.id = medias.getInt(idIdx); entity.displayName = medias.getString(displayIdx); entity.data = medias.getString(pathIdx); break; case RIGHT: Logs.i(TAG, "RIGHT ............"); continue; case BOTH: entity.id = medias.getInt(idIdx); entity.displayName = medias.getString(displayIdx); entity.data = medias.getString(pathIdx); entity.thumb = thumbnail.getString(thumbIdx); break; } setToFilter(entity); mList.add(entity); } mGridView = (GridView) findViewById(R.id.gridview); mAdapter = new ImageAdapter(); mGridView.setAdapter(mAdapter); medias.close(); thumbnail.close(); setBaseToolBarTitle("选择图片(" + 0 + "/" + mList.size() + ")"); mFilterName = (TextView) findViewById(R.id.filter_name); mFilterLayout = findViewById(R.id.filter_layout); mFilterListView = (ListView) findViewById(R.id.filter_listview); mFilterAdapter = new FilterAdapter(); mFilterListView.setAdapter(mFilterAdapter); mFilterListView.setOnItemClickListener(mFilterAdapter); mFilterName.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mFilterLayout.getVisibility() == View.VISIBLE) { mFilterLayout.setVisibility(View.GONE); } else { mFilterLayout.setVisibility(View.VISIBLE); } } }); }
/** * Conditionally adds a flow event if no flow event exists in the current upload blob. */ private void conditionallyAddFlowEvent() { /* * Creating a flow "event" is required to act as a placeholder so that the uploader will know that an upload needs to * occur. A flow event should only be created if there isn't already a flow event that hasn't been associated with an * upload blob. */ boolean foundUnassociatedFlowEvent = false; Cursor eventsCursor = null; Cursor blob_eventsCursor = null; try { eventsCursor = mProvider.query(EventsDbColumns.TABLE_NAME, PROJECTION_FLOW_EVENTS, SELECTION_FLOW_EVENTS, SELECTION_ARGS_FLOW_EVENTS, EVENTS_SORT_ORDER); blob_eventsCursor = mProvider.query(UploadBlobEventsDbColumns.TABLE_NAME, PROJECTION_FLOW_BLOBS, null, null, UPLOAD_BLOBS_EVENTS_SORT_ORDER); final CursorJoiner joiner = new CursorJoiner(eventsCursor, PROJECTION_FLOW_EVENTS, blob_eventsCursor, PROJECTION_FLOW_BLOBS); for (final CursorJoiner.Result joinerResult : joiner) { switch (joinerResult) { case LEFT: { foundUnassociatedFlowEvent = true; break; } case BOTH: break; case RIGHT: break; } } } finally { if (null != eventsCursor) { eventsCursor.close(); eventsCursor = null; } if (null != blob_eventsCursor) { blob_eventsCursor.close(); blob_eventsCursor = null; } } if (!foundUnassociatedFlowEvent) { tagEvent(FLOW_EVENT, null); } }
/** * Conditionally adds a flow event if no flow event exists in the current upload blob. */ private void conditionallyAddFlowEvent() { /* * Creating a flow "event" is required to act as a placeholder so that the uploader will know that an upload needs to * occur. A flow event should only be created if there isn't already a flow event that hasn't been associated with an * upload blob. */ boolean foundUnassociatedFlowEvent = false; Cursor eventsCursor = null; Cursor blob_eventsCursor = null; try { eventsCursor = mProvider.query(EventsDbColumns.TABLE_NAME, new String[] { EventsDbColumns._ID }, String.format("%s = ?", EventsDbColumns.EVENT_NAME), new String[] //$NON-NLS-1$ { FLOW_EVENT }, EVENTS_SORT_ORDER); blob_eventsCursor = mProvider.query(UploadBlobEventsDbColumns.TABLE_NAME, new String[] { UploadBlobEventsDbColumns.EVENTS_KEY_REF }, null, null, UPLOAD_BLOBS_EVENTS_SORT_ORDER); final CursorJoiner joiner = new CursorJoiner(eventsCursor, new String[] { EventsDbColumns._ID }, blob_eventsCursor, new String[] { UploadBlobEventsDbColumns.EVENTS_KEY_REF }); for (final CursorJoiner.Result joinerResult : joiner) { switch (joinerResult) { case LEFT: { foundUnassociatedFlowEvent = true; break; } case BOTH: break; case RIGHT: break; } } } finally { if (eventsCursor != null) { eventsCursor.close(); eventsCursor = null; } if (blob_eventsCursor != null) { blob_eventsCursor.close(); blob_eventsCursor = null; } } if (!foundUnassociatedFlowEvent) { tagEvent(FLOW_EVENT, null); } }