diff --git a/inventory/src/androidTest/java/org/flyve/inventory/BatteryTest.java b/inventory/src/androidTest/java/org/flyve/inventory/BatteryTest.java new file mode 100644 index 000000000..fa98b0a25 --- /dev/null +++ b/inventory/src/androidTest/java/org/flyve/inventory/BatteryTest.java @@ -0,0 +1,80 @@ +/** + * LICENSE + * + * This file is part of Flyve MDM Inventory Library for Android. + * + * Inventory Library for Android is a subproject of Flyve MDM. + * 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 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-library + * @link http://flyve.org/android-inventory-library/ + * @link https://flyve-mdm.com + * --------------------------------------------------------------------- + */ + +package org.flyve.inventory; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.flyve.inventory.categories.Battery; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertNotEquals; + +@RunWith(AndroidJUnit4.class) +public class BatteryTest { + + Context appContext = InstrumentationRegistry.getTargetContext(); + + @Test + public void getTechnology() { + assertNotEquals("", new Battery(appContext).getTechnology()); + } + + @Test + public void getTemperature() { + assertNotEquals("", new Battery(appContext).getTemperature()); + } + + @Test + public void getVoltage() { + assertNotEquals("", new Battery(appContext).getVoltage()); + } + + @Test + public void getLevel() { + assertNotEquals("", new Battery(appContext).getLevel()); + } + + @Test + public void getBatteryHealth() { + assertNotEquals("", new Battery(appContext).getBatteryHealth()); + } + + @Test + public void getBatteryStatus() { + assertNotEquals("", new Battery(appContext).getBatteryStatus()); + } + + @Test + public void getCapacity() { + assertNotEquals("", new Battery(appContext).getCapacity(appContext)); + } +} \ No newline at end of file diff --git a/inventory/src/main/java/org/flyve/inventory/categories/Battery.java b/inventory/src/main/java/org/flyve/inventory/categories/Battery.java index 7c665dc73..914d7a762 100644 --- a/inventory/src/main/java/org/flyve/inventory/categories/Battery.java +++ b/inventory/src/main/java/org/flyve/inventory/categories/Battery.java @@ -17,6 +17,7 @@ * GNU General Public License for more details. * --------------------------------------------------------------------- * @author Rafael Hernandez - + * @author Ivan del Pino - * @copyright Copyright Teclib. All rights reserved. * @copyright Copyright FusionInventory. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html @@ -28,15 +29,12 @@ package org.flyve.inventory.categories; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Build; -import org.flyve.inventory.FILog; - /** * This class get all the information of the baterry like level, voltage, temperature, status, health, technology * The constructor of the class trigger a BroadcastReceiver with the information @@ -57,13 +55,7 @@ public class Battery extends Categories { private static final long serialVersionUID = -4096347994131285426L; // Properties of this component - private String level = ""; - private String voltage = ""; - private String temperature = ""; - private String status = ""; - private String health = ""; - private String technology = ""; - private String capacity = ""; + private final Intent batteryIntent; /** * Indicates whether some other object is "equal to" this one @@ -88,13 +80,7 @@ public boolean equals(Object obj) { @Override public int hashCode() { int hash = super.hashCode(); - hash = 89 * hash + (this.level != null ? this.level.hashCode() : 0); - hash = 89 * hash + (this.voltage != null ? this.voltage.hashCode() : 0); - hash = 89 * hash + (this.temperature != null ? this.temperature.hashCode() : 0); - hash = 89 * hash + (this.status != null ? this.status.hashCode() : 0); - hash = 89 * hash + (this.health != null ? this.health.hashCode() : 0); - hash = 89 * hash + (this.technology != null ? this.technology.hashCode() : 0); - hash = 89 * hash + (this.capacity != null ? this.capacity.hashCode() : 0); + hash = 89 * hash + (this.batteryIntent != null ? this.batteryIntent.hashCode() : 0); return hash; } @@ -105,118 +91,110 @@ public int hashCode() { public Battery(Context xCtx) { super(xCtx); - // Trigger BroadcastReceiver - xCtx.registerReceiver(this.myBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - this.myBatteryReceiver.onReceive(xCtx, new Intent(Intent.ACTION_BATTERY_CHANGED)); - } - /** - * This BroadcastReceiver load the information of the component - */ - private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - - if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { - - level = String.valueOf(intent.getIntExtra("level", 0)) + "%"; - - voltage = String - .valueOf((float) intent.getIntExtra("voltage", 0) / 1000) - + "V"; - - temperature = String.valueOf((float) intent.getIntExtra( - "temperature", 0) / 10) - + "c"; - - technology = intent.getStringExtra("technology"); - - capacity = String.valueOf(getBatteryVoltage(context)); - - // get battery status - int intstatus = intent.getIntExtra("status", - BatteryManager.BATTERY_STATUS_UNKNOWN); - if (intstatus == BatteryManager.BATTERY_STATUS_CHARGING) { - status = "Charging"; - } else if (intstatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { - status = "Dis-charging"; - } else if (intstatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { - status = "Not charging"; - } else if (intstatus == BatteryManager.BATTERY_STATUS_FULL) { - status = "Full"; - } else { - status = "Unknown"; - } - - // get battery health - int inthealth = intent.getIntExtra("health", - BatteryManager.BATTERY_HEALTH_UNKNOWN); - if (inthealth == BatteryManager.BATTERY_HEALTH_GOOD) { - health = "Good"; - } else if (inthealth == BatteryManager.BATTERY_HEALTH_OVERHEAT) { - health = "Over Heat"; - } else if (inthealth == BatteryManager.BATTERY_HEALTH_DEAD) { - health = "Dead"; - } else if (inthealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) { - health = "Over Voltage"; - } else if (inthealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) { - health = "Unspecified Failure"; - } else { - health = "Unknown"; - } - - try { - if (!level.equals("0%")) { - // Load the information - Category c = new Category("BATTERIES", "batteries"); - c.put("CHEMISTRY", new CategoryValue(technology, "CHEMISTRY", "chemistry")); - c.put("TEMPERATURE", new CategoryValue(temperature, "TEMPERATURE", "temperature")); - c.put("VOLTAGE", new CategoryValue(voltage, "VOLTAGE", "voltage")); - c.put("LEVEL", new CategoryValue(level, "LEVEL", "level")); - c.put("HEALTH", new CategoryValue(health, "HEALTH", "health")); - c.put("STATUS", new CategoryValue(status, "STATUS", "status")); - c.put("CAPACITY", new CategoryValue(capacity, "CAPACITY", "capacity")); - Battery.this.add(c); - } - } catch (Exception ex) { - FILog.e(ex.getMessage()); - } - + IntentFilter batteryIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + batteryIntent = xCtx.registerReceiver(null, batteryIntentFilter); + + if (batteryIntent != null) { + if (!getLevel().equals("0%")) { + // Load the information + Category c = new Category("BATTERIES", "batteries"); + c.put("CHEMISTRY", new CategoryValue(getTechnology(), "CHEMISTRY", "chemistry")); + c.put("TEMPERATURE", new CategoryValue(getTemperature(), "TEMPERATURE", "temperature")); + c.put("VOLTAGE", new CategoryValue(getVoltage(), "VOLTAGE", "voltage")); + c.put("LEVEL", new CategoryValue(getLevel(), "LEVEL", "level")); + c.put("HEALTH", new CategoryValue(getBatteryHealth(), "HEALTH", "health")); + c.put("STATUS", new CategoryValue(getBatteryStatus(), "STATUS", "status")); + c.put("CAPACITY", new CategoryValue(getCapacity(xCtx), "CAPACITY", "capacity")); + this.add(c); } } + } - }; + public String getTechnology() { + return batteryIntent.getStringExtra("technology"); + } - private long getBatteryVoltage(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); - Integer chargeCounter = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); - Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); + public String getTemperature() { + return String.valueOf((float) batteryIntent.getIntExtra("temperature", 0) / 10) + "c"; + } + + public String getVoltage() { + return String.valueOf((float) batteryIntent.getIntExtra("voltage", 0) / 1000) + "V"; + } - if(chargeCounter == Integer.MIN_VALUE || capacity == Integer.MIN_VALUE) - return 0; + public String getLevel() { + return String.valueOf(batteryIntent.getIntExtra("level", 0)) + "%"; + } - return (chargeCounter/capacity) *100; + public String getBatteryHealth() { + String health; + int inthealth = batteryIntent.getIntExtra("health", + BatteryManager.BATTERY_HEALTH_UNKNOWN); + if (inthealth == BatteryManager.BATTERY_HEALTH_GOOD) { + health = "Good"; + } else if (inthealth == BatteryManager.BATTERY_HEALTH_OVERHEAT) { + health = "Over Heat"; + } else if (inthealth == BatteryManager.BATTERY_HEALTH_DEAD) { + health = "Dead"; + } else if (inthealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) { + health = "Over Voltage"; + } else if (inthealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) { + health = "Unspecified Failure"; } else { - Object mPowerProfile; - double batteryCapacity = 0; - final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"; + health = "Unknown"; + } + return health; + } - try { - mPowerProfile = Class.forName(POWER_PROFILE_CLASS) - .getConstructor(Context.class) - .newInstance(context); + public String getBatteryStatus() { + String status; + int intStatus = batteryIntent.getIntExtra("status", + BatteryManager.BATTERY_STATUS_UNKNOWN); + if (intStatus == BatteryManager.BATTERY_STATUS_CHARGING) { + status = "Charging"; + } else if (intStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { + status = "Dis-charging"; + } else if (intStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { + status = "Not charging"; + } else if (intStatus == BatteryManager.BATTERY_STATUS_FULL) { + status = "Full"; + } else { + status = "Unknown"; + } + return status; + } - batteryCapacity = (double) Class - .forName(POWER_PROFILE_CLASS) - .getMethod("getBatteryCapacity") - .invoke(mPowerProfile); + public String getCapacity(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); + assert mBatteryManager != null; + Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); + + if(capacity == Integer.MIN_VALUE) + return String.valueOf(0); + + return String.valueOf(capacity); + } else { + Object mPowerProfile; + double batteryCapacity = 0; + final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"; + + try { + mPowerProfile = Class.forName(POWER_PROFILE_CLASS) + .getConstructor(Context.class) + .newInstance(context); + + batteryCapacity = (double) Class + .forName(POWER_PROFILE_CLASS) + .getMethod("getBatteryCapacity") + .invoke(mPowerProfile); + + } catch (Exception e) { + e.printStackTrace(); + } - } catch (Exception e) { - e.printStackTrace(); - } + return String.valueOf(batteryCapacity); + } + } - return (long) batteryCapacity; - } - } }