Skip to content

Commit

Permalink
Upload audio with word name done.
Browse files Browse the repository at this point in the history
  • Loading branch information
manimaran96 committed Feb 16, 2019
1 parent 3f2beff commit 224ba56
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
83 changes: 68 additions & 15 deletions app/src/main/java/com/manimaran/wikiaudio/view/EndlessAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
Expand All @@ -29,6 +30,8 @@
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.text.BidiFormatter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
Expand Down Expand Up @@ -188,8 +191,9 @@ private void getPermissionToRecordAudio() {
}
}

private void showPopup(final Activity activity, int pos) {
private void showPopup(final Activity activity, final int pos) {

isRecorded = false;
// Dialog init
myDialog = new Dialog(activity);
myDialog.setContentView(R.layout.pop_up_record_ui);
Expand Down Expand Up @@ -231,7 +235,7 @@ public void onFinish() {
{
player.stopPlaying();
txtSec.setText("00:10");
recorder.stopRecording(getFilename());
recorder.stopRecording(getFilename(RECORDED_FILENAME));

// Reverse animation
btnRecord.animate()
Expand Down Expand Up @@ -312,15 +316,18 @@ public void run() {
public void onClick(View view) {
if(isRecorded)
{
uploadAudioToWikiServer();
String uploadName = "ta-"+ itemList.get(pos)+ ".wav";
//String uploadName = "ta-mani123.wav";
getRecordedFilename(uploadName);
uploadAudioToWikiServer(uploadName);
}else
GeneralUtils.showToast(ctx, "Please record audio first");
}
});

}

private void uploadAudioToWikiServer() {
private void uploadAudioToWikiServer(final String uploadFileName) {

progressDialog = ProgressDialog.show(activity, "Upload Audio", "Uploading your file...", true);
if(pref.getCsrfToken() == null) {
Expand Down Expand Up @@ -348,7 +355,7 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
GeneralUtils.logoutAlert(activity);
} else {
pref.setCsrfToken(editToken);
completeUpload(editToken);
completeUpload(editToken, uploadFileName);
}
} catch (JSONException e) {
e.printStackTrace();
Expand All @@ -365,28 +372,30 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
public void onFailure(Call<ResponseBody> call, Throwable t) {
dismissDialog("Please check your connection!");
t.printStackTrace();

}
});
}else
completeUpload(pref.getCsrfToken());
completeUpload(pref.getCsrfToken(), uploadFileName);


}
private static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
String extension = url.substring(url.lastIndexOf(".")+1);
if (!TextUtils.isEmpty(extension)) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}


private void completeUpload(String editToken) {
private void completeUpload(String editToken , String uploadFileName) {

GeneralUtils.showToast(ctx, editToken);
String title = RECORDED_FILENAME;
String filePath = getFilename();
String title = uploadFileName;
String filePath = getUplaodFilename(uploadFileName);
Log.e("NAME OF FILE ", "Filename " + filePath);

// create upload service client
MediaWikiClient service = ServiceGenerator.createService(MediaWikiClient.class, ctx);
Expand Down Expand Up @@ -423,13 +432,28 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
{
uploadJSONObject = reader.getJSONObject("upload");
String result = uploadJSONObject.getString("result");
dismissDialog("Upload: " + result);
if(result.toLowerCase().contains("warning"))
{
String errMsg = "File name already exist";
dismissDialog(errMsg);
}else
{
dismissDialog("Upload: " + result);
if(myDialog != null && myDialog.isShowing())
myDialog.dismiss();
}
}else
{
if(reader.has("error"))
{
String errMsg = reader.getJSONObject("error").getString("info");
dismissDialog(errMsg);
}else if(reader.has("warnings"))
{
uploadJSONObject = reader.getJSONObject("upload");
String result = uploadJSONObject.getString("result");
String errMsg = "File name already exist";
dismissDialog(errMsg);
}
}
} catch (JSONException e) {
Expand Down Expand Up @@ -466,7 +490,7 @@ private void onPlayStatusChanged() {
player.stopPlaying();
btnPlayPause.setImageResource(R.drawable.ic_play);
} else {
player.startPlaying(getFilename(), new Callable() {
player.startPlaying(getFilename(RECORDED_FILENAME), new Callable() {
@Override
public Object call() throws Exception {
onPlayStatusChanged();
Expand Down Expand Up @@ -524,14 +548,43 @@ private void showMsg(String msg)
}

// Get record file name
private String getFilename() {
private String getFilename(String fileName) {
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filePath, "/Wiki/Audios");
if (!file.exists()) {
if(!file.mkdirs())
Log.d(TAG, "Not create directory!");
}
return file.getAbsolutePath() + "/" +RECORDED_FILENAME;
return file.getAbsolutePath() + "/" + fileName;
}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private String getUplaodFilename(String fileName) {

BidiFormatter myBidiFormatter = BidiFormatter.getInstance();
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filePath, "/Wiki/Audios/Upload");
if (!file.exists()) {
if(!file.mkdirs())
Log.d(TAG, "Not create directory!");
}
return myBidiFormatter.unicodeWrap(file.getAbsolutePath() + "/" + fileName);
}

private String getRecordedFilename(String newName)
{
String filePath = Environment.getExternalStorageDirectory().getPath() + "/Wiki/Audios";
File fileFrom = new File(filePath, RECORDED_FILENAME);
File fileToFolder = new File(filePath , "/Upload");
if (!fileToFolder.exists()) {
if(!fileToFolder.mkdirs())
Log.d(TAG, "Not create directory!");
}

File fileTo = new File(filePath + "/Upload", newName);
fileFrom.renameTo(fileTo);

return fileTo.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Call<ResponseBody> fetchRecords(
);


@GET("https://ta.wiktionary.org/w/api.php?action=query&format=json&list=categorymembers&utf8=1" +
@GET("w/api.php?action=query&format=json&list=categorymembers&utf8=1" +
"&cmtitle=பகுப்பு:அறுபட்ட_கோப்பு_இணைப்புகள்_உள்ள_பக்கங்கள்&lang=ta&cmlimit=50&cmsort=timestamp&cmdir=desc")
Call<ResponseBody> fetchUnAudioRecords();

Expand All @@ -68,6 +68,8 @@ Call<ResponseBody> uploadFile(

/*
https://commons.wikimedia.org/wiki/Commons:API/MediaWiki
errorformat=plaintext --> show error as plain text format
{{Listen
| filename = Accordion chords-01.ogg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class ServiceGenerator {

// Base Wiki Api Url
public static final String BASE_URL = "https://en.wiktionary.org/w/api.php/";
public static final String BASE_URL = "https://ta.wiktionary.org/";

private static Retrofit retrofit;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<string name="check_internet">Please check your internet connection!</string>
<string name="invalid_credential">Provide Valid Username &amp; Password</string>
<string name="select_language">Select Language</string>
<string name="select_language_response_msg">Know Your Language is %s</string>
<string name="select_language_response_msg">Now Your Language is %s</string>
</resources>

0 comments on commit 224ba56

Please sign in to comment.