NekoX/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/BaseFragment.java

622 lines
18 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
2014-11-13 21:10:14 +01:00
package org.telegram.ui.ActionBar;
2021-04-14 03:44:46 +02:00
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.app.Activity;
2015-05-21 23:27:27 +02:00
import android.app.Dialog;
2015-04-09 20:00:14 +02:00
import android.content.Context;
import android.content.Intent;
2019-07-18 15:01:39 +02:00
import android.content.SharedPreferences;
2018-07-30 04:07:02 +02:00
import android.os.Build;
import android.os.Bundle;
2019-05-14 14:08:05 +02:00
import android.text.TextUtils;
2017-03-31 01:58:05 +02:00
import android.view.Menu;
2020-02-13 19:26:53 +01:00
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
2020-09-30 15:48:47 +02:00
import android.view.ViewParent;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityManager;
2020-09-30 15:48:47 +02:00
import android.widget.FrameLayout;
2019-05-14 14:08:05 +02:00
import org.telegram.messenger.AccountInstance;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.ContactsController;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.DownloadController;
import org.telegram.messenger.FileLoader;
import org.telegram.messenger.FileLog;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.LocationController;
2020-12-23 08:48:30 +01:00
import org.telegram.messenger.MediaController;
import org.telegram.messenger.MediaDataController;
2019-05-14 14:08:05 +02:00
import org.telegram.messenger.MessagesController;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.MessagesStorage;
2019-05-14 14:08:05 +02:00
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.NotificationsController;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.SecretChatHelper;
import org.telegram.messenger.SendMessagesHelper;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.UserConfig;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
2020-10-30 11:26:29 +01:00
import org.telegram.ui.Components.LayoutHelper;
2020-04-24 11:21:58 +02:00
import java.util.ArrayList;
2021-04-14 03:44:46 +02:00
public abstract class BaseFragment {
2015-05-21 23:27:27 +02:00
2018-07-30 04:07:02 +02:00
private boolean isFinished;
private boolean finishing;
protected Dialog visibleDialog;
protected int currentAccount = UserConfig.selectedAccount;
2014-11-11 23:16:17 +01:00
protected View fragmentView;
protected ActionBarLayout parentLayout;
2014-11-11 23:16:17 +01:00
protected ActionBar actionBar;
2018-07-30 04:07:02 +02:00
protected boolean inPreviewMode;
2020-07-26 10:03:38 +02:00
protected boolean inBubbleMode;
2018-07-30 04:07:02 +02:00
protected int classGuid;
protected Bundle arguments;
2015-04-09 20:00:14 +02:00
protected boolean hasOwnBackground = false;
2019-07-18 15:01:39 +02:00
protected boolean isPaused = true;
public BaseFragment() {
2018-07-30 04:07:02 +02:00
classGuid = ConnectionsManager.generateClassGuid();
}
public BaseFragment(Bundle args) {
arguments = args;
2018-07-30 04:07:02 +02:00
classGuid = ConnectionsManager.generateClassGuid();
}
public void setCurrentAccount(int account) {
if (fragmentView != null) {
throw new IllegalStateException("trying to set current account when fragment UI already created");
}
currentAccount = account;
}
2015-10-29 18:10:07 +01:00
public ActionBar getActionBar() {
return actionBar;
}
public View getFragmentView() {
return fragmentView;
}
2015-07-22 20:56:37 +02:00
public View createView(Context context) {
return null;
}
public Bundle getArguments() {
return arguments;
}
2018-07-30 04:07:02 +02:00
public int getCurrentAccount() {
return currentAccount;
}
2019-09-10 12:56:11 +02:00
public int getClassGuid() {
return classGuid;
}
2020-02-13 19:26:53 +01:00
public boolean isSwipeBackEnabled(MotionEvent event) {
return true;
}
2020-07-26 10:03:38 +02:00
public void setInBubbleMode(boolean value) {
inBubbleMode = value;
}
public boolean isInBubbleMode() {
return inBubbleMode;
}
2018-07-30 04:07:02 +02:00
protected void setInPreviewMode(boolean value) {
inPreviewMode = value;
if (actionBar != null) {
if (inPreviewMode) {
actionBar.setOccupyStatusBar(false);
} else {
actionBar.setOccupyStatusBar(Build.VERSION.SDK_INT >= 21);
}
}
}
2020-09-30 15:48:47 +02:00
protected void onPreviewOpenAnimationEnd() {
}
protected boolean hideKeyboardOnShow() {
return true;
}
2015-06-29 19:12:11 +02:00
protected void clearViews() {
if (fragmentView != null) {
ViewGroup parent = (ViewGroup) fragmentView.getParent();
if (parent != null) {
try {
2017-07-08 18:32:04 +02:00
onRemoveFromParent();
2019-12-31 14:08:08 +01:00
parent.removeViewInLayout(fragmentView);
2015-06-29 19:12:11 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
}
fragmentView = null;
}
if (actionBar != null) {
ViewGroup parent = (ViewGroup) actionBar.getParent();
if (parent != null) {
try {
2019-12-31 14:08:08 +01:00
parent.removeViewInLayout(actionBar);
2015-06-29 19:12:11 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
}
actionBar = null;
}
parentLayout = null;
}
2017-07-08 18:32:04 +02:00
protected void onRemoveFromParent() {
}
2019-07-18 15:01:39 +02:00
public void setParentFragment(BaseFragment fragment) {
setParentLayout(fragment.parentLayout);
fragmentView = createView(parentLayout.getContext());
}
2014-11-11 23:16:17 +01:00
protected void setParentLayout(ActionBarLayout layout) {
if (parentLayout != layout) {
parentLayout = layout;
2020-07-26 10:03:38 +02:00
inBubbleMode = parentLayout != null && parentLayout.isInBubbleMode();
if (fragmentView != null) {
ViewGroup parent = (ViewGroup) fragmentView.getParent();
if (parent != null) {
2014-10-11 13:30:32 +02:00
try {
2017-07-08 18:32:04 +02:00
onRemoveFromParent();
2019-12-31 14:08:08 +01:00
parent.removeViewInLayout(fragmentView);
2014-10-11 13:30:32 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2014-10-11 13:30:32 +02:00
}
}
2015-06-29 19:12:11 +02:00
if (parentLayout != null && parentLayout.getContext() != fragmentView.getContext()) {
fragmentView = null;
}
}
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
2016-10-11 13:57:01 +02:00
boolean differentParent = parentLayout != null && parentLayout.getContext() != actionBar.getContext();
2020-02-13 19:26:53 +01:00
if (actionBar.shouldAddToContainer() || differentParent) {
2016-10-11 13:57:01 +02:00
ViewGroup parent = (ViewGroup) actionBar.getParent();
if (parent != null) {
try {
2019-12-31 14:08:08 +01:00
parent.removeViewInLayout(actionBar);
2016-10-11 13:57:01 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2016-10-11 13:57:01 +02:00
}
2014-11-11 23:16:17 +01:00
}
2014-06-04 20:57:11 +02:00
}
2016-10-11 13:57:01 +02:00
if (differentParent) {
2015-06-29 19:12:11 +02:00
actionBar = null;
}
2014-11-11 23:16:17 +01:00
}
2015-06-29 19:12:11 +02:00
if (parentLayout != null && actionBar == null) {
2016-04-22 15:49:00 +02:00
actionBar = createActionBar(parentLayout.getContext());
2014-11-11 23:16:17 +01:00
actionBar.parentFragment = this;
}
}
}
2016-04-22 15:49:00 +02:00
protected ActionBar createActionBar(Context context) {
ActionBar actionBar = new ActionBar(context);
2017-03-31 01:58:05 +02:00
actionBar.setBackgroundColor(Theme.getColor(Theme.key_actionBarDefault));
actionBar.setItemsBackgroundColor(Theme.getColor(Theme.key_actionBarDefaultSelector), false);
actionBar.setItemsBackgroundColor(Theme.getColor(Theme.key_actionBarActionModeDefaultSelector), true);
actionBar.setItemsColor(Theme.getColor(Theme.key_actionBarDefaultIcon), false);
actionBar.setItemsColor(Theme.getColor(Theme.key_actionBarActionModeDefaultIcon), true);
2020-07-26 10:03:38 +02:00
if (inPreviewMode || inBubbleMode) {
2018-07-30 04:07:02 +02:00
actionBar.setOccupyStatusBar(false);
}
2016-04-22 15:49:00 +02:00
return actionBar;
}
2018-07-30 04:07:02 +02:00
public void movePreviewFragment(float dy) {
parentLayout.movePreviewFragment(dy);
}
public void finishPreviewFragment() {
parentLayout.finishPreviewFragment();
}
public void finishFragment() {
2014-06-13 17:03:06 +02:00
finishFragment(true);
}
public void finishFragment(boolean animated) {
if (isFinished || parentLayout == null) {
return;
}
2018-07-30 04:07:02 +02:00
finishing = true;
parentLayout.closeLastFragment(animated);
}
public void removeSelfFromStack() {
if (isFinished || parentLayout == null) {
return;
}
parentLayout.removeFragmentFromStack(this);
}
2018-07-30 04:07:02 +02:00
protected boolean isFinishing() {
return finishing;
}
public boolean onFragmentCreate() {
return true;
}
public void onFragmentDestroy() {
2020-03-30 14:00:09 +02:00
getConnectionsManager().cancelRequestsForGuid(classGuid);
getMessagesStorage().cancelTasksForGuid(classGuid);
isFinished = true;
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
actionBar.setEnabled(false);
}
}
2016-05-25 23:49:47 +02:00
public boolean needDelayOpenAnimation() {
return false;
}
2020-10-31 22:13:37 +01:00
protected void resumeDelayedFragmentAnimation() {
if (parentLayout != null) {
parentLayout.resumeDelayedFragmentAnimation();
}
}
public void onResume() {
2019-07-18 15:01:39 +02:00
isPaused = false;
}
public void onPause() {
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
actionBar.onPause();
}
2019-07-18 15:01:39 +02:00
isPaused = true;
try {
2015-10-29 18:10:07 +01:00
if (visibleDialog != null && visibleDialog.isShowing() && dismissDialogOnPause(visibleDialog)) {
visibleDialog.dismiss();
visibleDialog = null;
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
2017-07-08 18:32:04 +02:00
public BaseFragment getFragmentForAlert(int offset) {
if (parentLayout == null || parentLayout.fragmentsStack.size() <= 1 + offset) {
return this;
}
return parentLayout.fragmentsStack.get(parentLayout.fragmentsStack.size() - 2 - offset);
}
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
}
public boolean onBackPressed() {
return true;
}
public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {
}
2015-10-29 18:10:07 +01:00
public void onRequestPermissionsResultFragment(int requestCode, String[] permissions, int[] grantResults) {
}
public void saveSelfArgs(Bundle args) {
}
public void restoreSelfArgs(Bundle args) {
}
2021-04-14 03:44:46 +02:00
public boolean isLastFragment() {
return parentLayout != null && !parentLayout.fragmentsStack.isEmpty() && parentLayout.fragmentsStack.get(parentLayout.fragmentsStack.size() - 1) == this;
}
2019-12-31 14:08:08 +01:00
public ActionBarLayout getParentLayout() {
return parentLayout;
}
2020-09-30 15:48:47 +02:00
public FrameLayout getLayoutContainer() {
if (fragmentView != null) {
final ViewParent parent = fragmentView.getParent();
if (parent instanceof FrameLayout) {
return (FrameLayout) parent;
}
}
return null;
}
2018-07-30 04:07:02 +02:00
public boolean presentFragmentAsPreview(BaseFragment fragment) {
return parentLayout != null && parentLayout.presentFragmentAsPreview(fragment);
}
2014-10-01 00:36:18 +02:00
public boolean presentFragment(BaseFragment fragment) {
return parentLayout != null && parentLayout.presentFragment(fragment);
}
2014-10-01 00:36:18 +02:00
public boolean presentFragment(BaseFragment fragment, boolean removeLast) {
return parentLayout != null && parentLayout.presentFragment(fragment, removeLast);
}
2014-10-01 00:36:18 +02:00
public boolean presentFragment(BaseFragment fragment, boolean removeLast, boolean forceWithoutAnimation) {
2018-07-30 04:07:02 +02:00
return parentLayout != null && parentLayout.presentFragment(fragment, removeLast, forceWithoutAnimation, true, false);
}
public Activity getParentActivity() {
2014-09-28 15:37:26 +02:00
if (parentLayout != null) {
return parentLayout.parentActivity;
}
return null;
}
2019-05-14 14:08:05 +02:00
protected void setParentActivityTitle(CharSequence title) {
Activity activity = getParentActivity();
if (activity != null) {
activity.setTitle(title);
}
}
2014-10-01 00:36:18 +02:00
public void startActivityForResult(final Intent intent, final int requestCode) {
if (parentLayout != null) {
parentLayout.startActivityForResult(intent, requestCode);
}
}
2020-07-26 10:03:38 +02:00
public void dismissCurrentDialog() {
2016-10-11 13:57:01 +02:00
if (visibleDialog == null) {
return;
}
try {
visibleDialog.dismiss();
visibleDialog = null;
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2016-10-11 13:57:01 +02:00
}
}
2015-10-29 18:10:07 +01:00
public boolean dismissDialogOnPause(Dialog dialog) {
return true;
}
2019-05-14 14:08:05 +02:00
public boolean canBeginSlide() {
return true;
}
public void onBeginSlide() {
try {
if (visibleDialog != null && visibleDialog.isShowing()) {
visibleDialog.dismiss();
visibleDialog = null;
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
actionBar.onPause();
}
}
2021-04-14 03:44:46 +02:00
protected void onSlideProgress(boolean isOpen, float progress) {
}
protected void onTransitionAnimationProgress(boolean isOpen, float progress) {
}
2015-10-29 18:10:07 +01:00
protected void onTransitionAnimationStart(boolean isOpen, boolean backward) {
2015-05-21 23:27:27 +02:00
}
2015-10-29 18:10:07 +01:00
protected void onTransitionAnimationEnd(boolean isOpen, boolean backward) {
}
2015-06-29 19:12:11 +02:00
protected void onBecomeFullyVisible() {
2019-05-14 14:08:05 +02:00
AccessibilityManager mgr = (AccessibilityManager) ApplicationLoader.applicationContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (mgr.isEnabled()) {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
String title = actionBar.getTitle();
if (!TextUtils.isEmpty(title)) {
setParentActivityTitle(title);
}
}
}
2015-06-29 19:12:11 +02:00
}
2020-10-30 11:26:29 +01:00
protected int getPreviewHeight() {
return LayoutHelper.MATCH_PARENT;
}
2019-01-23 18:03:33 +01:00
protected void onBecomeFullyHidden() {
}
protected AnimatorSet onCustomTransitionAnimation(boolean isOpen, final Runnable callback) {
2015-10-29 18:10:07 +01:00
return null;
}
2015-10-29 18:10:07 +01:00
public void onLowMemory() {
2014-11-11 23:16:17 +01:00
}
2015-05-21 23:27:27 +02:00
public Dialog showDialog(Dialog dialog) {
2017-03-31 01:58:05 +02:00
return showDialog(dialog, false, null);
2015-09-24 22:52:02 +02:00
}
2017-03-31 01:58:05 +02:00
public Dialog showDialog(Dialog dialog, Dialog.OnDismissListener onDismissListener) {
return showDialog(dialog, false, onDismissListener);
}
public Dialog showDialog(Dialog dialog, boolean allowInTransition, final Dialog.OnDismissListener onDismissListener) {
2015-09-24 22:52:02 +02:00
if (dialog == null || parentLayout == null || parentLayout.animationInProgress || parentLayout.startedTracking || !allowInTransition && parentLayout.checkTransitionAnimation()) {
2015-04-09 20:00:14 +02:00
return null;
}
2014-06-13 17:03:06 +02:00
try {
if (visibleDialog != null) {
visibleDialog.dismiss();
visibleDialog = null;
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2014-11-20 15:45:33 +01:00
try {
2015-05-21 23:27:27 +02:00
visibleDialog = dialog;
2014-11-20 15:45:33 +01:00
visibleDialog.setCanceledOnTouchOutside(true);
2018-08-27 10:33:11 +02:00
visibleDialog.setOnDismissListener(dialog1 -> {
if (onDismissListener != null) {
onDismissListener.onDismiss(dialog1);
2014-11-20 15:45:33 +01:00
}
2019-12-31 14:08:08 +01:00
onDialogDismiss((Dialog) dialog1);
if (dialog1 == visibleDialog) {
visibleDialog = null;
}
2014-11-20 15:45:33 +01:00
});
2015-05-21 23:27:27 +02:00
visibleDialog.show();
2015-04-09 20:00:14 +02:00
return visibleDialog;
2014-11-20 15:45:33 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2014-11-20 15:45:33 +01:00
}
2015-04-09 20:00:14 +02:00
return null;
}
2015-09-24 22:52:02 +02:00
protected void onDialogDismiss(Dialog dialog) {
}
2015-05-21 23:27:27 +02:00
2020-09-30 15:48:47 +02:00
protected void onPanTranslationUpdate(float y) {
2020-02-13 19:26:53 +01:00
}
protected void onPanTransitionStart() {
}
protected void onPanTransitionEnd() {
}
2015-06-29 19:12:11 +02:00
public Dialog getVisibleDialog() {
return visibleDialog;
}
2015-05-21 23:27:27 +02:00
public void setVisibleDialog(Dialog dialog) {
visibleDialog = dialog;
}
2017-03-31 01:58:05 +02:00
public boolean extendActionMode(Menu menu) {
return false;
}
2020-04-24 11:21:58 +02:00
public ArrayList<ThemeDescription> getThemeDescriptions() {
return new ArrayList<>();
2017-03-31 01:58:05 +02:00
}
2019-05-14 14:08:05 +02:00
2019-07-18 15:01:39 +02:00
public AccountInstance getAccountInstance() {
2019-05-14 14:08:05 +02:00
return AccountInstance.getInstance(currentAccount);
}
2020-01-23 07:15:40 +01:00
public MessagesController getMessagesController() {
2019-05-14 14:08:05 +02:00
return getAccountInstance().getMessagesController();
}
protected ContactsController getContactsController() {
return getAccountInstance().getContactsController();
}
2020-02-13 19:26:53 +01:00
public MediaDataController getMediaDataController() {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getMediaDataController();
2019-05-14 14:08:05 +02:00
}
2020-01-23 07:15:40 +01:00
public ConnectionsManager getConnectionsManager() {
2019-05-14 14:08:05 +02:00
return getAccountInstance().getConnectionsManager();
}
2020-04-24 11:21:58 +02:00
public LocationController getLocationController() {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getLocationController();
}
2019-05-14 14:08:05 +02:00
protected NotificationsController getNotificationsController() {
return getAccountInstance().getNotificationsController();
}
2020-03-30 14:00:09 +02:00
public MessagesStorage getMessagesStorage() {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getMessagesStorage();
}
2020-02-13 19:26:53 +01:00
public SendMessagesHelper getSendMessagesHelper() {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getSendMessagesHelper();
}
2020-02-13 19:26:53 +01:00
public FileLoader getFileLoader() {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getFileLoader();
}
protected SecretChatHelper getSecretChatHelper() {
return getAccountInstance().getSecretChatHelper();
}
protected DownloadController getDownloadController() {
return getAccountInstance().getDownloadController();
}
protected SharedPreferences getNotificationsSettings() {
return getAccountInstance().getNotificationsSettings();
}
2019-05-14 14:08:05 +02:00
public NotificationCenter getNotificationCenter() {
return getAccountInstance().getNotificationCenter();
}
2019-09-10 12:56:11 +02:00
public MediaController getMediaController() {
return MediaController.getInstance();
}
2019-05-14 14:08:05 +02:00
public UserConfig getUserConfig() {
return getAccountInstance().getUserConfig();
}
2020-09-30 15:48:47 +02:00
public void setFragmentPanTranslationOffset(int offset) {
if (parentLayout != null) {
parentLayout.setFragmentPanTranslationOffset(offset);
}
}
2020-12-23 08:48:30 +01:00
public void saveKeyboardPositionBeforeTransition() {
}
2021-04-14 03:44:46 +02:00
protected Animator getCustomSlideTransition(boolean topFragment, boolean backAnimation, float distanceToMove) {
return null;
}
protected void prepareFragmentToSlide(boolean topFragment, boolean beginSlide) {
}
public void setProgressToDrawerOpened(float v) {
}
}