Cleanups to Device Admin sample code

1.  Rename SampleDeviceAdmin to DeviceAdminSample where it appears in
    filenames.  This makes it easier to find when scanning directories.
2.  Fix bug in max password length handling
3.  Add a toast to warn user that entering values in "Password Attempts"
    box has real consequences.
This commit is contained in:
Andrew Stadler
2010-02-08 21:00:05 -08:00
parent 9ae946ea13
commit 1f91425318
4 changed files with 26 additions and 22 deletions

View File

@@ -545,7 +545,7 @@
<!-- Device Admin Samples --> <!-- Device Admin Samples -->
<activity android:name=".app.SampleDeviceAdmin$Controller" <activity android:name=".app.DeviceAdminSample$Controller"
android:label="@string/activity_sample_device_admin"> android:label="@string/activity_sample_device_admin">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -554,12 +554,12 @@
</activity> </activity>
<!-- BEGIN_INCLUDE(device_admin_declaration) --> <!-- BEGIN_INCLUDE(device_admin_declaration) -->
<receiver android:name=".app.SampleDeviceAdmin" <receiver android:name=".app.DeviceAdminSample"
android:label="@string/sample_device_admin" android:label="@string/sample_device_admin"
android:description="@string/sample_device_admin_description" android:description="@string/sample_device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN"> android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" <meta-data android:name="android.app.device_admin"
android:resource="@xml/sample_device_admin" /> android:resource="@xml/device_admin_sample" />
<intent-filter> <intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter> </intent-filter>

View File

@@ -29,7 +29,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Debug;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
@@ -47,7 +46,7 @@ import android.widget.AdapterView.OnItemSelectedListener;
* Example of a do-nothing admin class. When enabled, it lets you control * Example of a do-nothing admin class. When enabled, it lets you control
* some of its policy and reports when there is interesting activity. * some of its policy and reports when there is interesting activity.
*/ */
public class SampleDeviceAdmin extends DeviceAdmin { public class DeviceAdminSample extends DeviceAdmin {
static SharedPreferences getSamplePreferences(Context context) { static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(DeviceAdmin.class.getName(), 0); return context.getSharedPreferences(DeviceAdmin.class.getName(), 0);
@@ -104,7 +103,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
DevicePolicyManager mDPM; DevicePolicyManager mDPM;
ActivityManager mAM; ActivityManager mAM;
ComponentName mSampleDeviceAdmin; ComponentName mDeviceAdminSample;
Button mEnableButton; Button mEnableButton;
Button mDisableButton; Button mDisableButton;
@@ -135,9 +134,9 @@ public class SampleDeviceAdmin extends DeviceAdmin {
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
mSampleDeviceAdmin = new ComponentName(Controller.this, SampleDeviceAdmin.class); mDeviceAdminSample = new ComponentName(Controller.this, DeviceAdminSample.class);
setContentView(R.layout.sample_device_admin); setContentView(R.layout.device_admin_sample);
// Watch for button clicks. // Watch for button clicks.
mEnableButton = (Button)findViewById(R.id.enable); mEnableButton = (Button)findViewById(R.id.enable);
@@ -189,7 +188,12 @@ public class SampleDeviceAdmin extends DeviceAdmin {
} }
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
try { try {
setMaxFailedPw(Integer.parseInt(s.toString())); int maxFailCount = Integer.parseInt(s.toString());
if (maxFailCount > 0) {
Toast.makeText(Controller.this, "WARNING: Phone will wipe after " +
s + " incorrect passwords", Toast.LENGTH_SHORT).show();
}
setMaxFailedPw(maxFailCount);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
} }
} }
@@ -202,7 +206,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
} }
void updateButtonStates() { void updateButtonStates() {
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) { if (active) {
mEnableButton.setEnabled(false); mEnableButton.setEnabled(false);
mDisableButton.setEnabled(true); mDisableButton.setEnabled(true);
@@ -247,13 +251,13 @@ public class SampleDeviceAdmin extends DeviceAdmin {
final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY, final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY,
DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0); final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
final int maxFailedPw = prefs.getInt(PREF_PASSWORD_LENGTH, 0); final int maxFailedPw = prefs.getInt(PREF_MAX_FAILED_PW, 0);
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) { if (active) {
mDPM.setPasswordQuality(mSampleDeviceAdmin, pwQuality); mDPM.setPasswordQuality(mDeviceAdminSample, pwQuality);
mDPM.setPasswordMinimumLength(mSampleDeviceAdmin, pwLength); mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength);
mDPM.setMaximumFailedPasswordsForWipe(mSampleDeviceAdmin, maxFailedPw); mDPM.setMaximumFailedPasswordsForWipe(mDeviceAdminSample, maxFailedPw);
} }
} }
@@ -286,9 +290,9 @@ public class SampleDeviceAdmin extends DeviceAdmin {
switch (requestCode) { switch (requestCode) {
case RESULT_ENABLE: case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
Log.i("SampleDeviceAdmin", "Admin enabled!"); Log.i("DeviceAdminSample", "Admin enabled!");
} else { } else {
Log.i("SampleDeviceAdmin", "Admin enable FAILED!"); Log.i("DeviceAdminSample", "Admin enable FAILED!");
} }
return; return;
} }
@@ -301,7 +305,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
// Launch the activity to have the user enable our admin. // Launch the activity to have the user enable our admin.
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mSampleDeviceAdmin); mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added."); "Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE); startActivityForResult(intent, RESULT_ENABLE);
@@ -310,7 +314,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
private OnClickListener mDisableListener = new OnClickListener() { private OnClickListener mDisableListener = new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
mDPM.removeActiveAdmin(mSampleDeviceAdmin); mDPM.removeActiveAdmin(mDeviceAdminSample);
updateButtonStates(); updateButtonStates();
} }
}; };
@@ -333,7 +337,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
builder.show(); builder.show();
return; return;
} }
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) { if (active) {
mDPM.resetPassword(mPassword.getText().toString()); mDPM.resetPassword(mPassword.getText().toString());
} }
@@ -350,7 +354,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
builder.show(); builder.show();
return; return;
} }
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) { if (active) {
mDPM.lockNow(); mDPM.lockNow();
} }
@@ -377,7 +381,7 @@ public class SampleDeviceAdmin extends DeviceAdmin {
+ "Are you really absolutely sure?"); + "Are you really absolutely sure?");
builder.setPositiveButton("BOOM!", new DialogInterface.OnClickListener() { builder.setPositiveButton("BOOM!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) { if (active) {
mDPM.wipeData(0); mDPM.wipeData(0);
} }