Skip to content

Commit

Permalink
Improved messages GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
niedev committed Sep 4, 2024
1 parent 8a78d19 commit 8029c6c
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
android:launchMode="singleTask"
android:theme="@style/Theme.Speech" />
<activity android:name="nie.translator.rtranslator.GeneralActivity" />
<activity android:name="nie.translator.rtranslator.tools.ImageActivity"
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/Theme.Speech"/>

<service android:name="nie.translator.rtranslator.voice_translation._conversation_mode._conversation.ConversationService"
android:foregroundServiceType="microphone"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import nie.translator.rtranslator.access.AccessActivity;
import nie.translator.rtranslator.tools.CustomLocale;
import nie.translator.rtranslator.tools.ErrorCodes;
import nie.translator.rtranslator.tools.ImageActivity;
import nie.translator.rtranslator.voice_translation.VoiceTranslationActivity;
import nie.translator.rtranslator.voice_translation.neural_networks.NeuralNetworkApi;
import nie.translator.rtranslator.voice_translation.neural_networks.translation.Translator;
Expand All @@ -37,6 +38,7 @@


public class LoadingActivity extends GeneralActivity {
private final boolean START_IMAGE = false;
private Handler mainHandler;
private boolean isVisible = false;
private Global global;
Expand Down Expand Up @@ -136,12 +138,26 @@ public void onFailure(int[] reasons, long value) {
}

private void startVoiceTranslationActivity() {
if(!START_IMAGE) {
startingActivity = true;
Intent intent = new Intent(LoadingActivity.this, VoiceTranslationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}else{
startImageActivity();
}
}

private void startImageActivity() {
startingActivity = true;
Intent intent = new Intent(LoadingActivity.this, VoiceTranslationActivity.class);
Intent intent = new Intent(LoadingActivity.this, ImageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();

}

private void notifyGoogleTTSErrorDialog() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package nie.translator.rtranslator.tools;

//This activity is used only to create the images of the conversation mode in the readme

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import nie.translator.rtranslator.R;
import nie.translator.rtranslator.bluetooth.Message;
import nie.translator.rtranslator.bluetooth.Peer;
import nie.translator.rtranslator.tools.gui.messages.GuiMessage;
import nie.translator.rtranslator.tools.gui.messages.MessagesAdapter;

public class ImageActivity extends Activity {
protected MessagesAdapter mAdapter;
protected RecyclerView mRecyclerView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_conversation);

View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

@Override
protected void onStart() {
super.onStart();
mRecyclerView = findViewById(R.id.recycler_view);

ArrayList<GuiMessage> messages = new ArrayList<>();
/*messages.add(new GuiMessage(new Message(this, "m", "Hello, how are you?"), true, true));
messages.add(new GuiMessage(new Message(this, new Peer(null, "Carlos11", true), "m", "I'm fine"), false, true));
messages.add(new GuiMessage(new Message(this, new Peer(null, "Denise12", true), "m", "Me too"), false, true));*/

messages.add(new GuiMessage(new Message(this, new Peer(null, "Alice10", true), "m", "¿Hola, cómo estás?"), false, true));
messages.add(new GuiMessage(new Message(this, "m", "Estoy bien"), true, true));
messages.add(new GuiMessage(new Message(this, new Peer(null, "Denise12", true), "m", "Yo también"), false, true));

/*messages.add(new GuiMessage(new Message(this, new Peer(null, "Alice10", true), "m", "Bonjour comment allez-vous?"), false, true));
messages.add(new GuiMessage(new Message(this, new Peer(null, "Carlos11", true), "m", "Je vais bien"), false, true));
messages.add(new GuiMessage(new Message(this, "m", "Moi aussi"), true, true));*/

mAdapter = new MessagesAdapter(messages, new MessagesAdapter.Callback() {
@Override
public void onFirstItemAdded() {
mRecyclerView.setVisibility(View.VISIBLE);
}
});
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(layoutManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package nie.translator.rtranslator.tools.gui.messages;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -70,8 +71,10 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
((ReceivedHolder) holder).text.setVisibility(View.GONE);
((ReceivedHolder) holder).containerSender.setVisibility(View.VISIBLE);
((ReceivedHolder) holder).sender.setText(message.getMessage().getSender().getName());
Log.d("recyclerview", "RecyclerView bind sender");
}
((MessageHolder) holder).setText(message.getMessage().getText());
Log.d("recyclerview", "RecyclerView bind text");
//holder.itemView.requestLayout();
}
}
Expand Down Expand Up @@ -103,7 +106,7 @@ public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {

@Override
public boolean onFailedToRecycleView(@NonNull RecyclerView.ViewHolder holder) {
return true;
return super.onFailedToRecycleView(holder);
}

public void addMessage(GuiMessage message) {
Expand All @@ -116,7 +119,8 @@ public void addMessage(GuiMessage message) {

public void setMessage(int index, GuiMessage message) {
mResults.set(index, message);
notifyItemRangeChanged(0, getItemCount());
//notifyItemRangeChanged(0, getItemCount());
notifyItemChanged(index);
}

public int getMessageIndex(long messageID){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -48,11 +49,11 @@

public abstract class VoiceTranslationFragment extends Fragment implements MicrophoneComunicable {
//gui
public static final int TIME_FOR_SCROLLING = 50;
protected VoiceTranslationActivity activity;
protected Global global;
protected MessagesAdapter mAdapter;
protected RecyclerView mRecyclerView;
protected RecyclerView.SmoothScroller smoothScroller;
protected TextView description;
protected View.OnClickListener micClickListener;

Expand All @@ -76,13 +77,16 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
layoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(layoutManager);
smoothScroller = new LinearSmoothScroller(activity) {
mRecyclerView.setItemAnimator(new DefaultItemAnimator(){
@Override
protected int calculateTimeForScrolling(int dx) {
return 100;
public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
dispatchChangeFinished(oldHolder, true);
dispatchChangeFinished(newHolder, false);
return true;
}
};
//smoothScroller = new LinearSmoothScroller(activity);
});
//mRecyclerView.setItemAnimator(null);
mRecyclerView.setHasFixedSize(true);
}

protected abstract void connectToService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
speak(conversationMessage.getPayload().getText(), conversationMessage.getPayload().getLanguage());
}
message.setText(conversationMessage.getPayload().getText()); // updating the text with the new translated text (and without the language code)
GuiMessage guiMessage = new GuiMessage(message, messageID, false, true);
GuiMessage guiMessage = new GuiMessage(message, messageID, false, isFinal);
notifyMessage(guiMessage);
// we save every new message in the exchanged messages so that the fragment can restore them
addOrUpdateMessage(guiMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

Expand Down Expand Up @@ -476,12 +479,23 @@ public void onMessage(GuiMessage message) {
if (message != null) {
int messageIndex = mAdapter.getMessageIndex(message.getMessageID());
if(messageIndex != -1){
mAdapter.setMessage(messageIndex, message);
if((!mRecyclerView.isAnimating() && !mRecyclerView.getLayoutManager().isSmoothScrolling()) || message.isFinal()) {
if (message.isFinal()) {
if (mRecyclerView.getItemAnimator() != null) {
mRecyclerView.getItemAnimator().endAnimations();
}
}
mAdapter.setMessage(messageIndex, message);
}
}else{
if(mRecyclerView.getItemAnimator() != null) {
mRecyclerView.getItemAnimator().endAnimations();
}
mAdapter.addMessage(message);
//smooth scroll
smoothScroller.setTargetPosition(mAdapter.getItemCount() - 1);
mRecyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
//we do an eventual automatic scroll (only if we are at the bottom of the recyclerview)
if(((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastVisibleItemPosition() == mAdapter.getItemCount()-2){
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount()-1);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import nie.translator.rtranslator.Global;
Expand Down Expand Up @@ -759,13 +762,24 @@ public void onMessage(GuiMessage message) {
super.onMessage(message);
if (message != null) {
int messageIndex = mAdapter.getMessageIndex(message.getMessageID());
if(messageIndex != -1){
mAdapter.setMessage(messageIndex, message);
if(messageIndex != -1) {
if((!mRecyclerView.isAnimating() && !mRecyclerView.getLayoutManager().isSmoothScrolling()) || message.isFinal()) {
if(message.isFinal()){
if(mRecyclerView.getItemAnimator() != null) {
mRecyclerView.getItemAnimator().endAnimations();
}
}
mAdapter.setMessage(messageIndex, message);
}
}else{
if(mRecyclerView.getItemAnimator() != null) {
mRecyclerView.getItemAnimator().endAnimations();
}
mAdapter.addMessage(message);
//smooth scroll
smoothScroller.setTargetPosition(mAdapter.getItemCount() - 1);
mRecyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
//we do an eventual automatic scroll (only if we are at the bottom of the recyclerview)
if(((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastVisibleItemPosition() == mAdapter.getItemCount()-2){
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount()-1);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
// we save every new message in the exchanged messages so that the fragment can restore them
WalkieTalkieService.super.addOrUpdateMessage(message);
//if the tts is not active we restart the mic here
if(tts == null || !CustomLocale.containsLanguage(ttsLanguages, languageOfText) || !tts.isActive() || isAudioMute){
if(isFinal && (tts == null || !CustomLocale.containsLanguage(ttsLanguages, languageOfText) || !tts.isActive() || isAudioMute)){
startVoiceRecorder();
notifyMicActivated();
}
Expand Down Expand Up @@ -344,7 +344,7 @@ public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
// we save every new message in the exchanged messages so that the fragment can restore them
WalkieTalkieService.super.addOrUpdateMessage(message);
//if the tts is not active we restart the mic here
if(tts == null || !CustomLocale.containsLanguage(ttsLanguages, languageOfText) || !tts.isActive() || isAudioMute){
if(isFinal && (tts == null || !CustomLocale.containsLanguage(ttsLanguages, languageOfText) || !tts.isActive() || isAudioMute)){
startVoiceRecorder();
notifyMicActivated();
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/res/layout/component_message_received.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tool:text="Ciao, come stai?"/>
tool:text="Ciao, come stai?"
tool:visibility="gone"/>

<LinearLayout
android:id="@+id/sender_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
android:visibility="gone"
tool:visibility="visible">

<TextView
android:id="@+id/text_sender"
Expand All @@ -75,7 +77,7 @@
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:fontFamily="@font/nunito_sans"
android:textColor="@color/very_dark_gray"
android:textColor="@color/primary"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -84,7 +86,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0"
tool:text="Alice"/>
tool:text="Alice" />

<TextView
android:id="@+id/text_content2"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_conversation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
style="@style/CustomCardViewStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
app:cardBackgroundColor="@color/white"
app:cardBackgroundColor="@color/accent_white"
app:cardElevation="0dp"
app:cardPreventCornerOverlap="false"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
Loading

0 comments on commit 8029c6c

Please sign in to comment.