aldes182

Untitled

Dec 16th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.43 KB | None | 0 0
  1. package com.sriyanksiddhartha.speechtotext;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.os.Handler;
  7. import android.speech.RecognizerIntent;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.EditText;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.android.volley.Request;
  18. import com.android.volley.RequestQueue;
  19. import com.android.volley.Response;
  20. import com.android.volley.VolleyError;
  21. import com.android.volley.toolbox.JsonObjectRequest;
  22. import com.android.volley.toolbox.Volley;
  23. import com.mapzen.speakerbox.Speakerbox;
  24.  
  25. import org.json.JSONArray;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.io.BufferedReader;
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.FileNotFoundException;
  33. import java.io.FileOutputStream;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.PrintWriter;
  38. import java.util.ArrayList;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. import java.util.Locale;
  42.  
  43. public class MainActivity extends AppCompatActivity {
  44.  
  45.     private TextView TextViewResult;
  46.     private TextView TextViewPanduan;
  47.     private ListView ListViewMsg;
  48.     private View ViewButtonSend;
  49.     private EditText EditText1;
  50.     private EditText EditTextMsg;
  51.     //--
  52.     private RequestQueue RequestQueue1;
  53.     //--
  54.     private static final String StringTAG = "MEDIA";
  55.     String StringKey="",StringFind="";
  56.     List<String> ListItems=new ArrayList<String>();
  57.  
  58.  
  59.  
  60.     boolean booleanisMine = true;
  61.     private List<ChatMessage> ListChatMessages;
  62.     private ArrayAdapter<ChatMessage> ArrayAdapterMessage;
  63.  
  64.     @Override
  65.     protected void onCreate(Bundle savedInstanceState) {
  66.         super.onCreate(savedInstanceState);
  67.         setContentView(R.layout.activity_main);
  68.         TextViewResult = (TextView) findViewById(R.id.txvResult);
  69.         TextViewPanduan = (TextView) findViewById(R.id.txtPanduan);
  70.         TextViewResult.setVisibility(View.INVISIBLE);
  71.         TextViewPanduan.setText("Panduan Disini");
  72.  
  73.         ListChatMessages = new ArrayList<>();
  74.         ListViewMsg = (ListView) findViewById(R.id.list_msg);
  75.         ViewButtonSend = findViewById(R.id.btn_chat_send);
  76.         EditTextMsg = (EditText) findViewById(R.id.msg_type);
  77.         //set ListView adapter first
  78.         adapter = new MessageAdapter(this, R.layout.item_chat_left, ListChatMessages);
  79.         ListViewMsg.setAdapter(adapter);
  80.  
  81.     }
  82.  
  83.     public void getSpeechInput(View view) {
  84.  
  85.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  86.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  87.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  88.  
  89.         if (intent.resolveActivity(getPackageManager()) != null) {
  90.             startActivityForResult(intent, 10);
  91.         } else {
  92.             Toast.makeText
  93.                     (
  94.                             this,
  95.                             "Perangkat Kamu Tidak Kompatible !!!",
  96.                             Toast.LENGTH_SHORT
  97.                     ).show();
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     protected void onActivityResult(int IntrequestCode, int IntresultCode, Intent IntentData) {
  103.         super.onActivityResult(IntrequestCode, IntResultCode, IntentData);
  104.  
  105.         switch (IntRequestCode) {
  106.             case 10:
  107.                 if (IntresultCode == RESULT_OK && IntentData != null)
  108.                 {
  109.                     ListViewMsg.setVisibility(View.VISIBLE);
  110.                     TextViewPanduan.setVisibility(View.GONE);
  111.                     ArrayList<String> result = IntentData.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //merubah suara ke text
  112.                     TextViewResult.setText(result.get(0)); //menampilkan hasil suara yang kita ucapkan dalam bentuk text
  113.  
  114.                     StringKey=result.get(0).toLowerCase();
  115.                     if(StringKey.equals("terima kasih")
  116.                                         || StringKey.equals("makasih") ||
  117.                                                 (
  118.                                                 (StringKey.contains("terima kasih") || StringKey.contains("makasih")
  119.                                                 )
  120.                                         && !StringKey.contains("berjudul")) )
  121.                     {
  122.                             StringFind="sama-sama";
  123.                     }
  124.                     else if(!StringKey.isEmpty()){
  125.                         RequestQueue1 = Volley.newRequestQueue(getApplicationContext()); //volley untuk mencari data buku
  126.                         FunctionJsonParse(); // fungsi untuk mencari buku
  127.                         if(StringFind.isEmpty()){
  128.                             StringFind="Keyword yang anda masukan belum tersedia, "
  129.                                     + "Silahkan bertanya dengan format 'buku berjudul "
  130.                                     + "<judul buku> pengarang <nama pengarang>' atau "
  131.                                     + "'judul <judul buku> karangan <nama pengarang>'";
  132.                         }
  133.                     }
  134.                     ChatMessage ChatMessage1 = new ChatMessage(StringKey, booleanisMine);
  135.                     ListChatMessages.add(ChatMessage1);
  136.                     ArrayAdapterMessage.notifyDataSetChanged();
  137.                     booleanisMine = false;
  138.  
  139.                     Runnable r = new Runnable() {
  140.                         @Override
  141.                         public void run(){
  142.                             Speakerbox speakerbox = new Speakerbox(getApplication());
  143.                             speakerbox.play(StringFind); //perintah untuk mengucapkan buku yang dicari
  144.                             ChatMessage ChatMessage1 = new ChatMessage(StringFind, booleanisMine);
  145.                             ListChatMessages.add(ChatMessage1);
  146.                             ArrayAdapterMessage.notifyDataSetChanged();
  147.                             booleanisMine = true;
  148.                             StringFind="";
  149.                         }
  150.                     };
  151.                     Handler handler1 = new Handler();
  152.                     handler1.postDelayed(r, 2500);
  153.  
  154.                 }
  155.                 break;
  156.         }
  157.     }
  158.  
  159.  
  160.     private void checkExternalMedia(){
  161.         boolean booleanExternalStorageAvailable = false;
  162.         boolean booleanExternalStorageWriteable = false;
  163.         String state = Environment.getExternalStorageState();
  164.  
  165.         if (Environment.MEDIA_MOUNTED.equals(state))
  166.         {
  167.             booleanExternalStorageAvailable = booleanExternalStorageWriteable = true;
  168.         }
  169.         else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
  170.         {
  171.             booleanExternalStorageAvailable = true;
  172.             booleanExternalStorageWriteable = false;
  173.         }
  174.         else
  175.             {
  176.             booleanExternalStorageAvailable = booleanExternalStorageWriteable = false;
  177.         }
  178.         Toast.makeText(
  179.                     getApplicationContext(),
  180.                     "\n\nExternal Media: readable="
  181.                 +   booleanExternalStorageAvailable
  182.                 +   " writable="+booleanExternalStorageWriteable,
  183.                 Toast.LENGTH_SHORT
  184.                 ).show();
  185.     }
  186.  
  187.     private void writeToSDFile(String tmp){
  188.  
  189.         File FileRoot = android.os.Environment.getExternalStorageDirectory();
  190.         Toast.makeText(
  191.                 getApplicationContext(),
  192.                 "External file system root: "
  193.                         +FileRoot,Toast.LENGTH_LONG
  194.         ).show();
  195.  
  196.         File FileDir = new File (
  197.                 FileRoot.getAbsolutePath()
  198.                 + "/download");
  199.         FileDir.mkdirs();
  200.         File File1 = new File(
  201.                 FileDir,
  202.                 "myData.txt");
  203.  
  204.         try {
  205.             FileOutputStream f = new FileOutputStream(File1,true);
  206.             PrintWriter pw = new PrintWriter(f);
  207.             pw.println(tmp);
  208.             pw.flush();
  209.             pw.close();
  210.             f.close();
  211.         } catch (FileNotFoundException e) {
  212.             e.printStackTrace();
  213.             Log.i(StringTAG,
  214.                     "******* File not found. Did you"
  215.                     + " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
  216.         }
  217.         catch (IOException e)
  218.         {
  219.             e.printStackTrace();
  220.         }
  221.         Toast.makeText(
  222.                         getApplicationContext(),
  223.                         "\n" +
  224.                         "\n" +
  225.                         "File written to " +
  226.                                 file,Toast.LENGTH_SHORT).show();
  227.     }
  228.  
  229.     private void readRaw(){
  230.  
  231.         File FileIs = new File(Environment.getExternalStorageDirectory()
  232.                 .getAbsolutePath()+"/download", "myData.txt");
  233.         if (FileIs.exists())
  234.         {
  235.             try
  236.             { FileInputStream fIn = new FileInputStream(FileIs);
  237.                 BufferedReader myReader = new BufferedReader(
  238.                         new InputStreamReader(fIn));
  239.                 String StringaDataRow = "";
  240.                 String aBuffer = "";
  241.                 while ((StringaDataRow = myReader.readLine()) != null) {
  242.                     aBuffer += StringaDataRow + "\n";}
  243.                 TextViewResult.setText(aBuffer);
  244.                 myReader.close();
  245.                 is.createNewFile();
  246.             }
  247.             catch (IOException e)
  248.             {
  249.                 // TODO Auto-generated catch block
  250.                 e.printStackTrace();
  251.             }
  252.  
  253.         }else{
  254.                     Toast.makeText
  255.                     (
  256.                     getApplicationContext(),
  257.                     "File Tidak Ditemukan",
  258.                     Toast.LENGTH_SHORT
  259.                     ).show();
  260.         }
  261.     }
  262.  
  263.     private static ReadIP ReadIP1 = new ReadIP();
  264.     private static final String StringReadIP=ReadIP1.text;
  265.     //fungsi untuk mencari data buku berdasarkan key
  266.     private void FunctionJsonParse(){
  267.         //Toast.makeText(getApplicationContext(),"JSON",Toast.LENGTH_SHORT).show();
  268.         String StringURL = StringReadIP+"reportBuku.php?id="+StringKey;
  269.         Toast.makeText(getApplicationContext(),StringURL,Toast.LENGTH_SHORT).show();
  270.         JsonObjectRequest JsonObjectRequest1 =  new JsonObjectRequest(Request.Method.GET, StringURL, null,
  271.                 new Response.Listener<JSONObject>() {
  272.                     @Override
  273.                     public void onResponse(JSONObject JSONObjectResponse) {
  274.                         try{
  275.                             JSONArray JSONArray1= JSONObjectResponse.getJSONArray("data");
  276.  
  277.                             for (int i=0; i < JSONArray1.length(); i++){
  278.                                 JSONObject data = JSONArray1.getJSONObject(i);
  279.                                 String
  280.                                         StringArrayColumn1 = data.getString("0"),
  281.                                         StringArrayColumn2 = data.getString("1"),
  282.                                         StringArrayColumn3 = data.getString("2"),
  283.                                         StringArrayColumn4 = data.getString("3");
  284.  
  285.  
  286.                                 StringFind="Buku yang berjudul "
  287.                                         +   StringArrayColumn1
  288.                                         +   " dengan pengarang "
  289.                                         +   StringArrayColumn2
  290.                                         +   " Berada di "
  291.                                         +   StringArrayColumn3
  292.                                         +   " "
  293.                                         +   StringArrayColumn4;
  294.                                 ;
  295.                             }
  296.                         }catch (JSONException e) {
  297.                             e.printStackTrace();
  298.                         }
  299.                     }
  300.                 }, new Response.ErrorListener() {
  301.             @Override
  302.             public void onErrorResponse(VolleyError error) {
  303.                 error.printStackTrace();
  304.             }
  305.         });
  306.         RequestQueue1.add(JsonObjectRequest1);
  307.     }
  308. }
  309.  
Add Comment
Please, Sign In to add comment