aldes182

Latihan

Dec 16th, 2020
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.64 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="+booleanExternalStorageWriteable,
  186.                 Toast.LENGTH_SHORT
  187.                 ).show();
  188.     }
  189.     private void writeToSDFile(String tmp){
  190.         File FileRoot = android.os.Environment.getExternalStorageDirectory();
  191.         Toast.makeText
  192.         (
  193.         getApplicationContext(),
  194.         "External file system root: "
  195.         + FileRoot,Toast.LENGTH_LONG
  196.         ).show();
  197.  
  198.         File FileDir = new File
  199.         (
  200.         FileRoot.getAbsolutePath()
  201.         + "/download");
  202.         FileDir.mkdirs();
  203.         File File1 = new File
  204.         (
  205.         FileDir,
  206.         "myData.txt");
  207.  
  208.         try
  209.         {
  210.             FileOutputStream f = new FileOutputStream(File1,true);
  211.             PrintWriter pw = new PrintWriter(f);
  212.             pw.println(tmp);
  213.             pw.flush();
  214.             pw.close();
  215.             f.close();
  216.         }
  217.         catch (FileNotFoundException e)
  218.         {
  219.             e.printStackTrace();
  220.             Log.i
  221.             (
  222.             StringTAG,
  223.             "******* File not found. Did you"
  224.             + " add a WRITE_EXTERNAL_STORAGE permission to the manifest?"
  225.             );
  226.         }
  227.         catch (IOException e)
  228.         {
  229.             e.printStackTrace();
  230.         }
  231.             Toast.makeText
  232.             (
  233.             getApplicationContext(),
  234.             "\n" +
  235.             "\n" +
  236.             "File written to " +
  237.             file,Toast.LENGTH_SHORT
  238.             ).show();
  239.     }
  240.  
  241.     private void readRaw(){
  242.         File FileIs = new File
  243.             (
  244.             Environment.getExternalStorageDirectory()
  245.             .getAbsolutePath()+"/download", "myData.txt"
  246.             );
  247.  
  248.         if (FileIs.exists())
  249.         {
  250.             try
  251.                 {  
  252.                     FileInputStream FileInputStream1 = new FileInputStream(FileIs);
  253.                     BufferedReader BufferedReader1 = new BufferedReader
  254.                     (
  255.                     new InputStreamReader(FileInputStream1));
  256.                     String StringaDataRow = "";
  257.                     String StringaBuffer = "";
  258.                 while ((StringaDataRow = BufferedReader1.readLine()) != null)
  259.                 {
  260.                     StringaBuffer += StringaDataRow + "\n";
  261.                 }
  262.                     TextViewResult.setText(StringaBuffer);
  263.                     BufferedReader1.close();
  264.                     FileIs.createNewFile();
  265.             }
  266.             catch (IOException e)
  267.             { // TODO Auto-generated catch block
  268.                 e.printStackTrace();
  269.             }
  270.         }
  271.         else
  272.         {
  273.         Toast.makeText
  274.         (
  275.         getApplicationContext(),
  276.         "File Tidak Ditemukan",
  277.         Toast.LENGTH_SHORT
  278.         ).show();
  279.         }
  280.     }
  281.     private static ReadIP ReadIP1 = new ReadIP();
  282.     private static final String StringReadIP=ReadIP1.text;
  283.    
  284.     private void FunctionJsonParse()    //fungsi untuk mencari data buku berdasarkan key
  285.     {
  286.         String StringURL =  StringReadIP
  287.                             +"reportBuku.php?id="
  288.                             +StringKey;
  289.         Toast.makeText
  290.                 (
  291.                 getApplicationContext(),
  292.                 StringURL,
  293.                 Toast.LENGTH_SHORT
  294.                 ).show();
  295.         JsonObjectRequest JsonObjectRequest1 =  new JsonObjectRequest
  296.         (
  297.             Request.Method.GET,
  298.             StringURL,
  299.             null,
  300.                 new Response.Listener<JSONObject>()
  301.                 {
  302.                     @Override
  303.                     public void onResponse(JSONObject JSONObjectResponse)
  304.                     {
  305.                         try
  306.                         {
  307.                             JSONArray JSONArray1 = JSONObjectResponse.getJSONArray("data");
  308.                             for (int i=0; i < JSONArray1.length(); i++)
  309.                             {
  310.                                 JSONObject data = JSONArray1.getJSONObject(i);
  311.                                 String
  312.                                         StringArrayColumn1 = data.getString("0"),
  313.                                         StringArrayColumn2 = data.getString("1"),
  314.                                         StringArrayColumn3 = data.getString("2"),
  315.                                         StringArrayColumn4 = data.getString("3");
  316.                                 StringFind="Buku yang berjudul "
  317.                                         +   StringArrayColumn1
  318.                                         +   " dengan pengarang "
  319.                                         +   StringArrayColumn2
  320.                                         +   " Berada di "
  321.                                         +   StringArrayColumn3
  322.                                         +   " "
  323.                                         +   StringArrayColumn4;
  324.                             }
  325.                         }
  326.                         catch (JSONException e)
  327.                         {
  328.                             e.printStackTrace();
  329.                         }
  330.                     }
  331.                 },
  332.                 new Response.ErrorListener()
  333.                 {
  334.             @Override
  335.             public void onErrorResponse(VolleyError error)
  336.             {
  337.             error.printStackTrace();
  338.             }
  339.         }
  340.         );
  341.         RequestQueue1.add(JsonObjectRequest1);
  342.     }
  343. }
  344.  
Advertisement
Add Comment
Please, Sign In to add comment