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

316 lines
8.8 KiB
Java
Raw Normal View History

/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.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-2016.
*/
2014-11-13 21:10:14 +01:00
package org.telegram.ui.ActionBar;
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.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import org.telegram.messenger.FileLog;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
public class BaseFragment {
2015-05-21 23:27:27 +02:00
private boolean isFinished = false;
2015-05-21 23:27:27 +02:00
protected Dialog visibleDialog = null;
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;
protected int classGuid = 0;
protected Bundle arguments;
2014-06-17 17:10:02 +02:00
protected boolean swipeBackEnabled = true;
2015-04-09 20:00:14 +02:00
protected boolean hasOwnBackground = false;
public BaseFragment() {
classGuid = ConnectionsManager.getInstance().generateClassGuid();
}
public BaseFragment(Bundle args) {
arguments = args;
classGuid = ConnectionsManager.getInstance().generateClassGuid();
}
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;
}
2015-06-29 19:12:11 +02:00
protected void clearViews() {
if (fragmentView != null) {
ViewGroup parent = (ViewGroup) fragmentView.getParent();
if (parent != null) {
try {
parent.removeView(fragmentView);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
fragmentView = null;
}
if (actionBar != null) {
ViewGroup parent = (ViewGroup) actionBar.getParent();
if (parent != null) {
try {
parent.removeView(actionBar);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
actionBar = null;
}
parentLayout = null;
}
2014-11-11 23:16:17 +01:00
protected void setParentLayout(ActionBarLayout layout) {
if (parentLayout != layout) {
parentLayout = layout;
if (fragmentView != null) {
ViewGroup parent = (ViewGroup) fragmentView.getParent();
if (parent != null) {
2014-10-11 13:30:32 +02:00
try {
parent.removeView(fragmentView);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
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) {
ViewGroup parent = (ViewGroup) actionBar.getParent();
if (parent != null) {
try {
parent.removeView(actionBar);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2014-06-04 20:57:11 +02:00
}
2015-06-29 19:12:11 +02:00
if (parentLayout != null && parentLayout.getContext() != actionBar.getContext()) {
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);
actionBar.setBackgroundColor(Theme.ACTION_BAR_COLOR);
actionBar.setItemsBackgroundColor(Theme.ACTION_BAR_SELECTOR_COLOR);
return actionBar;
}
public void finishFragment() {
2014-06-13 17:03:06 +02:00
finishFragment(true);
}
public void finishFragment(boolean animated) {
if (isFinished || parentLayout == null) {
return;
}
parentLayout.closeLastFragment(animated);
}
public void removeSelfFromStack() {
if (isFinished || parentLayout == null) {
return;
}
parentLayout.removeFragmentFromStack(this);
}
public boolean onFragmentCreate() {
return true;
}
public void onFragmentDestroy() {
2015-09-24 22:52:02 +02:00
ConnectionsManager.getInstance().cancelRequestsForGuid(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;
}
public void onResume() {
}
public void onPause() {
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
actionBar.onPause();
}
try {
2015-10-29 18:10:07 +01:00
if (visibleDialog != null && visibleDialog.isShowing() && dismissDialogOnPause(visibleDialog)) {
visibleDialog.dismiss();
visibleDialog = null;
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
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) {
}
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) {
return parentLayout != null && parentLayout.presentFragment(fragment, removeLast, forceWithoutAnimation, true);
}
public Activity getParentActivity() {
2014-09-28 15:37:26 +02:00
if (parentLayout != null) {
return parentLayout.parentActivity;
}
return null;
}
2014-10-01 00:36:18 +02:00
public void startActivityForResult(final Intent intent, final int requestCode) {
if (parentLayout != null) {
parentLayout.startActivityForResult(intent, requestCode);
}
}
2015-10-29 18:10:07 +01:00
public boolean dismissDialogOnPause(Dialog dialog) {
return true;
}
public void onBeginSlide() {
try {
if (visibleDialog != null && visibleDialog.isShowing()) {
visibleDialog.dismiss();
visibleDialog = null;
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2014-11-11 23:16:17 +01:00
if (actionBar != null) {
actionBar.onPause();
}
}
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() {
}
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) {
2015-09-24 22:52:02 +02:00
return showDialog(dialog, false);
}
public Dialog showDialog(Dialog dialog, boolean allowInTransition) {
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) {
FileLog.e("tmessages", 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);
visibleDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
2015-09-24 22:52:02 +02:00
onDialogDismiss(visibleDialog);
2014-11-20 15:45:33 +01:00
visibleDialog = null;
}
});
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) {
FileLog.e("tmessages", e);
}
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
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;
}
}