extract system contact info loading into Loader
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Vincent Breitmoser <v.breitmoser@mugenguild.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<SystemContactInfo> {
|
||||||
|
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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,13 +18,10 @@
|
|||||||
package org.sufficientlysecure.keychain.ui.widget;
|
package org.sufficientlysecure.keychain.ui.widget;
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.Cursor;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -32,21 +29,14 @@ import android.provider.ContactsContract;
|
|||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v4.content.CursorLoader;
|
|
||||||
import android.support.v4.content.Loader;
|
import android.support.v4.content.Loader;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
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;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
|
||||||
|
|
||||||
public class SystemContactPresenter implements LoaderCallbacks<Cursor> {
|
public class SystemContactPresenter implements LoaderCallbacks<SystemContactInfo> {
|
||||||
private static final String[] RAW_CONTACT_PROJECTION = {
|
|
||||||
ContactsContract.RawContacts.CONTACT_ID
|
|
||||||
};
|
|
||||||
private static final int INDEX_CONTACT_ID = 0;
|
|
||||||
|
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final SystemContactMvpView view;
|
private final SystemContactMvpView view;
|
||||||
private final int loaderId;
|
private final int loaderId;
|
||||||
@@ -88,63 +78,19 @@ public class SystemContactPresenter implements LoaderCallbacks<Cursor> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
public Loader<SystemContactInfo> onCreateLoader(int id, Bundle args) {
|
||||||
Uri baseUri = isSecret ? ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI :
|
return new SystemContactInfoLoader(context, context.getContentResolver(), masterKeyId, isSecret);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
public void onLoadFinished(Loader<SystemContactInfo> loader, SystemContactInfo data) {
|
||||||
if (!data.moveToFirst()) {
|
if (data == null) {
|
||||||
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<String> 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 {
|
|
||||||
view.hideLinkedSystemContact();
|
view.hideLinkedSystemContact();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.contactId = data.contactId;
|
||||||
|
view.showLinkedSystemContact(data.contactName, data.contactPicture);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user