NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/FragmentContextView.java

420 lines
19 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).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2015-10-29 18:10:07 +01:00
*/
package org.telegram.ui.Components;
import android.animation.Animator;
2017-03-31 01:58:05 +02:00
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
2015-10-29 18:10:07 +01:00
import android.content.Context;
2017-03-31 01:58:05 +02:00
import android.content.Intent;
2015-10-29 18:10:07 +01:00
import android.graphics.Canvas;
2017-03-31 01:58:05 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
2017-07-08 18:32:04 +02:00
import android.os.Bundle;
2015-10-29 18:10:07 +01:00
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.LocaleController;
2015-10-29 18:10:07 +01:00
import org.telegram.messenger.MediaController;
import org.telegram.messenger.MessageObject;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.voip.VoIPService;
2015-10-29 18:10:07 +01:00
import org.telegram.ui.ActionBar.BaseFragment;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.Theme;
2015-10-29 18:10:07 +01:00
import org.telegram.ui.AudioPlayerActivity;
2017-07-08 18:32:04 +02:00
import org.telegram.ui.ChatActivity;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.VoIPActivity;
2015-10-29 18:10:07 +01:00
2017-03-31 01:58:05 +02:00
public class FragmentContextView extends FrameLayout implements NotificationCenter.NotificationCenterDelegate {
2015-10-29 18:10:07 +01:00
private ImageView playButton;
private TextView titleTextView;
private MessageObject lastMessageObject;
private AnimatorSet animatorSet;
2015-10-29 18:10:07 +01:00
private float yPosition;
private BaseFragment fragment;
private float topPadding;
private boolean visible;
2017-03-31 01:58:05 +02:00
private FrameLayout frameLayout;
private ImageView closeButton;
private int currentStyle = -1;
2015-10-29 18:10:07 +01:00
2017-03-31 01:58:05 +02:00
public FragmentContextView(Context context, BaseFragment parentFragment) {
2015-10-29 18:10:07 +01:00
super(context);
fragment = parentFragment;
visible = true;
((ViewGroup) fragment.getFragmentView()).setClipToPadding(false);
setTag(1);
2017-03-31 01:58:05 +02:00
frameLayout = new FrameLayout(context);
2017-07-08 18:32:04 +02:00
frameLayout.setWillNotDraw(false);
2015-10-29 18:10:07 +01:00
addView(frameLayout, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.TOP | Gravity.LEFT, 0, 0, 0, 0));
View shadow = new View(context);
shadow.setBackgroundResource(R.drawable.header_shadow);
addView(shadow, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 3, Gravity.LEFT | Gravity.TOP, 0, 36, 0, 0));
playButton = new ImageView(context);
playButton.setScaleType(ImageView.ScaleType.CENTER);
2017-03-31 01:58:05 +02:00
playButton.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_inappPlayerPlayPause), PorterDuff.Mode.MULTIPLY));
2015-10-29 18:10:07 +01:00
addView(playButton, LayoutHelper.createFrame(36, 36, Gravity.TOP | Gravity.LEFT, 0, 0, 0, 0));
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
2017-07-08 18:32:04 +02:00
if (MediaController.getInstance().isMessagePaused()) {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
2015-10-29 18:10:07 +01:00
} else {
2017-07-08 18:32:04 +02:00
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
2015-10-29 18:10:07 +01:00
}
}
});
titleTextView = new TextView(context);
titleTextView.setMaxLines(1);
titleTextView.setLines(1);
titleTextView.setSingleLine(true);
titleTextView.setEllipsize(TextUtils.TruncateAt.END);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
titleTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
addView(titleTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.LEFT | Gravity.TOP, 35, 0, 36, 0));
2017-03-31 01:58:05 +02:00
closeButton = new ImageView(context);
2015-10-29 18:10:07 +01:00
closeButton.setImageResource(R.drawable.miniplayer_close);
2017-03-31 01:58:05 +02:00
closeButton.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_inappPlayerClose), PorterDuff.Mode.MULTIPLY));
2015-10-29 18:10:07 +01:00
closeButton.setScaleType(ImageView.ScaleType.CENTER);
addView(closeButton, LayoutHelper.createFrame(36, 36, Gravity.RIGHT | Gravity.TOP));
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MediaController.getInstance().cleanupPlayer(true, true);
}
});
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2017-03-31 01:58:05 +02:00
if (currentStyle == 0) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
2017-07-08 18:32:04 +02:00
if (fragment != null && messageObject != null) {
if (messageObject.isMusic()) {
fragment.presentFragment(new AudioPlayerActivity());
} else {
long dialog_id = 0;
if (fragment instanceof ChatActivity) {
dialog_id = ((ChatActivity) fragment).getDialogId();
}
if (messageObject.getDialogId() == dialog_id) {
((ChatActivity) fragment).scrollToMessageId(messageObject.getId(), 0, false, 0, true);
} else {
dialog_id = messageObject.getDialogId();
Bundle args = new Bundle();
int lower_part = (int) dialog_id;
int high_id = (int) (dialog_id >> 32);
if (lower_part != 0) {
if (high_id == 1) {
args.putInt("chat_id", lower_part);
} else {
if (lower_part > 0) {
args.putInt("user_id", lower_part);
} else if (lower_part < 0) {
args.putInt("chat_id", -lower_part);
}
}
} else {
args.putInt("enc_id", high_id);
}
args.putInt("message_id", messageObject.getId());
fragment.presentFragment(new ChatActivity(args), fragment instanceof ChatActivity);
}
}
2017-03-31 01:58:05 +02:00
}
} else if (currentStyle == 1) {
Intent intent = new Intent(getContext(), VoIPActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getContext().startActivity(intent);
2015-10-29 18:10:07 +01:00
}
}
});
}
public float getTopPadding() {
return topPadding;
}
public void setTopPadding(float value) {
topPadding = value;
if (fragment != null) {
View view = fragment.getFragmentView();
if (view != null) {
view.setPadding(0, (int) topPadding, 0, 0);
}
}
}
2017-03-31 01:58:05 +02:00
private void updateStyle(int style) {
if (currentStyle == style) {
return;
}
currentStyle = style;
if (style == 0) {
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_inappPlayerBackground));
titleTextView.setTextColor(Theme.getColor(Theme.key_inappPlayerTitle));
closeButton.setVisibility(VISIBLE);
playButton.setVisibility(VISIBLE);
titleTextView.setTypeface(Typeface.DEFAULT);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
titleTextView.setLayoutParams(LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.LEFT | Gravity.TOP, 35, 0, 36, 0));
} else if (style == 1) {
titleTextView.setText(LocaleController.getString("ReturnToCall", R.string.ReturnToCall));
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_returnToCallBackground));
titleTextView.setTextColor(Theme.getColor(Theme.key_returnToCallText));
closeButton.setVisibility(GONE);
playButton.setVisibility(GONE);
titleTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
titleTextView.setLayoutParams(LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 0, 0, 2));
}
}
2015-10-29 18:10:07 +01:00
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
topPadding = 0;
2017-07-08 18:32:04 +02:00
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.messagePlayingDidReset);
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.messagePlayingDidStarted);
2017-03-31 01:58:05 +02:00
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didStartedCall);
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didEndedCall);
2015-10-29 18:10:07 +01:00
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
2017-07-08 18:32:04 +02:00
NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagePlayingDidReset);
NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagePlayingDidStarted);
2017-03-31 01:58:05 +02:00
NotificationCenter.getInstance().addObserver(this, NotificationCenter.didStartedCall);
NotificationCenter.getInstance().addObserver(this, NotificationCenter.didEndedCall);
boolean callAvailable = VoIPService.getSharedInstance() != null && VoIPService.getSharedInstance().getCallState() != VoIPService.STATE_WAITING_INCOMING;
if (callAvailable) {
checkCall(true);
} else {
checkPlayer(true);
}
2015-10-29 18:10:07 +01:00
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2017-03-31 01:58:05 +02:00
super.onMeasure(widthMeasureSpec, AndroidUtilities.dp2(39));
2015-10-29 18:10:07 +01:00
}
@Override
public void didReceivedNotification(int id, Object... args) {
2017-07-08 18:32:04 +02:00
if (id == NotificationCenter.messagePlayingDidStarted || id == NotificationCenter.messagePlayingPlayStateChanged || id == NotificationCenter.messagePlayingDidReset || id == NotificationCenter.didEndedCall) {
2017-03-31 01:58:05 +02:00
checkPlayer(false);
} else if (id == NotificationCenter.didStartedCall) {
checkCall(false);
} else {
2015-10-29 18:10:07 +01:00
checkPlayer(false);
}
}
private void checkPlayer(boolean create) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
View fragmentView = fragment.getFragmentView();
if (!create && fragmentView != null) {
if (fragmentView.getParent() == null || ((View) fragmentView.getParent()).getVisibility() != VISIBLE) {
create = true;
}
}
2016-03-06 02:49:31 +01:00
if (messageObject == null || messageObject.getId() == 0/* || !messageObject.isMusic()*/) {
2015-10-29 18:10:07 +01:00
lastMessageObject = null;
if (visible) {
visible = false;
2016-04-22 15:49:00 +02:00
if (create) {
if (getVisibility() != GONE) {
setVisibility(GONE);
}
2015-10-29 18:10:07 +01:00
setTopPadding(0);
} else {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
2017-03-31 01:58:05 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "translationY", -AndroidUtilities.dp2(36)),
ObjectAnimator.ofFloat(this, "topPadding", 0));
2015-10-29 18:10:07 +01:00
animatorSet.setDuration(200);
2017-03-31 01:58:05 +02:00
animatorSet.addListener(new AnimatorListenerAdapter() {
2015-10-29 18:10:07 +01:00
@Override
public void onAnimationEnd(Animator animation) {
2015-10-29 18:10:07 +01:00
if (animatorSet != null && animatorSet.equals(animation)) {
setVisibility(GONE);
animatorSet = null;
}
}
});
animatorSet.start();
}
}
} else {
2017-03-31 01:58:05 +02:00
int prevStyle = currentStyle;
updateStyle(0);
2015-10-29 18:10:07 +01:00
if (create && topPadding == 0) {
2017-03-31 01:58:05 +02:00
setTopPadding(AndroidUtilities.dp2(36));
setTranslationY(0);
2015-10-29 18:10:07 +01:00
yPosition = 0;
}
if (!visible) {
if (!create) {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
2017-03-31 01:58:05 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "translationY", -AndroidUtilities.dp2(36), 0),
ObjectAnimator.ofFloat(this, "topPadding", AndroidUtilities.dp2(36)));
2015-10-29 18:10:07 +01:00
animatorSet.setDuration(200);
2017-03-31 01:58:05 +02:00
animatorSet.addListener(new AnimatorListenerAdapter() {
2015-10-29 18:10:07 +01:00
@Override
public void onAnimationEnd(Animator animation) {
2015-10-29 18:10:07 +01:00
if (animatorSet != null && animatorSet.equals(animation)) {
animatorSet = null;
}
}
});
animatorSet.start();
}
visible = true;
setVisibility(VISIBLE);
}
2017-07-08 18:32:04 +02:00
if (MediaController.getInstance().isMessagePaused()) {
2015-10-29 18:10:07 +01:00
playButton.setImageResource(R.drawable.miniplayer_play);
} else {
playButton.setImageResource(R.drawable.miniplayer_pause);
}
2017-03-31 01:58:05 +02:00
if (lastMessageObject != messageObject || prevStyle != 0) {
2015-10-29 18:10:07 +01:00
lastMessageObject = messageObject;
2016-03-06 02:49:31 +01:00
SpannableStringBuilder stringBuilder;
2017-07-08 18:32:04 +02:00
if (lastMessageObject.isVoice() || lastMessageObject.isRoundVideo()) {
2016-03-06 02:49:31 +01:00
stringBuilder = new SpannableStringBuilder(String.format("%s %s", messageObject.getMusicAuthor(), messageObject.getMusicTitle()));
titleTextView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
} else {
stringBuilder = new SpannableStringBuilder(String.format("%s - %s", messageObject.getMusicAuthor(), messageObject.getMusicTitle()));
titleTextView.setEllipsize(TextUtils.TruncateAt.END);
}
2017-03-31 01:58:05 +02:00
TypefaceSpan span = new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf"), 0, Theme.getColor(Theme.key_inappPlayerPerformer));
2015-10-29 18:10:07 +01:00
stringBuilder.setSpan(span, 0, messageObject.getMusicAuthor().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
titleTextView.setText(stringBuilder);
}
}
}
2017-03-31 01:58:05 +02:00
private void checkCall(boolean create) {
View fragmentView = fragment.getFragmentView();
if (!create && fragmentView != null) {
if (fragmentView.getParent() == null || ((View) fragmentView.getParent()).getVisibility() != VISIBLE) {
create = true;
}
}
boolean callAvailable = VoIPService.getSharedInstance() != null && VoIPService.getSharedInstance().getCallState() != VoIPService.STATE_WAITING_INCOMING;
if (!callAvailable) {
if (visible) {
visible = false;
if (create) {
if (getVisibility() != GONE) {
setVisibility(GONE);
}
setTopPadding(0);
} else {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "translationY", -AndroidUtilities.dp2(36)),
ObjectAnimator.ofFloat(this, "topPadding", 0));
animatorSet.setDuration(200);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (animatorSet != null && animatorSet.equals(animation)) {
setVisibility(GONE);
animatorSet = null;
}
}
});
animatorSet.start();
}
}
} else {
updateStyle(1);
if (create && topPadding == 0) {
setTopPadding(AndroidUtilities.dp2(36));
setTranslationY(0);
yPosition = 0;
}
if (!visible) {
if (!create) {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "translationY", -AndroidUtilities.dp2(36), 0),
ObjectAnimator.ofFloat(this, "topPadding", AndroidUtilities.dp2(36)));
animatorSet.setDuration(200);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (animatorSet != null && animatorSet.equals(animation)) {
animatorSet = null;
}
}
});
animatorSet.start();
}
visible = true;
setVisibility(VISIBLE);
}
}
}
2015-10-29 18:10:07 +01:00
@Override
public void setTranslationY(float translationY) {
super.setTranslationY(translationY);
yPosition = translationY;
invalidate();
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
int restoreToCount = canvas.save();
if (yPosition < 0) {
2017-03-31 01:58:05 +02:00
canvas.clipRect(0, (int) -yPosition, child.getMeasuredWidth(), AndroidUtilities.dp2(39));
2015-10-29 18:10:07 +01:00
}
final boolean result = super.drawChild(canvas, child, drawingTime);
canvas.restoreToCount(restoreToCount);
2015-10-29 18:10:07 +01:00
return result;
}
}