diff --git a/app/src/androidTest/java/org/flyve/inventory/agent/InventoryTaskTest.java b/app/src/androidTest/java/org/flyve/inventory/agent/InventoryTaskTest.java index b6b40ca08..e502d0dbd 100644 --- a/app/src/androidTest/java/org/flyve/inventory/agent/InventoryTaskTest.java +++ b/app/src/androidTest/java/org/flyve/inventory/agent/InventoryTaskTest.java @@ -26,7 +26,9 @@ import android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; + import org.flyve.inventory.InventoryTask; +import org.flyve.inventory.agent.model.ServerModel; import org.flyve.inventory.agent.utils.FlyveLog; import org.flyve.inventory.agent.utils.HttpInventory; import org.junit.Test; @@ -79,7 +81,12 @@ public void sendInventoryTest() throws Exception { // Send xml to default route HttpInventory httpInventory = new HttpInventory(appContext); - httpInventory.sendInventory(data, new HttpInventory.OnTaskCompleted() { + ServerModel serverModel = new ServerModel(); + serverModel.setAddress("https://demo-api.flyve.org/plugins/fusioninventory/"); + serverModel.setTag(""); + serverModel.setLogin(""); + serverModel.setPass(""); + httpInventory.sendInventory(data, serverModel, new HttpInventory.OnTaskCompleted() { @Override public void onTaskSuccess(String data) { FlyveLog.d(data); diff --git a/app/src/main/java/org/flyve/inventory/agent/adapter/ListServersAdapter.java b/app/src/main/java/org/flyve/inventory/agent/adapter/ListServersAdapter.java index a3307ecc5..228fe6805 100644 --- a/app/src/main/java/org/flyve/inventory/agent/adapter/ListServersAdapter.java +++ b/app/src/main/java/org/flyve/inventory/agent/adapter/ListServersAdapter.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-inventory-agent @@ -52,7 +52,7 @@ public ListServersAdapter(ArrayList data, Activity activity) { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { - int resource = R.layout.list_item_inventory_child; + int resource = R.layout.list_item_servers; View v = LayoutInflater.from(viewGroup.getContext()).inflate(resource, viewGroup, false); return new DataViewHolder(v); } diff --git a/app/src/main/java/org/flyve/inventory/agent/broadcast/TimeAlarm.java b/app/src/main/java/org/flyve/inventory/agent/broadcast/TimeAlarm.java index d73f0f260..0fe990815 100644 --- a/app/src/main/java/org/flyve/inventory/agent/broadcast/TimeAlarm.java +++ b/app/src/main/java/org/flyve/inventory/agent/broadcast/TimeAlarm.java @@ -34,6 +34,7 @@ import org.flyve.inventory.InventoryTask; import org.flyve.inventory.agent.R; +import org.flyve.inventory.agent.model.ServerModel; import org.flyve.inventory.agent.utils.FlyveLog; import org.flyve.inventory.agent.utils.Helpers; import org.flyve.inventory.agent.utils.HttpInventory; @@ -67,7 +68,12 @@ public void onReceive(final Context context, Intent intent) { @Override public void onTaskSuccess(String data) { HttpInventory httpInventory = new HttpInventory(context.getApplicationContext()); - httpInventory.sendInventory(data, new HttpInventory.OnTaskCompleted() { + ServerModel serverModel = new ServerModel(); + serverModel.setAddress("https://demo-api.flyve.org/plugins/fusioninventory/"); + serverModel.setTag(""); + serverModel.setLogin(""); + serverModel.setPass(""); + httpInventory.sendInventory(data, serverModel, new HttpInventory.OnTaskCompleted() { @Override public void onTaskSuccess(String data) { Helpers.sendToNotificationBar(context.getApplicationContext(), context.getResources().getString(R.string.inventory_notification_sent)); diff --git a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServer.java b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServer.java index ca056d928..742e8fc2e 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServer.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServer.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-mdm-agent @@ -23,6 +23,10 @@ package org.flyve.inventory.agent.core.detailserver; +import android.content.Context; + +import org.flyve.inventory.agent.model.ServerModel; + import java.util.ArrayList; public interface DetailServer { @@ -30,22 +34,26 @@ public interface DetailServer { interface View { void showError(String message); void successful(String message); + void modelServer(ServerModel model); } interface Presenter { // Views void showError(String message); void successful(String message); + void modelServer(ServerModel model); // Models - void saveServer(ArrayList message); - void deleteServer(String serverName); - void updateServer(ArrayList serverInfo, String serverName); + void saveServer(ArrayList message, Context applicationContext); + void deleteServer(String serverName, Context applicationContext); + void updateServer(ArrayList serverInfo, String serverName, Context applicationContext); + void loadServer(String serverName, Context applicationContext); } interface Model { - void saveServer(ArrayList message); - void deleteServer(String serverName); - void updateServer(ArrayList serverInfo, String serverName); + void saveServer(ArrayList message, Context applicationContext); + void deleteServer(String serverName, Context applicationContext); + void updateServer(ArrayList serverInfo, String serverName, Context applicationContext); + void loadServer(String serverName, Context applicationContext); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerModel.java b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerModel.java index 620e9945e..0a03e1eb5 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerModel.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerModel.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-mdm-agent @@ -23,6 +23,14 @@ package org.flyve.inventory.agent.core.detailserver; +import android.content.Context; + +import org.flyve.inventory.agent.model.ServerModel; +import org.flyve.inventory.agent.utils.FlyveLog; +import org.flyve.inventory.agent.utils.LocalPreferences; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; public class DetailServerModel implements DetailServer.Model { @@ -34,17 +42,94 @@ public class DetailServerModel implements DetailServer.Model { } @Override - public void saveServer(ArrayList message) { - presenter.successful("Successful"); + public void saveServer(ArrayList modelServer, Context context) { + LocalPreferences preferences = new LocalPreferences(context); + if (modelServer.size() >= 4) { + 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)); + ArrayList serverArray = preferences.loadServerArray(); + serverArray.add(modelServer.get(0)); + preferences.saveServerArray(serverArray); + } catch (JSONException e) { + FlyveLog.e(e.getMessage()); + presenter.showError("Error"); + } + preferences.saveJSONObject(modelServer.get(0), jo); + presenter.successful("Successful"); + } else { + presenter.showError("Missing server"); + } + } else { + presenter.showError("Error"); + } + } + + @Override + public void updateServer(ArrayList modelServer, String serverName, Context context) { + LocalPreferences preferences = new LocalPreferences(context); + if (modelServer.size() >= 4) { + 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)); + ArrayList serverArray = preferences.loadServerArray(); + for (int i = 0; i < serverArray.size(); i++) { + if (serverArray.get(i).equals(serverName)) { + serverArray.set(i, modelServer.get(0)); + preferences.saveServerArray(serverArray); + } + } + } catch (JSONException e) { + FlyveLog.e(e.getMessage()); + presenter.showError("Error"); + } + preferences.deletePreferences(serverName); + preferences.saveJSONObject(modelServer.get(0), jo); + presenter.successful("Successful"); + } else { + presenter.showError("Missing Server"); + } + } else { + presenter.showError("Error"); + } } @Override - public void deleteServer(String serverName) { - presenter.successful("Successful"); + public void loadServer(String serverName, Context context) { + LocalPreferences preferences = new LocalPreferences(context); + try { + JSONObject jo = preferences.loadJSONObject(serverName); + ServerModel serverModel = new ServerModel(); + serverModel.setAddress(jo.getString("address")); + serverModel.setTag(jo.getString("tag")); + serverModel.setLogin(jo.getString("login")); + serverModel.setPass(jo.getString("pass")); + presenter.modelServer(serverModel); + } catch (JSONException e) { + FlyveLog.e(e.getMessage()); + presenter.showError("Error"); + } } @Override - public void updateServer(ArrayList serverInfo, String serverName) { - presenter.successful("Successful"); + public void deleteServer(String serverName, Context context) { + if (serverName != null && !"".equals(serverName)) { + LocalPreferences preferences = new LocalPreferences(context); + preferences.deletePreferences(serverName); + ArrayList serverArray = preferences.loadServerArray(); + serverArray.remove(serverName); + preferences.saveServerArray(serverArray); + presenter.successful("Successful Delete"); + } else { + presenter.showError("Error"); + } } } diff --git a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerPresenter.java b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerPresenter.java index ee938ebee..8fa397c35 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerPresenter.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/detailserver/DetailServerPresenter.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-mdm-agent @@ -23,6 +23,10 @@ package org.flyve.inventory.agent.core.detailserver; +import android.content.Context; + +import org.flyve.inventory.agent.model.ServerModel; + import java.util.ArrayList; public class DetailServerPresenter implements DetailServer.Presenter { @@ -50,18 +54,30 @@ public void successful(String message) { } @Override - public void saveServer(ArrayList message) { - model.saveServer(message); + public void modelServer(ServerModel model) { + if (view != null) { + view.modelServer(model); + } + } + + @Override + public void saveServer(ArrayList message, Context applicationContext) { + model.saveServer(message, applicationContext); + } + + @Override + public void deleteServer(String serverName, Context applicationContext) { + model.deleteServer(serverName, applicationContext); } @Override - public void deleteServer(String serverName) { - model.deleteServer(serverName); + public void updateServer(ArrayList serverInfo, String serverName, Context applicationContext) { + model.updateServer(serverInfo, serverName, applicationContext); } @Override - public void updateServer(ArrayList serverInfo, String serverName) { - model.updateServer(serverInfo, serverName); + public void loadServer(String serverName, Context applicationContext) { + model.loadServer(serverName, applicationContext); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/core/home/HomeModel.java b/app/src/main/java/org/flyve/inventory/agent/core/home/HomeModel.java index 5c331c028..5204b13e3 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/home/HomeModel.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/home/HomeModel.java @@ -25,24 +25,20 @@ import android.app.Activity; import android.app.ActivityManager; -import android.app.ProgressDialog; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.ListView; -import org.flyve.inventory.InventoryTask; import org.flyve.inventory.agent.R; import org.flyve.inventory.agent.adapter.HomeAdapter; import org.flyve.inventory.agent.preference.GlobalParametersPreference; import org.flyve.inventory.agent.preference.InventoryParametersPreference; import org.flyve.inventory.agent.service.InventoryService; import org.flyve.inventory.agent.ui.ActivityInventoryReport; -import org.flyve.inventory.agent.ui.InventoryAgentApp; +import org.flyve.inventory.agent.ui.DialogListServers; import org.flyve.inventory.agent.utils.FlyveLog; -import org.flyve.inventory.agent.utils.Helpers; -import org.flyve.inventory.agent.utils.HttpInventory; import java.util.ArrayList; import java.util.List; @@ -51,7 +47,6 @@ public class HomeModel implements Home.Model { private Home.Presenter presenter; private ArrayList arrHome; - private Intent mServiceIntent; public HomeModel(Home.Presenter presenter) { this.presenter = presenter; @@ -63,7 +58,7 @@ public void doBindService(Activity activity) { // we know will be running in our own process (and thus won't be // supporting component replacement by other applications). InventoryService inventoryService = new InventoryService(); - mServiceIntent = new Intent(activity, inventoryService.getClass()); + Intent mServiceIntent = new Intent(activity, inventoryService.getClass()); if (!isMyServiceRunning(activity, inventoryService.getClass())) { ComponentName result = activity.startService(mServiceIntent); @@ -116,59 +111,26 @@ public void setupList(Activity activity, ListView lst) { @Override public void clickItem(final Activity activity, HomeSchema homeSchema) { - if(homeSchema.getId().equals("7")) { + if (homeSchema.getId().equals("7")) { Intent miIntent = new Intent(activity, GlobalParametersPreference.class); activity.startActivity(miIntent); } - if(homeSchema.getId().equals("5")) { + if (homeSchema.getId().equals("5")) { Intent miIntent = new Intent(activity, InventoryParametersPreference.class); activity.startActivity(miIntent); } // Show my inventory - if(homeSchema.getId().equals("4")) { + if (homeSchema.getId().equals("4")) { Intent miIntent = new Intent(activity, ActivityInventoryReport.class); activity.startActivity(miIntent); } // Sent inventory - if(homeSchema.getId().equals("3")) { - final ProgressDialog progressBar = ProgressDialog.show(activity, "Sending inventory", activity.getResources().getString(R.string.loading)); - - final InventoryTask inventoryTask = new InventoryTask(activity, Helpers.getAgentDescription(activity)); - - InventoryAgentApp agentApp = (InventoryAgentApp) activity.getApplicationContext(); - inventoryTask.setTag(agentApp.getTag()); - - // Sending anonymous information - inventoryTask.getXML(new InventoryTask.OnTaskCompleted() { - @Override - public void onTaskSuccess(String data) { - FlyveLog.d(data); - HttpInventory httpInventory = new HttpInventory(activity); - httpInventory.sendInventory(data, new HttpInventory.OnTaskCompleted() { - @Override - public void onTaskSuccess(String data) { - progressBar.dismiss(); - Helpers.snackClose(activity, data, activity.getResources().getString(R.string.snackButton), false); - Helpers.sendAnonymousData(activity, inventoryTask); - } - - @Override - public void onTaskError(String error) { - progressBar.dismiss(); - presenter.showError(error); - } - }); - } - - @Override - public void onTaskError(Throwable error) { - FlyveLog.e(error.getMessage()); - presenter.showError(error.getMessage()); - } - }); + if (homeSchema.getId().equals("3")) { + DialogListServers alert = new DialogListServers(); + alert.showDialog(activity, presenter); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/core/servers/Servers.java b/app/src/main/java/org/flyve/inventory/agent/core/servers/Servers.java index 0940d7853..9c5ec80ea 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/servers/Servers.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/servers/Servers.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-mdm-agent @@ -31,13 +31,13 @@ public interface Servers { interface View { void showError(String message); - void sendServers(ArrayList load); + void showServer(ArrayList load); } interface Presenter { // Views void showError(String message); - void sendServers(ArrayList model); + void showServers(ArrayList model); // Models void loadServers(final Activity activity); diff --git a/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersModel.java b/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersModel.java index f413dc9f3..7db570799 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersModel.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersModel.java @@ -36,8 +36,7 @@ public class ServersModel implements Servers.Model { } public void loadServers(final Activity activity) { - LocalPreferences preferences = new LocalPreferences(activity); - presenter.sendServers(preferences.loadServerArray()); + presenter.showServers(new LocalPreferences(activity).loadServerArray()); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersPresenter.java b/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersPresenter.java index 3166c6dbd..29d0f7e14 100644 --- a/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersPresenter.java +++ b/app/src/main/java/org/flyve/inventory/agent/core/servers/ServersPresenter.java @@ -13,7 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ - * @author Rafael Hernandez + * @author Ivan Del Pino * @copyright Copyright Teclib. All rights reserved. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyve-mdm/android-mdm-agent @@ -45,9 +45,9 @@ public void showError(String message) { } @Override - public void sendServers(ArrayList model) { + public void showServers(ArrayList model) { if (view != null) { - view.sendServers(model); + view.showServer(model); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/service/InventoryService.java b/app/src/main/java/org/flyve/inventory/agent/service/InventoryService.java index 1cfb26dd9..29038da25 100644 --- a/app/src/main/java/org/flyve/inventory/agent/service/InventoryService.java +++ b/app/src/main/java/org/flyve/inventory/agent/service/InventoryService.java @@ -36,6 +36,7 @@ import org.flyve.inventory.InventoryTask; import org.flyve.inventory.agent.R; +import org.flyve.inventory.agent.model.ServerModel; import org.flyve.inventory.agent.utils.FlyveLog; import org.flyve.inventory.agent.utils.Helpers; import org.flyve.inventory.agent.utils.HttpInventory; @@ -180,7 +181,12 @@ private void sendInventory() { @Override public void onTaskSuccess(String data) { HttpInventory httpInventory = new HttpInventory(context.getApplicationContext()); - httpInventory.sendInventory(data, new HttpInventory.OnTaskCompleted() { + ServerModel serverModel = new ServerModel(); + serverModel.setAddress("https://demo-api.flyve.org/plugins/fusioninventory/"); + serverModel.setTag(""); + serverModel.setLogin(""); + serverModel.setPass(""); + httpInventory.sendInventory(data, serverModel, new HttpInventory.OnTaskCompleted() { @Override public void onTaskSuccess(String data) { Helpers.sendToNotificationBar(context.getApplicationContext(), context.getResources().getString(R.string.inventory_notification_sent)); diff --git a/app/src/main/java/org/flyve/inventory/agent/ui/ActivityDetailServer.java b/app/src/main/java/org/flyve/inventory/agent/ui/ActivityDetailServer.java index 9bc4de2c0..04d83dabf 100644 --- a/app/src/main/java/org/flyve/inventory/agent/ui/ActivityDetailServer.java +++ b/app/src/main/java/org/flyve/inventory/agent/ui/ActivityDetailServer.java @@ -23,7 +23,9 @@ package org.flyve.inventory.agent.ui; +import android.content.Intent; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; @@ -33,8 +35,10 @@ import org.flyve.inventory.agent.R; import org.flyve.inventory.agent.core.detailserver.DetailServer; import org.flyve.inventory.agent.core.detailserver.DetailServerPresenter; +import org.flyve.inventory.agent.model.ServerModel; import org.flyve.inventory.agent.utils.FlyveLog; import org.flyve.inventory.agent.utils.Helpers; +import org.flyve.inventory.agent.utils.Utils; import java.util.ArrayList; @@ -42,6 +46,7 @@ public class ActivityDetailServer extends AppCompatActivity implements DetailSer private DetailServer.Presenter presenter; private Button deleteServer; + private Button actionServer; private EditText editUrlAddress; private EditText editTag; private EditText editLogin; @@ -51,6 +56,7 @@ public class ActivityDetailServer extends AppCompatActivity implements DetailSer /** * Called when the activity is starting, inflates the activity's UI + * * @param savedInstanceState if the activity is re-initialized, it contains the data it most recently supplied */ @Override @@ -63,8 +69,13 @@ protected void onCreate(Bundle savedInstanceState) { setToolbar(); serverName = getIntent().getStringExtra("serverName"); - if (serverName == null) + if (serverName == null) { + actionServer.setText(R.string.add_server); deleteServer.setVisibility(View.GONE); + } else { + actionServer.setText(R.string.update_server); + presenter.loadServer(serverName, getApplicationContext()); + } } private void setToolbar() { @@ -86,10 +97,13 @@ public void onClick(View v) { private void instanceObject() { toolbar = findViewById(R.id.toolbar); deleteServer = toolbar.findViewById(R.id.deleteServer); + actionServer = findViewById(R.id.actionServer); editUrlAddress = findViewById(R.id.editUrlAddress); editTag = findViewById(R.id.editTag); editLogin = findViewById(R.id.editLogin); editPassWord = findViewById(R.id.editPassWord); + actionServer.setOnClickListener(this); + deleteServer.setOnClickListener(this); } @@ -103,13 +117,19 @@ public void onClick(View v) { serverInfo.add(editLogin.getText().toString()); serverInfo.add(editPassWord.getText().toString()); if (serverName == null) { - presenter.saveServer(serverInfo); + presenter.saveServer(serverInfo, getApplicationContext()); } else { - presenter.updateServer(serverInfo, serverName); + presenter.updateServer(serverInfo, serverName, getApplicationContext()); } break; case R.id.deleteServer: - presenter.deleteServer(serverName); + String message = "You want delete this server"; + Utils.showAlertDialog(message, this, new Utils.OnTaskCompleted() { + @Override + public void onTaskSuccess() { + presenter.deleteServer(serverName, getApplicationContext()); + } + }); break; } } @@ -121,6 +141,20 @@ public void showError(String message) { @Override public void successful(String message) { - Helpers.snackClose(ActivityDetailServer.this, message, getString(R.string.permission_snack_ok), true); + onBackPressed(); + } + + @Override + public void modelServer(ServerModel model) { + editUrlAddress.setText(model.getAddress()); + editTag.setText(model.getTag()); + editLogin.setText(model.getLogin()); + editPassWord.setText(model.getPass()); + } + + @Override + public void onBackPressed() { + super.onBackPressed(); + LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("reload-servers")); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/ui/ActivityListServers.java b/app/src/main/java/org/flyve/inventory/agent/ui/ActivityListServers.java index 246886e4c..fa8d2fc17 100644 --- a/app/src/main/java/org/flyve/inventory/agent/ui/ActivityListServers.java +++ b/app/src/main/java/org/flyve/inventory/agent/ui/ActivityListServers.java @@ -23,15 +23,19 @@ package org.flyve.inventory.agent.ui; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; -import android.widget.ProgressBar; +import android.widget.RelativeLayout; import org.flyve.inventory.agent.R; import org.flyve.inventory.agent.adapter.ListServersAdapter; @@ -45,7 +49,13 @@ public class ActivityListServers extends AppCompatActivity implements Servers.View { private Servers.Presenter presenter; - private ProgressBar progressBar; + + private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + presenter.loadServers(ActivityListServers.this); + } + }; /** * Called when the activity is starting, inflates the activity's UI @@ -55,7 +65,6 @@ public class ActivityListServers extends AppCompatActivity implements Servers.Vi protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_servers); - progressBar = findViewById(R.id.progressBar); presenter = new ServersPresenter(this); Toolbar toolbar = findViewById(R.id.toolbar); try { @@ -81,6 +90,9 @@ public void onClick(View v) { }); presenter.loadServers(this); + + IntentFilter filter = new IntentFilter("reload-servers"); + LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter); } @@ -90,10 +102,22 @@ public void showError(String message) { } @Override - public void sendServers(ArrayList model) { - RecyclerView listServer = findViewById(R.id.recyclerListServer); - ListServersAdapter adapter = new ListServersAdapter(model, this); - listServer.setLayoutManager(new LinearLayoutManager(this)); - listServer.setAdapter(adapter); + public void showServer(ArrayList model) { + if (model.size() == 0) { + RelativeLayout containerNoServer = findViewById(R.id.containerNoServer); + containerNoServer.setVisibility(View.VISIBLE); + } else { + RecyclerView listServer = findViewById(R.id.recyclerListServer); + listServer.setVisibility(View.VISIBLE); + ListServersAdapter adapter = new ListServersAdapter(model, this); + listServer.setLayoutManager(new LinearLayoutManager(this)); + listServer.setAdapter(adapter); + } + } + + @Override + protected void onDestroy() { + LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); + super.onDestroy(); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/ui/DialogListServers.java b/app/src/main/java/org/flyve/inventory/agent/ui/DialogListServers.java new file mode 100644 index 000000000..15e19773e --- /dev/null +++ b/app/src/main/java/org/flyve/inventory/agent/ui/DialogListServers.java @@ -0,0 +1,127 @@ +/* + * Copyright Teclib. All rights reserved. + * + * Flyve MDM is a mobile device management software. + * + * Flyve MDM 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. + * + * Flyve MDM 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 Ivan Del Pino + * @copyright Copyright Teclib. All rights reserved. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/flyve-mdm/android-inventory-agent + * @link https://flyve-mdm.com + * ------------------------------------------------------------------------------ + */ + +package org.flyve.inventory.agent.ui; + +import android.app.Activity; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.view.View; +import android.view.Window; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; + +import org.flyve.inventory.InventoryTask; +import org.flyve.inventory.agent.R; +import org.flyve.inventory.agent.core.home.Home; +import org.flyve.inventory.agent.model.ServerModel; +import org.flyve.inventory.agent.utils.FlyveLog; +import org.flyve.inventory.agent.utils.Helpers; +import org.flyve.inventory.agent.utils.HttpInventory; +import org.flyve.inventory.agent.utils.LocalPreferences; + +import java.util.ArrayList; + +public class DialogListServers { + + private Dialog dialog; + private Spinner spinnerServers; + + public void showDialog(final Activity activity, final Home.Presenter presenter){ + dialog = new Dialog(activity); + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); + dialog.setCancelable(false); + dialog.setCanceledOnTouchOutside(true); + dialog.setContentView(R.layout.dialog_list_servers); + + setSpinner(activity); + + Button dialogButton = dialog.findViewById(R.id.btn_dialog); + dialogButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + sendInventory(activity, presenter); + } + }); + + dialog.show(); + + dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + dialog.dismiss(); + } + }); + } + + private void setSpinner(Activity activity) { + ArrayList serverArray = new LocalPreferences(activity).loadServerArray(); + ArrayAdapter adapter = new ArrayAdapter<>(activity, R.layout.spinner_item, serverArray); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinnerServers = dialog.findViewById(R.id.spinnerServers); + spinnerServers.setAdapter(adapter); + } + + private void sendInventory(final Activity activity, final Home.Presenter presenter) { + String message = activity.getResources().getString(R.string.loading); + final ProgressDialog progressBar = ProgressDialog.show(activity, "Sending inventory", message); + + final InventoryTask inventoryTask = new InventoryTask(activity, Helpers.getAgentDescription(activity)); + + // Sending anonymous information + inventoryTask.getXML(new InventoryTask.OnTaskCompleted() { + @Override + public void onTaskSuccess(String data) { + FlyveLog.d(data); + HttpInventory httpInventory = new HttpInventory(activity); + String serverName = spinnerServers.getSelectedItem().toString(); + ServerModel model = httpInventory.setServerModel(serverName); + httpInventory.sendInventory(data, model, new HttpInventory.OnTaskCompleted() { + @Override + public void onTaskSuccess(String data) { + progressBar.dismiss(); + String action = activity.getResources().getString(R.string.snackButton); + Helpers.snackClose(activity, data, action, false); + Helpers.sendAnonymousData(activity, inventoryTask); + dialog.dismiss(); + } + + @Override + public void onTaskError(String error) { + progressBar.dismiss(); + presenter.showError(error); + dialog.dismiss(); + } + }); + } + + @Override + public void onTaskError(Throwable error) { + FlyveLog.e(error.getMessage()); + presenter.showError(error.getMessage()); + } + }); + } +} \ No newline at end of file diff --git a/app/src/main/java/org/flyve/inventory/agent/utils/DataLoader.java b/app/src/main/java/org/flyve/inventory/agent/utils/DataLoader.java index 04c1acbb2..b02b2c629 100644 --- a/app/src/main/java/org/flyve/inventory/agent/utils/DataLoader.java +++ b/app/src/main/java/org/flyve/inventory/agent/utils/DataLoader.java @@ -50,7 +50,7 @@ import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; -import org.flyve.inventory.agent.ui.InventoryAgentApp; +import org.flyve.inventory.agent.model.ServerModel; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -66,7 +66,7 @@ public class DataLoader { - public HttpResponse secureLoadData(Context appContext, InventoryAgentApp mFusionApp, String lastXML) throws + public HttpResponse secureLoadData(Context appContext, ServerModel mFusionApp, String lastXML) throws IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { @@ -98,7 +98,7 @@ public HttpResponse secureLoadData(Context appContext, InventoryAgentApp mFusion DefaultHttpClient sslClient = new DefaultHttpClient(ccm, client.getParams()); HttpPost post; - post = new HttpPost(mFusionApp.getUrl()); + post = new HttpPost(mFusionApp.getAddress()); sslClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { @@ -115,15 +115,15 @@ public void process(HttpRequest request, HttpContext context) { } HttpContext context = new BasicHttpContext(); - URL url = new URL(mFusionApp.getUrl()); + URL url = new URL(mFusionApp.getAddress()); - String login = mFusionApp.getCredentialsLogin(); + String login = mFusionApp.getLogin(); if (!login.equals("")) { FlyveLog.log(this, "HTTP credentials given : use it if necessary", Log.VERBOSE); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(url.getHost(), AuthScope.ANY_PORT), - new UsernamePasswordCredentials(mFusionApp.getCredentialsLogin(), - mFusionApp.getCredentialsPassword())); + new UsernamePasswordCredentials(mFusionApp.getLogin(), + mFusionApp.getPass())); context.setAttribute("http.auth.credentials-provider", credentialsProvider); } diff --git a/app/src/main/java/org/flyve/inventory/agent/utils/HttpInventory.java b/app/src/main/java/org/flyve/inventory/agent/utils/HttpInventory.java index f508ba2a0..596affeae 100644 --- a/app/src/main/java/org/flyve/inventory/agent/utils/HttpInventory.java +++ b/app/src/main/java/org/flyve/inventory/agent/utils/HttpInventory.java @@ -33,7 +33,10 @@ import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.flyve.inventory.agent.R; +import org.flyve.inventory.agent.model.ServerModel; import org.flyve.inventory.agent.ui.InventoryAgentApp; +import org.json.JSONException; +import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; @@ -45,7 +48,6 @@ public class HttpInventory { private Context appContext; - private InventoryAgentApp mFusionApp; private URL url = null; private static Handler uiHandler; @@ -54,6 +56,8 @@ public class HttpInventory { uiHandler = new Handler(Looper.getMainLooper()); } + private ServerModel serverModel; + private static void runOnUI(Runnable runnable) { uiHandler.post(runnable); } @@ -64,15 +68,31 @@ private static void runOnUI(Runnable runnable) { */ public HttpInventory(Context context) { this.appContext = context; - mFusionApp = (InventoryAgentApp) context.getApplicationContext(); + } + + public ServerModel setServerModel(String serverName) { + LocalPreferences preferences = new LocalPreferences(appContext); + ServerModel serverModel = new ServerModel(); + try { + JSONObject jo = preferences.loadJSONObject(serverName); + serverModel.setAddress(jo.getString("address")); + serverModel.setTag(jo.getString("tag")); + serverModel.setLogin(jo.getString("login")); + serverModel.setPass(jo.getString("pass")); + } catch (JSONException e) { + FlyveLog.e(e.getMessage()); + } + return serverModel; } /** * Sends the inventory - * @param string lastXMLResult the inventory information + * @param lastXMLResult lastXMLResult the inventory information + * @param callback * @return boolean true if succeed, false otherwise */ - public void sendInventory(final String lastXMLResult, final OnTaskCompleted callback) { + public void sendInventory(final String lastXMLResult, ServerModel serverModel, final OnTaskCompleted callback) { + this.serverModel = serverModel; if (lastXMLResult == null) { FlyveLog.log(this, "No XML Inventory ", Log.ERROR); @@ -81,7 +101,7 @@ public void sendInventory(final String lastXMLResult, final OnTaskCompleted call } try { - url = new URL(mFusionApp.getUrl()); + url = new URL(serverModel.getAddress()); FlyveLog.d(url.toString()); } catch (MalformedURLException e) { FlyveLog.log(this, appContext.getResources().getString(R.string.error_url_is_malformed) + e.getLocalizedMessage(), Log.ERROR); @@ -89,13 +109,13 @@ public void sendInventory(final String lastXMLResult, final OnTaskCompleted call return; } - if (url == null || "".equals(mFusionApp.getUrl())) { + if (url == null || "".equals(serverModel.getAddress())) { FlyveLog.log(this, appContext.getResources().getString(R.string.error_url_is_not_found), Log.ERROR); callback.onTaskError(appContext.getResources().getString(R.string.error_url_is_not_found)); return; } - if (mFusionApp == null) { + if (this.serverModel == null) { FlyveLog.log(this, appContext.getResources().getString(R.string.error_send_fail), Log.ERROR); callback.onTaskError(appContext.getResources().getString(R.string.error_send_fail)); return; @@ -112,7 +132,7 @@ public void run() { private void connect(String lastXMLResult, OnTaskCompleted callback){ try { DataLoader dl = new DataLoader(); - HttpResponse response = dl.secureLoadData(appContext, mFusionApp, lastXMLResult); + HttpResponse response = dl.secureLoadData(appContext, serverModel, lastXMLResult); StringBuilder sb = new StringBuilder(); sb.append("HEADERS:\n\n"); diff --git a/app/src/main/java/org/flyve/inventory/agent/utils/LocalPreferences.java b/app/src/main/java/org/flyve/inventory/agent/utils/LocalPreferences.java index 493678ec8..c16482a13 100644 --- a/app/src/main/java/org/flyve/inventory/agent/utils/LocalPreferences.java +++ b/app/src/main/java/org/flyve/inventory/agent/utils/LocalPreferences.java @@ -1,3 +1,26 @@ +/* + * Copyright Teclib. All rights reserved. + * + * Flyve MDM is a mobile device management software. + * + * Flyve MDM 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. + * + * Flyve MDM 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 Ivan Del Pino + * @copyright Copyright Teclib. All rights reserved. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/flyve-mdm/android-inventory-agent + * @link https://flyve-mdm.com + * ------------------------------------------------------------------------------ + */ + package org.flyve.inventory.agent.utils; import android.content.Context; @@ -36,6 +59,11 @@ public void saveJSONObject(String key, JSONObject object) { editor.apply(); } + public void deletePreferences(String key) { + SharedPreferences preferences = getSettings(); + preferences.edit().remove(key).apply(); + } + /** * @return an Json Object */ @@ -46,7 +74,7 @@ public JSONObject loadJSONObject(String key) throws JSONException { public ArrayList loadServerArray() { ArrayList ids = new ArrayList<>(); SharedPreferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(mContext); - int size = mSharedPreference1.getInt("Status_size", 0); + int size = Integer.parseInt(mSharedPreference1.getString("Status_size", "")); for (int i = 0; i < size; i++) { ids.add(mSharedPreference1.getString("Status_" + i, "")); @@ -54,7 +82,7 @@ public ArrayList loadServerArray() { return ids; } - public boolean saveServerArray(ArrayList list) { + public void saveServerArray(ArrayList list) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); SharedPreferences.Editor mEdit1 = sp.edit(); /* sKey is an array */ @@ -64,6 +92,6 @@ public boolean saveServerArray(ArrayList list) { mEdit1.remove("Status_" + i); mEdit1.putString("Status_" + i, list.get(i)); } - return mEdit1.commit(); + mEdit1.apply(); } } diff --git a/app/src/main/java/org/flyve/inventory/agent/utils/Utils.java b/app/src/main/java/org/flyve/inventory/agent/utils/Utils.java new file mode 100644 index 000000000..e990f5ab5 --- /dev/null +++ b/app/src/main/java/org/flyve/inventory/agent/utils/Utils.java @@ -0,0 +1,64 @@ +/* + * Copyright Teclib. All rights reserved. + * + * Flyve MDM is a mobile device management software. + * + * Flyve MDM 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. + * + * Flyve MDM 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 Ivan Del Pino + * @copyright Copyright Teclib. All rights reserved. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/flyve-mdm/android-inventory-agent + * @link https://flyve-mdm.com + * ------------------------------------------------------------------------------ + */ + +package org.flyve.inventory.agent.utils; + +import android.content.Context; +import android.content.DialogInterface; +import android.support.v7.app.AlertDialog; + +public class Utils { + + public static void showAlertDialog(String message, Context context, final OnTaskCompleted callback) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setMessage(message); + builder.setCancelable(true); + + builder.setPositiveButton( + "Yes", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + callback.onTaskSuccess(); + } + }); + + builder.setNegativeButton( + "No", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }); + + AlertDialog alert = builder.create(); + alert.show(); + } + + public interface OnTaskCompleted { + /** + * if everything is Ok + */ + void onTaskSuccess(); + } + +} diff --git a/app/src/main/res/layout/activity_detail_server.xml b/app/src/main/res/layout/activity_detail_server.xml index 653f9a152..1e21b57f5 100644 --- a/app/src/main/res/layout/activity_detail_server.xml +++ b/app/src/main/res/layout/activity_detail_server.xml @@ -8,6 +8,7 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" + android:background="@color/colorAccent" android:minHeight="?android:attr/actionBarSize"> + android:textColor="@color/white" + android:textSize="18sp" + android:textStyle="bold" />