diff --git a/build.gradle b/build.gradle index 7a82cc95..7e6ff40c 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,11 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.3' + + classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -18,6 +20,7 @@ allprojects { repositories { jcenter() + maven { url "https://maven.google.com" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5192af6d..fbdbe684 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Mar 15 11:04:59 GMT 2017 +#Sat Feb 17 18:16:34 MYT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip diff --git a/library/src/main/java/com/readystatesoftware/chuck/internal/support/SearchHighlightUtil.java b/library/src/main/java/com/readystatesoftware/chuck/internal/support/SearchHighlightUtil.java new file mode 100644 index 00000000..46e7e532 --- /dev/null +++ b/library/src/main/java/com/readystatesoftware/chuck/internal/support/SearchHighlightUtil.java @@ -0,0 +1,44 @@ +package com.readystatesoftware.chuck.internal.support; + +import android.graphics.Color; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.style.BackgroundColorSpan; +import android.text.style.ForegroundColorSpan; +import android.text.style.UnderlineSpan; + +import java.util.ArrayList; +import java.util.List; + + +public class SearchHighlightUtil { + + public static SpannableStringBuilder format(String text, String criteria) { + List startIndexes = indexesOf(text, criteria); + return applySpannable(text, startIndexes, criteria.length()); + } + + private static List indexesOf(String text, String criteria) { + List startPositions = new ArrayList<>(); + int index = text.indexOf(criteria); + do { + startPositions.add(index); + index = text.indexOf(criteria, index + 1); + } while (index >= 0); + return startPositions; + } + + private static SpannableStringBuilder applySpannable(String text, List indexes, int length) { + SpannableStringBuilder builder = new SpannableStringBuilder(text); + for (Integer position : indexes) { + builder.setSpan(new UnderlineSpan(), + position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + builder.setSpan(new ForegroundColorSpan(Color.RED), + position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + builder.setSpan(new BackgroundColorSpan(Color.YELLOW), + position, position + length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + return builder; + } + +} \ No newline at end of file diff --git a/library/src/main/java/com/readystatesoftware/chuck/internal/ui/TransactionPayloadFragment.java b/library/src/main/java/com/readystatesoftware/chuck/internal/ui/TransactionPayloadFragment.java index c5b470d1..9c44d447 100644 --- a/library/src/main/java/com/readystatesoftware/chuck/internal/ui/TransactionPayloadFragment.java +++ b/library/src/main/java/com/readystatesoftware/chuck/internal/ui/TransactionPayloadFragment.java @@ -18,17 +18,22 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v7.widget.SearchView; import android.text.Html; import android.text.TextUtils; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.readystatesoftware.chuck.R; import com.readystatesoftware.chuck.internal.data.HttpTransaction; +import com.readystatesoftware.chuck.internal.support.SearchHighlightUtil; -public class TransactionPayloadFragment extends Fragment implements TransactionFragment { +public class TransactionPayloadFragment extends Fragment implements TransactionFragment, SearchView.OnQueryTextListener { public static final int TYPE_REQUEST = 0; public static final int TYPE_RESPONSE = 1; @@ -40,6 +45,7 @@ public class TransactionPayloadFragment extends Fragment implements TransactionF private int type; private HttpTransaction transaction; + private String originalBody; public TransactionPayloadFragment() { } @@ -57,6 +63,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); type = getArguments().getInt(ARG_TYPE); setRetainInstance(true); + setHasOptionsMenu(true); } @Override @@ -74,6 +81,19 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { populateUI(); } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + if(type == TYPE_RESPONSE){ + MenuItem searchMenuItem = menu.findItem(R.id.search); + searchMenuItem.setVisible(true); + SearchView searchView = (SearchView) searchMenuItem.getActionView(); + searchView.setOnQueryTextListener(this); + searchView.setIconifiedByDefault(true); + } + + super.onCreateOptionsMenu(menu, inflater); + } + @Override public void transactionUpdated(HttpTransaction transaction) { this.transaction = transaction; @@ -103,5 +123,22 @@ private void setText(String headersString, String bodyString, boolean isPlainTex } else { body.setText(bodyString); } + originalBody = body.getText().toString(); + + } + + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + String text = body.getText().toString(); + if (newText.trim().length() > 0 && text.contains(newText.trim())) + body.setText(SearchHighlightUtil.format(body.getText().toString(), newText)); + else + body.setText(originalBody); + return true; } } \ No newline at end of file diff --git a/library/src/main/res/menu/chuck_transaction.xml b/library/src/main/res/menu/chuck_transaction.xml index b9eeb258..f327b2de 100644 --- a/library/src/main/res/menu/chuck_transaction.xml +++ b/library/src/main/res/menu/chuck_transaction.xml @@ -15,16 +15,26 @@ ~ limitations under the License. --> - + + - - - - + android:title="@string/chuck_share" + app:showAsAction="always"> + + + +