From 755df7588b849a1cf898124bda5e56c66aa1a405 Mon Sep 17 00:00:00 2001 From: Nikos Linakis Date: Fri, 15 Dec 2017 21:23:50 +0200 Subject: [PATCH 1/2] Added option to limit logging on wifi only --- timber-loggly/build.gradle | 1 + .../tony19/timber/loggly/LogglyTree.java | 58 +++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/timber-loggly/build.gradle b/timber-loggly/build.gradle index e267620..99f776a 100644 --- a/timber-loggly/build.gradle +++ b/timber-loggly/build.gradle @@ -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' diff --git a/timber-loggly/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java b/timber-loggly/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java index a4e70eb..2d3c2b8 100644 --- a/timber-loggly/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java +++ b/timber-loggly/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Anthony K. Trinh - * + *

* 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 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* 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 Timber * tree for posting messages to Loggly @@ -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); + } } /** From b73702b2c3c69bdbffed992628dce6828339a2ac Mon Sep 17 00:00:00 2001 From: Nikos Linakis Date: Fri, 15 Dec 2017 21:39:47 +0200 Subject: [PATCH 2/2] Added wifi limit in readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 833ae48..289c86b 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,14 @@ Usage Timber.i("hello world"); ``` + +##### Limit on WiFi (optional) +If you want to reduce cellular data traffic you can limit logging only when connected to a WiFi network. If this s the case you need to add the `android.permission.ACCESS_NETWORK_STATE` in your AndroidManifest and Plant the `LogglyTree` with the limit. +```java +LogglyTree wifiLimitedLogglyTree = new LogglyTree(LOGGLY_TOKEN).limitWifi(this); +Timber.plant(wifiLimitedLogglyTree); +``` + Download --------