Search in sources :

Example 1 with AppDataBase

use of org.flyve.mdm.agent.data.database.setup.AppDataBase in project android-mdm-agent by flyve-mdm.

the class AppReceiver method onRemoveApp.

public void onRemoveApp(String mPackage, Context context) {
    String taskId = "0";
    AppDataBase dataBase = AppDataBase.getAppDatabase(context);
    // retrieve taskId from applicationDAO
    Application[] appsArray = dataBase.applicationDao().getApplicationByPackageName(mPackage);
    // app installed by agent
    if (appsArray.length == 1) {
        taskId = appsArray[0].taskId;
        FlyveLog.d("Retrieve TaskId -> " + appsArray[0].taskId);
        // remove file from DB
        FlyveLog.d("Remove File from DB -> " + mPackage);
        dataBase.FileDao().deleteByName(mPackage + ".apk");
        dataBase.FileDao().deleteByName(mPackage + ".upk");
        // remove app from DB
        FlyveLog.d("Remove App from DB -> " + mPackage);
        dataBase.applicationDao().deleteByPackageName(mPackage);
        // try to remove Package from device
        String filePath = "";
        try {
            filePath = new StorageFolder(context).getApkDir();
            filePath = filePath + mPackage + ".apk";
            // validating if file exists
            File fileApk = new File(filePath);
            if (!fileApk.exists()) {
                filePath = filePath + mPackage + ".upk";
            }
            // remove package from device
            String realPath = new StorageFolder(context).convertPath(filePath);
            File file = new File(realPath);
            if (file.delete()) {
                FlyveLog.d("Successful removal of package : " + filePath);
            } else {
                FlyveLog.d("Can't remove Package : " + filePath);
            }
        } catch (Exception ex) {
            FlyveLog.e(this.getClass().getName() + ", removeApk", ex.getMessage());
        }
        BasePolicies.sendTaskStatusbyHttp(context, BasePolicies.FEEDBACK_DONE, taskId);
    }
}
Also used : AppDataBase(org.flyve.mdm.agent.data.database.setup.AppDataBase) Application(org.flyve.mdm.agent.data.database.entity.Application) File(java.io.File) StorageFolder(org.flyve.mdm.agent.utils.StorageFolder)

Example 2 with AppDataBase

use of org.flyve.mdm.agent.data.database.setup.AppDataBase in project android-mdm-agent by flyve-mdm.

the class PoliciesFiles method removeApk.

/**
 * Uninstall the Android Package
 * @param mPackage to uninstall
 * @return int if it succeed 1, otherwise 0
 */
public void removeApk(String mPackage, final String taskId) {
    this.taskId = taskId;
    this.status = BasePolicies.FEEDBACK_WAITING;
    // update taskID from DAO app
    AppDataBase dataBase = AppDataBase.getAppDatabase(context);
    Application[] appsArray = dataBase.applicationDao().getApplicationByPackageName(mPackage);
    // app installed by agent
    if (appsArray.length == 1) {
        dataBase.applicationDao().updateTaskId(mPackage, this.taskId);
    }
    // is priv app uninstall silently
    if (Helpers.isSystemApp(context).equalsIgnoreCase("1")) {
        // Silently for System apps
        this.status = BasePolicies.FEEDBACK_WAITING;
        Helpers.uninstallApkSilently(mPackage);
    } else {
        // use activity to install apk
        this.status = BasePolicies.FEEDBACK_WAITING;
        Helpers.uninstallApk(context, mPackage);
    }
    BasePolicies.sendTaskStatusbyHttp(this.context, this.status, this.taskId);
}
Also used : AppDataBase(org.flyve.mdm.agent.data.database.setup.AppDataBase) Application(org.flyve.mdm.agent.data.database.entity.Application)

Example 3 with AppDataBase

use of org.flyve.mdm.agent.data.database.setup.AppDataBase in project android-mdm-agent by flyve-mdm.

the class MqttController method subscribe.

/**
 * Subscribe to the topic
 * When come from MQTT has a format like this {"subscribe":[{"topic":"/2/fleet/22"}]}
 */
public void subscribe(final String channel) {
    if (channel == null || channel.contains("null")) {
        // case of unsubscribe
        AppDataBase dataBase = AppDataBase.getAppDatabase(context);
        dataBase.TopicsDao().deleteFleets();
        return;
    }
    final List<Topics> topics = new TopicsData(context).setValue(channel, 0);
    // if topic null
    if (topics == null || topics.isEmpty()) {
        return;
    }
    // use List instead array because we need to keep only topic with status 0
    List<String> lstTopics = new ArrayList<>();
    List<Integer> lstQos = new ArrayList<>();
    for (int i = 0; i < topics.size(); i++) {
        // if not subscribed
        if (topics.get(i).status != 1) {
            lstTopics.add(topics.get(i).topic);
            lstQos.add(topics.get(i).qos);
        }
    }
    try {
        String[] arrayTopics;
        arrayTopics = lstTopics.toArray(new String[lstTopics.size()]);
        // transform list of Integer to array of int 'primitive'
        int size = lstQos.size();
        int[] arrayQos = new int[size];
        Integer[] temp = lstQos.toArray(new Integer[size]);
        for (int n = 0; n < size; ++n) {
            arrayQos[n] = temp[n];
        }
        IMqttToken subToken = client.subscribe(arrayTopics, arrayQos);
        subToken.setActionCallback(new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // The message was published
                for (String topic : asyncActionToken.getTopics()) {
                    new TopicsData(context).setStatusTopic(topic, 1);
                    FlyveLog.d("Subscribe from fleet " + topic);
                    broadcastReceivedLog(" -> " + topic, "Subscribed", String.valueOf(asyncActionToken.getTopics().length));
                }
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // The subscription could not be performed, maybe the user was not
                // authorized to subscribe on the specified topic e.g. using wildcards
                String errorMessage = " unknown";
                if (exception != null) {
                    errorMessage = exception.getMessage();
                    Log.d("Subscribe", "Error", exception);
                }
                FlyveLog.e(this.getClass().getName() + ", subscribe", "ERROR on subscribe: " + errorMessage);
                broadcastReceivedLog(ERROR, "Error on subscribe", errorMessage);
            }
        });
    } catch (Exception ex) {
        FlyveLog.e(this.getClass().getName() + ", subscribe", ex.getMessage());
    }
}
Also used : IMqttActionListener(org.eclipse.paho.client.mqttv3.IMqttActionListener) Topics(org.flyve.mdm.agent.data.database.entity.Topics) AppDataBase(org.flyve.mdm.agent.data.database.setup.AppDataBase) ArrayList(java.util.ArrayList) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) TopicsData(org.flyve.mdm.agent.data.database.TopicsData)

Example 4 with AppDataBase

use of org.flyve.mdm.agent.data.database.setup.AppDataBase in project android-mdm-agent by flyve-mdm.

the class Helpers method uninstallApk.

public static Boolean uninstallApk(Context context, String mPackage) {
    // check if the app is installed
    AppDataBase dataBase = AppDataBase.getAppDatabase(context);
    Application[] appsArray = dataBase.applicationDao().getApplicationByPackageName(mPackage);
    if (appsArray.length == 0 || !Helpers.isPackageInstalled(context, appsArray[0].appPackage)) {
        FlyveLog.d("This app is already uninstalled: " + appsArray[0].appName);
        return true;
    } else {
        // add notification
        Helpers.sendToNotificationBar(context, appsArray[0].id, context.getString(R.string.app_need_to_be_uninstall), appsArray[0].appName, true, MainActivity.class, "RemoveApp");
        return false;
    }
}
Also used : AppDataBase(org.flyve.mdm.agent.data.database.setup.AppDataBase) Application(org.flyve.mdm.agent.data.database.entity.Application)

Example 5 with AppDataBase

use of org.flyve.mdm.agent.data.database.setup.AppDataBase in project android-mdm-agent by flyve-mdm.

the class AppReceiver method onInstallApp.

public void onInstallApp(String mPackage, Context context) {
    // retrieve data from applicationDAO
    AppDataBase dataBase = AppDataBase.getAppDatabase(context);
    Application[] appsArray = dataBase.applicationDao().getApplicationByPackageName(mPackage);
    // app installed by agent update internal status and send status to flyveMDM
    if (appsArray.length == 1) {
        dataBase.applicationDao().updateStatus(Integer.toString(appsArray[0].id), "2");
        BasePolicies.sendTaskStatusbyHttp(context, BasePolicies.FEEDBACK_DONE, appsArray[0].taskId);
    }
}
Also used : AppDataBase(org.flyve.mdm.agent.data.database.setup.AppDataBase) Application(org.flyve.mdm.agent.data.database.entity.Application)

Aggregations

AppDataBase (org.flyve.mdm.agent.data.database.setup.AppDataBase)8 Application (org.flyve.mdm.agent.data.database.entity.Application)4 File (java.io.File)3 IMqttActionListener (org.eclipse.paho.client.mqttv3.IMqttActionListener)2 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)2 Topics (org.flyve.mdm.agent.data.database.entity.Topics)2 StorageFolder (org.flyve.mdm.agent.utils.StorageFolder)2 ArrayList (java.util.ArrayList)1 TopicsData (org.flyve.mdm.agent.data.database.TopicsData)1