Example usage for android.content ContentProviderClient close

List of usage examples for android.content ContentProviderClient close

Introduction

In this page you can find the example usage for android.content ContentProviderClient close.

Prototype

@Override
public void close() 

Source Link

Document

Closes this client connection, indicating to the system that the underlying ContentProvider is no longer needed.

Usage

From source file:org.voidsink.anewjkuapp.calendar.CalendarUtils.java

private static boolean deleteKusssEvents(Context context, String calId) {
    if (calId != null) {
        ContentProviderClient provider = context.getContentResolver()
                .acquireContentProviderClient(CalendarContractWrapper.Events.CONTENT_URI());

        if (provider == null) {
            return false;
        }/*from w ww. j  a  v a  2 s  .co  m*/

        try {
            Uri calUri = CalendarContractWrapper.Events.CONTENT_URI();

            Cursor c = loadEvent(provider, calUri, calId);
            if (c != null) {
                try {
                    ArrayList<ContentProviderOperation> batch = new ArrayList<>();
                    long deleteFrom = new Date().getTime() - DateUtils.DAY_IN_MILLIS;
                    while (c.moveToNext()) {
                        long eventDTStart = c.getLong(CalendarUtils.COLUMN_EVENT_DTSTART);
                        if (eventDTStart > deleteFrom) {
                            String eventId = c.getString(COLUMN_EVENT_ID);
                            //                        Log.d(TAG, "---------");
                            String eventKusssId = null;

                            // get kusssId from extended properties
                            Cursor c2 = provider.query(CalendarContract.ExtendedProperties.CONTENT_URI,
                                    CalendarUtils.EXTENDED_PROPERTIES_PROJECTION,
                                    CalendarContract.ExtendedProperties.EVENT_ID + " = ?",
                                    new String[] { eventId }, null);

                            if (c2 != null) {
                                while (c2.moveToNext()) {
                                    if (c2.getString(1).contains(EXTENDED_PROPERTY_NAME_KUSSS_ID)) {
                                        eventKusssId = c2.getString(2);
                                    }
                                }
                                c2.close();
                            }

                            if (TextUtils.isEmpty(eventKusssId)) {
                                eventKusssId = c.getString(COLUMN_EVENT_KUSSS_ID_LEGACY);
                            }

                            if (!TextUtils.isEmpty(eventKusssId)) {
                                if (eventKusssId.startsWith("at-jku-kusss-exam-")
                                        || eventKusssId.startsWith("at-jku-kusss-coursedate-")) {
                                    Uri deleteUri = calUri.buildUpon().appendPath(eventId).build();
                                    Log.d(TAG, "Scheduling delete: " + deleteUri);
                                    batch.add(ContentProviderOperation.newDelete(deleteUri).build());
                                }
                            }
                        }
                    }
                    if (batch.size() > 0) {
                        Log.d(TAG, "Applying batch update");
                        provider.applyBatch(batch);
                        Log.d(TAG, "Notify resolver");
                    } else {
                        Log.w(TAG, "No batch operations found! Do nothing");
                    }
                } catch (RemoteException | OperationApplicationException e) {
                    Analytics.sendException(context, e, true);
                    return false;
                }
            }
        } finally {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                provider.close();
            } else {
                provider.release();
            }
        }
        return false;
    }
    return true;
}