Skip to content

Commit

Permalink
Audio sending file (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TienDungUpwork authored Apr 22, 2022
1 parent 6ffe779 commit 16150f7
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 82 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:7.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enablerD8=true

UIKIT_VERSION = 2.2.5
UIKIT_VERSION = 2.5.2
UIKIT_VERSION_CODE = 1

6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jan 02 16:41:55 KST 2020
#Sat Jan 22 20:25:07 IST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
zipStoreBase=GRADLE_USER_HOME
6 changes: 3 additions & 3 deletions uikit-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.sendbird.uikit.sample"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
Expand All @@ -32,8 +32,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation project(":uikit")
implementation "com.sendbird.sdk:uikit:2.2.5"
implementation project(":uikit")
// implementation "com.sendbird.sdk:uikit:2.2.5"

implementation "androidx.multidex:multidex:2.0.1"
implementation 'com.google.firebase:firebase-messaging:21.0.0'
Expand Down
1 change: 1 addition & 0 deletions uikit-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission
android:name="android.permission.VIBRATE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:name=".BaseApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class BaseApplication extends MultiDexApplication {

private static final String APP_ID = "2D7B4CDB-932F-4082-9B09-A1153792DC8D";
private static final String APP_ID = "DF3B1DDF-5616-4060-BD16-444B38320B27";
private static final MutableLiveData<InitState> initState = new MutableLiveData<>();

@Override
Expand Down
11 changes: 10 additions & 1 deletion uikit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ android {
targetSdkVersion 30
versionCode Integer.parseInt(UIKIT_VERSION_CODE)
versionName UIKIT_VERSION

buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
}

buildTypes {
Expand All @@ -19,7 +22,7 @@ android {

libraryVariants.all { variant ->
variant.outputs.all {
def aarName = "sendbird-uikit-${project.version}-${variant.buildType.name}.aar"
def aarName = "sendbird-uikit-${project.version}.aar"

println "\n============================================================"
println "project module : Sendbird UIKit"
Expand All @@ -34,6 +37,10 @@ android {
debug {
debuggable true
}

lintOptions {
abortOnError false
}
}
compileOptions {
sourceCompatibility = 1.8
Expand Down Expand Up @@ -72,4 +79,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'com.github.tougee:audiorecorderview:1.0.4'
}
35 changes: 35 additions & 0 deletions uikit/src/main/java/com/sendbird/uikit/AudioRecorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.sendbird.uikit;

import android.content.Context;
import android.media.MediaRecorder;
import android.net.Uri;

import java.io.File;
import java.io.IOException;

public class AudioRecorder {

private MediaRecorder recorder = null;

public File startRecording(Context context) {
try {
File output = File.createTempFile("audio_", ".mp3", context.getFilesDir());
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(output.getAbsolutePath());
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.prepare();
recorder.start();
return output;
} catch (IOException e) {
return null;
}
}

public void stopRecording() {
recorder.stop();
recorder.release();
recorder = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.sendbird.android.handlers.GroupChannelContext;
import com.sendbird.android.handlers.MessageCollectionHandler;
import com.sendbird.android.handlers.MessageContext;
import com.sendbird.uikit.AudioRecorder;
import com.sendbird.uikit.R;
import com.sendbird.uikit.SendBirdUIKit;
import com.sendbird.uikit.activities.ChannelSettingsActivity;
Expand Down Expand Up @@ -93,6 +94,7 @@
import com.sendbird.uikit.widgets.EmojiReactionUserListView;
import com.sendbird.uikit.widgets.MessageInputView;
import com.sendbird.uikit.widgets.PagerRecyclerView;
import com.tougee.recorderview.AudioRecordView;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -109,7 +111,7 @@
*/
public class ChannelFragment extends BaseGroupChannelFragment implements OnIdentifiableItemClickListener<BaseMessage>,
OnIdentifiableItemLongClickListener<BaseMessage>,
LoadingDialogHandler {
LoadingDialogHandler, AudioRecordView.Callback {

private static final int CAPTURE_IMAGE_PERMISSIONS_REQUEST_CODE = 2001;
private static final int PICK_IMAGE_PERMISSIONS_REQUEST_CODE = 2002;
Expand All @@ -126,10 +128,12 @@ public class ChannelFragment extends BaseGroupChannelFragment implements OnIdent

private final AtomicInteger tooltipMessageCount = new AtomicInteger();
private Uri mediaUri;
private File audioFile;

@Nullable
private BaseMessage targetMessage;

private AudioRecorder audioRecorder = new AudioRecorder();
private View.OnClickListener headerLeftButtonListener;
private View.OnClickListener headerRightButtonListener;
private OnItemClickListener<BaseMessage> profileClickListener;
Expand Down Expand Up @@ -758,6 +762,9 @@ private void initMessageInput() {
targetMessage = null;
}
});

binding.vgInputBox.initAudioRecordView(getActivity(), this);
binding.vgInputBox.setOnAudioLongClickListener();
}

private void onScrollEndReaches(PagerRecyclerView.ScrollDirection direction) {
Expand Down Expand Up @@ -1634,6 +1641,46 @@ private void setOnListItemLongClickListener(OnIdentifiableItemLongClickListener<
this.listItemLongClickListener = listItemLongClickListener;
}

@Override
public boolean isReady() {
return true;
}

@Override
public void onRecordCancel() {
binding.vgInputBox.setInputMode(MessageInputView.Mode.DEFAULT);
}

@Override
public void onRecordEnd() {
binding.vgInputBox.setInputMode(MessageInputView.Mode.DEFAULT);
audioRecorder.stopRecording();

if (audioFile != null) {
FileMessageParams params = new FileMessageParams();
FileInfo info = new FileInfo(
audioFile.getPath(),
(int) audioFile.length(),
"audio/mp3",
audioFile.getName(),
null,
0,
0,
null
);
params.setFile(audioFile);
viewModel.sendFileMessage(params, info);

clearInput();
scrollToBottom();
}
}

@Override
public void onRecordStart() {
audioFile = audioRecorder.startRecording(requireContext());
}

public static class Builder {
private final Bundle bundle;
private ChannelFragment customFragment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sendbird.uikit.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -10,6 +11,7 @@
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
Expand Down Expand Up @@ -38,6 +40,7 @@
import com.sendbird.uikit.utils.SoftInputUtils;
import com.sendbird.uikit.utils.TextUtils;
import com.sendbird.uikit.utils.ViewUtils;
import com.tougee.recorderview.AudioRecordView;

import java.lang.reflect.Field;

Expand All @@ -59,7 +62,7 @@ public class MessageInputView extends FrameLayout {
private boolean showSendButtonAlways;

public enum Mode {
DEFAULT, EDIT, QUOTE_REPLY
DEFAULT, EDIT, QUOTE_REPLY, AUDIO_RECORD
}

public MessageInputView(@NonNull Context context) {
Expand Down Expand Up @@ -148,8 +151,10 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (!TextUtils.isEmpty(s) && Mode.EDIT != getInputMode() || showSendButtonAlways) {
setSendButtonVisibility(View.VISIBLE);
setAudioButtonVisibility(View.GONE);
} else {
setSendButtonVisibility(View.GONE);
setAudioButtonVisibility(View.VISIBLE);
}
}

Expand All @@ -167,8 +172,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(s) && Mode.EDIT != getInputMode() || showSendButtonAlways) {
setSendButtonVisibility(View.VISIBLE);
setAudioButtonVisibility(View.GONE);
} else {
setSendButtonVisibility(View.GONE);
setAudioButtonVisibility(View.VISIBLE);
}
}
});
Expand All @@ -179,21 +186,37 @@ public void afterTextChanged(Editable s) {
}
}

public void initAudioRecordView(Activity activity, AudioRecordView.Callback callback) {
binding.recordView.setActivity(activity);
binding.recordView.setCallback(callback);
binding.recordView.setTimeoutSeconds(20);
}

public void setInputMode(@NonNull final Mode mode) {
final Mode before = this.mode;
this.mode = mode;
if (Mode.EDIT == mode) {
setQuoteReplyPanelVisibility(GONE);
setEditPanelVisibility(VISIBLE);
setAudioPanelVisibility(GONE);
binding.ibtnAdd.setVisibility(GONE);
} else if (Mode.QUOTE_REPLY == mode) {
setQuoteReplyPanelVisibility(VISIBLE);
setEditPanelVisibility(GONE);
setAudioPanelVisibility(GONE);
setAddButtonVisibility(addButtonVisibility);
} else if (Mode.AUDIO_RECORD == mode) {
setQuoteReplyPanelVisibility(GONE);
setEditPanelVisibility(GONE);
setAddButtonVisibility(GONE);
setAudioPanelVisibility(VISIBLE);
setInputPanelVisibility(GONE);
} else {
setQuoteReplyPanelVisibility(GONE);
setEditPanelVisibility(GONE);
setAddButtonVisibility(addButtonVisibility);
setAudioPanelVisibility(GONE);
setInputPanelVisibility(VISIBLE);
setAddButtonVisibility(VISIBLE);
}

if (inputModeChangedListener != null) {
Expand Down Expand Up @@ -262,11 +285,22 @@ public void setSendButtonVisibility(int visibility) {
binding.ibtnSend.setVisibility(visibility);
}

public void setAudioButtonVisibility(int visibility) {
binding.ibtnAudio.setVisibility(visibility);
}

public void setOnSendClickListener(OnClickListener sendClickListener) {
this.sendClickListener = sendClickListener;
binding.ibtnSend.setOnClickListener(sendClickListener);
}

public void setOnAudioLongClickListener() {
binding.ibtnAudio.setOnLongClickListener(v -> {
setInputMode(Mode.AUDIO_RECORD);
return true;
});
}

public void setSendImageResource(@DrawableRes int sendImageResource) {
binding.ibtnSend.setImageResource(sendImageResource);
}
Expand Down Expand Up @@ -305,6 +339,14 @@ public void setEditPanelVisibility(int visibility) {
binding.editPanel.setVisibility(visibility);
}

public void setAudioPanelVisibility(int visibility) {
binding.recordView.setVisibility(visibility);
}

public void setInputPanelVisibility(int visibility) {
binding.inputPanel.setVisibility(visibility);
}

public void setQuoteReplyPanelVisibility(int visibility) {
binding.quoteReplyPanel.setVisibility(visibility);
binding.ivReplyDivider.setVisibility(visibility);
Expand Down
10 changes: 10 additions & 0 deletions uikit/src/main/res/drawable/ic_voice.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/primary_400">
<path
android:fillColor="@android:color/white"
android:pathData="M12,15c1.66,0 2.99,-1.34 2.99,-3L15,6c0,-1.66 -1.34,-3 -3,-3S9,4.34 9,6v6c0,1.66 1.34,3 3,3zM17.3,12c0,3 -2.54,5.1 -5.3,5.1S6.7,15 6.7,12L5,12c0,3.42 2.72,6.23 6,6.72L11,22h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z"/>
</vector>
Loading

0 comments on commit 16150f7

Please sign in to comment.