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

818 lines
40 KiB
Java
Raw Normal View History

2015-10-29 18:10:07 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-10-29 18:10:07 +01:00
* 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.
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;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
2017-07-08 18:32:04 +02:00
import android.os.Bundle;
2018-07-30 04:07:02 +02:00
import android.support.annotation.Keep;
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;
2017-12-08 18:35:59 +01:00
import org.telegram.messenger.LocationController;
2015-10-29 18:10:07 +01:00
import org.telegram.messenger.MediaController;
import org.telegram.messenger.MessageObject;
2017-12-08 18:35:59 +01:00
import org.telegram.messenger.MessagesController;
2015-10-29 18:10:07 +01:00
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
2017-12-08 18:35:59 +01:00
import org.telegram.messenger.SendMessagesHelper;
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.UserObject;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.voip.VoIPService;
2017-12-08 18:35:59 +01:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLRPC;
2019-01-23 18:03:33 +01:00
import org.telegram.ui.ActionBar.ActionBar;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.ActionBar.AlertDialog;
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;
2017-07-08 18:32:04 +02:00
import org.telegram.ui.ChatActivity;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.DialogsActivity;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.LaunchActivity;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.LocationActivity;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.VoIPActivity;
2015-10-29 18:10:07 +01:00
2017-12-08 18:35:59 +01:00
import java.util.ArrayList;
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 AnimatorSet animatorSet;
2015-10-29 18:10:07 +01:00
private BaseFragment fragment;
2017-03-31 01:58:05 +02:00
private FrameLayout frameLayout;
private ImageView closeButton;
2018-07-30 04:07:02 +02:00
private ImageView playbackSpeedButton;
private FragmentContextView additionalContextView;
private MessageObject lastMessageObject;
private float yPosition;
private float topPadding;
private boolean visible;
2017-03-31 01:58:05 +02:00
private int currentStyle = -1;
2017-12-08 18:35:59 +01:00
private String lastString;
private boolean isLocation;
2015-10-29 18:10:07 +01:00
2017-12-08 18:35:59 +01:00
private boolean firstLocationsLoaded;
private boolean loadingSharingCount;
private int lastLocationSharingCount = -1;
private Runnable checkLocationRunnable = new Runnable() {
@Override
public void run() {
checkLocationString();
AndroidUtilities.runOnUIThread(checkLocationRunnable, 1000);
}
};
public FragmentContextView(Context context, BaseFragment parentFragment, boolean location) {
2015-10-29 18:10:07 +01:00
super(context);
fragment = parentFragment;
visible = true;
2017-12-08 18:35:59 +01:00
isLocation = location;
2015-10-29 18:10:07 +01:00
((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));
2018-07-30 04:07:02 +02:00
addView(playButton, LayoutHelper.createFrame(36, 36, Gravity.TOP | Gravity.LEFT));
2019-01-23 18:03:33 +01:00
playButton.setOnClickListener(v -> {
if (currentStyle == 0) {
if (MediaController.getInstance().isMessagePaused()) {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
} else {
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));
2018-07-30 04:07:02 +02:00
if (!location) {
playbackSpeedButton = new ImageView(context);
playbackSpeedButton.setScaleType(ImageView.ScaleType.CENTER);
playbackSpeedButton.setImageResource(R.drawable.voice2x);
if (AndroidUtilities.density >= 3.0f) {
playbackSpeedButton.setPadding(0, 1, 0, 0);
}
addView(playbackSpeedButton, LayoutHelper.createFrame(36, 36, Gravity.TOP | Gravity.RIGHT, 0, 0, 36, 0));
2019-01-23 18:03:33 +01:00
playbackSpeedButton.setOnClickListener(v -> {
float currentPlaybackSpeed = MediaController.getInstance().getPlaybackSpeed();
if (currentPlaybackSpeed > 1) {
MediaController.getInstance().setPlaybackSpeed(1.0f);
} else {
MediaController.getInstance().setPlaybackSpeed(1.8f);
2018-07-30 04:07:02 +02:00
}
2019-01-23 18:03:33 +01:00
updatePlaybackButton();
2018-07-30 04:07:02 +02:00
});
updatePlaybackButton();
}
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));
2019-01-23 18:03:33 +01:00
closeButton.setOnClickListener(v -> {
if (currentStyle == 2) {
AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getParentActivity());
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
if (fragment instanceof DialogsActivity) {
builder.setMessage(LocaleController.getString("StopLiveLocationAlertAll", R.string.StopLiveLocationAlertAll));
} else {
ChatActivity activity = (ChatActivity) fragment;
TLRPC.Chat chat = activity.getCurrentChat();
TLRPC.User user = activity.getCurrentUser();
if (chat != null) {
builder.setMessage(LocaleController.formatString("StopLiveLocationAlertToGroup", R.string.StopLiveLocationAlertToGroup, chat.title));
} else if (user != null) {
builder.setMessage(LocaleController.formatString("StopLiveLocationAlertToUser", R.string.StopLiveLocationAlertToUser, UserObject.getFirstName(user)));
2017-12-08 18:35:59 +01:00
} else {
2019-01-23 18:03:33 +01:00
builder.setMessage(LocaleController.getString("AreYouSure", R.string.AreYouSure));
2017-12-08 18:35:59 +01:00
}
}
2019-01-23 18:03:33 +01:00
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), (dialogInterface, i) -> {
if (fragment instanceof DialogsActivity) {
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
LocationController.getInstance(a).removeAllLocationSharings();
}
} else {
LocationController.getInstance(fragment.getCurrentAccount()).removeSharingLocation(((ChatActivity) fragment).getDialogId());
}
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
builder.show();
} else {
MediaController.getInstance().cleanupPlayer(true, true);
2015-10-29 18:10:07 +01:00
}
});
2019-01-23 18:03:33 +01:00
setOnClickListener(v -> {
if (currentStyle == 0) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (fragment != null && messageObject != null) {
if (messageObject.isMusic()) {
fragment.showDialog(new AudioPlayerAlert(getContext()));
} 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);
2017-07-08 18:32:04 +02:00
} else {
2019-01-23 18:03:33 +01:00
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);
2017-07-08 18:32:04 +02:00
} else {
2019-01-23 18:03:33 +01:00
if (lower_part > 0) {
args.putInt("user_id", lower_part);
} else if (lower_part < 0) {
args.putInt("chat_id", -lower_part);
}
2017-07-08 18:32:04 +02:00
}
2019-01-23 18:03:33 +01:00
} else {
args.putInt("enc_id", high_id);
2017-07-08 18:32:04 +02:00
}
2019-01-23 18:03:33 +01:00
args.putInt("message_id", messageObject.getId());
fragment.presentFragment(new ChatActivity(args), fragment instanceof ChatActivity);
2017-07-08 18:32:04 +02:00
}
2017-03-31 01:58:05 +02:00
}
2019-01-23 18:03:33 +01: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);
} else if (currentStyle == 2) {
long did = 0;
int account = UserConfig.selectedAccount;
if (fragment instanceof ChatActivity) {
did = ((ChatActivity) fragment).getDialogId();
account = fragment.getCurrentAccount();
} else if (LocationController.getLocationsCount() == 1) {
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
ArrayList<LocationController.SharingLocationInfo> arrayList = LocationController.getInstance(a).sharingLocationsUI;
if (!arrayList.isEmpty()) {
LocationController.SharingLocationInfo info = LocationController.getInstance(a).sharingLocationsUI.get(0);
did = info.did;
account = info.messageObject.currentAccount;
break;
2018-07-30 04:07:02 +02:00
}
2017-12-08 18:35:59 +01:00
}
2019-01-23 18:03:33 +01:00
} else {
did = 0;
}
if (did != 0) {
openSharingLocation(LocationController.getInstance(account).getSharingLocationInfo(did));
} else {
fragment.showDialog(new SharingLocationsAlert(getContext(), this::openSharingLocation));
2015-10-29 18:10:07 +01:00
}
}
});
}
2018-07-30 04:07:02 +02:00
private void updatePlaybackButton() {
float currentPlaybackSpeed = MediaController.getInstance().getPlaybackSpeed();
if (currentPlaybackSpeed > 1) {
playbackSpeedButton.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_inappPlayerPlayPause), PorterDuff.Mode.MULTIPLY));
} else {
playbackSpeedButton.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_inappPlayerClose), PorterDuff.Mode.MULTIPLY));
}
}
2017-12-08 18:35:59 +01:00
public void setAdditionalContextView(FragmentContextView contextView) {
additionalContextView = contextView;
}
2018-07-30 04:07:02 +02:00
private void openSharingLocation(final LocationController.SharingLocationInfo info) {
if (info == null || fragment.getParentActivity() == null) {
2017-12-08 18:35:59 +01:00
return;
}
2018-07-30 04:07:02 +02:00
LaunchActivity launchActivity = ((LaunchActivity) fragment.getParentActivity());
launchActivity.switchToAccount(info.messageObject.currentAccount, true);
2017-12-08 18:35:59 +01:00
LocationActivity locationActivity = new LocationActivity(2);
locationActivity.setMessageObject(info.messageObject);
final long dialog_id = info.messageObject.getDialogId();
2019-01-23 18:03:33 +01:00
locationActivity.setDelegate((location, live) -> SendMessagesHelper.getInstance(info.messageObject.currentAccount).sendMessage(location, dialog_id, null, null, null));
2018-07-30 04:07:02 +02:00
launchActivity.presentFragment(locationActivity);
2017-12-08 18:35:59 +01:00
}
2015-10-29 18:10:07 +01:00
public float getTopPadding() {
return topPadding;
}
2017-12-08 18:35:59 +01:00
private void checkVisibility() {
boolean show = false;
if (isLocation) {
if (fragment instanceof DialogsActivity) {
2018-07-30 04:07:02 +02:00
show = LocationController.getLocationsCount() != 0;
2017-12-08 18:35:59 +01:00
} else {
2018-07-30 04:07:02 +02:00
show = LocationController.getInstance(fragment.getCurrentAccount()).isSharingLocation(((ChatActivity) fragment).getDialogId());
2017-12-08 18:35:59 +01:00
}
} else {
if (VoIPService.getSharedInstance() != null && VoIPService.getSharedInstance().getCallState() != VoIPService.STATE_WAITING_INCOMING) {
show = true;
} else {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null && messageObject.getId() != 0) {
show = true;
}
}
}
setVisibility(show ? VISIBLE : GONE);
}
2018-07-30 04:07:02 +02:00
@Keep
2015-10-29 18:10:07 +01:00
public void setTopPadding(float value) {
topPadding = value;
2019-01-23 18:03:33 +01:00
if (fragment != null && getParent() != null) {
2015-10-29 18:10:07 +01:00
View view = fragment.getFragmentView();
2019-01-23 18:03:33 +01:00
ActionBar actionBar = fragment.getActionBar();
2017-12-08 18:35:59 +01:00
int additionalPadding = 0;
2019-01-23 18:03:33 +01:00
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE && additionalContextView.getParent() != null) {
2017-12-08 18:35:59 +01:00
additionalPadding = AndroidUtilities.dp(36);
}
2019-01-23 18:03:33 +01:00
if (view != null && getParent() != null) {
2017-12-08 18:35:59 +01:00
view.setPadding(0, (int) topPadding + additionalPadding, 0, 0);
}
if (isLocation && additionalContextView != null) {
((LayoutParams) additionalContextView.getLayoutParams()).topMargin = -AndroidUtilities.dp(36) - (int) topPadding;
2015-10-29 18:10:07 +01:00
}
}
}
2017-03-31 01:58:05 +02:00
private void updateStyle(int style) {
if (currentStyle == style) {
return;
}
currentStyle = style;
2017-12-08 18:35:59 +01:00
if (style == 0 || style == 2) {
2017-03-31 01:58:05 +02:00
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_inappPlayerBackground));
2018-07-30 04:07:02 +02:00
frameLayout.setTag(Theme.key_inappPlayerBackground);
2017-03-31 01:58:05 +02:00
titleTextView.setTextColor(Theme.getColor(Theme.key_inappPlayerTitle));
2018-07-30 04:07:02 +02:00
titleTextView.setTag(Theme.key_inappPlayerTitle);
2017-03-31 01:58:05 +02:00
closeButton.setVisibility(VISIBLE);
playButton.setVisibility(VISIBLE);
titleTextView.setTypeface(Typeface.DEFAULT);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
2017-12-08 18:35:59 +01:00
if (style == 0) {
playButton.setLayoutParams(LayoutHelper.createFrame(36, 36, Gravity.TOP | Gravity.LEFT, 0, 0, 0, 0));
titleTextView.setLayoutParams(LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.LEFT | Gravity.TOP, 35, 0, 36, 0));
2018-07-30 04:07:02 +02:00
if (playbackSpeedButton != null) {
playbackSpeedButton.setVisibility(VISIBLE);
}
2017-12-08 18:35:59 +01:00
} else if (style == 2) {
playButton.setLayoutParams(LayoutHelper.createFrame(36, 36, Gravity.TOP | Gravity.LEFT, 8, 0, 0, 0));
titleTextView.setLayoutParams(LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.LEFT | Gravity.TOP, 35 + 16, 0, 36, 0));
}
2017-03-31 01:58:05 +02:00
} else if (style == 1) {
titleTextView.setText(LocaleController.getString("ReturnToCall", R.string.ReturnToCall));
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_returnToCallBackground));
2018-07-30 04:07:02 +02:00
frameLayout.setTag(Theme.key_returnToCallBackground);
2017-03-31 01:58:05 +02:00
titleTextView.setTextColor(Theme.getColor(Theme.key_returnToCallText));
2018-07-30 04:07:02 +02:00
titleTextView.setTag(Theme.key_returnToCallText);
2017-03-31 01:58:05 +02:00
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));
2018-07-30 04:07:02 +02:00
titleTextView.setPadding(0, 0, 0, 0);
if (playbackSpeedButton != null) {
playbackSpeedButton.setVisibility(GONE);
}
2017-03-31 01:58:05 +02:00
}
}
2015-10-29 18:10:07 +01:00
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
topPadding = 0;
2017-12-08 18:35:59 +01:00
if (isLocation) {
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.liveLocationsChanged);
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.liveLocationsCacheChanged);
2017-12-08 18:35:59 +01:00
} else {
2018-07-30 04:07:02 +02:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidReset);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
2019-01-23 18:03:33 +01:00
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidStart);
2018-07-30 04:07:02 +02:00
}
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.didStartedCall);
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.didEndedCall);
2017-12-08 18:35:59 +01:00
}
2015-10-29 18:10:07 +01:00
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
2017-12-08 18:35:59 +01:00
if (isLocation) {
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.liveLocationsChanged);
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.liveLocationsCacheChanged);
2017-12-08 18:35:59 +01:00
if (additionalContextView != null) {
additionalContextView.checkVisibility();
}
checkLiveLocation(true);
2017-03-31 01:58:05 +02:00
} else {
2018-07-30 04:07:02 +02:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidReset);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
2019-01-23 18:03:33 +01:00
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidStart);
2018-07-30 04:07:02 +02:00
}
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didStartedCall);
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didEndedCall);
2017-12-08 18:35:59 +01:00
if (additionalContextView != null) {
additionalContextView.checkVisibility();
}
if (VoIPService.getSharedInstance() != null && VoIPService.getSharedInstance().getCallState() != VoIPService.STATE_WAITING_INCOMING) {
checkCall(true);
} else {
checkPlayer(true);
2018-07-30 04:07:02 +02:00
updatePlaybackButton();
2017-12-08 18:35:59 +01:00
}
2017-03-31 01:58:05 +02:00
}
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
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
2017-12-08 18:35:59 +01:00
if (id == NotificationCenter.liveLocationsChanged) {
checkLiveLocation(false);
} else if (id == NotificationCenter.liveLocationsCacheChanged) {
if (fragment instanceof ChatActivity) {
long did = (Long) args[0];
if (((ChatActivity) fragment).getDialogId() == did) {
checkLocationString();
}
}
2019-01-23 18:03:33 +01:00
} else if (id == NotificationCenter.messagePlayingDidStart || 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);
}
}
2017-12-08 18:35:59 +01:00
private void checkLiveLocation(boolean create) {
View fragmentView = fragment.getFragmentView();
if (!create && fragmentView != null) {
if (fragmentView.getParent() == null || ((View) fragmentView.getParent()).getVisibility() != VISIBLE) {
create = true;
}
}
boolean show;
if (fragment instanceof DialogsActivity) {
2018-07-30 04:07:02 +02:00
show = LocationController.getLocationsCount() != 0;
2017-12-08 18:35:59 +01:00
} else {
2018-07-30 04:07:02 +02:00
show = LocationController.getInstance(fragment.getCurrentAccount()).isSharingLocation(((ChatActivity) fragment).getDialogId());
2017-12-08 18:35:59 +01:00
}
if (!show) {
lastLocationSharingCount = -1;
AndroidUtilities.cancelRunOnUIThread(checkLocationRunnable);
if (visible) {
visible = false;
if (create) {
if (getVisibility() != GONE) {
setVisibility(GONE);
}
setTopPadding(0);
} else {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "topPadding", 0));
2017-12-08 18:35:59 +01:00
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(2);
playButton.setImageDrawable(new ShareLocationDrawable(getContext(), true));
if (create && topPadding == 0) {
setTopPadding(AndroidUtilities.dp2(36));
yPosition = 0;
}
if (!visible) {
if (!create) {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "topPadding", AndroidUtilities.dp2(36)));
2017-12-08 18:35:59 +01:00
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);
}
if (fragment instanceof DialogsActivity) {
String liveLocation = LocaleController.getString("AttachLiveLocation", R.string.AttachLiveLocation);
String param;
2018-07-30 04:07:02 +02:00
ArrayList<LocationController.SharingLocationInfo> infos = new ArrayList<>();
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
infos.addAll(LocationController.getInstance(a).sharingLocationsUI);
}
2017-12-08 18:35:59 +01:00
if (infos.size() == 1) {
LocationController.SharingLocationInfo info = infos.get(0);
int lower_id = (int) info.messageObject.getDialogId();
if (lower_id > 0) {
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(info.messageObject.currentAccount).getUser(lower_id);
2017-12-08 18:35:59 +01:00
param = UserObject.getFirstName(user);
} else {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = MessagesController.getInstance(info.messageObject.currentAccount).getChat(-lower_id);
2017-12-08 18:35:59 +01:00
if (chat != null) {
param = chat.title;
} else {
param = "";
}
}
} else {
2018-07-30 04:07:02 +02:00
param = LocaleController.formatPluralString("Chats", infos.size());
2017-12-08 18:35:59 +01:00
}
String fullString = String.format(LocaleController.getString("AttachLiveLocationIsSharing", R.string.AttachLiveLocationIsSharing), liveLocation, param);
int start = fullString.indexOf(liveLocation);
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(fullString);
titleTextView.setEllipsize(TextUtils.TruncateAt.END);
TypefaceSpan span = new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf"), 0, Theme.getColor(Theme.key_inappPlayerPerformer));
stringBuilder.setSpan(span, start, start + liveLocation.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
titleTextView.setText(stringBuilder);
} else {
checkLocationRunnable.run();
checkLocationString();
}
}
}
private void checkLocationString() {
if (!(fragment instanceof ChatActivity) || titleTextView == null) {
return;
}
ChatActivity chatActivity = (ChatActivity) fragment;
long dialogId = chatActivity.getDialogId();
2018-07-30 04:07:02 +02:00
int currentAccount = chatActivity.getCurrentAccount();
ArrayList<TLRPC.Message> messages = LocationController.getInstance(currentAccount).locationsCache.get(dialogId);
2017-12-08 18:35:59 +01:00
if (!firstLocationsLoaded) {
2018-07-30 04:07:02 +02:00
LocationController.getInstance(currentAccount).loadLiveLocations(dialogId);
2017-12-08 18:35:59 +01:00
firstLocationsLoaded = true;
}
int locationSharingCount = 0;
TLRPC.User notYouUser = null;
if (messages != null) {
2018-07-30 04:07:02 +02:00
int currentUserId = UserConfig.getInstance(currentAccount).getClientUserId();
int date = ConnectionsManager.getInstance(currentAccount).getCurrentTime();
2017-12-08 18:35:59 +01:00
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
if (message.media == null) {
continue;
}
if (message.date + message.media.period > date) {
if (notYouUser == null && message.from_id != currentUserId) {
2018-07-30 04:07:02 +02:00
notYouUser = MessagesController.getInstance(currentAccount).getUser(message.from_id);
2017-12-08 18:35:59 +01:00
}
locationSharingCount++;
}
}
}
if (lastLocationSharingCount == locationSharingCount) {
return;
}
lastLocationSharingCount = locationSharingCount;
String liveLocation = LocaleController.getString("AttachLiveLocation", R.string.AttachLiveLocation);
String fullString;
if (locationSharingCount == 0) {
fullString = liveLocation;
} else {
int otherSharingCount = locationSharingCount - 1;
2018-07-30 04:07:02 +02:00
if (LocationController.getInstance(currentAccount).isSharingLocation(dialogId)) {
2017-12-08 18:35:59 +01:00
if (otherSharingCount != 0) {
if (otherSharingCount == 1 && notYouUser != null) {
fullString = String.format("%1$s - %2$s", liveLocation, LocaleController.formatString("SharingYouAndOtherName", R.string.SharingYouAndOtherName, UserObject.getFirstName(notYouUser)));
} else {
fullString = String.format("%1$s - %2$s %3$s", liveLocation, LocaleController.getString("ChatYourSelfName", R.string.ChatYourSelfName), LocaleController.formatPluralString("AndOther", otherSharingCount));
}
} else {
fullString = String.format("%1$s - %2$s", liveLocation, LocaleController.getString("ChatYourSelfName", R.string.ChatYourSelfName));
}
} else {
if (otherSharingCount != 0) {
fullString = String.format("%1$s - %2$s %3$s", liveLocation, UserObject.getFirstName(notYouUser), LocaleController.formatPluralString("AndOther", otherSharingCount));
} else {
fullString = String.format("%1$s - %2$s", liveLocation, UserObject.getFirstName(notYouUser));
}
}
}
if (lastString != null && fullString.equals(lastString)) {
return;
}
lastString = fullString;
int start = fullString.indexOf(liveLocation);
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(fullString);
titleTextView.setEllipsize(TextUtils.TruncateAt.END);
if (start >= 0) {
TypefaceSpan span = new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf"), 0, Theme.getColor(Theme.key_inappPlayerPerformer));
stringBuilder.setSpan(span, start, start + liveLocation.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
titleTextView.setText(stringBuilder);
}
2015-10-29 18:10:07 +01:00
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;
}
}
2019-03-03 21:40:48 +01:00
if (messageObject == null || messageObject.getId() == 0 || messageObject.isVideo()) {
2015-10-29 18:10:07 +01:00
lastMessageObject = null;
2018-07-30 04:07:02 +02:00
boolean callAvailable = VoIPService.getSharedInstance() != null && VoIPService.getSharedInstance().getCallState() != VoIPService.STATE_WAITING_INCOMING;
if (callAvailable) {
checkCall(false);
return;
}
2015-10-29 18:10:07 +01:00
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();
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(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));
2017-12-08 18:35:59 +01:00
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE) {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(72);
} else {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(36);
}
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-12-08 18:35:59 +01:00
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE) {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(72);
} else {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(36);
}
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(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()) {
2018-07-30 04:07:02 +02:00
if (playbackSpeedButton != null) {
playbackSpeedButton.setAlpha(1.0f);
playbackSpeedButton.setEnabled(true);
}
titleTextView.setPadding(0, 0, AndroidUtilities.dp(44), 0);
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 {
2018-07-30 04:07:02 +02:00
if (playbackSpeedButton != null) {
playbackSpeedButton.setAlpha(0.0f);
playbackSpeedButton.setEnabled(false);
}
titleTextView.setPadding(0, 0, 0, 0);
2016-03-06 02:49:31 +01:00
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();
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "topPadding", 0));
2017-03-31 01:58:05 +02:00
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));
2017-12-08 18:35:59 +01:00
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE) {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(72);
} else {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(36);
}
2017-03-31 01:58:05 +02:00
yPosition = 0;
}
if (!visible) {
if (!create) {
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
2017-12-08 18:35:59 +01:00
if (additionalContextView != null && additionalContextView.getVisibility() == VISIBLE) {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(72);
} else {
((LayoutParams) getLayoutParams()).topMargin = -AndroidUtilities.dp(36);
}
2018-07-30 04:07:02 +02:00
animatorSet.playTogether(ObjectAnimator.ofFloat(this, "topPadding", AndroidUtilities.dp2(36)));
2017-03-31 01:58:05 +02:00
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
}