Merge pull request #2301 from open-keychain/api-choose-sign-key
New identity chooser dialog for API
This commit is contained in:
@@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.ui.base;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.Gravity;
|
||||
@@ -136,8 +137,6 @@ public class RecyclerFragment<A extends RecyclerView.Adapter> extends Fragment {
|
||||
RecyclerView listView = new RecyclerView(context);
|
||||
listView.setId(INTERNAL_LIST_VIEW_ID);
|
||||
|
||||
int padding = FormattingUtils.dpToPx(context, 8);
|
||||
listView.setPadding(padding, 0, padding, 0);
|
||||
listView.setClipToPadding(false);
|
||||
|
||||
listContainer.addView(listView, new FrameLayout.LayoutParams(
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package org.sufficientlysecure.keychain.ui.keyview.loader;
|
||||
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.os.CancellationSignal;
|
||||
import android.support.v4.os.OperationCanceledException;
|
||||
|
||||
public abstract class AsyncTaskLiveData<T> extends LiveData<T> {
|
||||
@NonNull
|
||||
private final Context context;
|
||||
private Uri observedUri;
|
||||
|
||||
@NonNull
|
||||
private final ForceLoadContentObserver observer;
|
||||
|
||||
@Nullable
|
||||
private CancellationSignal cancellationSignal;
|
||||
|
||||
protected AsyncTaskLiveData(@NonNull Context context, @Nullable Uri observedUri) {
|
||||
super();
|
||||
this.context = context;
|
||||
this.observedUri = observedUri;
|
||||
this.observer = new ForceLoadContentObserver();
|
||||
}
|
||||
|
||||
protected abstract T asyncLoadData();
|
||||
|
||||
protected void updateDataInBackground() {
|
||||
new AsyncTask<Void, Void, T>() {
|
||||
@Override
|
||||
protected T doInBackground(Void... params) {
|
||||
try {
|
||||
synchronized (AsyncTaskLiveData.this) {
|
||||
cancellationSignal = new CancellationSignal();
|
||||
}
|
||||
try {
|
||||
return asyncLoadData();
|
||||
} finally {
|
||||
synchronized (AsyncTaskLiveData.this) {
|
||||
cancellationSignal = null;
|
||||
}
|
||||
}
|
||||
} catch (OperationCanceledException e) {
|
||||
if (hasActiveObservers()) {
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(T value) {
|
||||
setValue(value);
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActive() {
|
||||
T value = getValue();
|
||||
if (value == null) {
|
||||
updateDataInBackground();
|
||||
}
|
||||
|
||||
if (observedUri != null) {
|
||||
getContext().getContentResolver().registerContentObserver(observedUri, true, observer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInactive() {
|
||||
synchronized (AsyncTaskLiveData.this) {
|
||||
if (cancellationSignal != null) {
|
||||
cancellationSignal.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
if (observedUri != null) {
|
||||
getContext().getContentResolver().registerContentObserver(observedUri, true, observer);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public final class ForceLoadContentObserver extends ContentObserver {
|
||||
|
||||
ForceLoadContentObserver() {
|
||||
super(new Handler());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deliverSelfNotifications() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
updateDataInBackground();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user