/* * 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). * * Copyright Nikolai Kudashov, 2013-2018. */ package org.telegram.ui.ActionBar; import android.animation.Animator; import android.animation.AnimatorSet; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.view.accessibility.AccessibilityManager; import android.widget.FrameLayout; import org.telegram.messenger.AccountInstance; import org.telegram.messenger.ApplicationLoader; import org.telegram.messenger.ContactsController; import org.telegram.messenger.DownloadController; import org.telegram.messenger.FileLoader; import org.telegram.messenger.FileLog; import org.telegram.messenger.LocationController; import org.telegram.messenger.MediaController; import org.telegram.messenger.MediaDataController; import org.telegram.messenger.MessagesController; import org.telegram.messenger.MessagesStorage; import org.telegram.messenger.NotificationCenter; import org.telegram.messenger.NotificationsController; import org.telegram.messenger.SecretChatHelper; import org.telegram.messenger.SendMessagesHelper; import org.telegram.messenger.UserConfig; import org.telegram.tgnet.ConnectionsManager; import org.telegram.ui.Components.LayoutHelper; import java.util.ArrayList; public abstract class BaseFragment { private boolean isFinished; private boolean finishing; protected Dialog visibleDialog; protected int currentAccount = UserConfig.selectedAccount; protected View fragmentView; protected ActionBarLayout parentLayout; protected ActionBar actionBar; protected boolean inPreviewMode; protected boolean inBubbleMode; protected int classGuid; protected Bundle arguments; protected boolean hasOwnBackground = false; protected boolean isPaused = true; public BaseFragment() { classGuid = ConnectionsManager.generateClassGuid(); } public BaseFragment(Bundle args) { arguments = args; 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; } public ActionBar getActionBar() { return actionBar; } public View getFragmentView() { return fragmentView; } public View createView(Context context) { return null; } public Bundle getArguments() { return arguments; } public int getCurrentAccount() { return currentAccount; } public int getClassGuid() { return classGuid; } public boolean isSwipeBackEnabled(MotionEvent event) { return true; } public void setInBubbleMode(boolean value) { inBubbleMode = value; } public boolean isInBubbleMode() { return inBubbleMode; } protected void setInPreviewMode(boolean value) { inPreviewMode = value; if (actionBar != null) { if (inPreviewMode) { actionBar.setOccupyStatusBar(false); } else { actionBar.setOccupyStatusBar(Build.VERSION.SDK_INT >= 21); } } } protected void onPreviewOpenAnimationEnd() { } protected boolean hideKeyboardOnShow() { return true; } protected void clearViews() { if (fragmentView != null) { ViewGroup parent = (ViewGroup) fragmentView.getParent(); if (parent != null) { try { onRemoveFromParent(); parent.removeViewInLayout(fragmentView); } catch (Exception e) { FileLog.e(e); } } fragmentView = null; } if (actionBar != null) { ViewGroup parent = (ViewGroup) actionBar.getParent(); if (parent != null) { try { parent.removeViewInLayout(actionBar); } catch (Exception e) { FileLog.e(e); } } actionBar = null; } parentLayout = null; } protected void onRemoveFromParent() { } public void setParentFragment(BaseFragment fragment) { setParentLayout(fragment.parentLayout); fragmentView = createView(parentLayout.getContext()); } protected void setParentLayout(ActionBarLayout layout) { if (parentLayout != layout) { parentLayout = layout; inBubbleMode = parentLayout != null && parentLayout.isInBubbleMode(); if (fragmentView != null) { ViewGroup parent = (ViewGroup) fragmentView.getParent(); if (parent != null) { try { onRemoveFromParent(); parent.removeViewInLayout(fragmentView); } catch (Exception e) { FileLog.e(e); } } if (parentLayout != null && parentLayout.getContext() != fragmentView.getContext()) { fragmentView = null; } } if (actionBar != null) { boolean differentParent = parentLayout != null && parentLayout.getContext() != actionBar.getContext(); if (actionBar.shouldAddToContainer() || differentParent) { ViewGroup parent = (ViewGroup) actionBar.getParent(); if (parent != null) { try { parent.removeViewInLayout(actionBar); } catch (Exception e) { FileLog.e(e); } } } if (differentParent) { actionBar = null; } } if (parentLayout != null && actionBar == null) { actionBar = createActionBar(parentLayout.getContext()); actionBar.parentFragment = this; } } } protected ActionBar createActionBar(Context context) { ActionBar actionBar = new ActionBar(context); 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); if (inPreviewMode || inBubbleMode) { actionBar.setOccupyStatusBar(false); } return actionBar; } public void movePreviewFragment(float dy) { parentLayout.movePreviewFragment(dy); } public void finishPreviewFragment() { parentLayout.finishPreviewFragment(); } public void finishFragment() { finishFragment(true); } public void finishFragment(boolean animated) { if (isFinished || parentLayout == null) { return; } finishing = true; parentLayout.closeLastFragment(animated); } public void removeSelfFromStack() { if (isFinished || parentLayout == null) { return; } parentLayout.removeFragmentFromStack(this); } protected boolean isFinishing() { return finishing; } public boolean onFragmentCreate() { return true; } public void onFragmentDestroy() { getConnectionsManager().cancelRequestsForGuid(classGuid); getMessagesStorage().cancelTasksForGuid(classGuid); isFinished = true; if (actionBar != null) { actionBar.setEnabled(false); } } public boolean needDelayOpenAnimation() { return false; } protected void resumeDelayedFragmentAnimation() { if (parentLayout != null) { parentLayout.resumeDelayedFragmentAnimation(); } } public void onResume() { isPaused = false; } public void onPause() { if (actionBar != null) { actionBar.onPause(); } isPaused = true; try { if (visibleDialog != null && visibleDialog.isShowing() && dismissDialogOnPause(visibleDialog)) { visibleDialog.dismiss(); visibleDialog = null; } } catch (Exception e) { FileLog.e(e); } } 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) { } public void onRequestPermissionsResultFragment(int requestCode, String[] permissions, int[] grantResults) { } public void saveSelfArgs(Bundle args) { } public void restoreSelfArgs(Bundle args) { } public boolean isLastFragment() { return parentLayout != null && !parentLayout.fragmentsStack.isEmpty() && parentLayout.fragmentsStack.get(parentLayout.fragmentsStack.size() - 1) == this; } public ActionBarLayout getParentLayout() { return parentLayout; } public FrameLayout getLayoutContainer() { if (fragmentView != null) { final ViewParent parent = fragmentView.getParent(); if (parent instanceof FrameLayout) { return (FrameLayout) parent; } } return null; } public boolean presentFragmentAsPreview(BaseFragment fragment) { return parentLayout != null && parentLayout.presentFragmentAsPreview(fragment); } public boolean presentFragment(BaseFragment fragment) { return parentLayout != null && parentLayout.presentFragment(fragment); } public boolean presentFragment(BaseFragment fragment, boolean removeLast) { return parentLayout != null && parentLayout.presentFragment(fragment, removeLast); } public boolean presentFragment(BaseFragment fragment, boolean removeLast, boolean forceWithoutAnimation) { return parentLayout != null && parentLayout.presentFragment(fragment, removeLast, forceWithoutAnimation, true, false); } public Activity getParentActivity() { if (parentLayout != null) { return parentLayout.parentActivity; } return null; } protected void setParentActivityTitle(CharSequence title) { Activity activity = getParentActivity(); if (activity != null) { activity.setTitle(title); } } public void startActivityForResult(final Intent intent, final int requestCode) { if (parentLayout != null) { parentLayout.startActivityForResult(intent, requestCode); } } public void dismissCurrentDialog() { if (visibleDialog == null) { return; } try { visibleDialog.dismiss(); visibleDialog = null; } catch (Exception e) { FileLog.e(e); } } public boolean dismissDialogOnPause(Dialog dialog) { return true; } public boolean canBeginSlide() { return true; } public void onBeginSlide() { try { if (visibleDialog != null && visibleDialog.isShowing()) { visibleDialog.dismiss(); visibleDialog = null; } } catch (Exception e) { FileLog.e(e); } if (actionBar != null) { actionBar.onPause(); } } protected void onSlideProgress(boolean isOpen, float progress) { } protected void onTransitionAnimationProgress(boolean isOpen, float progress) { } protected void onTransitionAnimationStart(boolean isOpen, boolean backward) { } protected void onTransitionAnimationEnd(boolean isOpen, boolean backward) { } protected void onBecomeFullyVisible() { 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); } } } } protected int getPreviewHeight() { return LayoutHelper.MATCH_PARENT; } protected void onBecomeFullyHidden() { } protected AnimatorSet onCustomTransitionAnimation(boolean isOpen, final Runnable callback) { return null; } public void onLowMemory() { } public Dialog showDialog(Dialog dialog) { return showDialog(dialog, false, null); } public Dialog showDialog(Dialog dialog, Dialog.OnDismissListener onDismissListener) { return showDialog(dialog, false, onDismissListener); } public Dialog showDialog(Dialog dialog, boolean allowInTransition, final Dialog.OnDismissListener onDismissListener) { if (dialog == null || parentLayout == null || parentLayout.animationInProgress || parentLayout.startedTracking || !allowInTransition && parentLayout.checkTransitionAnimation()) { return null; } try { if (visibleDialog != null) { visibleDialog.dismiss(); visibleDialog = null; } } catch (Exception e) { FileLog.e(e); } try { visibleDialog = dialog; visibleDialog.setCanceledOnTouchOutside(true); visibleDialog.setOnDismissListener(dialog1 -> { if (onDismissListener != null) { onDismissListener.onDismiss(dialog1); } onDialogDismiss((Dialog) dialog1); if (dialog1 == visibleDialog) { visibleDialog = null; } }); visibleDialog.show(); return visibleDialog; } catch (Exception e) { FileLog.e(e); } return null; } protected void onDialogDismiss(Dialog dialog) { } protected void onPanTranslationUpdate(float y) { } protected void onPanTransitionStart() { } protected void onPanTransitionEnd() { } public Dialog getVisibleDialog() { return visibleDialog; } public void setVisibleDialog(Dialog dialog) { visibleDialog = dialog; } public boolean extendActionMode(Menu menu) { return false; } public ArrayList getThemeDescriptions() { return new ArrayList<>(); } public AccountInstance getAccountInstance() { return AccountInstance.getInstance(currentAccount); } public MessagesController getMessagesController() { return getAccountInstance().getMessagesController(); } protected ContactsController getContactsController() { return getAccountInstance().getContactsController(); } public MediaDataController getMediaDataController() { return getAccountInstance().getMediaDataController(); } public ConnectionsManager getConnectionsManager() { return getAccountInstance().getConnectionsManager(); } public LocationController getLocationController() { return getAccountInstance().getLocationController(); } protected NotificationsController getNotificationsController() { return getAccountInstance().getNotificationsController(); } public MessagesStorage getMessagesStorage() { return getAccountInstance().getMessagesStorage(); } public SendMessagesHelper getSendMessagesHelper() { return getAccountInstance().getSendMessagesHelper(); } public FileLoader getFileLoader() { return getAccountInstance().getFileLoader(); } protected SecretChatHelper getSecretChatHelper() { return getAccountInstance().getSecretChatHelper(); } protected DownloadController getDownloadController() { return getAccountInstance().getDownloadController(); } protected SharedPreferences getNotificationsSettings() { return getAccountInstance().getNotificationsSettings(); } public NotificationCenter getNotificationCenter() { return getAccountInstance().getNotificationCenter(); } public MediaController getMediaController() { return MediaController.getInstance(); } public UserConfig getUserConfig() { return getAccountInstance().getUserConfig(); } public void setFragmentPanTranslationOffset(int offset) { if (parentLayout != null) { parentLayout.setFragmentPanTranslationOffset(offset); } } public void saveKeyboardPositionBeforeTransition() { } protected Animator getCustomSlideTransition(boolean topFragment, boolean backAnimation, float distanceToMove) { return null; } protected void prepareFragmentToSlide(boolean topFragment, boolean beginSlide) { } public void setProgressToDrawerOpened(float v) { } }