From 53e5bd7631a885962b1f1401f72dc488f769fad3 Mon Sep 17 00:00:00 2001 From: Etash Guha Date: Tue, 20 Nov 2018 12:32:56 -0500 Subject: [PATCH 1/3] getting dictionary messages --- ...radle__com_android_volley_volley_1_0_0.xml | 12 ++++ app/app.iml | 1 + app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 2 + .../example/etashguha/etude/Dictionary.java | 65 +++++++++++++++++++ .../example/etashguha/etude/MainActivity.java | 10 +++ .../com/example/etashguha/etude/Reader.java | 22 ++++++- 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 .idea/libraries/Gradle__com_android_volley_volley_1_0_0.xml create mode 100644 app/src/main/java/com/example/etashguha/etude/Dictionary.java diff --git a/.idea/libraries/Gradle__com_android_volley_volley_1_0_0.xml b/.idea/libraries/Gradle__com_android_volley_volley_1_0_0.xml new file mode 100644 index 0000000..da73c4d --- /dev/null +++ b/.idea/libraries/Gradle__com_android_volley_volley_1_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 5b1caba..b0d381a 100644 --- a/app/app.iml +++ b/app/app.iml @@ -139,6 +139,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index bb6dc44..84a7cca 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,6 +23,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) api 'com.android.support:design:28.0.0-alpha3' implementation 'com.google.code.gson:gson:2.8.5' + implementation 'com.android.volley:volley:1.0.0' implementation 'com.github.barteksc:android-pdf-viewer:2.8.2' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ae2bd5e..e031b9f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/example/etashguha/etude/Dictionary.java b/app/src/main/java/com/example/etashguha/etude/Dictionary.java new file mode 100644 index 0000000..47a0cec --- /dev/null +++ b/app/src/main/java/com/example/etashguha/etude/Dictionary.java @@ -0,0 +1,65 @@ +package com.example.etashguha.etude; + +import android.app.Activity; +import android.net.Uri; +import android.os.Message; +import android.util.Log; + +import com.android.volley.AuthFailureError; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; + +import java.util.HashMap; +import java.util.Map; + +public class Dictionary extends Thread { + + Activity currentActivity; + Reader.DictionaryHandler handler; + String word; + public Dictionary(Activity activity, Reader.DictionaryHandler dictionaryHandler, String word){ + currentActivity = activity; + handler = dictionaryHandler; + this.word = word; + } + + public void run(){ + RequestQueue requestQueue = Volley.newRequestQueue(currentActivity); + String toParse = "https://wordsapiv1.p.mashape.com/words/" + word + "/definitions"; + String uri = Uri.parse(toParse) + .buildUpon() + .build().toString(); + + StringRequest stringRequest = new StringRequest( + Request.Method.GET, uri, new Response.Listener() { + @Override + public void onResponse(String response) { + Message message = new Message(); + message.obj = response; + handler.handleMessage(message); + } + + }, new Response.ErrorListener() { + + @Override + public void onErrorResponse(VolleyError error) { + Log.e("VolleyError", error.toString()); + } + + }) { + + @Override + public Map getHeaders() throws AuthFailureError { + Map params = new HashMap<>(); + params.put("X-Mashape-Key", "BNrk40KgrTmshu76EmLWGokuzqvXp1JgmKbjsnrYxFk7id8pWk"); + params.put("Accept", "text/plain"); + return params; + } + }; + requestQueue.add(stringRequest); + } +} diff --git a/app/src/main/java/com/example/etashguha/etude/MainActivity.java b/app/src/main/java/com/example/etashguha/etude/MainActivity.java index b66b931..7049177 100644 --- a/app/src/main/java/com/example/etashguha/etude/MainActivity.java +++ b/app/src/main/java/com/example/etashguha/etude/MainActivity.java @@ -9,12 +9,20 @@ public class MainActivity extends AppCompatActivity { MaterialButton button; + MaterialButton cancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.next_button); + cancel = findViewById(R.id.cancel_button); + cancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + } + }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -23,4 +31,6 @@ public void onClick(View v) { } }); } + + } diff --git a/app/src/main/java/com/example/etashguha/etude/Reader.java b/app/src/main/java/com/example/etashguha/etude/Reader.java index 9456a03..c14cba3 100644 --- a/app/src/main/java/com/example/etashguha/etude/Reader.java +++ b/app/src/main/java/com/example/etashguha/etude/Reader.java @@ -9,6 +9,7 @@ import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; @@ -21,9 +22,11 @@ public class Reader extends AppCompatActivity { PDFView pdfView; PausePlay pausePlayState = PausePlay.PAUSED; int pageNumber = 0; + Dictionary dictionary; Screenshot screenshot; boolean firstTimePlaying; - Reader.SSHandler ssHandler; + SSHandler ssHandler; + DictionaryHandler dictionaryHandler; Player player; BottomNavigationView bottomNavigationView; ProgressBar progBar; @@ -41,10 +44,15 @@ protected void onCreate(Bundle savedInstanceState) { progBar = findViewById(R.id.progressBar); progBar.setVisibility(View.INVISIBLE); firstTimePlaying = true; + dictionaryHandler = new DictionaryHandler(); StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); + pdfView.fromUri(uri).pages(pageNumber).load(); + dictionaryHandler = new DictionaryHandler(); + dictionary = new Dictionary(baseActivity, dictionaryHandler, "Chicken"); + dictionary.run(); bottomNavigationView = findViewById(R.id.bottomNavigation); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override @@ -159,4 +167,16 @@ public void handleMessage(Message msg){ } } } + + public class DictionaryHandler extends Handler{ + + public DictionaryHandler(){ + super(); + } + + @Override + public void handleMessage(Message msg) { + Log.d("MainActivity", "response: " + msg.obj); + } + } } From e9090f5aa76475b10d17919d29ffc17107d0e33b Mon Sep 17 00:00:00 2001 From: Etash Guha Date: Wed, 21 Nov 2018 06:30:29 -0500 Subject: [PATCH 2/3] Working bottom sheet --- .../com/example/etashguha/etude/Reader.java | 44 +++++++++-- app/src/main/res/layout/reader.xml | 79 +++++++++++++++---- 2 files changed, 100 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/example/etashguha/etude/Reader.java b/app/src/main/java/com/example/etashguha/etude/Reader.java index c14cba3..a3dcde4 100644 --- a/app/src/main/java/com/example/etashguha/etude/Reader.java +++ b/app/src/main/java/com/example/etashguha/etude/Reader.java @@ -8,11 +8,15 @@ import android.os.StrictMode; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; +import android.support.design.widget.BottomSheetBehavior; +import android.support.design.widget.CoordinatorLayout; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; +import android.widget.ScrollView; +import android.widget.TextView; import com.github.barteksc.pdfviewer.PDFView; @@ -20,7 +24,7 @@ public class Reader extends AppCompatActivity { PDFView pdfView; - PausePlay pausePlayState = PausePlay.PAUSED; + PausePlay pausePlayState; int pageNumber = 0; Dictionary dictionary; Screenshot screenshot; @@ -31,28 +35,49 @@ public class Reader extends AppCompatActivity { BottomNavigationView bottomNavigationView; ProgressBar progBar; Activity baseActivity; + TextView definition; + BottomSheetBehavior behavior; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reader); - baseActivity = this; - player = new Player(""); - final Uri uri = getIntent().getData(); pdfView = findViewById(R.id.pdfView); progBar = findViewById(R.id.progressBar); + definition = findViewById(R.id.definition); progBar.setVisibility(View.INVISIBLE); + CoordinatorLayout coordinatorLayout = findViewById(R.id.main_content); + View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet); + + baseActivity = this; + player = new Player(""); + final Uri uri = getIntent().getData(); firstTimePlaying = true; dictionaryHandler = new DictionaryHandler(); + pausePlayState = PausePlay.PAUSED; StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); + behavior = BottomSheetBehavior.from(bottomSheet); + behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { + @Override + public void onStateChanged(@NonNull View bottomSheet, int newState) { + // React to state change + Log.e("onStateChanged", "onStateChanged:" + newState); + } + @Override + public void onSlide(@NonNull View bottomSheet, float slideOffset) { + // React to dragging events + Log.e("onSlide", "onSlide"); + } + }); + + behavior.setPeekHeight(0); + behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); pdfView.fromUri(uri).pages(pageNumber).load(); dictionaryHandler = new DictionaryHandler(); - dictionary = new Dictionary(baseActivity, dictionaryHandler, "Chicken"); - dictionary.run(); bottomNavigationView = findViewById(R.id.bottomNavigation); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override @@ -89,6 +114,8 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { pageNumber++; pdfView.fromUri(uri).pages(pageNumber).load(); sideButtonReset(); + dictionary = new Dictionary(baseActivity, dictionaryHandler, "Chicken"); + dictionary.run(); } return false; } @@ -176,7 +203,10 @@ public DictionaryHandler(){ @Override public void handleMessage(Message msg) { - Log.d("MainActivity", "response: " + msg.obj); + String response = (String)msg.obj; + definition.setText(response.substring(response.indexOf("\"definition\":\"") + 14, response.indexOf("\",\"partOfSpeech"))); + + behavior.setState(BottomSheetBehavior.STATE_EXPANDED); } } } diff --git a/app/src/main/res/layout/reader.xml b/app/src/main/res/layout/reader.xml index 138a966..f6d12f8 100644 --- a/app/src/main/res/layout/reader.xml +++ b/app/src/main/res/layout/reader.xml @@ -7,7 +7,6 @@ android:layout_height="match_parent" tools:context=".Reader"> - - - - + tools:layout_editor_absoluteX="0dp"> + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From ea3512b53edb768464299f4253cb5f76bbbc9f59 Mon Sep 17 00:00:00 2001 From: Etash Guha Date: Fri, 23 Nov 2018 10:13:37 -0500 Subject: [PATCH 3/3] Testing coordinates --- app/app.iml | 1 + .../com/example/etashguha/etude/Reader.java | 23 ++++++++++++++----- app/src/main/res/layout/reader.xml | 3 +++ build.gradle | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/app.iml b/app/app.iml index b0d381a..62cba90 100644 --- a/app/app.iml +++ b/app/app.iml @@ -92,6 +92,7 @@ + diff --git a/app/src/main/java/com/example/etashguha/etude/Reader.java b/app/src/main/java/com/example/etashguha/etude/Reader.java index a3dcde4..fac3e85 100644 --- a/app/src/main/java/com/example/etashguha/etude/Reader.java +++ b/app/src/main/java/com/example/etashguha/etude/Reader.java @@ -7,12 +7,14 @@ import android.os.Message; import android.os.StrictMode; import android.support.annotation.NonNull; +import android.support.constraint.ConstraintLayout; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.BottomSheetBehavior; import android.support.design.widget.CoordinatorLayout; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.widget.ProgressBar; import android.widget.ScrollView; @@ -26,7 +28,6 @@ public class Reader extends AppCompatActivity { PDFView pdfView; PausePlay pausePlayState; int pageNumber = 0; - Dictionary dictionary; Screenshot screenshot; boolean firstTimePlaying; SSHandler ssHandler; @@ -36,7 +37,10 @@ public class Reader extends AppCompatActivity { ProgressBar progBar; Activity baseActivity; TextView definition; + ConstraintLayout baseLayout; BottomSheetBehavior behavior; + CoordinatorLayout coordinatorLayout; + View bottomSheet; @Override protected void onCreate(Bundle savedInstanceState) { @@ -47,8 +51,9 @@ protected void onCreate(Bundle savedInstanceState) { progBar = findViewById(R.id.progressBar); definition = findViewById(R.id.definition); progBar.setVisibility(View.INVISIBLE); - CoordinatorLayout coordinatorLayout = findViewById(R.id.main_content); - View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet); + coordinatorLayout = findViewById(R.id.main_content); + bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet); + baseLayout = findViewById(R.id.container); baseActivity = this; player = new Player(""); @@ -59,7 +64,7 @@ protected void onCreate(Bundle savedInstanceState) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); - behavior = BottomSheetBehavior.from(bottomSheet); + behavior = BottomSheetBehavior.from(bottomSheet); behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { @@ -73,6 +78,14 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) { } }); + baseLayout.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + Log.d("coordinates", event.getX() + " " + event.getY()); + return false; + } + }); + behavior.setPeekHeight(0); behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); @@ -114,8 +127,6 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { pageNumber++; pdfView.fromUri(uri).pages(pageNumber).load(); sideButtonReset(); - dictionary = new Dictionary(baseActivity, dictionaryHandler, "Chicken"); - dictionary.run(); } return false; } diff --git a/app/src/main/res/layout/reader.xml b/app/src/main/res/layout/reader.xml index f6d12f8..c612786 100644 --- a/app/src/main/res/layout/reader.xml +++ b/app/src/main/res/layout/reader.xml @@ -4,6 +4,9 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" + android:clickable="true" + android:focusable="true" + android:focusableInTouchMode="true" android:layout_height="match_parent" tools:context=".Reader"> diff --git a/build.gradle b/build.gradle index 43c0708..96dccc7 100644 --- a/build.gradle +++ b/build.gradle @@ -8,11 +8,11 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' - // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } + } allprojects {