Search in sources :

Example 1 with StorageFolder

use of org.flyve.mdm.agent.utils.StorageFolder in project android-mdm-agent by flyve-mdm.

the class PoliciesFiles method removeFile.

/**
 * Remove the file according to the given path
 * @param filePath
 * @return boolean true if file deleted, false otherwise
 */
public boolean removeFile(String filePath) {
    try {
        String realPath = new StorageFolder(context).convertPath(filePath);
        File file = new File(realPath);
        return file.delete();
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        return false;
    }
}
Also used : File(java.io.File) ActivityNotFoundException(android.content.ActivityNotFoundException) StorageFolder(org.flyve.mdm.agent.utils.StorageFolder)

Example 2 with StorageFolder

use of org.flyve.mdm.agent.utils.StorageFolder 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 3 with StorageFolder

use of org.flyve.mdm.agent.utils.StorageFolder in project android-mdm-agent by flyve-mdm.

the class PoliciesFiles method downloadFile.

/**
 * Download and save file from Id to path
 * @param path String path to save the file on device
 * @param id String Id from
 * @param sessionToken
 */
public void downloadFile(String path, String id, String sessionToken, String taskId) {
    // prevent CPU from going off if the user presses the power button during download
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wl.acquire();
    String filePath = "";
    try {
        filePath = new StorageFolder(context).convertPath(path);
    } catch (Exception ex) {
        FlyveLog.e(this.getClass().getName() + ", downloadFile", ex.getMessage());
        this.status = BasePolicies.FEEDBACK_FAILED;
    }
    final String url = routes.pluginFlyvemdmFile(id);
    String completeFilePath = download(url, filePath, sessionToken, taskId);
    if (!completeFilePath.isEmpty()) {
        this.status = BasePolicies.FEEDBACK_DONE;
    } else {
        this.status = BasePolicies.FEEDBACK_FAILED;
    }
}
Also used : PowerManager(android.os.PowerManager) StorageFolder(org.flyve.mdm.agent.utils.StorageFolder)

Example 4 with StorageFolder

use of org.flyve.mdm.agent.utils.StorageFolder in project android-mdm-agent by flyve-mdm.

the class PoliciesFiles method getFile.

private String getFile(JSONObject jsonObjDownload, String path, String url, String data, String sessionToken, String taskId) {
    String fileName = "";
    try {
        // Both has name
        if (jsonObjDownload.has("name")) {
            fileName = jsonObjDownload.getString("name");
        }
        // is APK / UPK
        if (jsonObjDownload.has("dl_filename")) {
            fileName = jsonObjDownload.getString("package_name");
            if (jsonObjDownload.getString("dl_filename").contains(".apk")) {
                fileName = fileName + ".apk";
            } else {
                fileName = fileName + ".upk";
            }
        }
        // validating if folder exists or create
        new File(path).mkdirs();
        // validating if file exists
        String filePath = path + fileName;
        File file = new File(filePath);
        if (file.exists()) {
            // if file return absolute patch
            if (type.equals("file")) {
                FlyveLog.i("File exists: " + filePath);
                addFile(file, fileName, taskId);
                return file.getAbsolutePath();
            // if package remove file and donwload new version
            } else {
                FlyveLog.i("Package exists: " + filePath + "Let's remove it to download new version");
                try {
                    // remove package from device
                    String realPath = new StorageFolder(context).convertPath(filePath);
                    File fileToRemove = new File(realPath);
                    if (fileToRemove.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());
                }
            }
        }
        Boolean isSave = ConnectionHTTP.getSyncFile(url, filePath, sessionToken, new ConnectionHTTP.ProgressCallback() {

            @Override
            public void progress(int value) {
                publishProgress(value);
            }
        });
        if (isSave) {
            publishProgress(100);
            FlyveLog.i(context.getString(R.string.download_file_ready) + file.getAbsolutePath());
            addFile(file, fileName, taskId);
            return file.getAbsolutePath();
        } else {
            publishProgress(100);
            FlyveLog.e(this.getClass().getName() + ", getFile", "Download fail: " + data + "\n" + url);
            return "";
        }
    } catch (Exception ex) {
        FlyveLog.e(this.getClass().getName() + ", getFile", ex.getMessage() + "\n" + url);
        return "";
    }
}
Also used : ConnectionHTTP(org.flyve.mdm.agent.utils.ConnectionHTTP) File(java.io.File) StorageFolder(org.flyve.mdm.agent.utils.StorageFolder)

Example 5 with StorageFolder

use of org.flyve.mdm.agent.utils.StorageFolder in project android-mdm-agent by flyve-mdm.

the class PoliciesFiles method downloadFile.

/**
 * Download and save file from Id to path
 * @param path String path to save the file on device
 * @param id String Id from
 * @param sessionToken
 */
public Boolean downloadFile(String path, String id, String sessionToken) {
    // prevent CPU from going off if the user presses the power button during download
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wl.acquire();
    String filePath = "";
    try {
        filePath = new StorageFolder(context).convertPath(path);
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
    }
    final String url = routes.pluginFlyvemdmFile(id, sessionToken);
    String completeFilePath = download(url, filePath);
    return (completeFilePath.equalsIgnoreCase(""));
}
Also used : PowerManager(android.os.PowerManager) ActivityNotFoundException(android.content.ActivityNotFoundException) StorageFolder(org.flyve.mdm.agent.utils.StorageFolder)

Aggregations

StorageFolder (org.flyve.mdm.agent.utils.StorageFolder)9 PowerManager (android.os.PowerManager)4 File (java.io.File)4 ActivityNotFoundException (android.content.ActivityNotFoundException)3 AppDataBase (org.flyve.mdm.agent.data.database.setup.AppDataBase)2 View (android.view.View)1 Button (android.widget.Button)1 CompoundButton (android.widget.CompoundButton)1 EditText (android.widget.EditText)1 Switch (android.widget.Switch)1 PoliciesData (org.flyve.mdm.agent.data.database.PoliciesData)1 Application (org.flyve.mdm.agent.data.database.entity.Application)1 FlyveAdminReceiver (org.flyve.mdm.agent.receivers.FlyveAdminReceiver)1 ConnectionHTTP (org.flyve.mdm.agent.utils.ConnectionHTTP)1 AndroidPolicies (org.flyve.policies.manager.AndroidPolicies)1 CustomPolicies (org.flyve.policies.manager.CustomPolicies)1