Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to limit logging on wifi only #14

Open
wants to merge 2 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Added option to limit logging on wifi only

  • Loading branch information
linakis committed Dec 15, 2017
commit 755df7588b849a1cf898124bda5e56c66aa1a405
@@ -24,6 +24,7 @@ dependencies {
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.6.3'
testCompile 'com.fasterxml.jackson.core:jackson-annotations:2.6.3'

compile 'com.android.support:support-annotations:23.3.0'
compile 'com.jakewharton.timber:timber:4.1.2'
compile ('com.github.tony19:loggly-client:1.0.3') {
exclude group:'com.jakewharton.timber', module:'timber'
@@ -1,12 +1,12 @@
/**
* Copyright (C) 2015 Anthony K. Trinh
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,8 +15,18 @@
*/
package com.github.tony19.timber.loggly;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;

import com.github.tony19.loggly.ILogglyClient;
import com.github.tony19.loggly.LogglyClient;

import timber.log.Timber;

/**
@@ -31,6 +41,11 @@
private ILogglyClient.Callback handler;
private IFormatter formatter;

private boolean isLimitWifi = false;

private Context context;
private ConnectivityManager connectivityManager;

/**
* Creates a <a href="https://github.com/JakeWharton/timber">Timber</a>
* tree for posting messages to <a href="http://loggly.com">Loggly</a>
@@ -55,6 +70,37 @@ public LogglyTree(String token) {
this.formatter = formatter;
}

/**
* Limits the {@link LogglyTree} to only send logs while connected to WiFi or suppress them otherwise.
* @param context Context
* @return a {@link LogglyTree} that only logs over WiFi
*/
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public LogglyTree limitWifi(@NonNull Context context) {
isLimitWifi = true;

this.context = context;
this.connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

return this;
}

/**
* Determines if the device is connected to a WiFi network.
* @return true if device is connected to WiFi or false otherwise
*/
@SuppressLint("MissingPermission")
private boolean isOnWifi() {
if(context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)
== PackageManager.PERMISSION_GRANTED) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
return activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}
}
return false;
}

/**
* Writes a log message to its destination. Called for all level-specific methods by default.
*
@@ -65,7 +111,9 @@ public LogglyTree(String token) {
*/
@Override
protected void log(int priority, String tag, String message, Throwable t) {
loggly.log(formatter.format(priority, tag, message, t), handler);
if (!isLimitWifi || isOnWifi()) {
loggly.log(formatter.format(priority, tag, message, t), handler);
}
}

/**
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.