From 908e251a3a21732eb83a4e460bfdc3e4268fe701 Mon Sep 17 00:00:00 2001 From: Marek Sebera Date: Thu, 16 Jul 2015 22:47:29 +0200 Subject: [PATCH] Added LogInterface usage into Sample application as an example --- CHANGELOG.md | 1 + .../http/sample/Redirect302Sample.java | 22 ++++++---- .../http/sample/SampleParentActivity.java | 41 +++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 331e439bf..a85df1098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Sample application now can be built with LeakCanary, use i.e. `gradle :sample:installWithLeakCanaryDebug` to use it - Updated RequestParams documentation on handling arrays, sets and maps, along with new RequestParamsDebug sample - Added BlackholeHttpResponseHandler implementation, which discards all response contents and silents all various log messages (see #416) + - Added LogInterface, it's default implementation and interface option to disable/enable logging library-wide and set logging verbosity ## 1.4.7 (released 9. 5. 2015) diff --git a/sample/src/main/java/com/loopj/android/http/sample/Redirect302Sample.java b/sample/src/main/java/com/loopj/android/http/sample/Redirect302Sample.java index 0907fac2b..69ce98abd 100644 --- a/sample/src/main/java/com/loopj/android/http/sample/Redirect302Sample.java +++ b/sample/src/main/java/com/loopj/android/http/sample/Redirect302Sample.java @@ -33,23 +33,27 @@ public class Redirect302Sample extends GetSample { private boolean enableRelativeRedirects = true; private boolean enableCircularRedirects = true; + private static final int MENU_ENABLE_REDIRECTS = 10; + private static final int MENU_ENABLE_CIRCULAR_REDIRECTS = 11; + private static final int MENU_ENABLE_RELATIVE_REDIRECTs = 12; + @Override public boolean onCreateOptionsMenu(Menu menu) { - menu.add(Menu.NONE, 0, Menu.NONE, "Enable redirects").setCheckable(true); - menu.add(Menu.NONE, 1, Menu.NONE, "Enable relative redirects").setCheckable(true); - menu.add(Menu.NONE, 2, Menu.NONE, "Enable circular redirects").setCheckable(true); + menu.add(Menu.NONE, MENU_ENABLE_REDIRECTS, Menu.NONE, "Enable redirects").setCheckable(true); + menu.add(Menu.NONE, MENU_ENABLE_RELATIVE_REDIRECTs, Menu.NONE, "Enable relative redirects").setCheckable(true); + menu.add(Menu.NONE, MENU_ENABLE_CIRCULAR_REDIRECTS, Menu.NONE, "Enable circular redirects").setCheckable(true); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { - MenuItem menuItemEnableRedirects = menu.findItem(0); + MenuItem menuItemEnableRedirects = menu.findItem(MENU_ENABLE_REDIRECTS); if (menuItemEnableRedirects != null) menuItemEnableRedirects.setChecked(enableRedirects); - MenuItem menuItemEnableRelativeRedirects = menu.findItem(1); + MenuItem menuItemEnableRelativeRedirects = menu.findItem(MENU_ENABLE_RELATIVE_REDIRECTs); if (menuItemEnableRelativeRedirects != null) menuItemEnableRelativeRedirects.setChecked(enableRelativeRedirects); - MenuItem menuItemEnableCircularRedirects = menu.findItem(2); + MenuItem menuItemEnableCircularRedirects = menu.findItem(MENU_ENABLE_CIRCULAR_REDIRECTS); if (menuItemEnableCircularRedirects != null) menuItemEnableCircularRedirects.setChecked(enableCircularRedirects); return super.onPrepareOptionsMenu(menu); @@ -59,11 +63,11 @@ public boolean onPrepareOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { if (item.isCheckable()) { item.setChecked(!item.isChecked()); - if (item.getItemId() == 0) { + if (item.getItemId() == MENU_ENABLE_REDIRECTS) { enableRedirects = item.isChecked(); - } else if (item.getItemId() == 1) { + } else if (item.getItemId() == MENU_ENABLE_RELATIVE_REDIRECTs) { enableRelativeRedirects = item.isChecked(); - } else if (item.getItemId() == 2) { + } else if (item.getItemId() == MENU_ENABLE_CIRCULAR_REDIRECTS) { enableCircularRedirects = item.isChecked(); } } diff --git a/sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java b/sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java index 9336bf2ba..1ee17502a 100755 --- a/sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java +++ b/sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java @@ -20,7 +20,9 @@ import android.annotation.TargetApi; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.graphics.Color; import android.os.Build; import android.os.Bundle; @@ -75,8 +77,11 @@ protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpCon private static final int MENU_USE_HTTPS = 0; private static final int MENU_CLEAR_VIEW = 1; + private static final int MENU_LOGGING_VERBOSITY = 2; + private static final int MENU_ENABLE_LOGGING = 3; private boolean useHttps = true; + private boolean enableLogging = true; protected static final String PROTOCOL_HTTP = "http://"; protected static final String PROTOCOL_HTTPS = "https://"; @@ -128,6 +133,10 @@ public boolean onPrepareOptionsMenu(Menu menu) { if (useHttpsMenuItem != null) { useHttpsMenuItem.setChecked(useHttps); } + MenuItem enableLoggingMenuItem = menu.findItem(MENU_ENABLE_LOGGING); + if (enableLoggingMenuItem != null) { + enableLoggingMenuItem.setChecked(enableLogging); + } return super.onPrepareOptionsMenu(menu); } @@ -135,6 +144,8 @@ public boolean onPrepareOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, MENU_USE_HTTPS, Menu.NONE, R.string.menu_use_https).setCheckable(true); menu.add(Menu.NONE, MENU_CLEAR_VIEW, Menu.NONE, R.string.menu_clear_view); + menu.add(Menu.NONE, MENU_ENABLE_LOGGING, Menu.NONE, "Enable Logging").setCheckable(true); + menu.add(Menu.NONE, MENU_LOGGING_VERBOSITY, Menu.NONE, "Set Logging Verbosity"); return super.onCreateOptionsMenu(menu); } @@ -146,6 +157,13 @@ public boolean onOptionsItemSelected(MenuItem item) { PROTOCOL = useHttps ? PROTOCOL_HTTPS : PROTOCOL_HTTP; urlEditText.setText(getDefaultURL()); return true; + case MENU_ENABLE_LOGGING: + enableLogging = !enableLogging; + getAsyncHttpClient().setLoggingEnabled(enableLogging); + return true; + case MENU_LOGGING_VERBOSITY: + showLoggingVerbosityDialog(); + return true; case MENU_CLEAR_VIEW: clearOutputs(); return true; @@ -172,6 +190,29 @@ public void addRequestHandle(RequestHandle handle) { } } + private void showLoggingVerbosityDialog() { + AlertDialog ad = new AlertDialog.Builder(this) + .setTitle("Set Logging Verbosity") + .setSingleChoiceItems(new String[]{ + "VERBOSE", + "DEBUG", + "INFO", + "WARN", + "ERROR", + "WTF" + }, getAsyncHttpClient().getLoggingLevel() - 2, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getAsyncHttpClient().setLoggingLevel(which + 2); + dialog.dismiss(); + } + }) + .setCancelable(true) + .setNeutralButton("Cancel", null) + .create(); + ad.show(); + } + public void onRunButtonPressed() { addRequestHandle(executeSample(getAsyncHttpClient(), getUrlText(getDefaultURL()),