Commit 5804a7fb authored by Thomas's avatar Thomas
Browse files

Fix null pointer when reference to account is destroyed

parent a6d015f5
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import static app.fedilab.android.BaseMainActivity.status.DISCONNECTED;
import static app.fedilab.android.BaseMainActivity.status.UNKNOWN;
import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN;
import static app.fedilab.android.helper.Helper.deleteDir;
import static app.fedilab.android.helper.Helper.getCurrentAccount;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
@@ -73,7 +74,6 @@ import com.google.android.material.snackbar.Snackbar;
import com.jaredrummler.cyanea.Cyanea;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
@@ -135,7 +135,6 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
    public static HashMap<String, List<Emoji>> emojis = new HashMap<>();
    public static Account.API api;
    public static boolean admin;
    public static WeakReference<Account> accountWeakReference;
    public static status networkAvailable = UNKNOWN;
    public static Instance instanceInfo;
    public static List<Filter> mainFilters;
@@ -654,7 +653,6 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
                regex_local = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_LOCAL) + currentUserID + currentInstance, null);
                regex_public = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_PUBLIC) + currentUserID + currentInstance, null);
                show_art_nsfw = sharedpreferences.getBoolean(getString(R.string.SET_ART_WITH_NSFW) + currentUserID + currentInstance, false);
                accountWeakReference = new WeakReference<>(account);
                binding.profilePicture.setOnClickListener(v -> binding.drawerLayout.openDrawer(GravityCompat.START));
                Helper.loadPP(binding.profilePicture, account);
                headerMainBinding.accountAcc.setText(String.format("%s@%s", account.mastodon_account.username, account.instance));
@@ -682,8 +680,18 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
                        .observe(BaseMainActivity.this, filters -> mainFilters = filters);
                new ViewModelProvider(BaseMainActivity.this).get(AccountsVM.class).getConnectedAccount(currentInstance, currentToken)
                        .observe(BaseMainActivity.this, account1 -> {

                            BaseMainActivity.accountWeakReference.get().mastodon_account = account1;
                            //Initialize static var
                            getCurrentAccount(BaseMainActivity.this);
                            //Set the Mastodon account
                            Helper.setMastodonAccount(account1);
                            new Thread(() -> {
                                try {
                                    //Update account in db
                                    new Account(BaseMainActivity.this).insertOrUpdate(getCurrentAccount(BaseMainActivity.this));
                                } catch (DBException e) {
                                    e.printStackTrace();
                                }
                            }).start();
                        });
                //Update pinned timelines
                new ViewModelProvider(BaseMainActivity.this).get(TopBarVM.class).getDBPinned()
+4 −3
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
            statusDraft.statusDraftList = statuses;
        }
        if (account == null) {
            account = BaseMainActivity.accountWeakReference.get();
            account = Helper.getCurrentAccount(ComposeActivity.this);
        }
        if (account == null) {
            Toasty.error(ComposeActivity.this, getString(R.string.toast_error), Toasty.LENGTH_SHORT).show();
@@ -267,7 +267,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
                    //We change order for mentions
                    //At first place the account that has been mentioned if it's not our
                    statusDraftList.get(0).mentions = new ArrayList<>();
                    if (!statusReply.account.acct.equalsIgnoreCase(MainActivity.accountWeakReference.get().mastodon_account.acct)) {
                    if (!statusReply.account.acct.equalsIgnoreCase(Helper.getCurrentAccount(ComposeActivity.this).mastodon_account.acct)) {
                        Mention mention = new Mention();
                        mention.acct = "@" + statusReply.account.acct;
                        mention.url = statusReply.account.url;
@@ -278,7 +278,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
                    //There are other mentions to
                    if (statusReply.mentions != null && statusReply.mentions.size() > 0) {
                        for (Mention mentionTmp : statusReply.mentions) {
                            if (!mentionTmp.acct.equalsIgnoreCase(statusReply.account.acct) && !mentionTmp.acct.equalsIgnoreCase(MainActivity.accountWeakReference.get().mastodon_account.acct)) {
                            if (!mentionTmp.acct.equalsIgnoreCase(statusReply.account.acct) && !mentionTmp.acct.equalsIgnoreCase(Helper.getCurrentAccount(ComposeActivity.this).mastodon_account.acct)) {
                                statusDraftList.get(0).mentions.add(mentionTmp);
                            }
                        }
@@ -292,6 +292,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
                        for (Mention mentionTmp : statusDraftList.get(0).mentions) {
                            if (mentionTmp.acct.equalsIgnoreCase(mentionBooster.acct)) {
                                present = true;
                                break;
                            }
                        }
                        if (!present) {
+2 −5
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.PreferenceManager;

import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R;
import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.client.entities.app.QuickLoad;
@@ -85,9 +84,7 @@ public class ContextActivity extends BaseActivity {
            finish();
            return;
        }
        if (BaseMainActivity.accountWeakReference.get() != null) {
            MastodonHelper.loadPPMastodon(binding.profilePicture, BaseMainActivity.accountWeakReference.get().mastodon_account);
        }
        MastodonHelper.loadPPMastodon(binding.profilePicture, Helper.getCurrentAccount(ContextActivity.this).mastodon_account);
        Bundle bundle = new Bundle();
        new Thread(() -> {
            focusedStatus = SpannableHelper.convertStatus(getApplication().getApplicationContext(), focusedStatus);
@@ -109,7 +106,7 @@ public class ContextActivity extends BaseActivity {
            new Thread(() -> {
                try {
                    new StatusCache(getApplication()).updateIfExists(statusCache);
                    new QuickLoad(getApplication().getApplicationContext()).updateStatus(MainActivity.accountWeakReference.get(), status);
                    new QuickLoad(getApplication().getApplicationContext()).updateStatus(Helper.getCurrentAccount(ContextActivity.this), status);
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    //Update UI
                    Runnable myRunnable = () -> sendAction(ContextActivity.this, Helper.ARG_STATUS_ACTION, status, null);
+1 −2
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import androidx.preference.PreferenceManager;
import java.util.List;
import java.util.Set;

import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R;
import app.fedilab.android.client.entities.api.Attachment;
import app.fedilab.android.client.entities.api.Emoji;
@@ -100,7 +99,7 @@ public class CustomSharingActivity extends BaseActivity implements OnCustomShari
            bundle_thumbnailurl = status.account.avatar;
        }
        if (!bundle_creator.contains("@")) {
            bundle_creator = bundle_creator + "@" + BaseMainActivity.accountWeakReference.get().instance;
            bundle_creator = bundle_creator + "@" + Helper.getCurrentAccount(CustomSharingActivity.this).instance;
        }

        binding.setCustomSharingTitle.setEllipsize(TextUtils.TruncateAt.END);
+2 −3
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;

import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R;
import app.fedilab.android.client.entities.api.Attachment;
import app.fedilab.android.client.entities.api.Status;
@@ -81,7 +80,7 @@ public class DraftActivity extends BaseActivity implements StatusDraftAdapter.Dr
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        timelinesVM = new ViewModelProvider(DraftActivity.this).get(TimelinesVM.class);
        timelinesVM.getDrafts(BaseMainActivity.accountWeakReference.get())
        timelinesVM.getDrafts(Helper.getCurrentAccount(DraftActivity.this))
                .observe(DraftActivity.this, this::initializeDraftView);
    }

@@ -177,7 +176,7 @@ public class DraftActivity extends BaseActivity implements StatusDraftAdapter.Dr
        super.onResume();
        //We need to check if drafts changed (ie when coming back from the compose activity)
        if (statusDrafts != null && timelinesVM != null) {
            timelinesVM.getDrafts(BaseMainActivity.accountWeakReference.get())
            timelinesVM.getDrafts(Helper.getCurrentAccount(DraftActivity.this))
                    .observe(DraftActivity.this, this::updateDrafts);
        }
    }
Loading