Search in sources :

Example 1 with MDMAgent

use of org.flyve.mdm.agent.ui.MDMAgent in project android-mdm-agent by flyve-mdm.

the class PoliciesAsyncTask method doInBackground.

protected Boolean doInBackground(Object... object) {
    final Context context = (Context) object[0];
    final Integer action = (Integer) object[1];
    final String topic = (String) object[2];
    final String message = (String) object[3];
    mHandler.post(new Runnable() {

        public void run() {
            switch(action) {
                case LOCK:
                    {
                        if (!MDMAgent.isSecureVersion()) {
                            try {
                                JSONObject jsonObj = new JSONObject(message);
                                if (jsonObj.has("lock")) {
                                    String lock = jsonObj.getString("lock");
                                    AndroidPolicies androidPolicies = new AndroidPolicies(context, FlyveAdminReceiver.class);
                                    if (lock.equalsIgnoreCase("now")) {
                                        // lock screen
                                        androidPolicies.lockScreen(LockActivity.class, context);
                                        // lock device
                                        androidPolicies.lockDevice();
                                    } else {
                                        // unlock screen
                                        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && Settings.canDrawOverlays(context)) {
                                            try {
                                                MDMAgent mainActivity = ((MDMAgent) context);
                                                mainActivity.getLockActivity().unlockScreen();
                                            } catch (Exception e) {
                                            }
                                        }
                                        // unlock device
                                        androidPolicies.unlockDevice();
                                        Helpers.sendBroadcast("unlock", "org.flyvemdm.finishlock", context);
                                    }
                                }
                            } catch (Exception ex) {
                                FlyveLog.e(this.getClass().getName() + ", LOCK ", ex.getMessage());
                            }
                        }
                    }
                case PING:
                    {
                        String data = "{\"input\":{\"_pong\":\"!\"}}";
                        Routes routes = new Routes(context);
                        MqttData cache = new MqttData(context);
                        String url = routes.pluginFlyvemdmAgent(cache.getAgentId());
                        pluginHttpResponse(context, url, data);
                    }
                    break;
                case WIPE:
                    {
                        if (!MDMAgent.isSecureVersion()) {
                            try {
                                JSONObject jsonObj = new JSONObject(message);
                                if (jsonObj.has("wipe") && "NOW".equalsIgnoreCase(jsonObj.getString("wipe"))) {
                                    sendStatusbyHttp(context, false);
                                    new AndroidPolicies(context, FlyveAdminReceiver.class).wipe();
                                }
                            } catch (Exception ex) {
                                FlyveLog.e(this.getClass().getName() + ", WIPE ", ex.getMessage());
                            }
                        }
                    }
                    break;
                case UNENROLL:
                    {
                        // set offline
                        sendStatusbyHttp(context, false);
                        // Remove all the information
                        new ApplicationData(context).deleteAll();
                        new FileData(context).deleteAll();
                        new MqttData(context).deleteAll();
                        new PoliciesData(context).deleteAll();
                    }
                    break;
                case GEOLOCATE:
                    {
                        FastLocationProvider fastLocationProvider = new FastLocationProvider();
                        Routes routes = new Routes(context);
                        final String url = routes.pluginFlyvemdmGeolocation();
                        boolean isAvailable = fastLocationProvider.getLocation(context, new FastLocationProvider.LocationResult() {

                            @Override
                            public void gotLocation(Location location) {
                                if (location == null) {
                                    FlyveLog.e(this.getClass().getName() + ", sendGPS", "without location yet...");
                                    try {
                                        JSONObject jsonPayload = new JSONObject();
                                        jsonPayload.put("_datetime", Helpers.getUnixTime(context));
                                        jsonPayload.put("_agents_id", new MqttData(context).getAgentId());
                                        jsonPayload.put("computers_id", new MqttData(context).getComputersId());
                                        jsonPayload.put("_gps", "off");
                                        JSONObject jsonInput = new JSONObject();
                                        jsonInput.put("input", jsonPayload);
                                        String payload = jsonInput.toString();
                                        pluginHttpResponse(context, url, payload);
                                    } catch (Exception ex) {
                                        Helpers.storeLog("fcm", "Error on GPS location", ex.getMessage());
                                    }
                                } else {
                                    try {
                                        String latitude = String.valueOf(location.getLatitude());
                                        String longitude = String.valueOf(location.getLongitude());
                                        // "{"input":{"_agents_id":":id","_datetime":":string","latitude":":float","longitude":":float"}}"
                                        JSONObject jsonGPS = new JSONObject();
                                        jsonGPS.put("latitude", latitude);
                                        jsonGPS.put("longitude", longitude);
                                        jsonGPS.put("_datetime", Helpers.getUnixTime(context));
                                        jsonGPS.put("_agents_id", new MqttData(context).getAgentId());
                                        jsonGPS.put("computers_id", new MqttData(context).getComputersId());
                                        JSONObject jsonInput = new JSONObject();
                                        jsonInput.put("input", jsonGPS);
                                        String payload = jsonInput.toString();
                                        pluginHttpResponse(context, url, payload);
                                    } catch (Exception ex) {
                                        FlyveLog.e(this.getClass().getName() + ", sendGPS", ex.getMessage());
                                        Helpers.storeLog("fcm", "Error on GPS location", ex.getMessage());
                                    }
                                }
                            }
                        });
                        if (!isAvailable) {
                            try {
                                JSONObject jsonPayload = new JSONObject();
                                jsonPayload.put("_datetime", Helpers.getUnixTime(context));
                                jsonPayload.put("_agents_id", new MqttData(context).getAgentId());
                                jsonPayload.put("_gps", "off");
                                jsonPayload.put("computers_id", new MqttData(context).getComputersId());
                                JSONObject jsonInput = new JSONObject();
                                jsonInput.put("input", jsonPayload);
                                String payload = jsonInput.toString();
                                pluginHttpResponse(context, url, payload);
                            } catch (Exception ex) {
                                Helpers.storeLog("fcm", "Error on GPS location", ex.getMessage());
                            }
                        }
                    }
                    break;
                case INVENTORY:
                    {
                        Inventory inventory = new Inventory();
                        inventory.getXMLInventory(context, new InventoryTask.OnTaskCompleted() {

                            @Override
                            public void onTaskSuccess(String s) {
                                Routes routes = new Routes(context);
                                MqttData cache = new MqttData(context);
                                String url = routes.pluginFlyvemdmAgent(cache.getAgentId());
                                try {
                                    JSONObject jsonPayload = new JSONObject();
                                    jsonPayload.put("_inventory", Helpers.base64encode(s));
                                    JSONObject jsonInput = new JSONObject();
                                    jsonInput.put("input", jsonPayload);
                                    String payload = jsonInput.toString();
                                    pluginHttpResponse(context, url, payload);
                                    Helpers.storeLog("fcm", "Inventory", "Inventory Send");
                                } catch (Exception ex) {
                                    Helpers.storeLog("fcm", "Error on json createInventory", ex.getMessage());
                                }
                            }

                            @Override
                            public void onTaskError(Throwable throwable) {
                                Helpers.storeLog("fcm", "Error on createInventory", throwable.getMessage());
                            }
                        });
                    }
                case POLICIES:
                    {
                        // Policy/passwordEnabled
                        callPolicy(context, PasswordEnablePolicy.class, PasswordEnablePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordQuality
                        callPolicy(context, PasswordQualityPolicy.class, PasswordQualityPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinLength
                        callPolicy(context, PasswordMinLengthPolicy.class, PasswordMinLengthPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinLowerCase
                        callPolicy(context, PasswordMinLowerCasePolicy.class, PasswordMinLowerCasePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinUpperCase
                        callPolicy(context, PasswordMinUpperCasePolicy.class, PasswordMinUpperCasePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinNonLetter
                        callPolicy(context, PasswordMinNonLetterPolicy.class, PasswordMinNonLetterPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinLetters
                        callPolicy(context, PasswordMinLetterPolicy.class, PasswordMinLetterPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinNumeric
                        callPolicy(context, PasswordMinNumericPolicy.class, PasswordMinNumericPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/passwordMinSymbols
                        callPolicy(context, PasswordMinSymbolsPolicy.class, PasswordMinSymbolsPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/MaximumFailedPasswordsForWipe
                        callPolicy(context, MaximumFailedPasswordForWipePolicy.class, MaximumFailedPasswordForWipePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/MaximumTimeToLock
                        callPolicy(context, MaximumTimeToLockPolicy.class, MaximumTimeToLockPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/storageEncryption
                        callPolicy(context, StorageEncryptionPolicy.class, StorageEncryptionPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableCamera
                        callPolicy(context, CameraPolicy.class, CameraPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableBluetooth
                        callPolicy(context, BluetoothPolicy.class, BluetoothPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableHostpotTethering
                        callPolicy(context, HostpotTetheringPolicy.class, HostpotTetheringPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableRoaming
                        callPolicy(context, RoamingPolicy.class, RoamingPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableWifi
                        callPolicy(context, WifiPolicy.class, WifiPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableSpeakerphone
                        callPolicy(context, SpeakerphonePolicy.class, SpeakerphonePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableCreateVpnProfiles
                        callPolicy(context, VPNPolicy.class, VPNPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamMusic
                        callPolicy(context, StreamMusicPolicy.class, StreamMusicPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamRing
                        callPolicy(context, StreamRingPolicy.class, StreamRingPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamAlarm
                        callPolicy(context, StreamAlarmPolicy.class, StreamAlarmPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamNotification
                        callPolicy(context, StreamNotificationPolicy.class, StreamNotificationPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamAccessibility
                        callPolicy(context, StreamAccessibilityPolicy.class, StreamAccessibilityPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamVoiceCall
                        callPolicy(context, StreamVoiceCallPolicy.class, StreamVoiceCallPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStreamDTMF
                        callPolicy(context, StreamVoiceCallPolicy.class, StreamVoiceCallPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableScreenCapture
                        // ROOT REQUIRED
                        callPolicy(context, ScreenCapturePolicy.class, ScreenCapturePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableAirplaneMode
                        // ROOT REQUIRED
                        callPolicy(context, AirplaneModePolicy.class, AirplaneModePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableGPS
                        // ROOT REQUIRED
                        callPolicy(context, GPSPolicy.class, GPSPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableMobileLine
                        // ROOT
                        callPolicy(context, MobileLinePolicy.class, MobileLinePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableNfc
                        // ROOT
                        callPolicy(context, NFCPolicy.class, NFCPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableStatusBar
                        // ROOT
                        callPolicy(context, StatusBarPolicy.class, StatusBarPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableUsbMtp
                        // ROOT
                        callPolicy(context, UsbMtpPolicy.class, UsbMtpPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableUsbPtp
                        // ROOT
                        callPolicy(context, UsbPtpPolicy.class, UsbPtpPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/disableUsbAdb
                        // ROOT
                        callPolicy(context, UsbAdbPolicy.class, UsbAdbPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/deployApp
                        // ROOT
                        callPolicy(context, DeployAppPolicy.class, DeployAppPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/removeApp
                        // ROOT
                        callPolicy(context, RemoveAppPolicy.class, RemoveAppPolicy.POLICY_NAME, priority, topic, message);
                        // Policy/deployFile
                        // ROOT
                        callPolicy(context, DeployFilePolicy.class, DeployFilePolicy.POLICY_NAME, priority, topic, message);
                        // Policy/removeFile
                        // ROOT
                        callPolicy(context, RemoveFilePolicy.class, RemoveFilePolicy.POLICY_NAME, priority, topic, message);
                    }
                    break;
                default:
            }
        }
    });
    return true;
}
Also used : Context(android.content.Context) MqttData(org.flyve.mdm.agent.data.database.MqttData) MDMAgent(org.flyve.mdm.agent.ui.MDMAgent) FlyveAdminReceiver(org.flyve.mdm.agent.receivers.FlyveAdminReceiver) ApplicationData(org.flyve.mdm.agent.data.database.ApplicationData) Routes(org.flyve.mdm.agent.core.Routes) PoliciesData(org.flyve.mdm.agent.data.database.PoliciesData) InventoryTask(org.flyve.inventory.InventoryTask) JSONObject(org.json.JSONObject) FastLocationProvider(org.flyve.mdm.agent.utils.FastLocationProvider) AndroidPolicies(org.flyve.policies.manager.AndroidPolicies) FileData(org.flyve.mdm.agent.data.database.FileData) Inventory(org.flyve.mdm.agent.utils.Inventory) Location(android.location.Location)

Aggregations

Context (android.content.Context)1 Location (android.location.Location)1 InventoryTask (org.flyve.inventory.InventoryTask)1 Routes (org.flyve.mdm.agent.core.Routes)1 ApplicationData (org.flyve.mdm.agent.data.database.ApplicationData)1 FileData (org.flyve.mdm.agent.data.database.FileData)1 MqttData (org.flyve.mdm.agent.data.database.MqttData)1 PoliciesData (org.flyve.mdm.agent.data.database.PoliciesData)1 FlyveAdminReceiver (org.flyve.mdm.agent.receivers.FlyveAdminReceiver)1 MDMAgent (org.flyve.mdm.agent.ui.MDMAgent)1 FastLocationProvider (org.flyve.mdm.agent.utils.FastLocationProvider)1 Inventory (org.flyve.mdm.agent.utils.Inventory)1 AndroidPolicies (org.flyve.policies.manager.AndroidPolicies)1 JSONObject (org.json.JSONObject)1