extract KeyInfoInteractor from KeyLoader

This commit is contained in:
Vincent Breitmoser 2018-03-28 21:16:03 +02:00
parent 4c92426fe5
commit 69121bfa98
6 changed files with 105 additions and 55 deletions

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.remote.ui.dialog;
package org.sufficientlysecure.keychain.livedata;
import java.util.ArrayList;
@ -23,19 +23,17 @@ import java.util.Collections;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
import com.google.auto.value.AutoValue;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeyInfo;
import org.sufficientlysecure.keychain.util.DatabaseUtil;
public class KeyLoader extends AsyncTaskLoader<List<KeyInfo>> {
public class KeyInfoInteractor {
// These are the rows that we will retrieve.
private String[] QUERY_PROJECTION = new String[]{
KeyRings._ID,
@ -61,29 +59,27 @@ public class KeyLoader extends AsyncTaskLoader<List<KeyInfo>> {
private static final int INDEX_EMAIL = 8;
private static final int INDEX_COMMENT = 9;
private static final String QUERY_WHERE = Tables.KEYS + "." + KeyRings.IS_REVOKED +
private static final String QUERY_WHERE_ALL = Tables.KEYS + "." + KeyRings.IS_REVOKED +
" = 0 AND " + KeyRings.IS_EXPIRED + " = 0";
private static final String QUERY_WHERE_SECRET = Tables.KEYS + "." + KeyRings.IS_REVOKED +
" = 0 AND " + KeyRings.IS_EXPIRED + " = 0" + " AND " + KeyRings.HAS_ANY_SECRET + " != 0";
private static final String QUERY_ORDER = Tables.KEYS + "." + KeyRings.CREATION + " DESC";
private final ContentResolver contentResolver;
private final KeySelector keySelector;
private List<KeyInfo> cachedResult;
KeyLoader(Context context, ContentResolver contentResolver, KeySelector keySelector) {
super(context);
public KeyInfoInteractor(ContentResolver contentResolver) {
this.contentResolver = contentResolver;
this.keySelector = keySelector;
}
@Override
public List<KeyInfo> loadInBackground() {
public List<KeyInfo> loadKeyInfo(KeySelector keySelector) {
ArrayList<KeyInfo> keyInfos = new ArrayList<>();
Cursor cursor;
String selection = keySelector.isOnlySecret() ? QUERY_WHERE_SECRET : QUERY_WHERE_ALL;
String additionalSelection = keySelector.getSelection();
String selection = QUERY_WHERE + (additionalSelection != null ? " AND " + additionalSelection : "");
selection = DatabaseUtil.concatenateWhere(selection, additionalSelection);
cursor = contentResolver.query(keySelector.getKeyRingUri(), QUERY_PROJECTION, selection, null, QUERY_ORDER);
if (cursor == null) {
@ -98,33 +94,6 @@ public class KeyLoader extends AsyncTaskLoader<List<KeyInfo>> {
return Collections.unmodifiableList(keyInfos);
}
@Override
public void deliverResult(List<KeyInfo> keySubkeyStatus) {
cachedResult = keySubkeyStatus;
if (isStarted()) {
super.deliverResult(keySubkeyStatus);
}
}
@Override
protected void onStartLoading() {
if (cachedResult != null) {
deliverResult(cachedResult);
}
if (takeContentChanged() || cachedResult == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
super.onStopLoading();
cachedResult = null;
}
@AutoValue
public abstract static class KeyInfo {
public abstract long getMasterKeyId();
@ -153,7 +122,7 @@ public class KeyLoader extends AsyncTaskLoader<List<KeyInfo>> {
String email = cursor.getString(INDEX_EMAIL);
String comment = cursor.getString(INDEX_COMMENT);
return new AutoValue_KeyLoader_KeyInfo(
return new AutoValue_KeyInfoInteractor_KeyInfo(
masterKeyId, creationDate, hasEncrypt, hasAuthenticate, hasAnySecret, isVerified, name, email, comment);
}
}
@ -163,9 +132,14 @@ public class KeyLoader extends AsyncTaskLoader<List<KeyInfo>> {
public abstract Uri getKeyRingUri();
@Nullable
public abstract String getSelection();
public abstract boolean isOnlySecret();
static KeySelector create(Uri keyRingUri, String selection) {
return new AutoValue_KeyLoader_KeySelector(keyRingUri, selection);
public static KeySelector create(Uri keyRingUri, String selection) {
return new AutoValue_KeyInfoInteractor_KeySelector(keyRingUri, selection, false);
}
public static KeySelector createOnlySecret(Uri keyRingUri, String selection) {
return new AutoValue_KeyInfoInteractor_KeySelector(keyRingUri, selection, true);
}
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (C) 2017 Schürmann & Breitmoser GbR
*
* 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.remote.ui.dialog;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeyInfo;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeySelector;
public class KeyInfoLoader extends AsyncTaskLoader<List<KeyInfo>> {
private final KeySelector keySelector;
private List<KeyInfo> cachedResult;
private KeyInfoInteractor keyInfoInteractor;
KeyInfoLoader(Context context, ContentResolver contentResolver, KeySelector keySelector) {
super(context);
this.keySelector = keySelector;
this.keyInfoInteractor = new KeyInfoInteractor(contentResolver);
}
@Override
public List<KeyInfo> loadInBackground() {
return keyInfoInteractor.loadKeyInfo(keySelector);
}
@Override
public void deliverResult(List<KeyInfo> keySubkeyStatus) {
cachedResult = keySubkeyStatus;
if (isStarted()) {
super.deliverResult(keySubkeyStatus);
}
}
@Override
protected void onStartLoading() {
if (cachedResult != null) {
deliverResult(cachedResult);
}
if (takeContentChanged() || cachedResult == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
super.onStopLoading();
cachedResult = null;
}
}

View file

@ -51,7 +51,7 @@ import android.widget.TextView;
import com.mikepenz.materialdrawer.util.KeyboardUtil;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.remote.ui.RemoteSecurityTokenOperationActivity;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeyInfo;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeyInfo;
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicatePresenter.RemoteDeduplicateView;
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;

View file

@ -33,8 +33,8 @@ import android.support.v4.content.Loader;
import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeyInfo;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeySelector;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeyInfo;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeySelector;
import timber.log.Timber;
@ -95,7 +95,7 @@ class RemoteDeduplicatePresenter implements LoaderCallbacks<List<KeyInfo>> {
KeySelector keySelector = KeySelector.create(
KeyRings.buildUnifiedKeyRingsFindByEmailUri(duplicateAddress), null);
return new KeyLoader(context, context.getContentResolver(), keySelector);
return new KeyInfoLoader(context, context.getContentResolver(), keySelector);
}
@Override

View file

@ -54,7 +54,7 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.ApiDataAccessObject;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.remote.ui.RemoteSecurityTokenOperationActivity;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeyInfo;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeyInfo;
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteSelectAuthenticationKeyPresenter.RemoteSelectAuthenticationKeyView;
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;

View file

@ -18,6 +18,8 @@
package org.sufficientlysecure.keychain.remote.ui.dialog;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@ -29,12 +31,10 @@ import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeyInfo;
import org.sufficientlysecure.keychain.remote.ui.dialog.KeyLoader.KeySelector;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeyInfo;
import org.sufficientlysecure.keychain.livedata.KeyInfoInteractor.KeySelector;
import timber.log.Timber;
import java.util.List;
class RemoteSelectAuthenticationKeyPresenter implements LoaderCallbacks<List<KeyInfo>> {
private final PackageManager packageManager;
@ -84,7 +84,7 @@ class RemoteSelectAuthenticationKeyPresenter implements LoaderCallbacks<List<Key
String selection = KeyRings.HAS_AUTHENTICATE_SECRET + " != 0";
KeySelector keySelector = KeySelector.create(
KeyRings.buildUnifiedKeyRingsUri(), selection);
return new KeyLoader(context, context.getContentResolver(), keySelector);
return new KeyInfoLoader(context, context.getContentResolver(), keySelector);
}
@Override