Voice messages

This commit is contained in:
tom79 2019-06-26 11:00:59 +02:00
parent aa07a91fa9
commit f10ecf34d4
5 changed files with 62 additions and 21 deletions

View File

@ -110,4 +110,6 @@ dependencies {
implementation "net.gotev:uploadservice-okhttp:$uploadServiceVersion" implementation "net.gotev:uploadservice-okhttp:$uploadServiceVersion"
implementation "info.guardianproject.netcipher:netcipher:$netCipherVersion" implementation "info.guardianproject.netcipher:netcipher:$netCipherVersion"
implementation "info.guardianproject.netcipher:netcipher-okhttp3:$netCipherVersion" implementation "info.guardianproject.netcipher:netcipher-okhttp3:$netCipherVersion"
implementation 'com.github.adrielcafe:AndroidAudioRecorder:0.3.0'
} }

View File

@ -22,6 +22,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

View File

@ -53,6 +53,7 @@ import android.text.Html;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.InputType; import android.text.InputType;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -178,6 +179,10 @@ import app.fedilab.android.sqlite.CustomEmojiDAO;
import app.fedilab.android.sqlite.SearchDAO; import app.fedilab.android.sqlite.SearchDAO;
import app.fedilab.android.sqlite.Sqlite; import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO; import app.fedilab.android.sqlite.StatusStoredDAO;
import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
import static app.fedilab.android.helper.Helper.changeDrawableColor; import static app.fedilab.android.helper.Helper.changeDrawableColor;
@ -253,7 +258,8 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
private TextWatcher textWatcher; private TextWatcher textWatcher;
private int pollCountItem; private int pollCountItem;
private UploadServiceSingleBroadcastReceiver uploadReceiver; private UploadServiceSingleBroadcastReceiver uploadReceiver;
public static final int REQUEST_CAMERA_PERMISSION_RESULT = 1653;
public static final int SEND_VOICE_MESSAGE = 1423;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -898,6 +904,11 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
} }
break; break;
} }
case REQUEST_CAMERA_PERMISSION_RESULT: {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
recordAudio();
}
}
} }
} }
@ -1025,13 +1036,10 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
} }
} }
}else if(requestCode == Helper.REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK){ }else if (requestCode == SEND_VOICE_MESSAGE && resultCode == RESULT_OK) {
if (null != data) {
ArrayList<String> result = data Uri uri = Uri.fromFile(new File(getCacheDir() + "/fedialb_recorded_audio.wav"));
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); upload(TootActivity.this, uri, "fedialb_recorded_audio.wav");
toot_content.setText(result.get(0));
toot_content.setSelection(toot_content.getText().length());
}
}else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) { }else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
if( photo_editor) { if( photo_editor) {
Intent intent = new Intent(TootActivity.this, PhotoEditorActivity.class); Intent intent = new Intent(TootActivity.this, PhotoEditorActivity.class);
@ -1092,6 +1100,9 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
} }
index++; index++;
} }
File audioFile = new File(getCacheDir() + "/fedialb_recorded_audio.wav");
audioFile.delete();
if( !alreadyAdded){ if( !alreadyAdded){
toot_picture_container.setVisibility(View.VISIBLE); toot_picture_container.setVisibility(View.VISIBLE);
String url = attachment.getPreview_url(); String url = attachment.getPreview_url();
@ -1576,19 +1587,24 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
return true; return true;
case R.id.action_microphone: case R.id.action_microphone:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); PackageManager.PERMISSION_GRANTED) {
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); recordAudio();
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, } else {
getString(R.string.speech_prompt)); if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
try { Toast.makeText(this,
startActivityForResult(intent, Helper.REQ_CODE_SPEECH_INPUT); getString(R.string.audio), Toast.LENGTH_SHORT).show();
} catch (ActivityNotFoundException a) { }
Toasty.info(getApplicationContext(), requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
getString(R.string.speech_not_supported), }, REQUEST_CAMERA_PERMISSION_RESULT);
Toast.LENGTH_SHORT).show(); }
} else {
recordAudio();
} }
return true; return true;
case R.id.action_store: case R.id.action_store:
storeToot(true, true); storeToot(true, true);
@ -3359,4 +3375,23 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
return cwLength + contentLength; return cwLength + contentLength;
} }
private void recordAudio(){
String filePath = getCacheDir() + "/fedialb_recorded_audio.wav";
int color = getResources().getColor(R.color.mastodonC1);
AndroidAudioRecorder.with(this)
// Required
.setFilePath(filePath)
.setColor(color)
.setRequestCode(SEND_VOICE_MESSAGE)
// Optional
.setSource(AudioSource.MIC)
.setChannel(AudioChannel.STEREO)
.setSampleRate(AudioSampleRate.HZ_44100)
.setAutoStart(true)
.setKeepDisplayOn(true)
// Start recording
.record();
}
} }

View File

@ -28,7 +28,7 @@
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item <item
android:id="@+id/action_microphone" android:id="@+id/action_microphone"
android:title="@string/microphone" android:title="@string/voice_message"
android:icon="@drawable/ic_mic" android:icon="@drawable/ic_mic"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item <item

View File

@ -1081,6 +1081,9 @@
<string name="mark_unresolved">Mark as unresolved</string> <string name="mark_unresolved">Mark as unresolved</string>
<string name="toast_empty_content">Empty content!</string> <string name="toast_empty_content">Empty content!</string>
<string name="set_display_fedilab_features_button">Display Fedilab features button</string> <string name="set_display_fedilab_features_button">Display Fedilab features button</string>
<string name="audio">The application needs to access audio recording</string>
<string name="toot_message_record_error">An error occurred when recording the voice message!</string>
<string name="voice_message">Voice message</string>
<plurals name="number_of_vote"> <plurals name="number_of_vote">
<item quantity="one">%d vote</item> <item quantity="one">%d vote</item>
<item quantity="other">%d votes</item> <item quantity="other">%d votes</item>