From d919180219f6801b3ddd07296f0679257c0272cf Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 21 May 2017 01:52:37 +0200 Subject: [PATCH] extract system contact info loading into Loader --- .../ui/widget/SystemContactInfoLoader.java | 151 ++++++++++++++++++ .../ui/widget/SystemContactPresenter.java | 74 ++------- 2 files changed, 161 insertions(+), 64 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactInfoLoader.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactInfoLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactInfoLoader.java new file mode 100644 index 000000000..b45effb82 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactInfoLoader.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2017 Vincent Breitmoser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.widget; + + +import java.util.List; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.net.Uri; +import android.provider.ContactsContract; +import android.support.v4.content.AsyncTaskLoader; +import android.util.Log; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.ui.widget.SystemContactInfoLoader.SystemContactInfo; +import org.sufficientlysecure.keychain.util.ContactHelper; + + +class SystemContactInfoLoader extends AsyncTaskLoader { + private static final String[] PROJECTION = { + ContactsContract.RawContacts.CONTACT_ID + }; + private static final int INDEX_CONTACT_ID = 0; + private static final String CONTACT_NOT_DELETED = "0"; + + + private final ContentResolver contentResolver; + private final long masterKeyId; + private final boolean isSecret; + + private SystemContactInfo cachedResult; + + + SystemContactInfoLoader(Context context, ContentResolver contentResolver, long masterKeyId, boolean isSecret) { + super(context); + + this.contentResolver = contentResolver; + this.masterKeyId = masterKeyId; + this.isSecret = isSecret; + } + + @Override + public SystemContactInfo loadInBackground() { + Uri baseUri = isSecret ? ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI : + ContactsContract.RawContacts.CONTENT_URI; + Cursor cursor = contentResolver.query(baseUri, PROJECTION, + ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + + ContactsContract.RawContacts.SOURCE_ID + " = ? AND " + + ContactsContract.RawContacts.DELETED + " = ?", + new String[] { + Constants.ACCOUNT_TYPE, + Long.toString(masterKeyId), + CONTACT_NOT_DELETED + }, null); + + if (cursor == null) { + Log.e(Constants.TAG, "Error loading key items!"); + return null; + } + + try { + if (!cursor.moveToFirst()) { + return null; + } + + long contactId = cursor.getLong(INDEX_CONTACT_ID); + if (contactId == -1) { + return null; + } + + ContactHelper contactHelper = new ContactHelper(getContext()); + + String contactName = null; + if (isSecret) { //all secret keys are linked to "me" profile in contacts + List mainProfileNames = contactHelper.getMainProfileContactName(); + if (mainProfileNames != null && mainProfileNames.size() > 0) { + contactName = mainProfileNames.get(0); + } + } else { + contactName = contactHelper.getContactName(contactId); + } + + if (contactName == null) { + return null; + } + + Bitmap contactPicture; + if (isSecret) { + contactPicture = contactHelper.loadMainProfilePhoto(false); + } else { + contactPicture = contactHelper.loadPhotoByContactId(contactId, false); + } + + return new SystemContactInfo(masterKeyId, contactId, contactName, contactPicture); + } finally { + cursor.close(); + } + } + + @Override + public void deliverResult(SystemContactInfo systemContactInfo) { + cachedResult = systemContactInfo; + + if (isStarted()) { + super.deliverResult(systemContactInfo); + } + } + + @Override + protected void onStartLoading() { + if (cachedResult != null) { + deliverResult(cachedResult); + } + + if (takeContentChanged() || cachedResult == null) { + forceLoad(); + } + } + + static class SystemContactInfo { + final long masterKeyId; + final long contactId; + final String contactName; + final Bitmap contactPicture; + + SystemContactInfo(long masterKeyId, long contactId, String contactName, Bitmap contactPicture) { + this.masterKeyId = masterKeyId; + this.contactId = contactId; + this.contactName = contactName; + this.contactPicture = contactPicture; + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactPresenter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactPresenter.java index 7b8919250..3c6b575c6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactPresenter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SystemContactPresenter.java @@ -18,13 +18,10 @@ package org.sufficientlysecure.keychain.ui.widget; -import java.util.List; - import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; @@ -32,21 +29,14 @@ import android.provider.ContactsContract; import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.ContextCompat; -import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.util.ContactHelper; +import org.sufficientlysecure.keychain.ui.widget.SystemContactInfoLoader.SystemContactInfo; import org.sufficientlysecure.keychain.util.Log; -public class SystemContactPresenter implements LoaderCallbacks { - private static final String[] RAW_CONTACT_PROJECTION = { - ContactsContract.RawContacts.CONTACT_ID - }; - private static final int INDEX_CONTACT_ID = 0; - - +public class SystemContactPresenter implements LoaderCallbacks { private final Context context; private final SystemContactMvpView view; private final int loaderId; @@ -88,63 +78,19 @@ public class SystemContactPresenter implements LoaderCallbacks { } @Override - public Loader onCreateLoader(int id, Bundle args) { - Uri baseUri = isSecret ? ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI : - ContactsContract.RawContacts.CONTENT_URI; - - return new CursorLoader(context, baseUri, RAW_CONTACT_PROJECTION, - ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + - ContactsContract.RawContacts.SOURCE_ID + "=? AND " + - ContactsContract.RawContacts.DELETED + "=?", - new String[] { - Constants.ACCOUNT_TYPE, - Long.toString(masterKeyId), - "0" // "0" for "not deleted" - }, - null); + public Loader onCreateLoader(int id, Bundle args) { + return new SystemContactInfoLoader(context, context.getContentResolver(), masterKeyId, isSecret); } @Override - public void onLoadFinished(Loader loader, Cursor data) { - if (!data.moveToFirst()) { - return; - } - - long contactId = data.getLong(INDEX_CONTACT_ID); - loadLinkedSystemContact(contactId); - } - - private void loadLinkedSystemContact(final long contactId) { - this.contactId = contactId; - - if (contactId == -1) { - return; - } - - ContactHelper contactHelper = new ContactHelper(context); - - String contactName = null; - if (isSecret) { //all secret keys are linked to "me" profile in contacts - List mainProfileNames = contactHelper.getMainProfileContactName(); - if (mainProfileNames != null && mainProfileNames.size() > 0) { - contactName = mainProfileNames.get(0); - } - } else { - contactName = contactHelper.getContactName(contactId); - } - - if (contactName != null) { //contact name exists for given master key - Bitmap picture; - if (isSecret) { - picture = contactHelper.loadMainProfilePhoto(false); - } else { - picture = contactHelper.loadPhotoByContactId(contactId, false); - } - - view.showLinkedSystemContact(contactName, picture); - } else { + public void onLoadFinished(Loader loader, SystemContactInfo data) { + if (data == null) { view.hideLinkedSystemContact(); + return; } + + this.contactId = data.contactId; + view.showLinkedSystemContact(data.contactName, data.contactPicture); } @Override