Make directory refresh async task more careful.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-07-15 14:27:37 -07:00
parent 0e7fcc6893
commit 32cb489d1d
2 changed files with 34 additions and 49 deletions

View File

@ -16,6 +16,7 @@
*/
package org.thoughtcrime.securesms;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
@ -37,6 +38,7 @@ import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme;
import java.io.IOException;
import java.lang.ref.WeakReference;
/**
* Base activity container for selecting a list of contacts.
@ -158,24 +160,7 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB
@Override
public void onRefresh() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
DirectoryHelper.refreshDirectory(ContactSelectionActivity.this);
} catch (IOException e) {
Log.w(TAG, e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
searchText.setText("");
contactsFragment.resetQueryFilter();
}
}.execute();
new RefreshDirectoryTask(this).execute(getApplicationContext());
}
@Override
@ -195,4 +180,35 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB
return editText.getText().length() <= 0;
}
}
private static class RefreshDirectoryTask extends AsyncTask<Context, Void, Void> {
private final WeakReference<ContactSelectionActivity> activity;
private RefreshDirectoryTask(ContactSelectionActivity activity) {
this.activity = new WeakReference<>(activity);
}
@Override
protected Void doInBackground(Context... params) {
try {
DirectoryHelper.refreshDirectory(params[0]);
} catch (IOException e) {
Log.w(TAG, e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
ContactSelectionActivity activity = this.activity.get();
if (activity != null && !activity.isFinishing()) {
activity.searchText.setText("");
activity.contactsFragment.resetQueryFilter();
}
}
}
}

View File

@ -30,37 +30,6 @@ import java.util.Set;
public class DirectoryHelper {
private static final String TAG = DirectoryHelper.class.getSimpleName();
public static void refreshDirectoryWithProgressDialog(final Context context, final DirectoryUpdateFinishedListener listener) {
if (!TextSecurePreferences.isPushRegistered(context)) {
Toast.makeText(context.getApplicationContext(),
context.getString(R.string.SingleContactSelectionActivity_you_are_not_registered_with_the_push_service),
Toast.LENGTH_LONG).show();
return;
}
new ProgressDialogAsyncTask<Void,Void,Void>(context,
R.string.SingleContactSelectionActivity_updating_directory,
R.string.SingleContactSelectionActivity_updating_push_directory)
{
@Override
protected Void doInBackground(Void... voids) {
try {
DirectoryHelper.refreshDirectory(context.getApplicationContext());
} catch (IOException e) {
Log.w(TAG, e);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (listener != null) listener.onUpdateFinished();
}
}.execute();
}
public static void refreshDirectory(final Context context) throws IOException {
refreshDirectory(context, TextSecureCommunicationFactory.createManager(context));
}