From 0e0fb6e13c964d0202c31cef3c8fd8b770385a88 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 23 Nov 2022 10:52:09 +0100 Subject: [PATCH] Fix crashes from bug reports. --- .../app/fedilab/android/BaseMainActivity.java | 35 +++++++++++++------ .../android/activities/FilterActivity.java | 4 +++ .../app/fedilab/android/helper/Helper.java | 2 +- .../android/ui/drawer/FilterAdapter.java | 4 +-- .../android/ui/drawer/StatusAdapter.java | 18 ++++++++-- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 3bc50a74..a353434e 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -586,7 +586,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { - if (currentAccount == null) { + if (currentAccount == null || currentAccount.mastodon_account == null) { //It is not, the user is redirected to the login page Intent myIntent = new Intent(BaseMainActivity.this, LoginActivity.class); startActivity(myIntent); @@ -701,16 +701,18 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt new ViewModelProvider(BaseMainActivity.this).get(AccountsVM.class).getConnectedAccount(currentInstance, currentToken) .observe(BaseMainActivity.this, mastodonAccount -> { //Initialize static var - currentAccount.mastodon_account = mastodonAccount; - displayReleaseNotesIfNeeded(BaseMainActivity.this, false); - new Thread(() -> { - try { - //Update account in db - new Account(BaseMainActivity.this).insertOrUpdate(currentAccount); - } catch (DBException e) { - e.printStackTrace(); - } - }).start(); + if (mastodonAccount != null) { + currentAccount.mastodon_account = mastodonAccount; + displayReleaseNotesIfNeeded(BaseMainActivity.this, false); + new Thread(() -> { + try { + //Update account in db + new Account(BaseMainActivity.this).insertOrUpdate(currentAccount); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + } }); }; @@ -1035,6 +1037,17 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt Matcher matcherLink = null; matcherLink = link.matcher(url); if (matcherLink.find()) { + if (currentAccount == null) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(BaseMainActivity.this); + if (currentToken == null || currentToken.trim().isEmpty()) { + currentToken = sharedpreferences.getString(Helper.PREF_USER_TOKEN, null); + } + try { + currentAccount = new Account(BaseMainActivity.this).getConnectedAccount(); + } catch (DBException e) { + e.printStackTrace(); + } + } if (matcherLink.group(3) != null && Objects.requireNonNull(matcherLink.group(3)).length() > 0) { //It's a toot CrossActionHelper.fetchRemoteStatus(BaseMainActivity.this, currentAccount, url, new CrossActionHelper.Callback() { @Override diff --git a/app/src/main/java/app/fedilab/android/activities/FilterActivity.java b/app/src/main/java/app/fedilab/android/activities/FilterActivity.java index 474d89a7..ed8d616a 100644 --- a/app/src/main/java/app/fedilab/android/activities/FilterActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/FilterActivity.java @@ -277,7 +277,11 @@ public class FilterActivity extends BaseActivity implements FilterAdapter.Delete binding.addFilter.setOnClickListener(v -> addEditFilter(FilterActivity.this, null, filter -> { if (filter != null) { + if (MainActivity.mainFilters == null) { + MainActivity.mainFilters = new ArrayList<>(); + } filterList.add(0, filter); + MainActivity.mainFilters.add(filter); if (filterAdapter != null) { filterAdapter.notifyItemInserted(0); } else { diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 7e45b21f..aee215dd 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -1029,7 +1029,7 @@ public class Helper { final Activity activity = (Activity) context; return !activity.isDestroyed() && !activity.isFinishing(); } - return true; + return false; } /** diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/FilterAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/FilterAdapter.java index 88c59199..954369d4 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/FilterAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/FilterAdapter.java @@ -78,14 +78,14 @@ public class FilterAdapter extends RecyclerView.Adapter FilterActivity.addEditFilter(context, filter, filter1 -> { - if (filter1 != null) { + if (filter1 != null && BaseMainActivity.mainFilters.size() > position) { BaseMainActivity.mainFilters.get(position).context = filter1.context; BaseMainActivity.mainFilters.get(position).expires_at = filter1.expires_at; BaseMainActivity.mainFilters.get(position).filter_action = filter1.filter_action; BaseMainActivity.mainFilters.get(position).keywords = filter1.keywords; BaseMainActivity.mainFilters.get(position).title = filter1.title; + filterAdapter.notifyItemChanged(position); } - filterAdapter.notifyItemChanged(position); })); holder.binding.deleteFilter.setOnClickListener(v -> { AlertDialog.Builder builder = new AlertDialog.Builder(context, Helper.dialogStyle()); diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index acfd5c03..ddba4f6f 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -1611,7 +1611,11 @@ public class StatusAdapter extends RecyclerView.Adapter .observe((LifecycleOwner) context, poll -> { int i = 0; for (Poll.PollItem item : statusToDeal.poll.options) { - poll.options.get(i).span_title = item.span_title; + if (item.span_title != null) { + poll.options.get(i).span_title = item.span_title; + } else { + poll.options.get(i).span_title = new SpannableString(item.title); + } i++; } statusToDeal.poll = poll; @@ -1627,7 +1631,11 @@ public class StatusAdapter extends RecyclerView.Adapter if (poll != null) { int i = 0; for (Poll.PollItem item : statusToDeal.poll.options) { - poll.options.get(i).span_title = item.span_title; + if (item.span_title != null) { + poll.options.get(i).span_title = item.span_title; + } else { + poll.options.get(i).span_title = new SpannableString(item.title); + } i++; } statusToDeal.poll = poll; @@ -1642,7 +1650,11 @@ public class StatusAdapter extends RecyclerView.Adapter //Store span elements int i = 0; for (Poll.PollItem item : statusToDeal.poll.options) { - poll.options.get(i).span_title = item.span_title; + if (item.span_title != null) { + poll.options.get(i).span_title = item.span_title; + } else { + poll.options.get(i).span_title = new SpannableString(item.title); + } i++; } statusToDeal.poll = poll;