Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
GLPI
java-library-glpi
Commits
b65c1f8f
Commit
b65c1f8f
authored
Oct 26, 2017
by
Rafa Hernandez
Committed by
Alexander Salas Bastidas
Oct 26, 2017
Browse files
docs(example): add logger on example
parent
21c7ea91
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/build.gradle
View file @
b65c1f8f
...
...
@@ -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'
}
app/src/main/java/org/glpi/glpiproject/FlyveLog.java
0 → 100644
View file @
b65c1f8f
/* ---------------------------------------------------------------------
*
* 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 - <rhernandez@teclib.com>
* @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
());
}
}
}
}
}
}
app/src/main/java/org/glpi/glpiproject/MainActivity.java
View file @
b65c1f8f
...
...
@@ -27,12 +27,15 @@ package org.glpi.glpiproject;
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 @@ public class MainActivity extends AppCompatActivity {
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 class MainActivity extends AppCompatActivity {
glpi
.
initSessionByCredentials
(
BuildConfig
.
GLPI_USER
,
BuildConfig
.
GLPI_PASSWORD
,
new
GLPI
.
InitSessionCallback
()
{
@Override
public
void
onResponse
(
InitSession
response
)
{
Log
.
d
(
"initSession"
,
response
.
getSessionToken
());
Flyve
Log
.
i
(
"initSession
: %s
"
,
response
.
getSessionToken
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"initSession"
,
errorMessage
);
Flyve
Log
.
e
(
"initSession
: %s
"
,
errorMessage
);
}
});
}
...
...
@@ -74,132 +86,132 @@ public class MainActivity extends AppCompatActivity {
glpi
.
getMyProfiles
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getMyProfiles"
,
response
.
toString
());
Flyve
Log
.
i
(
"getMyProfiles
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getMyProfiles"
,
errorMessage
);
Flyve
Log
.
e
(
"getMyProfiles
: %s
"
,
errorMessage
);
}
});
glpi
.
getActiveProfile
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getActiveProfile"
,
response
.
toString
());
Flyve
Log
.
i
(
"getActiveProfile
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getActiveProfile"
,
errorMessage
);
Flyve
Log
.
e
(
"getActiveProfile
: %s
"
,
errorMessage
);
}
});
glpi
.
getMyEntities
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getMyEntities"
,
response
.
toString
());
Flyve
Log
.
i
(
"getMyEntities
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getMyEntities"
,
errorMessage
);
Flyve
Log
.
e
(
"getMyEntities
: %s
"
,
errorMessage
);
}
});
glpi
.
getActiveEntities
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getActiveEntities"
,
response
.
toString
());
Flyve
Log
.
i
(
"getActiveEntities
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getActiveEntities"
,
errorMessage
);
Flyve
Log
.
e
(
"getActiveEntities
: %s
"
,
errorMessage
);
}
});
glpi
.
getFullSession
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getFullSession"
,
response
.
toString
());
Flyve
Log
.
i
(
"getFullSession
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getFullSession"
,
errorMessage
);
Flyve
Log
.
e
(
"getFullSession
: %s
"
,
errorMessage
);
}
});
glpi
.
getGlpiConfig
(
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getGlpiConfig"
,
response
.
toString
());
Flyve
Log
.
i
(
"getGlpiConfig
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getGlpiConfig"
,
errorMessage
);
Flyve
Log
.
e
(
"getGlpiConfig
: %s
"
,
errorMessage
);
}
});
glpi
.
getAllItems
(
itemType
.
Computer
,
new
GLPI
.
JsonArrayCallback
()
{
@Override
public
void
onResponse
(
JsonArray
response
)
{
Log
.
d
(
"getAllItems"
,
response
.
toString
());
Flyve
Log
.
i
(
"getAllItems
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getAllItems"
,
errorMessage
);
Flyve
Log
.
e
(
"getAllItems
: %s
"
,
errorMessage
);
}
});
glpi
.
getAnItem
(
itemType
.
Computer
,
"110"
,
new
GLPI
.
JsonObjectCallback
()
{
@Override
public
void
onResponse
(
JsonObject
response
)
{
Log
.
d
(
"getAnItem"
,
response
.
toString
());
Flyve
Log
.
i
(
"getAnItem
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getAnItem"
,
errorMessage
);
Flyve
Log
.
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
());
Flyve
Log
.
i
(
"getSubItems"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"getSubItems"
,
errorMessage
);
Flyve
Log
.
e
(
"getSubItems
: %s
"
,
errorMessage
);
}
});
glpi
.
changeActiveProfile
(
"9"
,
new
GLPI
.
VoidCallback
()
{
@Override
public
void
onResponse
(
String
response
)
{
Log
.
d
(
"changeActiveProfile"
,
response
);
Flyve
Log
.
i
(
"changeActiveProfile
: %s
"
,
response
);
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"changeActiveProfile"
,
errorMessage
);
Flyve
Log
.
e
(
"changeActiveProfile
: %s
"
,
errorMessage
);
}
});
glpi
.
changeActiveEntities
(
"1"
,
false
,
new
GLPI
.
VoidCallback
()
{
@Override
public
void
onResponse
(
String
response
)
{
Log
.
d
(
"changeActiveEntities"
,
response
);
Flyve
Log
.
i
(
"changeActiveEntities
: %s
"
,
response
);
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"changeActiveEntities"
,
errorMessage
);
Flyve
Log
.
e
(
"changeActiveEntities
: %s
"
,
errorMessage
);
}
});
...
...
@@ -217,36 +229,36 @@ public class MainActivity extends AppCompatActivity {
glpi
.
addItems
(
itemType
.
Computer
,
obj
,
new
GLPI
.
JsonArrayCallback
()
{
@Override
public
void
onResponse
(
JsonArray
response
)
{
Log
.
d
(
"addItems"
,
response
.
toString
());
Flyve
Log
.
i
(
"addItems
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"addItems"
,
errorMessage
);
Flyve
Log
.
e
(
"addItems
: %s
"
,
errorMessage
);
}
});
glpi
.
updateItems
(
itemType
.
Computer
,
"10"
,
obj
,
new
GLPI
.
JsonArrayCallback
()
{
@Override
public
void
onResponse
(
JsonArray
response
)
{
Log
.
d
(
"updateItems"
,
response
.
toString
());
Flyve
Log
.
i
(
"updateItems
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"updateItems"
,
errorMessage
);
Flyve
Log
.
e
(
"updateItems
: %s
"
,
errorMessage
);
}
});
glpi
.
deleteItems
(
itemType
.
Computer
,
"10"
,
new
GLPI
.
JsonArrayCallback
()
{
@Override
public
void
onResponse
(
JsonArray
response
)
{
Log
.
d
(
"deleteItems"
,
response
.
toString
());
Flyve
Log
.
i
(
"deleteItems
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"deleteItems"
,
errorMessage
);
Flyve
Log
.
e
(
"deleteItems
: %s
"
,
errorMessage
);
}
});
...
...
@@ -264,36 +276,36 @@ public class MainActivity extends AppCompatActivity {
glpi
.
deleteItems
(
itemType
.
Computer
,
deleteObj
,
new
GLPI
.
JsonArrayCallback
()
{
@Override
public
void
onResponse
(
JsonArray
response
)
{
Log
.
d
(
"deleteItems"
,
response
.
toString
());
Flyve
Log
.
i
(
"deleteItems
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"deleteItems"
,
errorMessage
);
Flyve
Log
.
e
(
"deleteItems
: %s
"
,
errorMessage
);
}
});
glpi
.
lostPassword
(
"youremail@yourdomain.com"
,
new
GLPI
.
VoidCallback
()
{
@Override
public
void
onResponse
(
String
response
)
{
Log
.
d
(
"lostPassword"
,
response
);
Flyve
Log
.
i
(
"lostPassword
: %s
"
,
response
);
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"lostPassword"
,
errorMessage
);
Flyve
Log
.
e
(
"lostPassword
: %s
"
,
errorMessage
);
}
});
glpi
.
recoveryPassword
(
"youremail@yourdomain.com"
,
"asdfasdfafsASDFd333A"
,
"1234"
,
new
GLPI
.
VoidCallback
()
{
@Override
public
void
onResponse
(
String
response
)
{
Log
.
d
(
"recoveryPassword"
,
response
);
Flyve
Log
.
i
(
"recoveryPassword
: %s
"
,
response
);
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"recoveryPassword"
,
errorMessage
);
Flyve
Log
.
e
(
"recoveryPassword
: %s
"
,
errorMessage
);
}
});
}
...
...
@@ -307,12 +319,12 @@ public class MainActivity extends AppCompatActivity {
glpi
.
killSession
(
new
GLPI
.
VoidCallback
()
{
@Override
public
void
onResponse
(
String
response
)
{
Log
.
d
(
"killSession"
,
response
.
toString
());
Flyve
Log
.
i
(
"killSession
: %s
"
,
response
.
toString
());
}
@Override
public
void
onFailure
(
String
errorMessage
)
{
Log
.
e
(
"killSession"
,
errorMessage
);
Flyve
Log
.
e
(
"killSession
: %s
"
,
errorMessage
);
}
});
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment