Singleton handler

pull/184/head
M66B 4 years ago
parent a8a95b0953
commit c48ee83925

@ -28,6 +28,7 @@ import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -513,6 +514,10 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
return super.shouldUpRecreateTask(targetIntent); return super.shouldUpRecreateTask(targetIntent);
} }
Handler getMainHandler() {
return ApplicationEx.getMainHandler();
}
public interface IKeyPressedListener { public interface IKeyPressedListener {
boolean onKeyPressed(KeyEvent event); boolean onKeyPressed(KeyEvent event);

@ -24,7 +24,6 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
@ -62,7 +61,7 @@ public class ActivityMain extends ActivityBase implements FragmentManager.OnBack
final SimpleTask boot = new SimpleTask<Boolean>() { final SimpleTask boot = new SimpleTask<Boolean>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
new Handler().postDelayed(new Runnable() { getMainHandler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
getWindow().setBackgroundDrawableResource(R.drawable.splash); getWindow().setBackgroundDrawableResource(R.drawable.splash);
@ -102,7 +101,7 @@ public class ActivityMain extends ActivityBase implements FragmentManager.OnBack
startActivity(view); startActivity(view);
} }
new Handler().postDelayed(new Runnable() { getMainHandler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
ServiceSynchronize.eval(ActivityMain.this, "main"); ServiceSynchronize.eval(ActivityMain.this, "main");

@ -35,7 +35,6 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
@ -332,7 +331,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
ibExpanderExtra.setImageLevel(minimal ? 1 /* more */ : 0 /* less */); ibExpanderExtra.setImageLevel(minimal ? 1 /* more */ : 0 /* less */);
rvMenuExtra.setVisibility(minimal ? View.GONE : View.VISIBLE); rvMenuExtra.setVisibility(minimal ? View.GONE : View.VISIBLE);
if (!minimal) if (!minimal)
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
drawerContainer.fullScroll(View.FOCUS_DOWN); drawerContainer.fullScroll(View.FOCUS_DOWN);
@ -676,7 +675,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
else { else {
exit = true; exit = true;
ToastEx.makeText(this, R.string.app_exit, Toast.LENGTH_SHORT).show(); ToastEx.makeText(this, R.string.app_exit, Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() { getMainHandler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
exit = false; exit = false;
@ -773,7 +772,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
snackbar.show(); snackbar.show();
// Wait // Wait
new Handler().postDelayed(new Runnable() { getMainHandler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
Log.i("Undo timeout"); Log.i("Undo timeout");

@ -32,7 +32,6 @@ import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
@ -972,7 +971,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
set(all); set(all);
// Delay search until after expanding // Delay search until after expanding
new Handler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
int pos = -1; int pos = -1;

@ -50,8 +50,6 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.CalendarContract; import android.provider.CalendarContract;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.Settings; import android.provider.Settings;
@ -1766,7 +1764,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
tvHeaders.setText(null); tvHeaders.setText(null);
if (scroll) if (scroll)
new Handler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
View inHeaders = itemView.findViewById(R.id.inHeaders); View inHeaders = itemView.findViewById(R.id.inHeaders);
@ -2969,7 +2967,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
firstClick = !firstClick; firstClick = !firstClick;
if (firstClick) { if (firstClick) {
new Handler().postDelayed(new Runnable() { ApplicationEx.getMainHandler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
if (firstClick) { if (firstClick) {
@ -5491,13 +5489,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
try { try {
// https://issuetracker.google.com/issues/135628748 // https://issuetracker.google.com/issues/135628748
Handler handler = new Handler(Looper.getMainLooper());
Field mMainThreadExecutor = this.differ.getClass().getDeclaredField("mMainThreadExecutor"); Field mMainThreadExecutor = this.differ.getClass().getDeclaredField("mMainThreadExecutor");
mMainThreadExecutor.setAccessible(true); mMainThreadExecutor.setAccessible(true);
mMainThreadExecutor.set(this.differ, new Executor() { mMainThreadExecutor.set(this.differ, new Executor() {
@Override @Override
public void execute(final Runnable command) { public void execute(final Runnable command) {
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -5967,7 +5964,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
protected void onExecuted(Bundle args, Pair<String, IPInfo.Organization> data) { protected void onExecuted(Bundle args, Pair<String, IPInfo.Organization> data) {
tvHost.setText(data.first); tvHost.setText(data.first);
tvOwner.setText(data.second.name == null ? "?" : data.second.name); tvOwner.setText(data.second.name == null ? "?" : data.second.name);
new Handler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
dview.scrollTo(0, tvOwner.getBottom()); dview.scrollTo(0, tvOwner.getBottom());

@ -32,6 +32,8 @@ import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Printer; import android.util.Printer;
import android.webkit.CookieManager; import android.webkit.CookieManager;
@ -393,4 +395,12 @@ public class ApplicationEx extends Application {
Helper.clearAuthentication(ApplicationEx.this); Helper.clearAuthentication(ApplicationEx.this);
} }
}; };
private static Handler handler = null;
synchronized static Handler getMainHandler() {
if (handler == null)
handler = new Handler(Looper.getMainLooper());
return handler;
}
} }

@ -21,8 +21,6 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -87,8 +85,6 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
private IBoundaryCallbackMessages intf; private IBoundaryCallbackMessages intf;
private Handler handler;
private State state; private State state;
private static final int SEARCH_LIMIT_DEVICE = 1000; private static final int SEARCH_LIMIT_DEVICE = 1000;
@ -113,7 +109,6 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
} }
void setCallback(IBoundaryCallbackMessages intf) { void setCallback(IBoundaryCallbackMessages intf) {
this.handler = new Handler(Looper.getMainLooper());
this.intf = intf; this.intf = intf;
this.state = new State(); this.state = new State();
} }
@ -149,7 +144,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
return; return;
if (intf != null) if (intf != null)
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
intf.onLoading(); intf.onLoading();
@ -174,7 +169,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
state.error = true; state.error = true;
Log.e("Boundary", ex); Log.e("Boundary", ex);
if (intf != null) if (intf != null)
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
intf.onException(ex); intf.onException(ex);
@ -182,7 +177,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
}); });
} finally { } finally {
if (intf != null) if (intf != null)
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
intf.onLoaded(); intf.onLoaded();

@ -32,8 +32,6 @@ import android.graphics.Color;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.text.TextUtils; import android.text.TextUtils;
@ -519,9 +517,7 @@ public class ContactInfo {
static void init(final Context context) { static void init(final Context context) {
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) { if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
Handler handler = new Handler(Looper.getMainLooper()); ContentObserver observer = new ContentObserver(ApplicationEx.getMainHandler()) {
ContentObserver observer = new ContentObserver(handler) {
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
Log.i("Contact changed uri=" + uri); Log.i("Contact changed uri=" + uri);

@ -20,8 +20,6 @@ package eu.faircode.email;
*/ */
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -30,42 +28,33 @@ import androidx.annotation.Nullable;
public class ContentLoadingProgressBar extends ProgressBar { public class ContentLoadingProgressBar extends ProgressBar {
private int visibility; private int visibility;
private Handler handler;
private static final int VISIBILITY_DELAY = 500; // milliseconds private static final int VISIBILITY_DELAY = 500; // milliseconds
public ContentLoadingProgressBar(@NonNull Context context) { public ContentLoadingProgressBar(@NonNull Context context) {
this(context, null); this(context, null);
init();
} }
public ContentLoadingProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) { public ContentLoadingProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs, 0); super(context, attrs, 0);
init();
} }
public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
init();
} }
public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { public ContentLoadingProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
handler = new Handler(Looper.getMainLooper());
} }
@Override @Override
public void setVisibility(int visibility) { public void setVisibility(int visibility) {
this.visibility = visibility; this.visibility = visibility;
handler.removeCallbacks(delayedShow); ApplicationEx.getMainHandler().removeCallbacks(delayedShow);
if (visibility == VISIBLE) { if (visibility == VISIBLE) {
super.setVisibility(INVISIBLE); super.setVisibility(INVISIBLE);
handler.postDelayed(delayedShow, VISIBILITY_DELAY); ApplicationEx.getMainHandler().postDelayed(delayedShow, VISIBILITY_DELAY);
} else } else
super.setVisibility(visibility); super.setVisibility(visibility);
} }

@ -28,7 +28,6 @@ import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -393,7 +392,7 @@ public class FragmentAccount extends FragmentBase {
int visibility = (grpAdvanced.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); int visibility = (grpAdvanced.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
grpAdvanced.setVisibility(visibility); grpAdvanced.setVisibility(visibility);
if (visibility == View.VISIBLE) if (visibility == View.VISIBLE)
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, btnAdvanced.getTop()); scroll.smoothScrollTo(0, btnAdvanced.getTop());
@ -750,7 +749,7 @@ public class FragmentAccount extends FragmentBase {
if (!cbTrust.isChecked()) if (!cbTrust.isChecked())
cbTrust.setVisibility(View.GONE); cbTrust.setVisibility(View.GONE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, cbIdentity.getBottom()); scroll.smoothScrollTo(0, cbIdentity.getBottom());
@ -1384,7 +1383,7 @@ public class FragmentAccount extends FragmentBase {
tvInstructions.setVisibility(View.VISIBLE); tvInstructions.setVisibility(View.VISIBLE);
} }
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (provider != null && provider.documentation != null) if (provider != null && provider.documentation != null)
@ -1593,7 +1592,7 @@ public class FragmentAccount extends FragmentBase {
}; };
// Load after provider has been selected // Load after provider has been selected
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
task.execute(FragmentAccount.this, args, "account:folders"); task.execute(FragmentAccount.this, args, "account:folders");
@ -1660,7 +1659,7 @@ public class FragmentAccount extends FragmentBase {
case REQUEST_SAVE: case REQUEST_SAVE:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
final boolean save = (btnSave.getVisibility() == View.VISIBLE); final boolean save = (btnSave.getVisibility() == View.VISIBLE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, (save ? btnSave : btnCheck).getBottom()); scroll.smoothScrollTo(0, (save ? btnSave : btnCheck).getBottom());

@ -32,6 +32,7 @@ import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -570,4 +571,8 @@ public class FragmentBase extends Fragment {
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
} }
Handler getMainHandler() {
return ApplicationEx.getMainHandler();
}
} }

@ -49,7 +49,6 @@ import android.net.NetworkRequest;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.provider.Settings; import android.provider.Settings;
@ -1417,7 +1416,7 @@ public class FragmentCompose extends FragmentBase {
private void onMenuAddresses() { private void onMenuAddresses() {
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (grpAddresses.getVisibility() == View.GONE) if (grpAddresses.getVisibility() == View.GONE)
@ -4102,8 +4101,7 @@ public class FragmentCompose extends FragmentBase {
EntityOperation.queue(context, draft, EntityOperation.MOVE, trash.id); EntityOperation.queue(context, draft, EntityOperation.MOVE, trash.id);
} }
Handler handler = new Handler(context.getMainLooper()); getMainHandler().post(new Runnable() {
handler.post(new Runnable() {
public void run() { public void run() {
ToastEx.makeText(context, R.string.title_draft_deleted, Toast.LENGTH_LONG).show(); ToastEx.makeText(context, R.string.title_draft_deleted, Toast.LENGTH_LONG).show();
} }
@ -4475,8 +4473,7 @@ public class FragmentCompose extends FragmentBase {
final String msg = context.getString(mid) + final String msg = context.getString(mid) +
(BuildConfig.DEBUG ? ":" + draft.revision : ""); (BuildConfig.DEBUG ? ":" + draft.revision : "");
Handler handler = new Handler(context.getMainLooper()); getMainHandler().post(new Runnable() {
handler.post(new Runnable() {
public void run() { public void run() {
ToastEx.makeText(context, msg, Toast.LENGTH_LONG).show(); ToastEx.makeText(context, msg, Toast.LENGTH_LONG).show();
} }
@ -4555,8 +4552,7 @@ public class FragmentCompose extends FragmentBase {
feedback = context.getString(R.string.title_queued_at, DTF.format(draft.ui_snoozed)); feedback = context.getString(R.string.title_queued_at, DTF.format(draft.ui_snoozed));
} }
Handler handler = new Handler(context.getMainLooper()); getMainHandler().post(new Runnable() {
handler.post(new Runnable() {
public void run() { public void run() {
ToastEx.makeText(context, feedback, Toast.LENGTH_LONG).show(); ToastEx.makeText(context, feedback, Toast.LENGTH_LONG).show();
} }
@ -4894,7 +4890,7 @@ public class FragmentCompose extends FragmentBase {
else else
target = etBody; target = etBody;
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
target.requestFocus(); target.requestFocus();

@ -24,7 +24,6 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -339,7 +338,7 @@ public class FragmentFolder extends FragmentBase {
break; break;
case REQUEST_SAVE_CHANGES: case REQUEST_SAVE_CHANGES:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, btnSave.getBottom()); scroll.smoothScrollTo(0, btnSave.getBottom());

@ -33,7 +33,6 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -235,7 +234,7 @@ public class FragmentGmail extends FragmentBase {
etName.setEnabled(granted); etName.setEnabled(granted);
btnSelect.setEnabled(granted); btnSelect.setEnabled(granted);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
etName.requestFocus(); etName.requestFocus();
@ -289,7 +288,7 @@ public class FragmentGmail extends FragmentBase {
tvError.setText(Log.formatThrowable(ex)); tvError.setText(Log.formatThrowable(ex));
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, tvError.getBottom()); scroll.smoothScrollTo(0, tvError.getBottom());
@ -490,7 +489,7 @@ public class FragmentGmail extends FragmentBase {
tvError.setText(Log.formatThrowable(ex)); tvError.setText(Log.formatThrowable(ex));
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, tvError.getBottom()); scroll.smoothScrollTo(0, tvError.getBottom());

@ -26,7 +26,6 @@ import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -361,7 +360,7 @@ public class FragmentIdentity extends FragmentBase {
grpAdvanced.setVisibility(visibility); grpAdvanced.setVisibility(visibility);
checkPassword(tilPassword.getEditText().getText().toString()); checkPassword(tilPassword.getEditText().getText().toString());
if (visibility == View.VISIBLE) if (visibility == View.VISIBLE)
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, btnAdvanced.getTop()); scroll.smoothScrollTo(0, btnAdvanced.getTop());
@ -1042,7 +1041,7 @@ public class FragmentIdentity extends FragmentBase {
tvInstructions.setVisibility(View.VISIBLE); tvInstructions.setVisibility(View.VISIBLE);
} }
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (provider != null && provider.documentation != null) if (provider != null && provider.documentation != null)
@ -1321,7 +1320,7 @@ public class FragmentIdentity extends FragmentBase {
break; break;
case REQUEST_SAVE: case REQUEST_SAVE:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, btnSave.getBottom()); scroll.smoothScrollTo(0, btnSave.getBottom());

@ -46,7 +46,6 @@ import android.net.NetworkRequest;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel; import android.os.Parcel;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.Parcelable; import android.os.Parcelable;
@ -1689,7 +1688,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
@Override @Override
public void scrollTo(final int pos, final int y) { public void scrollTo(final int pos, final int y) {
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
LinearLayoutManager llm = (LinearLayoutManager) rvMessage.getLayoutManager(); LinearLayoutManager llm = (LinearLayoutManager) rvMessage.getLayoutManager();
@ -1702,7 +1701,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
@Override @Override
public void scrollBy(int x, int y) { public void scrollBy(int x, int y) {
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
rvMessage.scrollBy(x, y); rvMessage.scrollBy(x, y);
@ -1778,8 +1777,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}; };
private ItemTouchHelper.Callback touchHelper = new ItemTouchHelper.Callback() { private ItemTouchHelper.Callback touchHelper = new ItemTouchHelper.Callback() {
private Handler handler = new Handler();
private Runnable enableSelection = new Runnable() { private Runnable enableSelection = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -1841,11 +1838,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (selectionPredicate != null) { if (selectionPredicate != null) {
handler.removeCallbacks(enableSelection); getMainHandler().removeCallbacks(enableSelection);
if (isCurrentlyActive) if (isCurrentlyActive)
selectionPredicate.setEnabled(false); selectionPredicate.setEnabled(false);
else else
handler.postDelayed(enableSelection, SWIPE_DISABLE_SELECT_DURATION); getMainHandler().postDelayed(enableSelection, SWIPE_DISABLE_SELECT_DURATION);
} }
int pos = viewHolder.getAdapterPosition(); int pos = viewHolder.getAdapterPosition();
@ -4147,7 +4144,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
model.getIds(getContext(), getViewLifecycleOwner(), new Observer<List<Long>>() { model.getIds(getContext(), getViewLifecycleOwner(), new Observer<List<Long>>() {
@Override @Override
public void onChanged(List<Long> ids) { public void onChanged(List<Long> ids) {
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
selectionTracker.clearSelection(); selectionTracker.clearSelection();

@ -28,7 +28,6 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.Pair; import android.util.Pair;
@ -641,7 +640,7 @@ public class FragmentOAuth extends FragmentBase {
btnOAuth.setEnabled(true); btnOAuth.setEnabled(true);
pbOAuth.setVisibility(View.GONE); pbOAuth.setVisibility(View.GONE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, tvError.getBottom()); scroll.smoothScrollTo(0, tvError.getBottom());

@ -27,7 +27,6 @@ import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -445,14 +444,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
} }
}); });
new Handler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
etPin.requestFocus();
}
});
new Handler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
etPin.requestFocus(); etPin.requestFocus();

@ -26,7 +26,6 @@ import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -549,7 +548,7 @@ public class FragmentPop extends FragmentBase {
tvError.setText(Log.formatThrowable(ex, false)); tvError.setText(Log.formatThrowable(ex, false));
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, tvError.getBottom()); scroll.smoothScrollTo(0, tvError.getBottom());

@ -25,7 +25,6 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -486,7 +485,7 @@ public class FragmentQuickSetup extends FragmentBase {
tvError.setText(ex.getMessage()); tvError.setText(ex.getMessage());
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, tvErrorHint.getBottom()); scroll.smoothScrollTo(0, tvErrorHint.getBottom());
@ -509,7 +508,7 @@ public class FragmentQuickSetup extends FragmentBase {
tvInstructions.setVisibility(View.VISIBLE); tvInstructions.setVisibility(View.VISIBLE);
} }
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (args.containsKey("documentation")) if (args.containsKey("documentation"))

@ -27,7 +27,6 @@ import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech;
import android.text.TextUtils; import android.text.TextUtils;
@ -419,7 +418,7 @@ public class FragmentRule extends FragmentBase {
private void onActionSelected(int type) { private void onActionSelected(int type) {
showActionParameters(type); showActionParameters(type);
new Handler().post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
scroll.smoothScrollTo(0, content.getBottom()); scroll.smoothScrollTo(0, content.getBottom());

@ -40,9 +40,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.LocaleList; import android.os.LocaleList;
import android.os.Looper;
import android.os.Parcel; import android.os.Parcel;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.StatFs; import android.os.StatFs;
@ -1258,8 +1256,6 @@ public class Helper {
static void authenticate(final FragmentActivity activity, final LifecycleOwner owner, static void authenticate(final FragmentActivity activity, final LifecycleOwner owner,
Boolean enabled, final Boolean enabled, final
Runnable authenticated, final Runnable cancelled) { Runnable authenticated, final Runnable cancelled) {
final Handler handler = new Handler();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
String pin = prefs.getString("pin", null); String pin = prefs.getString("pin", null);
@ -1289,7 +1285,7 @@ public class Helper {
if (errorCode != BiometricPrompt.ERROR_NEGATIVE_BUTTON && if (errorCode != BiometricPrompt.ERROR_NEGATIVE_BUTTON &&
errorCode != BiometricPrompt.ERROR_CANCELED && errorCode != BiometricPrompt.ERROR_CANCELED &&
errorCode != BiometricPrompt.ERROR_USER_CANCELED) errorCode != BiometricPrompt.ERROR_USER_CANCELED)
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
ToastEx.makeText(activity, ToastEx.makeText(activity,
@ -1298,20 +1294,20 @@ public class Helper {
} }
}); });
handler.post(cancelled); ApplicationEx.getMainHandler().post(cancelled);
} }
@Override @Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
Log.i("Biometric succeeded"); Log.i("Biometric succeeded");
setAuthenticated(activity); setAuthenticated(activity);
handler.post(authenticated); ApplicationEx.getMainHandler().post(authenticated);
} }
@Override @Override
public void onAuthenticationFailed() { public void onAuthenticationFailed() {
Log.w("Biometric failed"); Log.w("Biometric failed");
handler.post(cancelled); ApplicationEx.getMainHandler().post(cancelled);
} }
}); });
@ -1328,12 +1324,12 @@ public class Helper {
} }
}; };
handler.postDelayed(cancelPrompt, 60 * 1000L); ApplicationEx.getMainHandler().postDelayed(cancelPrompt, 60 * 1000L);
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() { public void onDestroy() {
handler.post(cancelPrompt); ApplicationEx.getMainHandler().post(cancelPrompt);
} }
}); });
@ -1352,21 +1348,21 @@ public class Helper {
if (pin.equals(entered)) { if (pin.equals(entered)) {
setAuthenticated(activity); setAuthenticated(activity);
handler.post(authenticated); ApplicationEx.getMainHandler().post(authenticated);
} else } else
handler.post(cancelled); ApplicationEx.getMainHandler().post(cancelled);
} }
}) })
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
handler.post(cancelled); ApplicationEx.getMainHandler().post(cancelled);
} }
}) })
.setOnDismissListener(new DialogInterface.OnDismissListener() { .setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
handler.post(cancelled); ApplicationEx.getMainHandler().post(cancelled);
} }
}) })
.create(); .create();
@ -1390,7 +1386,7 @@ public class Helper {
} }
}); });
new Handler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
etPin.requestFocus(); etPin.requestFocus();
@ -1413,8 +1409,6 @@ public class Helper {
static void selectKeyAlias(final Activity activity, final LifecycleOwner owner, final String alias, final IKeyAlias intf) { static void selectKeyAlias(final Activity activity, final LifecycleOwner owner, final String alias, final IKeyAlias intf) {
final Context context = activity.getApplicationContext(); final Context context = activity.getApplicationContext();
final Handler handler = new Handler(Looper.getMainLooper());
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -1431,7 +1425,7 @@ public class Helper {
Log.e(ex); Log.e(ex);
} }
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
KeyChain.choosePrivateKeyAlias(activity, new KeyChainAliasCallback() { KeyChain.choosePrivateKeyAlias(activity, new KeyChainAliasCallback() {
@ -1447,7 +1441,7 @@ public class Helper {
} }
private void deliver(final String selected) { private void deliver(final String selected) {
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import androidx.lifecycle.LifecycleService; import androidx.lifecycle.LifecycleService;
@ -62,4 +63,8 @@ abstract class ServiceBase extends LifecycleService {
super.onDestroy(); super.onDestroy();
} }
Handler getMainHandler() {
return ApplicationEx.getMainHandler();
}
} }

@ -31,7 +31,6 @@ import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -70,7 +69,6 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
private Network lastActive = null; private Network lastActive = null;
private boolean lastSuitable = false; private boolean lastSuitable = false;
private Handler handler;
private PowerManager.WakeLock wlOutbox; private PowerManager.WakeLock wlOutbox;
private TwoStateOwner owner = new TwoStateOwner("send"); private TwoStateOwner owner = new TwoStateOwner("send");
private List<Long> handling = new ArrayList<>(); private List<Long> handling = new ArrayList<>();
@ -88,8 +86,6 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
super.onCreate(); super.onCreate();
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build()); startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
handler = new Handler();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send"); wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
@ -175,7 +171,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback); cm.unregisterNetworkCallback(networkCallback);
handler.removeCallbacks(_checkConnectivity); getMainHandler().removeCallbacks(_checkConnectivity);
owner.stop(); owner.stop();
handling.clear(); handling.clear();
@ -284,7 +280,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
}; };
private void checkConnectivity() { private void checkConnectivity() {
handler.postDelayed(_checkConnectivity, CONNECTIVITY_DELAY); getMainHandler().postDelayed(_checkConnectivity, CONNECTIVITY_DELAY);
} }
private Runnable _checkConnectivity = new Runnable() { private Runnable _checkConnectivity = new Runnable() {

@ -35,7 +35,6 @@ import android.net.NetworkInfo;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.text.TextUtils; import android.text.TextUtils;
@ -901,7 +900,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
state.reset(); state.reset();
Log.i(account.name + " run thread=" + currentThread); Log.i(account.name + " run thread=" + currentThread);
Handler handler = new Handler(getMainLooper());
final List<TwoStateOwner> cowners = new ArrayList<>(); final List<TwoStateOwner> cowners = new ArrayList<>();
// Debug // Debug
@ -1210,7 +1208,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
mapFolders.put(folder, null); mapFolders.put(folder, null);
Log.d(folder.name + " observing"); Log.d(folder.name + " observing");
handler.post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name); TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
@ -1576,7 +1574,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.account().setAccountState(account.id, "closing"); db.account().setAccountState(account.id, "closing");
// Stop watching for operations // Stop watching for operations
handler.post(new Runnable() { getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
for (TwoStateOwner owner : cowners) for (TwoStateOwner owner : cowners)

@ -23,8 +23,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -94,8 +92,6 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) { private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) {
final Handler handler = new Handler(Looper.getMainLooper());
this.name = name; this.name = name;
if (owner instanceof TwoStateOwner) if (owner instanceof TwoStateOwner)
@ -148,7 +144,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
// Run on UI thread // Run on UI thread
handler.post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
Lifecycle.State state = owner.getLifecycle().getCurrentState(); Lifecycle.State state = owner.getLifecycle().getCurrentState();

Loading…
Cancel
Save