Code example for DevicePolicyManager

Methods: isAdminActive, removeActiveAdmin

1
            mWipeAllDataButton.setOnClickListener(mWipeDataListener);
 
            mTimeout = (EditText) findViewById(R.id.timeout);
            mTimeoutButton = (Button) findViewById(R.id.set_timeout);
            mTimeoutButton.setOnClickListener(mSetTimeoutListener);
        } 
 
        void updateButtonStates() { 
            boolean active = mDPM.isAdminActive(mDeviceAdminSample);
            if (active) {
                mEnableButton.setEnabled(false);
                mDisableButton.setEnabled(true);
                mPasswordQuality.setEnabled(true);
                mPasswordLength.setEnabled(true);
                mSetPasswordButton.setEnabled(true);
                mPassword.setEnabled(true);
                mResetPasswordButton.setEnabled(true);
                mForceLockButton.setEnabled(true);
                mWipeDataButton.setEnabled(true);
                mWipeAllDataButton.setEnabled(true);
            } else { 
                mEnableButton.setEnabled(true);
                mDisableButton.setEnabled(false);
                mPasswordQuality.setEnabled(false);
                mPasswordLength.setEnabled(false);
                mSetPasswordButton.setEnabled(false);
                mPassword.setEnabled(false);
                mResetPasswordButton.setEnabled(false);
                mForceLockButton.setEnabled(false);
                mWipeDataButton.setEnabled(false);
                mWipeAllDataButton.setEnabled(false);
            } 
        } 
 
        void updateControls() { 
            SharedPreferences prefs = getSamplePreferences(this);
            final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY,
                    DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
            final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
            final int maxFailedPw = prefs.getInt(PREF_MAX_FAILED_PW, 0);
 
            for (int i=0; i<mPasswordQualityValues.length; i++) {
                if (mPasswordQualityValues[i] == pwQuality) {
                    mPasswordQuality.setSelection(i);
                } 
            } 
            mPasswordLength.setText(Integer.toString(pwLength));
            mMaxFailedPw.setText(Integer.toString(maxFailedPw));
        } 
 
        void updatePolicies() { 
            SharedPreferences prefs = getSamplePreferences(this);
            final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY,
                    DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
            final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
            final int maxFailedPw = prefs.getInt(PREF_MAX_FAILED_PW, 0);
 
            boolean active = mDPM.isAdminActive(mDeviceAdminSample);
            if (active) {
                mDPM.setPasswordQuality(mDeviceAdminSample, pwQuality);
                mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength);
                mDPM.setMaximumFailedPasswordsForWipe(mDeviceAdminSample, maxFailedPw);
            } 
        } 
 
        void setPasswordQuality(int quality) {
            SharedPreferences prefs = getSamplePreferences(this);
            prefs.edit().putInt(PREF_PASSWORD_QUALITY, quality).commit();
            updatePolicies(); 
        } 
 
        void setPasswordLength(int length) {
            SharedPreferences prefs = getSamplePreferences(this);
            prefs.edit().putInt(PREF_PASSWORD_LENGTH, length).commit();
            updatePolicies(); 
        } 
 
        void setMaxFailedPw(int length) {
            SharedPreferences prefs = getSamplePreferences(this);
            prefs.edit().putInt(PREF_MAX_FAILED_PW, length).commit();
            updatePolicies(); 
        } 
 
        @Override 
        protected void onResume() { 
            super.onResume(); 
            updateButtonStates(); 
        } 
 
        @Override 
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case RESULT_ENABLE:
                    if (resultCode == Activity.RESULT_OK) {
                        Log.i("DeviceAdminSample", "Admin enabled!");
                    } else { 
                        Log.i("DeviceAdminSample", "Admin enable FAILED!");
                    } 
                    return; 
            } 
 
            super.onActivityResult(requestCode, resultCode, data);
        } 
 
        private OnClickListener mEnableListener = new OnClickListener() {
            public void onClick(View v) { 
                // Launch the activity to have the user enable our admin. 
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, 
                        mDeviceAdminSample); 
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, 
                        "Additional text explaining why this needs to be added."); 
                startActivityForResult(intent, RESULT_ENABLE); 
            } 
        }; 
 
        private OnClickListener mDisableListener = new OnClickListener() {
            public void onClick(View v) { 
                mDPM.removeActiveAdmin(mDeviceAdminSample);