From 64a6edd3fb42e51db54a68f098004b20d665efc7 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 21 Sep 2021 11:41:35 +0200 Subject: [PATCH] Revert "Migrate Fragments to AndroidX" This reverts commit 231d97ea81bb2249afa432b43d78ac963b80594a. Migrating to AndroidX Fragments seems to have some unforseen side effects and no clear benefits --- .../ui/ConversationFragment.java | 43 +++++++-------- .../ui/ConversationsActivity.java | 54 +++++++++---------- .../ui/ConversationsOverviewFragment.java | 15 +++--- .../siacs/conversations/ui/XmppFragment.java | 3 +- .../ui/adapter/ConversationAdapter.java | 2 +- .../ui/adapter/MessageAdapter.java | 4 +- .../ui/util/MucDetailsContextMenuHelper.java | 2 +- 7 files changed, 61 insertions(+), 62 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index bf9aa93d3..5bc24fee3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -3,6 +3,8 @@ package eu.siacs.conversations.ui; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.Fragment; +import android.app.FragmentManager; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; @@ -50,8 +52,6 @@ import androidx.appcompat.app.AlertDialog; import androidx.core.view.inputmethod.InputConnectionCompat; import androidx.core.view.inputmethod.InputContentInfoCompat; import androidx.databinding.DataBindingUtil; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; import com.google.common.base.Optional; @@ -467,41 +467,41 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private boolean firstWord = false; private Message mPendingDownloadableMessage; - private static ConversationFragment findConversationFragment(FragmentManager fragmentManager) { - Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); + private static ConversationFragment findConversationFragment(Activity activity) { + Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; } - fragment = fragmentManager.findFragmentById(R.id.secondary_fragment); + fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; } return null; } - public static void startStopPending(FragmentManager fragmentManager) { - ConversationFragment fragment = findConversationFragment(fragmentManager); + public static void startStopPending(Activity activity) { + ConversationFragment fragment = findConversationFragment(activity); if (fragment != null) { fragment.messageListAdapter.startStopPending(); } } - public static void downloadFile(FragmentManager fragmentManager, Message message) { - ConversationFragment fragment = findConversationFragment(fragmentManager); + public static void downloadFile(Activity activity, Message message) { + ConversationFragment fragment = findConversationFragment(activity); if (fragment != null) { fragment.startDownloadable(message); } } - public static void registerPendingMessage(FragmentManager fragmentManager, Message message) { - ConversationFragment fragment = findConversationFragment(fragmentManager); + public static void registerPendingMessage(Activity activity, Message message) { + ConversationFragment fragment = findConversationFragment(activity); if (fragment != null) { fragment.pendingMessage.push(message); } } - public static void openPendingMessage(FragmentManager fragmentManager) { - ConversationFragment fragment = findConversationFragment(fragmentManager); + public static void openPendingMessage(Activity activity) { + ConversationFragment fragment = findConversationFragment(activity); if (fragment != null) { Message message = fragment.pendingMessage.pop(); if (message != null) { @@ -510,12 +510,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static Conversation getConversation(FragmentManager fragmentManager) { - return getConversation(fragmentManager, R.id.secondary_fragment); + public static Conversation getConversation(Activity activity) { + return getConversation(activity, R.id.secondary_fragment); } - private static Conversation getConversation(FragmentManager fragmentManager, @IdRes int res) { - final Fragment fragment = fragmentManager.findFragmentById(res); + private static Conversation getConversation(Activity activity, @IdRes int res) { + final Fragment fragment = activity.getFragmentManager().findFragmentById(res); if (fragment instanceof ConversationFragment) { return ((ConversationFragment) fragment).getConversation(); } else { @@ -523,7 +523,8 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static ConversationFragment get(FragmentManager fragmentManager) { + public static ConversationFragment get(Activity activity) { + FragmentManager fragmentManager = activity.getFragmentManager(); Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; @@ -533,12 +534,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static Conversation getConversationReliable(FragmentManager fragmentManager) { - final Conversation conversation = getConversation(fragmentManager, R.id.secondary_fragment); + public static Conversation getConversationReliable(Activity activity) { + final Conversation conversation = getConversation(activity, R.id.secondary_fragment); if (conversation != null) { return conversation; } - return getConversation(fragmentManager, R.id.main_fragment); + return getConversation(activity, R.id.main_fragment); } private static boolean scrolledToBottom(AbsListView listView) { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index 21032ea04..fbdba5724 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -34,6 +34,9 @@ import static eu.siacs.conversations.ui.ConversationFragment.REQUEST_DECRYPT_PGP import android.annotation.SuppressLint; import android.app.Activity; +import android.app.Fragment; +import android.app.FragmentManager; +import android.app.FragmentTransaction; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; @@ -52,9 +55,6 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.databinding.DataBindingUtil; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; import org.openintents.openpgp.util.OpenPgpApi; @@ -165,8 +165,8 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } invalidateActionBarTitle(); - if (binding.secondaryFragment != null && ConversationFragment.getConversation(getSupportFragmentManager()) == null) { - Conversation conversation = ConversationsOverviewFragment.getSuggestion(getSupportFragmentManager()); + if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) { + Conversation conversation = ConversationsOverviewFragment.getSuggestion(this); if (conversation != null) { openConversation(conversation, null); } @@ -202,7 +202,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (xmppConnectionService == null) { return; } - final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_fragment); + final Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationsOverviewFragment) { if (ExceptionHelper.checkForCrash(this)) { return; @@ -255,14 +255,14 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void notifyFragmentOfBackendConnected(@IdRes int id) { - final Fragment fragment = getSupportFragmentManager().findFragmentById(id); + final Fragment fragment = getFragmentManager().findFragmentById(id); if (fragment instanceof OnBackendConnected) { ((OnBackendConnected) fragment).onBackendConnected(); } } private void refreshFragment(@IdRes int id) { - final Fragment fragment = getSupportFragmentManager().findFragmentById(id); + final Fragment fragment = getFragmentManager().findFragmentById(id); if (fragment instanceof XmppFragment) { ((XmppFragment) fragment).refresh(); } @@ -288,10 +288,10 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio switch (requestCode) { case REQUEST_OPEN_MESSAGE: refreshUiReal(); - ConversationFragment.openPendingMessage(getSupportFragmentManager()); + ConversationFragment.openPendingMessage(this); break; case REQUEST_PLAY_PAUSE: - ConversationFragment.startStopPending(getSupportFragmentManager()); + ConversationFragment.startStopPending(this); break; } } @@ -318,7 +318,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void handleNegativeActivityResult(int requestCode) { - Conversation conversation = ConversationFragment.getConversationReliable(getSupportFragmentManager()); + Conversation conversation = ConversationFragment.getConversationReliable(this); switch (requestCode) { case REQUEST_DECRYPT_PGP: if (conversation == null) { @@ -333,7 +333,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void handlePositiveActivityResult(int requestCode, final Intent data) { - Conversation conversation = ConversationFragment.getConversationReliable(getSupportFragmentManager()); + Conversation conversation = ConversationFragment.getConversationReliable(this); if (conversation == null) { Log.d(Config.LOGTAG, "conversation not found"); return; @@ -365,8 +365,8 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations); setSupportActionBar(binding.toolbar); configureActionBar(getSupportActionBar()); - this.getSupportFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle); - this.getSupportFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview); + this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle); + this.getFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview); this.initializeFragments(); this.invalidateActionBarTitle(); final Intent intent; @@ -387,7 +387,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio final MenuItem qrCodeScanMenuItem = menu.findItem(R.id.action_scan_qr_code); if (qrCodeScanMenuItem != null) { if (isCameraFeatureAvailable()) { - Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_fragment); + Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment); boolean visible = getResources().getBoolean(R.bool.show_qr_code_scan) && fragment instanceof ConversationsOverviewFragment; qrCodeScanMenuItem.setVisible(visible); @@ -401,7 +401,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override public void onConversationSelected(Conversation conversation) { clearPendingViewIntent(); - if (ConversationFragment.getConversation(getSupportFragmentManager()) == conversation) { + if (ConversationFragment.getConversation(this) == conversation) { Log.d(Config.LOGTAG, "ignore onConversationSelected() because conversation is already open"); return; } @@ -429,7 +429,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void openConversation(Conversation conversation, Bundle extras) { - final FragmentManager fragmentManager = getSupportFragmentManager(); + final FragmentManager fragmentManager = getFragmentManager(); executePendingTransactions(fragmentManager); ConversationFragment conversationFragment = (ConversationFragment) fragmentManager.findFragmentById(R.id.secondary_fragment); final boolean mainNeedsRefresh; @@ -489,7 +489,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } switch (item.getItemId()) { case android.R.id.home: - FragmentManager fm = getSupportFragmentManager(); + FragmentManager fm = getFragmentManager(); if (fm.getBackStackEntryCount() > 0) { try { fm.popBackStack(); @@ -506,7 +506,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio startActivity(new Intent(this, SearchActivity.class)); return true; case R.id.action_search_this_conversation: - final Conversation conversation = ConversationFragment.getConversation(getSupportFragmentManager()); + final Conversation conversation = ConversationFragment.getConversation(this); if (conversation == null) { return true; } @@ -521,7 +521,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP && keyEvent.isCtrlPressed()) { - final ConversationFragment conversationFragment = ConversationFragment.get(getSupportFragmentManager()); + final ConversationFragment conversationFragment = ConversationFragment.get(this); if (conversationFragment != null && conversationFragment.onArrowUpCtrlPressed()) { return true; } @@ -576,14 +576,14 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void initializeFragments() { - final FragmentManager fragmentManager = getSupportFragmentManager(); + final FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); final Fragment secondaryFragment = fragmentManager.findFragmentById(R.id.secondary_fragment); if (mainFragment != null) { if (binding.secondaryFragment != null) { if (mainFragment instanceof ConversationFragment) { - getSupportFragmentManager().popBackStack(); + getFragmentManager().popBackStack(); transaction.remove(mainFragment); transaction.commit(); fragmentManager.executePendingTransactions(); @@ -597,7 +597,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (secondaryFragment instanceof ConversationFragment) { transaction.remove(secondaryFragment); transaction.commit(); - getSupportFragmentManager().executePendingTransactions(); + getFragmentManager().executePendingTransactions(); transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.main_fragment, secondaryFragment); transaction.addToBackStack(null); @@ -619,7 +619,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (actionBar == null) { return; } - final FragmentManager fragmentManager = getSupportFragmentManager(); + final FragmentManager fragmentManager = getFragmentManager(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); if (mainFragment instanceof ConversationFragment) { final Conversation conversation = ((ConversationFragment) mainFragment).getConversation(); @@ -656,7 +656,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (performRedirectIfNecessary(conversation, false)) { return; } - final FragmentManager fragmentManager = getSupportFragmentManager(); + final FragmentManager fragmentManager = getFragmentManager(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); if (mainFragment instanceof ConversationFragment) { try { @@ -670,7 +670,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio final Fragment secondaryFragment = fragmentManager.findFragmentById(R.id.secondary_fragment); if (secondaryFragment instanceof ConversationFragment) { if (((ConversationFragment) secondaryFragment).getConversation() == conversation) { - Conversation suggestion = ConversationsOverviewFragment.getSuggestion(getSupportFragmentManager(), conversation); + Conversation suggestion = ConversationsOverviewFragment.getSuggestion(this, conversation); if (suggestion != null) { openConversation(suggestion, null); } @@ -680,7 +680,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override public void onConversationsListItemUpdated() { - Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_fragment); + Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationsOverviewFragment) { ((ConversationsOverviewFragment) fragment).refresh(); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java index ff33c6155..eebd94df5 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java @@ -31,6 +31,7 @@ package eu.siacs.conversations.ui; import android.app.Activity; import android.app.AlertDialog; +import android.app.Fragment; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Paint; @@ -45,8 +46,6 @@ import android.view.ViewGroup; import android.widget.Toast; import androidx.databinding.DataBindingUtil; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -140,7 +139,7 @@ public class ConversationsOverviewFragment extends XmppFragment { activity.xmppConnectionService.archiveConversation(c); return; } - final boolean formerlySelected = ConversationFragment.getConversation(requireActivity().getSupportFragmentManager()) == swipedConversation.peek(); + final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek(); if (activity instanceof OnConversationArchived) { ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek()); } @@ -203,19 +202,19 @@ public class ConversationsOverviewFragment extends XmppFragment { private ItemTouchHelper touchHelper; - public static Conversation getSuggestion(FragmentManager fragmentManager) { + public static Conversation getSuggestion(Activity activity) { final Conversation exception; - Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); + Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationsOverviewFragment) { exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek(); } else { exception = null; } - return getSuggestion(fragmentManager, exception); + return getSuggestion(activity, exception); } - public static Conversation getSuggestion(FragmentManager fragmentManager, Conversation exception) { - Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); + public static Conversation getSuggestion(Activity activity, Conversation exception) { + Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationsOverviewFragment) { List conversations = ((ConversationsOverviewFragment) fragment).conversations; if (conversations.size() > 0) { diff --git a/src/main/java/eu/siacs/conversations/ui/XmppFragment.java b/src/main/java/eu/siacs/conversations/ui/XmppFragment.java index a16d0ae7f..30524d2f5 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppFragment.java @@ -30,8 +30,7 @@ package eu.siacs.conversations.ui; import android.app.Activity; - -import androidx.fragment.app.Fragment; +import android.app.Fragment; import eu.siacs.conversations.ui.interfaces.OnBackendConnected; diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index 496c087bd..049703597 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -60,7 +60,7 @@ public class ConversationAdapter extends RecyclerView.Adapter { viewHolder.audioPlayer.setVisibility(View.GONE); viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setText(text); - viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity.getSupportFragmentManager(), message)); + viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message)); } private void displayOpenableMessage(ViewHolder viewHolder, final Message message, final boolean darkBackground) { @@ -872,7 +872,7 @@ public class MessageAdapter extends ArrayAdapter { public void openDownloadable(Message message) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - ConversationFragment.registerPendingMessage(activity.getSupportFragmentManager(), message); + ConversationFragment.registerPendingMessage(activity, message); ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationsActivity.REQUEST_OPEN_MESSAGE); return; } diff --git a/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java b/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java index 6f9287236..da1ac7a44 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java +++ b/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java @@ -172,7 +172,7 @@ public final class MucDetailsContextMenuHelper { return true; case R.id.send_private_message: if (activity instanceof ConversationsActivity) { - ConversationFragment conversationFragment = ConversationFragment.get(activity.getSupportFragmentManager()); + ConversationFragment conversationFragment = ConversationFragment.get(activity); if (conversationFragment != null) { conversationFragment.privateMessageWith(user.getFullJid()); return true;