Skip to content

Commit

Permalink
feat(core): native inventory compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Teclib <[email protected]>
  • Loading branch information
stonebuzz committed Apr 21, 2022
1 parent 07602bd commit ca04a57
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
implementation 'com.orhanobut:logger:2.2.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.preference:preference:1.1.1'
implementation 'org.dom4j:dom4j:2.1.3'


// Add the Firebase Crashlytics SDK.
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/assets/about.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
about.version=1.1.0
about.build=39329
about.date=Thu Sep 30 09:16:01 2021
about.commit=6c9db58
about.commitFull=6c9db58604c4c69710b6903b1eb727edfb88b18b
about.date=mer. avril 06 13:04:50 2022
about.commit=
about.commitFull=
about.github=https://github.com/glpi-project/android-inventory-agent
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ public class DetailServerModel implements DetailServer.Model {
@Override
public void saveServer(ArrayList<String> modelServer, Context context) {
LocalPreferences preferences = new LocalPreferences(context);
if (modelServer.size() >= 4) {
if (modelServer.size() >= 5) {
if (!"".equals(modelServer.get(0))) {
JSONObject jo = new JSONObject();
try {
jo.put("address", modelServer.get(0));
jo.put("tag", modelServer.get(1));
jo.put("login", modelServer.get(2));
jo.put("pass", modelServer.get(3));
jo.put("itemtype", modelServer.get(4));
ArrayList<String> serverArray = preferences.loadServer();
serverArray.add(modelServer.get(0));
preferences.saveServer(serverArray);
Expand All @@ -84,14 +85,15 @@ public void saveServer(ArrayList<String> modelServer, Context context) {
@Override
public void updateServer(ArrayList<String> modelServer, String serverName, Context context) {
LocalPreferences preferences = new LocalPreferences(context);
if (modelServer.size() >= 4) {
if (modelServer.size() >= 5) {
if (!"".equals(modelServer.get(0))) {
JSONObject jo = new JSONObject();
try {
jo.put("address", modelServer.get(0));
jo.put("tag", modelServer.get(1));
jo.put("login", modelServer.get(2));
jo.put("pass", modelServer.get(3));
jo.put("itemtype", modelServer.get(4));
ArrayList<String> serverArray = preferences.loadServer();
for (int i = 0; i < serverArray.size(); i++) {
if (serverArray.get(i).equals(serverName)) {
Expand Down Expand Up @@ -124,6 +126,7 @@ public void loadServer(String serverName, Context context) {
serverSchema.setTag(jo.getString("tag"));
serverSchema.setLogin(jo.getString("login"));
serverSchema.setPass(jo.getString("pass"));
serverSchema.setItemtype(jo.getString("itemtype"));
presenter.modelServer(serverSchema);
} catch (JSONException e) {
AgentLog.e(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class DetailServerPresenter implements DetailServer.Presenter {
private DetailServer.View view;
private DetailServer.Model model;

public DetailServerPresenter(DetailServer.View view){
public DetailServerPresenter(DetailServer.View view){
this.view = view;
model = new DetailServerModel(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ServerSchema {
private String tag;
private String login;
private String pass;
private String itemtype;

public String getAddress() {
return address;
Expand Down Expand Up @@ -73,4 +74,12 @@ public String getPass() {
public void setPass(String pass) {
this.pass = pass;
}

public String getItemtype() {
return itemtype;
}

public void setItemtype(String itemtype) {
this.itemtype = itemtype;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import android.util.Xml;

import org.flyve.inventory.InventoryTask;
import org.glpi.inventory.agent.R;
Expand All @@ -63,7 +64,11 @@
import org.glpi.inventory.agent.utils.HttpInventory;
import org.glpi.inventory.agent.utils.LocalPreferences;
import org.glpi.inventory.agent.utils.LocalStorage;
import org.glpi.inventory.agent.utils.Utils;
import org.xmlpull.v1.XmlSerializer;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -259,6 +264,7 @@ private void sendInventory() {
inventory.getXML(new InventoryTask.OnTaskCompleted() {
@Override
public void onTaskSuccess(String data) {
data = Utils.addItemtypeNode(this, data, model.getItemtype());
httpInventory.sendInventory(data, model, new HttpInventory.OnTaskCompleted() {
@Override
public void onTaskSuccess(String data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
import android.preference.PreferenceManager;
import android.util.Base64;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -76,6 +78,7 @@ public class ActivityDetailServer extends AppCompatActivity implements DetailSer
private EditText editTag;
private EditText editLogin;
private EditText editPassWord;
private Spinner assetItemtype;
private Toolbar toolbar;
private String serverName;
private JSONObject extra_Data = null;
Expand Down Expand Up @@ -144,6 +147,7 @@ public void onClick(View view) {
editTag.setText(extra_Data.getString("TAG"));
editLogin.setText(extra_Data.getString("LOGIN"));
editPassWord.setText(extra_Data.getString("PASSWORD"));
assetItemtype.setSelection(((ArrayAdapter)assetItemtype.getAdapter()).getPosition(extra_Data.getString("ASSET_ITEMTYPE")));
} catch (Exception ex) {
Toast.makeText(this, getApplicationContext().getResources().getString(R.string.bad_deeplink_format), Toast.LENGTH_LONG).show();
AgentLog.e(getApplicationContext().getResources().getString(R.string.bad_deeplink_format));
Expand Down Expand Up @@ -182,6 +186,7 @@ public void onActivityResult(final int requestCode, final int resultCode, final
editTag.setText(extra_Data.getString("TAG"));
editLogin.setText(extra_Data.getString("LOGIN"));
editPassWord.setText(extra_Data.getString("PASSWORD"));
assetItemtype.setSelection(((ArrayAdapter)assetItemtype.getAdapter()).getPosition(extra_Data.getString("ASSET_ITEMTYPE")));
} catch (Exception ex) {
Toast.makeText(this, getApplicationContext().getResources().getString(R.string.bad_qr_code_format), Toast.LENGTH_LONG).show();
AgentLog.e(getApplicationContext().getResources().getString(R.string.bad_qr_code_format));
Expand Down Expand Up @@ -219,6 +224,13 @@ private void instanceObject() {
actionServer.setOnClickListener(this);
deleteServer.setOnClickListener(this);
btnScan = findViewById(R.id.btnQRScan);
assetItemtype = findViewById(R.id.spinnerAssetItemtype);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.AssetItemtype, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assetItemtype.setAdapter(adapter);

}

@Override
Expand All @@ -230,6 +242,7 @@ public void onClick(View v) {
serverInfo.add(editTag.getText().toString());
serverInfo.add(editLogin.getText().toString());
serverInfo.add(editPassWord.getText().toString());
serverInfo.add(assetItemtype.getSelectedItem().toString());
if (serverName == null) {
presenter.saveServer(serverInfo, getApplicationContext());
//manage automatic inventory
Expand Down Expand Up @@ -287,6 +300,7 @@ public void modelServer(ServerSchema model) {
editTag.setText(model.getTag());
editLogin.setText(model.getLogin());
editPassWord.setText(model.getPass());
assetItemtype.setSelection(((ArrayAdapter)assetItemtype.getAdapter()).getPosition(model.getItemtype()));
btnScan.hide();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.flyve.inventory.InventoryTask;
import org.glpi.inventory.agent.R;
import org.glpi.inventory.agent.core.home.Home;
Expand All @@ -53,8 +62,11 @@
import org.glpi.inventory.agent.utils.Helpers;
import org.glpi.inventory.agent.utils.HttpInventory;
import org.glpi.inventory.agent.utils.LocalPreferences;
import org.glpi.inventory.agent.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class DialogListServers {

Expand Down Expand Up @@ -134,6 +146,8 @@ private void sendInventory(final Activity activity, final Home.Presenter present
inventoryTask.getXML(new InventoryTask.OnTaskCompleted() {
@Override
public void onTaskSuccess(String data) {

data = Utils.addItemtypeNode(this, data, model.getItemtype());
AgentLog.d(data);
httpInventory.sendInventory(data, model, new HttpInventory.OnTaskCompleted() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public ServerSchema setServerModel(String serverName) {
serverSchema.setTag(jo.getString("tag"));
serverSchema.setLogin(jo.getString("login"));
serverSchema.setPass(jo.getString("pass"));
serverSchema.setItemtype(jo.getString("itemtype"));
} catch (JSONException e) {
AgentLog.e(e.getMessage());
}
Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/org/glpi/inventory/agent/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,126 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.flyve.inventory.InventoryLog;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class Utils {

public static String fileNameXML = "Inventory.xml";

public static String addItemtypeNode(Object obj, String data, String asset_itemtype) {
try {
Document document = DocumentHelper.parseText(data);
Element root = document.getRootElement();

//defined default value
if(asset_itemtype.isEmpty() || asset_itemtype.equals(null)){
asset_itemtype = "Computer";
}
DOMElement itemtype = new DOMElement("ITEMTYPE");
itemtype.addCDATA(asset_itemtype);

List elements = root.elements();
elements.add(0, itemtype);

data = document.asXML();
Utils.storeFile(data, Utils.fileNameXML);
return data;

} catch (Exception ex) {
AgentLog.log(obj, "Can't set ITEMTYPE", Log.ERROR);
AgentLog.log(obj, ex.getMessage(), Log.ERROR);
return data;
}
}

/**
* Logs the message in a directory
* @param message the message
* @param filename name of the file
*/
public static void storeFile(String message, String filename) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
File dir = new File(path);

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {

if(!dir.exists()) {
if(dir.mkdirs()) {
InventoryLog.d("create path");
} else {
InventoryLog.e("cannot create path");
return;
}
}

File logFile = new File(path + "/" + filename);

if (!logFile.exists()) {
try {
if(logFile.createNewFile()) {
InventoryLog.d("File created");
} else {
InventoryLog.d("Cannot create file");
return;
}
} catch (IOException ex) {
InventoryLog.e(ex.getMessage());
}
}

FileWriter fw = null;

try {
//BufferedWriter for performance, true to set append to file flag
fw = new FileWriter(logFile, false);
BufferedWriter buf = new BufferedWriter(fw);

buf.write(message);
buf.newLine();
buf.flush();
buf.close();
fw.close();
InventoryLog.d("Inventory stored");
}
catch (IOException ex) {
InventoryLog.e(ex.getMessage());
}
finally {
if(fw!=null) {
try {
fw.close();
} catch(Exception ex) {
InventoryLog.e(ex.getMessage());
}
}
}
} else {
InventoryLog.d("External Storage is not available");
}
}

public static void showAlertDialog(String message, Context context, final OnTaskCompleted callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message);
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_detail_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword" />

<Spinner
android:id="@+id/spinnerAssetItemtype"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<item>Week</item>
<item>Month</item>
</string-array>
<string-array name="AssetItemtype">
<item>Computer</item>
<item>Phone</item>
</string-array>
<plurals name="easter_egg_attempts">
<item quantity="one">You have %d Attempt</item>
<item quantity="other">You have %d Attempts</item>
Expand Down

0 comments on commit ca04a57

Please sign in to comment.