From 0850a90e0c1bb03c4d2797b8a93f7e634a37b583 Mon Sep 17 00:00:00 2001 From: Roestzwiebler Date: Sat, 11 Nov 2023 14:39:12 +0100 Subject: [PATCH 01/16] merged e11 into main branch, upgraded agp to 8.1.3 --- .idea/misc.xml | 1 - build.gradle | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/build.gradle b/build.gradle index 0ebef73..db48ce8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '8.1.0' apply false - id 'com.android.library' version '8.1.0' apply false + id 'com.android.application' version '8.1.3' apply false + id 'com.android.library' version '8.1.3' apply false } \ No newline at end of file From 91e6c69fb14825996af644fe1cdf53ddb5c607cf Mon Sep 17 00:00:00 2001 From: Roestzwiebler Date: Sat, 11 Nov 2023 15:27:54 +0100 Subject: [PATCH 02/16] fixed error where app crashes if speech input is empty, added settings --- .../app_entwicklung/chatgpt/MainFragment.java | 36 ++++++++++--------- app/src/main/res/xml/root_preferences.xml | 20 ++++++++++- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java index 0d8dded..3e25518 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java @@ -34,24 +34,26 @@ public class MainFragment extends Fragment { private final ActivityResultLauncher getTextFromSpeech = registerForActivityResult( new LaunchSpeechRecognition(), query -> { - Message userMessage = new Message(Author.User, query); - chat.addMessage(userMessage); - if (chat.getMessages().size() > 1) { - getTextView().append(CHAT_SEPARATOR); + if (query != null) { + Message userMessage = new Message(Author.User, query); + chat.addMessage(userMessage); + if (chat.getMessages().size() > 1) { + getTextView().append(CHAT_SEPARATOR); + } + getTextView().append(toString(userMessage)); + + MainActivity.backgroundExecutorService.execute(() -> { + String apiToken = prefs.getApiToken(); + ChatGpt chatGpt = new ChatGpt(apiToken); + String answer = chatGpt.getChatCompletion(chat); + + Message answerMessage = new Message(Author.Assistant, answer); + chat.addMessage(answerMessage); + getTextView().append(CHAT_SEPARATOR); + getTextView().append(toString(answerMessage)); + textToSpeech.speak(answer); + }); } - getTextView().append(toString(userMessage)); - - MainActivity.backgroundExecutorService.execute(() -> { - String apiToken = prefs.getApiToken(); - ChatGpt chatGpt = new ChatGpt(apiToken); - String answer = chatGpt.getChatCompletion(chat); - - Message answerMessage = new Message(Author.Assistant, answer); - chat.addMessage(answerMessage); - getTextView().append(CHAT_SEPARATOR); - getTextView().append(toString(answerMessage)); - textToSpeech.speak(answer); - }); }); public MainFragment() { diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 9c31de1..04d1e46 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -1,4 +1,5 @@ - + @@ -9,5 +10,22 @@ app:useSimpleSummaryProvider="true" /> + + + + + \ No newline at end of file From 0acb352765f92b9dca7483752c1b0b6e3c04862c Mon Sep 17 00:00:00 2001 From: Roestzwiebler Date: Sun, 12 Nov 2023 14:24:25 +0100 Subject: [PATCH 03/16] added color, added username/gptname --- .../app_entwicklung/chatgpt/MainFragment.java | 18 ++++++++++++++++-- .../app_entwicklung/chatgpt/PrefsFacade.java | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java index 3e25518..fee5007 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java @@ -1,6 +1,9 @@ package de.fhdw.app_entwicklung.chatgpt; +import android.graphics.Color; import android.os.Bundle; +import android.text.Spannable; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -40,7 +43,8 @@ public class MainFragment extends Fragment { if (chat.getMessages().size() > 1) { getTextView().append(CHAT_SEPARATOR); } - getTextView().append(toString(userMessage)); + //getTextView().append(toString(userMessage)); + appendColoredText(getTextView(), prefs.getUsername() + ": " + toString(userMessage), Color.parseColor("#0000FF")); MainActivity.backgroundExecutorService.execute(() -> { String apiToken = prefs.getApiToken(); @@ -50,7 +54,8 @@ public class MainFragment extends Fragment { Message answerMessage = new Message(Author.Assistant, answer); chat.addMessage(answerMessage); getTextView().append(CHAT_SEPARATOR); - getTextView().append(toString(answerMessage)); + //getTextView().append(toString(answerMessage)); + appendColoredText(getTextView(), prefs.getGptName() + ": " + toString(answerMessage), Color.parseColor("#FF0000")); textToSpeech.speak(answer); }); } @@ -128,4 +133,13 @@ private Button getAskButton() { return getView().findViewById(R.id.button_ask); } + public static void appendColoredText(TextView tv, CharSequence text, int color) { + int start = tv.getText().length(); + tv.append(text); + int end = tv.getText().length(); + + Spannable spannableText = (Spannable) tv.getText(); + spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0); + } + } \ No newline at end of file diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/PrefsFacade.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/PrefsFacade.java index 58e130d..a06edf1 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/PrefsFacade.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/PrefsFacade.java @@ -17,4 +17,12 @@ public String getApiToken() { return PreferenceManager.getDefaultSharedPreferences(context).getString("api_token", ""); } + public String getUsername(){ + return PreferenceManager.getDefaultSharedPreferences(context).getString("username", ""); + } + + public String getGptName(){ + return PreferenceManager.getDefaultSharedPreferences(context).getString("gpt_name", ""); + } + } \ No newline at end of file From 90c82160a32ea6d73b488ba9de7d998d23134b77 Mon Sep 17 00:00:00 2001 From: Roestzwiebler Date: Sun, 12 Nov 2023 14:29:29 +0100 Subject: [PATCH 04/16] changed CharSequence to String --- .../java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java index fee5007..28072cc 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java @@ -119,7 +119,7 @@ private void updateTextView() { } } - private CharSequence toString(Message message) { + private String toString(Message message) { return message.message; } @@ -133,7 +133,7 @@ private Button getAskButton() { return getView().findViewById(R.id.button_ask); } - public static void appendColoredText(TextView tv, CharSequence text, int color) { + public static void appendColoredText(TextView tv, String text, int color) { int start = tv.getText().length(); tv.append(text); int end = tv.getText().length(); From 63fd9cddb698b1e2613172a34195bf44ef3bcb79 Mon Sep 17 00:00:00 2001 From: Roestzwiebler Date: Sun, 12 Nov 2023 17:53:57 +0100 Subject: [PATCH 05/16] added translations, edited Message class to allow color and names to save, edited tostring and updatetextview to restore save properly --- .../app_entwicklung/chatgpt/MainFragment.java | 16 +++++---- .../chatgpt/model/Message.java | 14 +++++--- app/src/main/res/layout/fragment_main.xml | 36 +++++++++++++++---- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java index 28072cc..bb4156e 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/MainFragment.java @@ -38,24 +38,24 @@ public class MainFragment extends Fragment { new LaunchSpeechRecognition(), query -> { if (query != null) { - Message userMessage = new Message(Author.User, query); + Message userMessage = new Message(Author.User, query, prefs.getUsername(), "#0000FF"); chat.addMessage(userMessage); if (chat.getMessages().size() > 1) { getTextView().append(CHAT_SEPARATOR); } //getTextView().append(toString(userMessage)); - appendColoredText(getTextView(), prefs.getUsername() + ": " + toString(userMessage), Color.parseColor("#0000FF")); + appendColoredText(getTextView(), toString(userMessage), Color.parseColor(userMessage.color)); MainActivity.backgroundExecutorService.execute(() -> { String apiToken = prefs.getApiToken(); ChatGpt chatGpt = new ChatGpt(apiToken); String answer = chatGpt.getChatCompletion(chat); - Message answerMessage = new Message(Author.Assistant, answer); + Message answerMessage = new Message(Author.Assistant, answer, prefs.getGptName(), "#FF0000"); chat.addMessage(answerMessage); getTextView().append(CHAT_SEPARATOR); //getTextView().append(toString(answerMessage)); - appendColoredText(getTextView(), prefs.getGptName() + ": " + toString(answerMessage), Color.parseColor("#FF0000")); + appendColoredText(getTextView(), toString(answerMessage), Color.parseColor(answerMessage.color)); textToSpeech.speak(answer); }); } @@ -111,16 +111,18 @@ private void updateTextView() { getTextView().setText(""); List messages = chat.getMessages(); if (!messages.isEmpty()) { - getTextView().append(toString(messages.get(0))); + appendColoredText(getTextView(), toString(messages.get(0)), Color.parseColor(messages.get(0).color)); + //getTextView().append(toString(messages.get(0))); for (int i = 1; i < messages.size(); i++) { getTextView().append(CHAT_SEPARATOR); - getTextView().append(toString(messages.get(i))); + //getTextView().append(toString(messages.get(i))); + appendColoredText(getTextView(), toString(messages.get(i)), Color.parseColor(messages.get(i).color)); } } } private String toString(Message message) { - return message.message; + return message.name + ": " + message.message; } private TextView getTextView() { diff --git a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/model/Message.java b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/model/Message.java index 6a131b3..8d8c21a 100644 --- a/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/model/Message.java +++ b/app/src/main/java/de/fhdw/app_entwicklung/chatgpt/model/Message.java @@ -9,19 +9,23 @@ public class Message implements Parcelable { public final Date date; public final Author author; public final String message; + public final String name; + public final String color; - public Message(Author author, String message) { - this(new Date(), author, message); + public Message(Author author, String message, String name, String color) { + this(new Date(), author, message, name, color); } - public Message(Date date, Author author, String message) { + public Message(Date date, Author author, String message, String name, String color) { this.date = date; this.author = author; this.message = message; + this.name = name; + this.color = color; } protected Message(Parcel in) { - this(new Date(in.readLong()), Author.valueOf(in.readString()), in.readString()); + this(new Date(in.readLong()), Author.valueOf(in.readString()), in.readString(), in.readString(), in.readString()); } @Override @@ -29,6 +33,8 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeLong(date.getTime()); dest.writeString(author.name()); dest.writeString(message); + dest.writeString(name); + dest.writeString(color); } @Override diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index 73c25fa..423d747 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -11,30 +11,54 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context=".MainActivity" > + tools:context=".MainActivity"> + app:layout_constraintTop_toTopOf="parent" />