diff --git a/src/org/thoughtcrime/securesms/ContactSelectionActivity.java b/src/org/thoughtcrime/securesms/ContactSelectionActivity.java index 3684b384fd..857d151e3b 100644 --- a/src/org/thoughtcrime/securesms/ContactSelectionActivity.java +++ b/src/org/thoughtcrime/securesms/ContactSelectionActivity.java @@ -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() { - @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 { + + private final WeakReference 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(); + } + } + } } diff --git a/src/org/thoughtcrime/securesms/util/DirectoryHelper.java b/src/org/thoughtcrime/securesms/util/DirectoryHelper.java index 059e25487e..1078d0e310 100644 --- a/src/org/thoughtcrime/securesms/util/DirectoryHelper.java +++ b/src/org/thoughtcrime/securesms/util/DirectoryHelper.java @@ -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(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)); }