aldes182

Untitled

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