Skip to content

Commit

Permalink
Added LogInterface usage into Sample application as an example
Browse files Browse the repository at this point in the history
  • Loading branch information
smarek committed Jul 16, 2015
1 parent 9164374 commit 908e251
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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://";
Expand Down Expand Up @@ -128,13 +133,19 @@ 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);
}

@Override
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);
}

Expand All @@ -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;
Expand All @@ -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()),
Expand Down

0 comments on commit 908e251

Please sign in to comment.