From 231d97ea81bb2249afa432b43d78ac963b80594a Mon Sep 17 00:00:00 2001 From: Ashique Bava <67498676+ashique-perumbavoor@users.noreply.github.com> Date: Mon, 20 Sep 2021 11:52:55 +0530 Subject: [PATCH] Migrate Fragments to AndroidX --- .../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, 62 insertions(+), 61 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 93a5ad2bf..5828ee221 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -3,8 +3,6 @@ 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; @@ -52,6 +50,8 @@ 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; @@ -465,41 +465,41 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private boolean firstWord = false; private Message mPendingDownloadableMessage; - private static ConversationFragment findConversationFragment(Activity activity) { - Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); + private static ConversationFragment findConversationFragment(FragmentManager fragmentManager) { + Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; } - fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment); + fragment = fragmentManager.findFragmentById(R.id.secondary_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; } return null; } - public static void startStopPending(Activity activity) { - ConversationFragment fragment = findConversationFragment(activity); + public static void startStopPending(FragmentManager fragmentManager) { + ConversationFragment fragment = findConversationFragment(fragmentManager); if (fragment != null) { fragment.messageListAdapter.startStopPending(); } } - public static void downloadFile(Activity activity, Message message) { - ConversationFragment fragment = findConversationFragment(activity); + public static void downloadFile(FragmentManager fragmentManager, Message message) { + ConversationFragment fragment = findConversationFragment(fragmentManager); if (fragment != null) { fragment.startDownloadable(message); } } - public static void registerPendingMessage(Activity activity, Message message) { - ConversationFragment fragment = findConversationFragment(activity); + public static void registerPendingMessage(FragmentManager fragmentManager, Message message) { + ConversationFragment fragment = findConversationFragment(fragmentManager); if (fragment != null) { fragment.pendingMessage.push(message); } } - public static void openPendingMessage(Activity activity) { - ConversationFragment fragment = findConversationFragment(activity); + public static void openPendingMessage(FragmentManager fragmentManager) { + ConversationFragment fragment = findConversationFragment(fragmentManager); if (fragment != null) { Message message = fragment.pendingMessage.pop(); if (message != null) { @@ -508,12 +508,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static Conversation getConversation(Activity activity) { - return getConversation(activity, R.id.secondary_fragment); + public static Conversation getConversation(FragmentManager fragmentManager) { + return getConversation(fragmentManager, R.id.secondary_fragment); } - private static Conversation getConversation(Activity activity, @IdRes int res) { - final Fragment fragment = activity.getFragmentManager().findFragmentById(res); + private static Conversation getConversation(FragmentManager fragmentManager, @IdRes int res) { + final Fragment fragment = fragmentManager.findFragmentById(res); if (fragment instanceof ConversationFragment) { return ((ConversationFragment) fragment).getConversation(); } else { @@ -521,8 +521,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static ConversationFragment get(Activity activity) { - FragmentManager fragmentManager = activity.getFragmentManager(); + public static ConversationFragment get(FragmentManager fragmentManager) { Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationFragment) { return (ConversationFragment) fragment; @@ -532,12 +531,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - public static Conversation getConversationReliable(Activity activity) { - final Conversation conversation = getConversation(activity, R.id.secondary_fragment); + public static Conversation getConversationReliable(FragmentManager fragmentManager) { + final Conversation conversation = getConversation(fragmentManager, R.id.secondary_fragment); if (conversation != null) { return conversation; } - return getConversation(activity, R.id.main_fragment); + return getConversation(fragmentManager, 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 171eea6ad..fb0f0e696 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -34,9 +34,6 @@ 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; @@ -55,6 +52,9 @@ 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(this) == null) { - Conversation conversation = ConversationsOverviewFragment.getSuggestion(this); + if (binding.secondaryFragment != null && ConversationFragment.getConversation(getSupportFragmentManager()) == null) { + Conversation conversation = ConversationsOverviewFragment.getSuggestion(getSupportFragmentManager()); if (conversation != null) { openConversation(conversation, null); } @@ -202,7 +202,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (xmppConnectionService == null) { return; } - final Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment); + final Fragment fragment = getSupportFragmentManager().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 = getFragmentManager().findFragmentById(id); + final Fragment fragment = getSupportFragmentManager().findFragmentById(id); if (fragment instanceof OnBackendConnected) { ((OnBackendConnected) fragment).onBackendConnected(); } } private void refreshFragment(@IdRes int id) { - final Fragment fragment = getFragmentManager().findFragmentById(id); + final Fragment fragment = getSupportFragmentManager().findFragmentById(id); if (fragment instanceof XmppFragment) { ((XmppFragment) fragment).refresh(); } @@ -287,10 +287,10 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio switch (requestCode) { case REQUEST_OPEN_MESSAGE: refreshUiReal(); - ConversationFragment.openPendingMessage(this); + ConversationFragment.openPendingMessage(getSupportFragmentManager()); break; case REQUEST_PLAY_PAUSE: - ConversationFragment.startStopPending(this); + ConversationFragment.startStopPending(getSupportFragmentManager()); break; } } @@ -317,7 +317,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void handleNegativeActivityResult(int requestCode) { - Conversation conversation = ConversationFragment.getConversationReliable(this); + Conversation conversation = ConversationFragment.getConversationReliable(getSupportFragmentManager()); switch (requestCode) { case REQUEST_DECRYPT_PGP: if (conversation == null) { @@ -332,7 +332,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void handlePositiveActivityResult(int requestCode, final Intent data) { - Conversation conversation = ConversationFragment.getConversationReliable(this); + Conversation conversation = ConversationFragment.getConversationReliable(getSupportFragmentManager()); if (conversation == null) { Log.d(Config.LOGTAG, "conversation not found"); return; @@ -364,8 +364,8 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations); setSupportActionBar(binding.toolbar); configureActionBar(getSupportActionBar()); - this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle); - this.getFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview); + this.getSupportFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle); + this.getSupportFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview); this.initializeFragments(); this.invalidateActionBarTitle(); final Intent intent; @@ -386,7 +386,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 = getFragmentManager().findFragmentById(R.id.main_fragment); + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_fragment); boolean visible = getResources().getBoolean(R.bool.show_qr_code_scan) && fragment instanceof ConversationsOverviewFragment; qrCodeScanMenuItem.setVisible(visible); @@ -400,7 +400,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override public void onConversationSelected(Conversation conversation) { clearPendingViewIntent(); - if (ConversationFragment.getConversation(this) == conversation) { + if (ConversationFragment.getConversation(getSupportFragmentManager()) == conversation) { Log.d(Config.LOGTAG, "ignore onConversationSelected() because conversation is already open"); return; } @@ -428,7 +428,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void openConversation(Conversation conversation, Bundle extras) { - final FragmentManager fragmentManager = getFragmentManager(); + final FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.executePendingTransactions(); ConversationFragment conversationFragment = (ConversationFragment) fragmentManager.findFragmentById(R.id.secondary_fragment); final boolean mainNeedsRefresh; @@ -480,7 +480,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } switch (item.getItemId()) { case android.R.id.home: - FragmentManager fm = getFragmentManager(); + FragmentManager fm = getSupportFragmentManager(); if (fm.getBackStackEntryCount() > 0) { try { fm.popBackStack(); @@ -497,7 +497,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(this); + final Conversation conversation = ConversationFragment.getConversation(getSupportFragmentManager()); if (conversation == null) { return true; } @@ -512,7 +512,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(this); + final ConversationFragment conversationFragment = ConversationFragment.get(getSupportFragmentManager()); if (conversationFragment != null && conversationFragment.onArrowUpCtrlPressed()) { return true; } @@ -567,14 +567,14 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } private void initializeFragments() { - final FragmentManager fragmentManager = getFragmentManager(); + final FragmentManager fragmentManager = getSupportFragmentManager(); 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) { - getFragmentManager().popBackStack(); + getSupportFragmentManager().popBackStack(); transaction.remove(mainFragment); transaction.commit(); fragmentManager.executePendingTransactions(); @@ -588,7 +588,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (secondaryFragment instanceof ConversationFragment) { transaction.remove(secondaryFragment); transaction.commit(); - getFragmentManager().executePendingTransactions(); + getSupportFragmentManager().executePendingTransactions(); transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.main_fragment, secondaryFragment); transaction.addToBackStack(null); @@ -610,7 +610,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (actionBar == null) { return; } - final FragmentManager fragmentManager = getFragmentManager(); + final FragmentManager fragmentManager = getSupportFragmentManager(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); if (mainFragment instanceof ConversationFragment) { final Conversation conversation = ((ConversationFragment) mainFragment).getConversation(); @@ -647,7 +647,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (performRedirectIfNecessary(conversation, false)) { return; } - final FragmentManager fragmentManager = getFragmentManager(); + final FragmentManager fragmentManager = getSupportFragmentManager(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); if (mainFragment instanceof ConversationFragment) { try { @@ -661,7 +661,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(this, conversation); + Conversation suggestion = ConversationsOverviewFragment.getSuggestion(getSupportFragmentManager(), conversation); if (suggestion != null) { openConversation(suggestion, null); } @@ -671,7 +671,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio @Override public void onConversationsListItemUpdated() { - Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment); + Fragment fragment = getSupportFragmentManager().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 eebd94df5..ff33c6155 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java @@ -31,7 +31,6 @@ 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; @@ -46,6 +45,8 @@ 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; @@ -139,7 +140,7 @@ public class ConversationsOverviewFragment extends XmppFragment { activity.xmppConnectionService.archiveConversation(c); return; } - final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek(); + final boolean formerlySelected = ConversationFragment.getConversation(requireActivity().getSupportFragmentManager()) == swipedConversation.peek(); if (activity instanceof OnConversationArchived) { ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek()); } @@ -202,19 +203,19 @@ public class ConversationsOverviewFragment extends XmppFragment { private ItemTouchHelper touchHelper; - public static Conversation getSuggestion(Activity activity) { + public static Conversation getSuggestion(FragmentManager fragmentManager) { final Conversation exception; - Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); + Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment); if (fragment instanceof ConversationsOverviewFragment) { exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek(); } else { exception = null; } - return getSuggestion(activity, exception); + return getSuggestion(fragmentManager, exception); } - public static Conversation getSuggestion(Activity activity, Conversation exception) { - Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); + public static Conversation getSuggestion(FragmentManager fragmentManager, Conversation exception) { + Fragment fragment = fragmentManager.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 30524d2f5..a16d0ae7f 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppFragment.java @@ -30,7 +30,8 @@ package eu.siacs.conversations.ui; import android.app.Activity; -import android.app.Fragment; + +import androidx.fragment.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 049703597..496c087bd 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, message)); + viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity.getSupportFragmentManager(), 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, message); + ConversationFragment.registerPendingMessage(activity.getSupportFragmentManager(), 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 da1ac7a44..6f9287236 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); + ConversationFragment conversationFragment = ConversationFragment.get(activity.getSupportFragmentManager()); if (conversationFragment != null) { conversationFragment.privateMessageWith(user.getFullJid()); return true;