From afdbb5b8fa600b5128c6643c7404697badcf8ae8 Mon Sep 17 00:00:00 2001 From: Rafa Hernandez Date: Thu, 26 Oct 2017 10:56:32 -0400 Subject: [PATCH 1/3] build(gradle): check empty values --- app/build.gradle | 10 +++++++--- glpi/build.gradle | 14 ++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4bed249..839d51f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,9 +13,13 @@ android { } buildTypes { buildTypes.each { - it.buildConfigField 'String', 'GLPI_USER', glpi_user - it.buildConfigField 'String', 'GLPI_PASSWORD', glpi_password - it.buildConfigField 'String', 'GLPI_URL', glpi_url + def glpi_user_val = glpi_user ?: "\"\"" + def glpi_password_val = glpi_password ?: "\"\"" + def glpi_url_val = glpi_url ?: "\"\"" + + it.buildConfigField 'String', 'GLPI_USER', glpi_user_val + it.buildConfigField 'String', 'GLPI_PASSWORD', glpi_password_val + it.buildConfigField 'String', 'GLPI_URL', glpi_url_val } release { minifyEnabled false diff --git a/glpi/build.gradle b/glpi/build.gradle index fd08963..c1571ea 100644 --- a/glpi/build.gradle +++ b/glpi/build.gradle @@ -17,12 +17,14 @@ android { buildTypes { buildTypes.each { - if(glpi_user == null) { glpi_user = "-" } - if(glpi_password == null) { glpi_password = "-" } - if(glpi_url == null) { glpi_url = "-" } - it.buildConfigField 'String', 'GLPI_USER', glpi_user - it.buildConfigField 'String', 'GLPI_PASSWORD', glpi_password - it.buildConfigField 'String', 'GLPI_URL', glpi_url + + def glpi_user_val = glpi_user ?: "\"\"" + def glpi_password_val = glpi_password ?: "\"\"" + def glpi_url_val = glpi_url ?: "\"\"" + + it.buildConfigField 'String', 'GLPI_USER', glpi_user_val + it.buildConfigField 'String', 'GLPI_PASSWORD', glpi_password_val + it.buildConfigField 'String', 'GLPI_URL', glpi_url_val } release { minifyEnabled false From ec0c3c1d0ae1776edb57ba0457d548d9fc5ff270 Mon Sep 17 00:00:00 2001 From: Rafa Hernandez Date: Thu, 26 Oct 2017 11:38:36 -0400 Subject: [PATCH 2/3] docs(example): add logger on example --- app/build.gradle | 1 + .../java/org/glpi/glpiproject/FlyveLog.java | 225 ++++++++++++++++++ .../org/glpi/glpiproject/MainActivity.java | 90 ++++--- 3 files changed, 277 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/org/glpi/glpiproject/FlyveLog.java diff --git a/app/build.gradle b/app/build.gradle index 839d51f..508331e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,4 +37,5 @@ dependencies { compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile project(':glpi') + compile 'com.orhanobut:logger:2.1.0' } diff --git a/app/src/main/java/org/glpi/glpiproject/FlyveLog.java b/app/src/main/java/org/glpi/glpiproject/FlyveLog.java new file mode 100644 index 0000000..602317a --- /dev/null +++ b/app/src/main/java/org/glpi/glpiproject/FlyveLog.java @@ -0,0 +1,225 @@ +/* --------------------------------------------------------------------- +* +* LICENSE +* +* This file is part of the GLPI API Client Library for Java, +* a subproject of GLPI. GLPI is a free IT Asset Management. +* +* GLPI is free software: you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 3 +* of the License, or (at your option) any later version. +* +* GLPI is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* -------------------------------------------------------------------- +* @author Rafael Hernandez - +* @copyright (C) 2017 Teclib' and contributors. +* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html +* @link https://github.com/glpi-project/java-library-glpi +* @link http://www.glpi-project.org/ +* -------------------------------------------------------------------- +*/ + +package org.glpi.glpiproject; + +import android.os.Environment; +import com.orhanobut.logger.Logger; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * This is a Log wrapper + */ +public class FlyveLog { + + private static final String FILE_NAME_FEEDBACK = "FlyveMDMFeedback.txt"; + public static final String FILE_NAME_LOG = "FlyveMDMLog.txt"; + + /** + * private constructor to prevent instances of this class + */ + private FlyveLog() { + } + + /** + * Send a DEBUG log message + * @param object Object to write + */ + public static void d(Object object) { + Logger.d(object); + } + + /** + * Send a DEBUG log message + * @param message String message to log + * @param args Objects + */ + public static void d(String message, Object... args) { + // do something for a debug build + Logger.d(message,args); + + } + + /** + * Send a VERBOSE log message + * @param message String message + * @param args Objects + */ + public static void v(String message, Object... args) { + Logger.v(message, args); + } + + /** + * Send INFORMATION log message + * @param message String message + * @param args Objects + */ + public static void i(String message, Object... args) { + Logger.i(message, args); + } + + /** + * Send ERROR log message + * @param throwable Throwable error + * @param message String message + * @param args Objects + */ + public static void e(Throwable throwable, String message, Object... args) { + Logger.e(throwable, message, args); + } + + /** + * Send Error log message + * @param message String message + * @param args Objects + */ + public static void e(String message, Object... args) { + Logger.e(message, args); + } + + /** + * send What a Terrible Failure log message + * @param message String message + * @param args Objects + */ + public static void wtf(String message, Object... args) { + Logger.wtf(message, args); + } + + /** + * Send a JSON log message + * @param json String the json to show + */ + public static void json(String json) { + Logger.json(json); + } + + /** + * Send a XML log message + * @param xml String the xml to show + */ + public static void xml(String xml) { + Logger.xml(xml); + } + + /** + * Logs the message in a directory + * @param string the message + * @param string the filename + */ + public static void f(String message, String filename) { + String state = Environment.getExternalStorageState(); + + File dir = new File("/sdcard/FlyveMDM"); + if (Environment.MEDIA_MOUNTED.equals(state)) { + if(!dir.exists()) { + FlyveLog.d("Dir created ", "Dir created "); + dir.mkdirs(); + } + + File logFile = new File("/sdcard/FlyveMDM/" + filename); + + if (!logFile.exists()) { + try { + FlyveLog.d("File created ", "File created "); + logFile.createNewFile(); + } catch (IOException ex) { + FlyveLog.e(ex.getMessage()); + } + } + + FileWriter fw = null; + try { + //BufferedWriter for performance, true to set append to file flag + fw = new FileWriter(logFile, true); + BufferedWriter buf = new BufferedWriter(fw); + + buf.write(message); + buf.newLine(); + buf.flush(); + buf.close(); + fw.close(); + } + catch (IOException ex) { + e(ex.getMessage()); + } + finally { + if(fw!=null) { + try { + fw.close(); + } catch(Exception ex) { + FlyveLog.e(ex.getMessage()); + } + } + } + } + } + + /** + * Clear the log + * @param string the file name + * @throws Exception an error message + */ + public static void clearLog(String filename) { + String state = Environment.getExternalStorageState(); + + File dir = new File("/sdcard/FlyveMDM"); + if (Environment.MEDIA_MOUNTED.equals(state)) { + if (!dir.exists()) { + FlyveLog.d("Dir created ", "Dir created "); + dir.mkdirs(); + } + + File logFile = new File("/sdcard/FlyveMDM/" + filename); + + FileWriter fw = null; + try { + //BufferedWriter for performance, true to set append to file flag + fw = new FileWriter(logFile, false); + PrintWriter pwOb = new PrintWriter(fw, false); + pwOb.flush(); + pwOb.close(); + } + catch (IOException ex) { + e(ex.getMessage()); + } + finally { + if(fw!=null) { + try { + fw.close(); + } catch(Exception ex) { + FlyveLog.e(ex.getMessage()); + } + } + } + + } + } + +} diff --git a/app/src/main/java/org/glpi/glpiproject/MainActivity.java b/app/src/main/java/org/glpi/glpiproject/MainActivity.java index 4ac2a8d..b841f89 100644 --- a/app/src/main/java/org/glpi/glpiproject/MainActivity.java +++ b/app/src/main/java/org/glpi/glpiproject/MainActivity.java @@ -27,12 +27,15 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; import android.view.View; import android.widget.Button; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import com.orhanobut.logger.AndroidLogAdapter; +import com.orhanobut.logger.FormatStrategy; +import com.orhanobut.logger.Logger; +import com.orhanobut.logger.PrettyFormatStrategy; import org.glpi.api.GLPI; import org.glpi.api.itemType; @@ -47,6 +50,15 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder() + .showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true + .methodCount(0) // (Optional) How many method line to show. Default 2 + .methodOffset(7) // (Optional) Hides internal method calls up to offset. Default 5 + .tag("org.glpi.api") // (Optional) Global tag for every log. Default PRETTY_LOGGER + .build(); + + Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy)); + final GLPI glpi = new GLPI(MainActivity.this, BuildConfig.GLPI_URL); Button btnInit = (Button) findViewById(R.id.btnInit); @@ -56,12 +68,12 @@ public void onClick(View v) { glpi.initSessionByCredentials(BuildConfig.GLPI_USER, BuildConfig.GLPI_PASSWORD, new GLPI.InitSessionCallback() { @Override public void onResponse(InitSession response) { - Log.d("initSession", response.getSessionToken()); + FlyveLog.i("initSession: %s", response.getSessionToken()); } @Override public void onFailure(String errorMessage) { - Log.e("initSession", errorMessage); + FlyveLog.e("initSession: %s", errorMessage); } }); } @@ -74,132 +86,132 @@ public void onClick(View v) { glpi.getMyProfiles(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getMyProfiles", response.toString()); + FlyveLog.i("getMyProfiles: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getMyProfiles", errorMessage); + FlyveLog.e("getMyProfiles: %s", errorMessage); } }); glpi.getActiveProfile(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getActiveProfile", response.toString()); + FlyveLog.i("getActiveProfile: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getActiveProfile", errorMessage); + FlyveLog.e("getActiveProfile: %s", errorMessage); } }); glpi.getMyEntities(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getMyEntities", response.toString()); + FlyveLog.i("getMyEntities: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getMyEntities", errorMessage); + FlyveLog.e("getMyEntities: %s", errorMessage); } }); glpi.getActiveEntities(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getActiveEntities", response.toString()); + FlyveLog.i("getActiveEntities: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getActiveEntities", errorMessage); + FlyveLog.e("getActiveEntities: %s", errorMessage); } }); glpi.getFullSession(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getFullSession", response.toString()); + FlyveLog.i("getFullSession: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getFullSession", errorMessage); + FlyveLog.e("getFullSession: %s", errorMessage); } }); glpi.getGlpiConfig(new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getGlpiConfig", response.toString()); + FlyveLog.i("getGlpiConfig: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getGlpiConfig", errorMessage); + FlyveLog.e("getGlpiConfig: %s", errorMessage); } }); glpi.getAllItems(itemType.Computer, new GLPI.JsonArrayCallback() { @Override public void onResponse(JsonArray response) { - Log.d("getAllItems", response.toString()); + FlyveLog.i("getAllItems: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getAllItems", errorMessage); + FlyveLog.e("getAllItems: %s", errorMessage); } }); glpi.getAnItem(itemType.Computer, "110", new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getAnItem", response.toString()); + FlyveLog.i("getAnItem: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getAnItem", errorMessage); + FlyveLog.e("getAnItem: %s", errorMessage); } }); glpi.getSubItems(itemType.Computer, "2", itemType.ComputerType, new GLPI.JsonObjectCallback() { @Override public void onResponse(JsonObject response) { - Log.d("getSubItems", response.toString()); + FlyveLog.i("getSubItems", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("getSubItems", errorMessage); + FlyveLog.e("getSubItems: %s", errorMessage); } }); glpi.changeActiveProfile("9", new GLPI.VoidCallback() { @Override public void onResponse(String response) { - Log.d("changeActiveProfile", response); + FlyveLog.i("changeActiveProfile: %s", response); } @Override public void onFailure(String errorMessage) { - Log.e("changeActiveProfile", errorMessage); + FlyveLog.e("changeActiveProfile: %s", errorMessage); } }); glpi.changeActiveEntities("1", false, new GLPI.VoidCallback() { @Override public void onResponse(String response) { - Log.d("changeActiveEntities", response); + FlyveLog.i("changeActiveEntities: %s", response); } @Override public void onFailure(String errorMessage) { - Log.e("changeActiveEntities", errorMessage); + FlyveLog.e("changeActiveEntities: %s", errorMessage); } }); @@ -217,36 +229,36 @@ public void onFailure(String errorMessage) { glpi.addItems(itemType.Computer, obj, new GLPI.JsonArrayCallback() { @Override public void onResponse(JsonArray response) { - Log.d("addItems", response.toString()); + FlyveLog.i("addItems: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("addItems", errorMessage); + FlyveLog.e("addItems: %s", errorMessage); } }); glpi.updateItems(itemType.Computer, "10", obj, new GLPI.JsonArrayCallback() { @Override public void onResponse(JsonArray response) { - Log.d("updateItems", response.toString()); + FlyveLog.i("updateItems: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("updateItems", errorMessage); + FlyveLog.e("updateItems: %s", errorMessage); } }); glpi.deleteItems(itemType.Computer, "10", new GLPI.JsonArrayCallback() { @Override public void onResponse(JsonArray response) { - Log.d("deleteItems", response.toString()); + FlyveLog.i("deleteItems: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("deleteItems", errorMessage); + FlyveLog.e("deleteItems: %s", errorMessage); } }); @@ -264,36 +276,36 @@ public void onFailure(String errorMessage) { glpi.deleteItems(itemType.Computer, deleteObj, new GLPI.JsonArrayCallback() { @Override public void onResponse(JsonArray response) { - Log.d("deleteItems", response.toString()); + FlyveLog.i("deleteItems: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("deleteItems", errorMessage); + FlyveLog.e("deleteItems: %s", errorMessage); } }); glpi.lostPassword("youremail@yourdomain.com", new GLPI.VoidCallback() { @Override public void onResponse(String response) { - Log.d("lostPassword", response); + FlyveLog.i("lostPassword: %s", response); } @Override public void onFailure(String errorMessage) { - Log.e("lostPassword", errorMessage); + FlyveLog.e("lostPassword: %s", errorMessage); } }); glpi.recoveryPassword("youremail@yourdomain.com", "asdfasdfafsASDFd333A", "1234", new GLPI.VoidCallback() { @Override public void onResponse(String response) { - Log.d("recoveryPassword", response); + FlyveLog.i("recoveryPassword: %s", response); } @Override public void onFailure(String errorMessage) { - Log.e("recoveryPassword", errorMessage); + FlyveLog.e("recoveryPassword: %s", errorMessage); } }); } @@ -307,12 +319,12 @@ public void onClick(View v) { glpi.killSession(new GLPI.VoidCallback() { @Override public void onResponse(String response) { - Log.d("killSession", response.toString()); + FlyveLog.i("killSession: %s", response.toString()); } @Override public void onFailure(String errorMessage) { - Log.e("killSession", errorMessage); + FlyveLog.e("killSession: %s", errorMessage); } }); } From 25019abf5264885bb6aa955408fa8e57b67fe7e1 Mon Sep 17 00:00:00 2001 From: Rafa Hernandez Date: Thu, 26 Oct 2017 12:55:37 -0400 Subject: [PATCH 3/3] refactor(bintray): change artifact to api --- glpi/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glpi/build.gradle b/glpi/build.gradle index c1571ea..1301515 100644 --- a/glpi/build.gradle +++ b/glpi/build.gradle @@ -69,8 +69,8 @@ ext { bintrayUserOrg = "glpi-project" publishedGroupId = 'org.glpi' - libraryName = 'glpi-api-client' - artifact = 'glpi-api-client' + libraryName = 'api' + artifact = 'api' libraryDescription = 'GLPI API Client Library for Java provides functionality common to all GLPI APIs, for example HTTP transport, error handling, authentication, JSON parsing, media download/upload, and batching.'