Use runOnUiThread to show UI toasts

This commit is contained in:
Herbert Reiter
2020-12-28 11:54:46 +01:00
parent 6d6c7577da
commit 203d225955
2 changed files with 12 additions and 10 deletions
@@ -286,8 +286,7 @@ public class CalendarSyncAdapter extends AbstractThreadedSyncAdapter {
if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_CALENDAR)) {
Log.d(TAG, "User has permanently denied permission WRITE_CALENDAR, informing him");
String hint = context.getString(R.string.calendar_permission_request, "WRITE_CALENDAR");
Toast toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
toast.show();
activity.runOnUiThread(() -> Toast.makeText(context, hint, Toast.LENGTH_SHORT).show());
}
Log.d(TAG, "Ask for permission WRITE_CALENDAR");
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_CALENDAR}, 0);
@@ -297,8 +296,7 @@ public class CalendarSyncAdapter extends AbstractThreadedSyncAdapter {
if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_CALENDAR)) {
Log.d(TAG, "User has permanently denied permission READ_CALENDAR, informing him");
String hint = context.getString(R.string.calendar_permission_request, "READ_CALENDAR");
Toast toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
toast.show();
activity.runOnUiThread(() -> Toast.makeText(context, hint, Toast.LENGTH_SHORT).show());
}
Log.d(TAG, "Ask for permission READ_CALENDAR");
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CALENDAR}, 0);
@@ -358,26 +358,30 @@ public class MainActivity extends AppCompatActivity {
private void runSynchronizationWithServer() {
int filesCount;
try {
filesCount = synchronizeWikiClient.synchronizeRepository(this::showProgressBar);
filesCount = synchronizeWikiClient.synchronizeRepository(this::syncProgress);
} catch (ServiceException e) {
Log.e(TAG, "Error synchronizing repository with server", e);
showToast(getString(R.string.settings_synchronize_failed));
runOnUiThread(() -> showToast(getString(R.string.settings_synchronize_failed)));
return;
}
if (filesCount > 0) {
showToast(getString(R.string.settings_synchronize_successful, filesCount));
runOnUiThread(() -> showToast(getString(R.string.settings_synchronize_successful, filesCount)));
WikiEngineApplication app = (WikiEngineApplication) getApplication();
app.resetServices();
CalendarSyncAdapter.requestCalendarSync(this);
} else {
showToast(getString(R.string.settings_synchronize_not_necessary));
runOnUiThread(() -> showToast(getString(R.string.settings_synchronize_not_necessary)));
}
hideProgressBar();
updateLayoutVisibility();
runOnUiThread(this::hideProgressBar);
runOnUiThread(this::updateLayoutVisibility);
}
private void syncProgress(int progress, int max) {
runOnUiThread(() -> showProgressBar(progress, max));
}
/**