Example usage for android.app.admin DevicePolicyManager WIPE_EXTERNAL_STORAGE

List of usage examples for android.app.admin DevicePolicyManager WIPE_EXTERNAL_STORAGE

Introduction

In this page you can find the example usage for android.app.admin DevicePolicyManager WIPE_EXTERNAL_STORAGE.

Prototype

int WIPE_EXTERNAL_STORAGE

To view the source code for android.app.admin DevicePolicyManager WIPE_EXTERNAL_STORAGE.

Click Source Link

Document

Flag for #wipeData(int) : also erase the device's external storage (such as SD cards).

Usage

From source file:org.wso2.emm.agent.services.operation.OperationManagerCOSU.java

@Override
public void wipeDevice(Operation operation) throws AndroidAgentException {
    String inputPin = null;// www  .  ja v a 2s . com
    String savedPin = Preference.getString(getContext(),
            getContextResources().getString(R.string.shared_pref_pin));
    JSONObject result = new JSONObject();
    String ownershipType = Preference.getString(getContext(), Constants.DEVICE_TYPE);
    if (Constants.DEFAULT_OWNERSHIP != null) {
        ownershipType = Constants.OWNERSHIP_COSU;
    }
    try {
        JSONObject wipeKey;
        String status;
        if (operation.getPayLoad() != null) {
            wipeKey = new JSONObject(operation.getPayLoad().toString());
            if (!wipeKey.isNull(getContextResources().getString(R.string.shared_pref_pin))) {
                inputPin = (String) wipeKey.get(getContextResources().getString(R.string.shared_pref_pin));
            }
        }

        status = "true";
        if (Constants.OWNERSHIP_COSU.equals(ownershipType.trim())) {
            result.put(getContextResources().getString(R.string.operation_status), status);
            operation.setPayLoad(result.toString());
            operation.setStatus(getContextResources().getString(R.string.operation_value_completed));
            getResultBuilder().build(operation);
            getDevicePolicyManager().wipeData(
                    DevicePolicyManager.WIPE_EXTERNAL_STORAGE | DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
            if (Constants.DEBUG_MODE_ENABLED) {
                Log.d(TAG, "Started to wipe data");
            }
        }
    } catch (JSONException e) {
        operation.setStatus(getContextResources().getString(R.string.operation_value_error));
        operation.setOperationResponse("Error in parsing WIPE payload.");
        getResultBuilder().build(operation);
        throw new AndroidAgentException("Invalid JSON format.", e);
    }
}

From source file:org.wso2.iot.agent.services.operation.OperationManagerCOSU.java

@Override
public void wipeDevice(Operation operation) throws AndroidAgentException {
    String inputPin = null;/*from   w ww .  ja v a2 s . c  o m*/
    String savedPin = Preference.getString(getContext(),
            getContextResources().getString(R.string.shared_pref_pin));
    JSONObject result = new JSONObject();
    String ownershipType = Preference.getString(getContext(), Constants.DEVICE_TYPE);
    if (Constants.DEFAULT_OWNERSHIP != null) {
        ownershipType = Constants.OWNERSHIP_COSU;
    }
    try {
        JSONObject wipeKey;
        String status;
        if (operation.getPayLoad() != null) {
            wipeKey = new JSONObject(operation.getPayLoad().toString());
            if (!wipeKey.isNull(getContextResources().getString(R.string.shared_pref_pin))) {
                inputPin = (String) wipeKey.get(getContextResources().getString(R.string.shared_pref_pin));
            }
        }

        status = "true";
        if (Constants.OWNERSHIP_COSU.equals(ownershipType.trim())) {
            result.put(getContextResources().getString(R.string.operation_status), status);
            operation.setPayLoad(result.toString());
            operation.setStatus(getContextResources().getString(R.string.operation_value_completed));
            getResultBuilder().build(operation);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                getDevicePolicyManager().wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE
                        | DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
            }
            if (Constants.DEBUG_MODE_ENABLED) {
                Log.d(TAG, "Started to wipe data");
            }
        }
    } catch (JSONException e) {
        operation.setStatus(getContextResources().getString(R.string.operation_value_error));
        operation.setOperationResponse("Error in parsing WIPE payload.");
        getResultBuilder().build(operation);
        throw new AndroidAgentException("Invalid JSON format.", e);
    }
}

From source file:org.wso2.emm.agent.AuthenticationActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_authentication);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(R.layout.custom_sherlock_bar);
    getSupportActionBar().setTitle(Constants.EMPTY_STRING);

    context = this;
    deviceInfo = new DeviceInfo(context);
    etDomain = (EditText) findViewById(R.id.etDomain);
    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    radioBYOD = (RadioButton) findViewById(R.id.radioBYOD);
    loginLayout = (LinearLayout) findViewById(R.id.errorLayout);
    etDomain.setFocusable(true);//from   w  w w  .  j  a v  a  2s  .  c  om
    etDomain.requestFocus();
    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnRegister.setOnClickListener(onClickAuthenticate);
    btnRegister.setEnabled(false);

    // change button color background till user enters a valid input
    btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_grey));
    btnRegister.setTextColor(getResources().getColor(R.color.black));

    if (Constants.HIDE_LOGIN_UI) {
        loginLayout.setVisibility(View.GONE);
    }

    if (Constants.DEFAULT_OWNERSHIP == Constants.OWNERSHIP_COSU) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            startLockTask();
        }
    }

    textViewWipeData = (TextView) this.findViewById(R.id.textViewWipeData);
    if (Constants.DEFAULT_OWNERSHIP == Constants.OWNERSHIP_COSU && Constants.DISPLAY_WIPE_DEVICE_BUTTON) {
        textViewWipeData.setVisibility(View.VISIBLE);
        textViewWipeData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(AuthenticationActivity.this).setTitle(getString(R.string.app_name))
                        .setMessage(R.string.wipe_confirmation)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext()
                                        .getSystemService(Context.DEVICE_POLICY_SERVICE);
                                devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE
                                        | DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
                            }
                        }).setNegativeButton(android.R.string.no, null).show();
            }
        });
    }

    etUsername.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            enableSubmitIfReady();
        }

        @Override
        public void afterTextChanged(Editable s) {
            enableSubmitIfReady();
        }
    });

    etPassword.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            enableSubmitIfReady();
        }

        @Override
        public void afterTextChanged(Editable s) {
            enableSubmitIfReady();
        }
    });

    if (org.wso2.emm.agent.proxy.utils.Constants.Authenticator.AUTHENTICATOR_IN_USE
            .equals(org.wso2.emm.agent.proxy.utils.Constants.Authenticator.MUTUAL_SSL_AUTHENTICATOR)) {

        AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory();
        authenticator = authenticatorFactory.getClient(
                org.wso2.emm.agent.proxy.utils.Constants.Authenticator.AUTHENTICATOR_IN_USE,
                AuthenticationActivity.this, Constants.AUTHENTICATION_REQUEST_CODE);
        authenticator.doAuthenticate();
    }

    //This is an override to ownership type.
    if (Constants.DEFAULT_OWNERSHIP != null) {
        deviceType = Constants.DEFAULT_OWNERSHIP;
        Preference.putString(context, Constants.DEVICE_TYPE, deviceType);
    }

    // This is added so that in case due to an agent customisation, if the authentication
    // activity is called the AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED is set, the activity
    // must be finished.
    if (Constants.AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED == true) {
        finish();
    }

}

From source file:org.wso2.iot.agent.activities.AuthenticationActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {/*  w  w  w .  java2 s.c o  m*/
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    setContentView(R.layout.activity_authentication);
    RelativeLayout relativeLayout = (RelativeLayout) this.findViewById(R.id.relavtiveLayoutAuthentication);

    if (Constants.DEFAULT_OWNERSHIP.equals(Constants.OWNERSHIP_COSU)) {
        relativeLayout.setVisibility(RelativeLayout.GONE);
    }
    deviceInfo = new DeviceInfo(context);
    etDomain = (EditText) findViewById(R.id.etDomain);
    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    etDomain.setFocusable(true);
    etDomain.requestFocus();
    btnSignIn = (Button) findViewById(R.id.btnSignIn);
    btnSignIn.setOnClickListener(onClickAuthenticate);
    btnSignIn.setEnabled(false);

    // change button color background till user enters a valid input
    btnSignIn.setBackgroundResource(R.drawable.btn_grey);
    btnSignIn.setTextColor(ContextCompat.getColor(this, R.color.black));
    TextView textViewSignIn = (TextView) findViewById(R.id.textViewSignIn);
    LinearLayout loginLayout = (LinearLayout) findViewById(R.id.loginLayout);

    if (Preference.hasPreferenceKey(context, Constants.TOKEN_EXPIRED)) {
        etDomain.setEnabled(false);
        etDomain.setTextColor(ContextCompat.getColor(this, R.color.black));
        etUsername.setEnabled(false);
        etUsername.setTextColor(ContextCompat.getColor(this, R.color.black));
        btnSignIn.setText(R.string.btn_sign_in);
        etPassword.setFocusable(true);
        etPassword.requestFocus();
        String tenantedUserName = Preference.getString(context, Constants.USERNAME);
        int tenantSeparator = tenantedUserName.lastIndexOf('@');
        etUsername.setText(tenantedUserName.substring(0, tenantSeparator));
        etDomain.setText(tenantedUserName.substring(tenantSeparator + 1, tenantedUserName.length()));
        isReLogin = true;
        textViewSignIn.setText(R.string.msg_need_to_sign_in);
    } else if (Constants.CLOUD_MANAGER != null) {
        isCloudLogin = true;
        etDomain.setVisibility(View.GONE);
        textViewSignIn.setText(R.string.txt_sign_in_cloud);
    }

    if (Preference.getBoolean(context, Constants.PreferenceFlag.DEVICE_ACTIVE) && !isReLogin) {
        Intent intent = new Intent(AuthenticationActivity.this, AlreadyRegisteredActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return;
    }

    TextView textViewSignUp = (TextView) findViewById(R.id.textViewSignUp);
    if (!isReLogin && Constants.SIGN_UP_URL != null) {
        Linkify.TransformFilter transformFilter = new Linkify.TransformFilter() {
            @Override
            public String transformUrl(Matcher match, String url) {
                return Constants.SIGN_UP_URL;
            }
        };
        Pattern pattern = Pattern.compile(getResources().getString(R.string.txt_sign_up_linkify));
        Linkify.addLinks(textViewSignUp, pattern, null, null, transformFilter);
    } else {
        textViewSignUp.setVisibility(View.GONE);
    }

    if (Constants.HIDE_LOGIN_UI) {
        loginLayout.setVisibility(View.GONE);
    }

    if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            startLockTask();
        }
    }

    TextView textViewWipeData = (TextView) this.findViewById(R.id.textViewWipeData);
    if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP) && Constants.DISPLAY_WIPE_DEVICE_BUTTON) {
        textViewWipeData.setVisibility(View.VISIBLE);
        textViewWipeData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(AuthenticationActivity.this).setTitle(getString(R.string.app_name))
                        .setMessage(R.string.wipe_confirmation)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext()
                                        .getSystemService(Context.DEVICE_POLICY_SERVICE);
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                                    devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE
                                            | DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
                                } else {
                                    devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
                                }
                            }
                        }).setNegativeButton(android.R.string.no, null).show();
            }
        });
    }

    ImageView logo = (ImageView) findViewById(R.id.imageViewLogo);
    if (Constants.COSU_SECRET_EXIT) {
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                kioskExit++;
                if (kioskExit == 6) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        stopLockTask();
                    }
                    finish();
                }
            }
        });
    }

    etUsername.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            enableSubmitIfReady();
        }

        @Override
        public void afterTextChanged(Editable s) {
            enableSubmitIfReady();
        }
    });

    etPassword.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            enableSubmitIfReady();
        }

        @Override
        public void afterTextChanged(Editable s) {
            enableSubmitIfReady();
        }
    });

    if (org.wso2.iot.agent.proxy.utils.Constants.Authenticator.AUTHENTICATOR_IN_USE
            .equals(org.wso2.iot.agent.proxy.utils.Constants.Authenticator.MUTUAL_SSL_AUTHENTICATOR)) {

        AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory();
        ClientAuthenticator authenticator = authenticatorFactory.getClient(
                org.wso2.iot.agent.proxy.utils.Constants.Authenticator.AUTHENTICATOR_IN_USE,
                AuthenticationActivity.this, Constants.AUTHENTICATION_REQUEST_CODE);
        authenticator.doAuthenticate();
    }

    //This is an override to ownership type.
    if (Constants.DEFAULT_OWNERSHIP != null) {
        deviceType = Constants.DEFAULT_OWNERSHIP;
        Preference.putString(context, Constants.DEVICE_TYPE, deviceType);
    } else {
        deviceType = Constants.OWNERSHIP_BYOD;
    }

    if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
        Intent intent = getIntent();
        if (intent.hasExtra("android.app.extra.token")) {
            adminAccessToken = intent.getStringExtra("android.app.extra.token");
            proceedToAuthentication();
        }
    }

    // This is added so that in case due to an agent customisation, if the authentication
    // activity is called the AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED is set, the activity
    // must be finished.
    if (Constants.AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED) {
        finish();
    }
}

From source file:com.afwsamples.testdpc.policy.PolicyManagementFragment.java

/**
 * Shows a prompt to ask for confirmation on wiping the data and also provide an option
 * to set if external storage and factory reset protection data also needs to wiped.
 *//*from  w  ww .  j  a  v a  2  s .c o m*/
private void showWipeDataPrompt() {
    final LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.wipe_data_dialog_prompt, null);
    final CheckBox externalStorageCheckBox = (CheckBox) dialogView.findViewById(R.id.external_storage_checkbox);
    final CheckBox resetProtectionCheckBox = (CheckBox) dialogView.findViewById(R.id.reset_protection_checkbox);

    new AlertDialog.Builder(getActivity()).setTitle(R.string.wipe_data_title).setView(dialogView)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    int flags = 0;
                    flags |= (externalStorageCheckBox.isChecked() ? DevicePolicyManager.WIPE_EXTERNAL_STORAGE
                            : 0);
                    flags |= (resetProtectionCheckBox.isChecked()
                            ? DevicePolicyManager.WIPE_RESET_PROTECTION_DATA
                            : 0);
                    mDevicePolicyManager.wipeData(flags);
                }
            }).setNegativeButton(android.R.string.cancel, null).show();
}