Added capability to filter apps with system installed apps or user installed apps #539

Open
ruwin wants to merge 3 commits from ruwin/device-mgt-core:classify-apps into master

@ -54,6 +54,8 @@ public class Application implements Serializable {
private int memoryUsage; private int memoryUsage;
@ApiModelProperty(name = "isActive", value = "Is the application actively running", required = true) @ApiModelProperty(name = "isActive", value = "Is the application actively running", required = true)
private boolean isActive; private boolean isActive;
@ApiModelProperty(name = "isSystemApp", value = "Is the application a system app", required = true)
private int isSystemApp;
@ApiModelProperty(name = "hourlyUsage", value = "App hourly usage") @ApiModelProperty(name = "hourlyUsage", value = "App hourly usage")
private long hourlyUsage; private long hourlyUsage;
@ApiModelProperty(name = "dailyUsage", value = "App daily usage") @ApiModelProperty(name = "dailyUsage", value = "App daily usage")
@ -186,6 +188,14 @@ public class Application implements Serializable {
isActive = active; isActive = active;
} }
public int isSystemApp() {
return isSystemApp;
}
public void setSystemApp(int system) {
isSystemApp = system;
}
public long getHourlyUsage() { public long getHourlyUsage() {
return hourlyUsage; return hourlyUsage;
} }

@ -258,7 +258,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
if (installedApps.containsKey(newApp.getApplicationIdentifier())) { if (installedApps.containsKey(newApp.getApplicationIdentifier())) {
Application oldApp = installedApps.get(newApp.getApplicationIdentifier()); Application oldApp = installedApps.get(newApp.getApplicationIdentifier());
if (oldApp.isActive() != newApp.isActive() || oldApp.getMemoryUsage() != newApp.getMemoryUsage() if (oldApp.isActive() != newApp.isActive() || oldApp.getMemoryUsage() != newApp.getMemoryUsage()
|| !newApp.getVersion().equals(oldApp.getVersion())) { || !newApp.getVersion().equals(oldApp.getVersion()) || oldApp.isSystemApp() != newApp.isSystemApp()) {
newApp.setId(oldApp.getId()); newApp.setId(oldApp.getId());
appsToUpdate.put(newApp.getApplicationIdentifier(), newApp); appsToUpdate.put(newApp.getApplicationIdentifier(), newApp);
} }

@ -103,7 +103,7 @@ public interface ApplicationDAO {
* @param tenantId tenant ID * @param tenantId tenant ID
* @throws DeviceManagementDAOException If any database error occurred * @throws DeviceManagementDAOException If any database error occurred
*/ */
List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId, int isSystemApp)
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
/** /**

@ -56,8 +56,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " + stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " +
"CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID, " + "CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID, " +
"APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE, DEVICE_ID, ENROLMENT_ID) " + "APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE, DEVICE_ID, ENROLMENT_ID, IS_SYSTEM_APP) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (Application application : applications) { for (Application application : applications) {
stmt.setString(1, application.getName()); stmt.setString(1, application.getName());
@ -73,6 +73,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setBoolean(11, application.isActive()); stmt.setBoolean(11, application.isActive());
stmt.setInt(12, deviceId); stmt.setInt(12, deviceId);
stmt.setInt(13, enrolmentId); stmt.setInt(13, enrolmentId);
stmt.setInt(14, application.isSystemApp());
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
@ -91,7 +92,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " + stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " +
"VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " + "VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ?, IS_SYSTEM_APP = ? " +
"WHERE ID = ?"); "WHERE ID = ?");
for (Application application : applications) { for (Application application : applications) {
@ -104,7 +105,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, application.getMemoryUsage()); stmt.setInt(8, application.getMemoryUsage());
stmt.setBoolean(9, application.isActive()); stmt.setBoolean(9, application.isActive());
stmt.setInt(10, application.getId()); stmt.setInt(10, application.isSystemApp());
stmt.setInt(11, application.getId());
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
@ -211,7 +213,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID " + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, IS_SYSTEM_APP, TENANT_ID " +
"FROM DM_APPLICATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND APP_IDENTIFIER = ? AND " + "FROM DM_APPLICATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND APP_IDENTIFIER = ? AND " +
"VERSION = ? AND TENANT_ID = ?"); "VERSION = ? AND TENANT_ID = ?");
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
@ -222,7 +224,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
application = this.loadApplication(rs); application = this.loadApplicationDetails(rs);
} }
return application; return application;
} catch (SQLException e) { } catch (SQLException e) {
@ -237,6 +239,51 @@ public class ApplicationDAOImpl implements ApplicationDAO {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }
private Application loadApplicationDetails(ResultSet rs) throws DeviceManagementDAOException {
Properties properties;
Application application = new Application();
try {
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME"));
application.setType(rs.getString("TYPE"));
if (rs.getBytes("APP_PROPERTIES") != null) {
byte[] appProperties = rs.getBytes("APP_PROPERTIES");
try (ByteArrayInputStream bais = new ByteArrayInputStream(appProperties);
ObjectInputStream ois = new ObjectInputStream(bais)) {
properties = (Properties) ois.readObject();
application.setAppProperties(properties);
}
}
application.setCategory(rs.getString("CATEGORY"));
application.setImageUrl(rs.getString("IMAGE_URL"));
application.setLocationUrl(rs.getString("LOCATION_URL"));
application.setPlatform(rs.getString("PLATFORM"));
application.setVersion(rs.getString("VERSION"));
application.setMemoryUsage(rs.getInt("MEMORY_USAGE"));
application.setActive(rs.getBoolean("IS_ACTIVE"));
application.setSystemApp(rs.getInt("IS_SYSTEM_APP"));
application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER"));
} catch (IOException e) {
String msg = "IO error occurred while retrieving application properties";
log.error(msg, e);
throw new DeviceManagementDAOException("IO error occurred fetch at app properties", e);
} catch (ClassNotFoundException e) {
String msg = "Class not found error occurred while retrieving application properties";
log.error(msg, e);
throw new DeviceManagementDAOException("Class not found error occurred fetch at app properties", e);
} catch (SQLException e) {
String msg = "SQL error occurred while retrieving application properties";
log.error(msg, e);
throw new DeviceManagementDAOException("SQL error occurred fetch at application", e);
}
return application;
}
@Override @Override
public List<Application> getInstalledApplications(int deviceId, int enrolmentId, int tenantId) public List<Application> getInstalledApplications(int deviceId, int enrolmentId, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
@ -248,7 +295,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION " + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, IS_SYSTEM_APP, TENANT_ID FROM DM_APPLICATION " +
"WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); "WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?");
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
@ -257,7 +304,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
application = loadApplication(rs); application = loadApplicationDetails(rs);
applications.add(application); applications.add(application);
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -422,6 +469,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
return application; return application;
} }
@Override @Override
public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId)
throws DeviceManagementDAOException{ throws DeviceManagementDAOException{
@ -547,7 +596,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
} }
@Override @Override
public List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) public List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId, int isSystemApp)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;
List<Application> applicationList = new ArrayList<>(); List<Application> applicationList = new ArrayList<>();
@ -565,24 +614,30 @@ public class ApplicationDAOImpl implements ApplicationDAO {
"APP_PROPERTIES, " + "APP_PROPERTIES, " +
"MEMORY_USAGE, " + "MEMORY_USAGE, " +
"IS_ACTIVE, " + "IS_ACTIVE, " +
"IS_SYSTEM_APP, " +
"TENANT_ID " + "TENANT_ID " +
"FROM DM_APPLICATION " + "FROM DM_APPLICATION " +
"WHERE DEVICE_ID = ? AND " + "WHERE DEVICE_ID = ? AND " +
"ENROLMENT_ID = ? AND " + "ENROLMENT_ID = ? AND " +
"TENANT_ID = ? " + "TENANT_ID = ? " +
(isSystemApp != 0 ? "AND IS_SYSTEM_APP = ? " : "") +
"LIMIT ? " + "LIMIT ? " +
"OFFSET ?"; "OFFSET ? ";
try { try {
conn = this.getConnection(); conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId); int paramIndex = 1;
stmt.setInt(2, enrolmentId); stmt.setInt(paramIndex++, deviceId);
stmt.setInt(3, tenantId); stmt.setInt(paramIndex++, enrolmentId);
stmt.setInt(4, limit); stmt.setInt(paramIndex++, tenantId);
stmt.setInt(5, offset); if (isSystemApp != 0) {
stmt.setInt(paramIndex++, isSystemApp);
}
stmt.setInt(paramIndex++, limit);
stmt.setInt(paramIndex, offset);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
application = loadApplication(rs); application = loadApplicationDetails(rs);
applicationList.add(application); applicationList.add(application);
} }
} }
@ -590,7 +645,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
} catch (SQLException e) { } catch (SQLException e) {
String msg = "SQL Error occurred while retrieving the list of Applications " + String msg = "SQL Error occurred while retrieving the list of Applications " +
"installed in device id '" + deviceId; "installed on device id '" + deviceId + "'";
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }
@ -615,6 +670,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
"APP_PROPERTIES, " + "APP_PROPERTIES, " +
"MEMORY_USAGE, " + "MEMORY_USAGE, " +
"IS_ACTIVE, " + "IS_ACTIVE, " +
"IS_SYSTEM_APP, " +
"TENANT_ID " + "TENANT_ID " +
"FROM DM_APPLICATION " + "FROM DM_APPLICATION " +
"WHERE DEVICE_ID = ? AND " + "WHERE DEVICE_ID = ? AND " +
@ -628,7 +684,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
application = loadApplication(rs); application = loadApplicationDetails(rs);
applicationList.add(application); applicationList.add(application);
} }
} }

@ -1055,7 +1055,7 @@ public interface DeviceManagementProviderService {
* @return list of applications {@link Application} * @return list of applications {@link Application}
* @throws DeviceManagementException if any service level or DAO level error occurs * @throws DeviceManagementException if any service level or DAO level error occurs
*/ */
List<Application> getInstalledApplicationsOnDevice(Device device, int offset, int limit) List<Application> getInstalledApplicationsOnDevice(Device device, int offset, int limit, int isSystemApp)
throws DeviceManagementException; throws DeviceManagementException;
/** /**
* This method is for getting the installed application list of a device * This method is for getting the installed application list of a device

@ -5157,13 +5157,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public List<Application> getInstalledApplicationsOnDevice(Device device, int offset, int limit) throws DeviceManagementException { public List<Application> getInstalledApplicationsOnDevice(Device device, int offset, int limit, int isSystemApp) throws DeviceManagementException {
List<Application> applications; List<Application> applications;
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(), applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(),
device.getEnrolmentInfo().getId(), offset, limit, tenantId); device.getEnrolmentInfo().getId(), offset, limit, tenantId, isSystemApp);
if (applications == null) { if (applications == null) {
String msg = "Couldn't found applications for device identifier '" + device.getId() + "'"; String msg = "Couldn't found applications for device identifier '" + device.getId() + "'";
log.error(msg); log.error(msg);

@ -439,6 +439,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
APP_PROPERTIES BLOB NULL, APP_PROPERTIES BLOB NULL,
MEMORY_USAGE INTEGER NULL, MEMORY_USAGE INTEGER NULL,
IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE,
IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
ENROLMENT_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,

@ -405,6 +405,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
APP_PROPERTIES BLOB NULL, APP_PROPERTIES BLOB NULL,
MEMORY_USAGE INTEGER NULL, MEMORY_USAGE INTEGER NULL,
IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE,
IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
ENROLMENT_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,

@ -462,6 +462,7 @@ CREATE TABLE DM_APPLICATION (
APP_PROPERTIES VARBINARY(MAX) NULL, APP_PROPERTIES VARBINARY(MAX) NULL,
MEMORY_USAGE INTEGER NULL, MEMORY_USAGE INTEGER NULL,
IS_ACTIVE BIT NOT NULL DEFAULT 0, IS_ACTIVE BIT NOT NULL DEFAULT 0,
IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
ENROLMENT_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,

@ -462,6 +462,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
APP_PROPERTIES BLOB NULL, APP_PROPERTIES BLOB NULL,
MEMORY_USAGE INTEGER(10) NULL, MEMORY_USAGE INTEGER(10) NULL,
IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE,
IS_SYSTEM_APP TINYINT(1) NOT NULL DEFAULT 0,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
ENROLMENT_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,

@ -728,6 +728,7 @@ CREATE TABLE DM_APPLICATION (
APP_PROPERTIES BLOB NULL, APP_PROPERTIES BLOB NULL,
MEMORY_USAGE NUMBER(10) NULL, MEMORY_USAGE NUMBER(10) NULL,
IS_ACTIVE NUMBER(10) DEFAULT 0 NOT NULL, IS_ACTIVE NUMBER(10) DEFAULT 0 NOT NULL,
IS_SYSTEM_APP NUMBER(1) DEFAULT 0 NOT NULL,
DEVICE_ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL,
ENROLMENT_ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL,
TENANT_ID NUMBER(10) NOT NULL, TENANT_ID NUMBER(10) NOT NULL,

@ -486,6 +486,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
APP_PROPERTIES BYTEA NULL, APP_PROPERTIES BYTEA NULL,
MEMORY_USAGE INTEGER NULL, MEMORY_USAGE INTEGER NULL,
IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE,
IS_SYSTEM_APP SMALLINT NOT NULL DEFAULT 0,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
ENROLMENT_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,

Loading…
Cancel
Save