Start implementing UI for changing requested policies.

This commit is contained in:
Dianne Hackborn
2010-01-20 13:43:01 -08:00
parent 4fe6d38a68
commit cc4ee91892
5 changed files with 304 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project <!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -14,10 +14,10 @@
limitations under the License. limitations under the License.
--> -->
<!-- Demonstrates starting and stopping a local service. <!-- Demonstrates implementation of a DeviceAdmin. -->
See corresponding Java code com.android.sdk.app.LocalSerice.java. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
@@ -27,16 +27,82 @@
android:paddingBottom="4dip" android:paddingBottom="4dip"
android:text="@string/sample_device_admin_summary"/> android:text="@string/sample_device_admin_summary"/>
<Button android:id="@+id/enable" <LinearLayout android:orientation="horizontal" android:gravity="center"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/enable"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/enable_admin">
<requestFocus />
</Button>
<Button android:id="@+id/disable"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/disable_admin">
</Button>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:gravity="center"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Spinner android:id="@+id/password_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/password_mode">
</Spinner>
<EditText android:id="@+id/password_length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/password_length_hint"
android:inputType="number">
</EditText>
</LinearLayout>
<Button android:id="@+id/set_password"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/enable_admin"> android_layout_gravity="east|center_vertical"
<requestFocus /> android:text="@string/set_password">
</Button> </Button>
<Button android:id="@+id/disable" <LinearLayout android:orientation="horizontal" android:gravity="center"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_height="wrap_content">
android:text="@string/disable_admin">
</Button> <EditText android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/password_hint"
android:freezesText="true">
</EditText>
<Button android:id="@+id/reset_password"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/reset_password">
</Button>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:gravity="center"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/force_lock"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/force_lock">
</Button>
<Button android:id="@+id/wipe_data"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/wipe_data">
</Button>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@@ -85,4 +85,12 @@
<item>charlie</item> <item>charlie</item>
</string-array> </string-array>
<!-- Used in app/Sample Device Admin -->
<string-array name="password_modes">
<item>Unspecified</item>
<item>Something</item>
<item>Numeric</item>
<item>Alphanumeric</item>
</string-array>
</resources> </resources>

View File

@@ -444,6 +444,13 @@
class for administering the user\'s device.</string> class for administering the user\'s device.</string>
<string name="enable_admin">Enable Admin</string> <string name="enable_admin">Enable Admin</string>
<string name="disable_admin">Disable Admin</string> <string name="disable_admin">Disable Admin</string>
<string name="password_mode">Password Mode</string>
<string name="password_length_hint">Minimum Length</string>
<string name="set_password">Set Password</string>
<string name="password_hint">Password</string>
<string name="reset_password">Reset Password</string>
<string name="force_lock">Force Lock</string>
<string name="wipe_data">Wipe Data</string>
<!-- ============================== --> <!-- ============================== -->
<!-- app/voice recognition examples strings --> <!-- app/voice recognition examples strings -->

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2007 The Android Open Source Project * Copyright (C) 2010 The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,17 +19,27 @@ package com.example.android.apis.app;
import com.example.android.apis.R; import com.example.android.apis.R;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.DeviceAdmin; import android.app.DeviceAdmin;
import android.app.DevicePolicyManager; import android.app.DevicePolicyManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast; import android.widget.Toast;
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
@@ -37,6 +47,13 @@ import android.widget.Toast;
*/ */
public class SampleDeviceAdmin extends DeviceAdmin { public class SampleDeviceAdmin extends DeviceAdmin {
static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(DeviceAdmin.class.getName(), 0);
}
static String PREF_PASSWORD_MODE = "password_mode";
static String PREF_PASSWORD_LENGTH = "password_length";
void showToast(Context context, CharSequence msg) { void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
} }
@@ -61,11 +78,15 @@ public class SampleDeviceAdmin extends DeviceAdmin {
showToast(context, "Sample Device Admin: pw failed"); showToast(context, "Sample Device Admin: pw failed");
} }
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw succeeded");
}
/** /**
* <p>Example of explicitly starting and stopping the local service. * <p>UI control for the sample device admin. This provides an interface
* This demonstrates the implementation of a service that runs in the same * to enable, disable, and perform other operations with it to see
* process as the rest of the application, which is explicitly started and stopped * their effect.</p>
* as desired.</p>
* *
* <p>Note that this is implemented as an inner class only keep the sample * <p>Note that this is implemented as an inner class only keep the sample
* all together; typically this code would appear in some separate class. * all together; typically this code would appear in some separate class.
@@ -79,6 +100,24 @@ public class SampleDeviceAdmin extends DeviceAdmin {
Button mEnableButton; Button mEnableButton;
Button mDisableButton; Button mDisableButton;
// Password mode spinner choices
// This list must match the list found in samples/ApiDemos/res/values/arrays.xml
final static int mPasswordModeValues[] = new int[] {
DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED,
DevicePolicyManager.PASSWORD_MODE_SOMETHING,
DevicePolicyManager.PASSWORD_MODE_NUMERIC,
DevicePolicyManager.PASSWORD_MODE_ALPHANUMERIC
};
Spinner mPasswordMode;
EditText mPasswordLength;
Button mSetPasswordButton;
EditText mPassword;
Button mResetPasswordButton;
Button mForceLockButton;
Button mWipeDataButton;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -93,6 +132,47 @@ public class SampleDeviceAdmin extends DeviceAdmin {
mEnableButton.setOnClickListener(mEnableListener); mEnableButton.setOnClickListener(mEnableListener);
mDisableButton = (Button)findViewById(R.id.disable); mDisableButton = (Button)findViewById(R.id.disable);
mDisableButton.setOnClickListener(mDisableListener); mDisableButton.setOnClickListener(mDisableListener);
mPasswordMode = (Spinner)findViewById(R.id.password_mode);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.password_modes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mPasswordMode.setAdapter(adapter);
mPasswordMode.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
setPasswordMode(mPasswordModeValues[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
setPasswordMode(DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED);
}
});
mPasswordLength = (EditText)findViewById(R.id.password_length);
mPasswordLength.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
setPasswordLength(Integer.parseInt(s.toString()));
} catch (NumberFormatException e) {
}
}
});
mSetPasswordButton = (Button)findViewById(R.id.set_password);
mSetPasswordButton.setOnClickListener(mSetPasswordListener);
mPassword = (EditText)findViewById(R.id.password);
mResetPasswordButton = (Button)findViewById(R.id.reset_password);
mResetPasswordButton.setOnClickListener(mResetPasswordListener);
mForceLockButton = (Button)findViewById(R.id.force_lock);
mForceLockButton.setOnClickListener(mForceLockListener);
mWipeDataButton = (Button)findViewById(R.id.wipe_data);
mWipeDataButton.setOnClickListener(mWipeDataListener);
} }
void updateButtonStates() { void updateButtonStates() {
@@ -100,12 +180,64 @@ public class SampleDeviceAdmin extends DeviceAdmin {
if (active) { if (active) {
mEnableButton.setEnabled(false); mEnableButton.setEnabled(false);
mDisableButton.setEnabled(true); mDisableButton.setEnabled(true);
mPasswordMode.setEnabled(true);
mPasswordLength.setEnabled(true);
mSetPasswordButton.setEnabled(true);
mPassword.setEnabled(true);
mResetPasswordButton.setEnabled(true);
mForceLockButton.setEnabled(true);
mWipeDataButton.setEnabled(true);
} else { } else {
mEnableButton.setEnabled(true); mEnableButton.setEnabled(true);
mDisableButton.setEnabled(false); mDisableButton.setEnabled(false);
mPasswordMode.setEnabled(false);
mPasswordLength.setEnabled(false);
mSetPasswordButton.setEnabled(false);
mPassword.setEnabled(false);
mResetPasswordButton.setEnabled(false);
mForceLockButton.setEnabled(false);
mWipeDataButton.setEnabled(false);
} }
} }
void updateControls() {
SharedPreferences prefs = getSamplePreferences(this);
final int pwMode = prefs.getInt(PREF_PASSWORD_MODE,
DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED);
final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
for (int i=0; i<mPasswordModeValues.length; i++) {
if (mPasswordModeValues[i] == pwMode) {
mPasswordMode.setSelection(i);
}
}
mPasswordLength.setText(Integer.toString(pwLength));
}
void updatePolicies() {
SharedPreferences prefs = getSamplePreferences(this);
final int pwMode = prefs.getInt(PREF_PASSWORD_MODE,
DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED);
final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin);
if (active) {
mDPM.setPasswordMode(mSampleDeviceAdmin, pwMode);
mDPM.setMinimumPasswordLength(mSampleDeviceAdmin, pwLength);
}
}
void setPasswordMode(int mode) {
SharedPreferences prefs = getSamplePreferences(this);
prefs.edit().putInt(PREF_PASSWORD_MODE, mode).commit();
updatePolicies();
}
void setPasswordLength(int length) {
SharedPreferences prefs = getSamplePreferences(this);
prefs.edit().putInt(PREF_PASSWORD_LENGTH, length).commit();
updatePolicies();
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@@ -143,7 +275,48 @@ public class SampleDeviceAdmin extends DeviceAdmin {
updateButtonStates(); updateButtonStates();
} }
}; };
private OnClickListener mSetPasswordListener = new OnClickListener() {
public void onClick(View v) {
// Launch the activity to have the user set a new password.
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
}
};
private OnClickListener mResetPasswordListener = new OnClickListener() {
public void onClick(View v) {
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin);
if (active) {
mDPM.resetPassword(mPassword.getText().toString());
}
}
};
private OnClickListener mForceLockListener = new OnClickListener() {
public void onClick(View v) {
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin);
if (active) {
mDPM.lockNow();
}
}
};
private OnClickListener mWipeDataListener = new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this);
builder.setMessage("This will erase all of your data. Are you sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean active = mDPM.isAdminActive(mSampleDeviceAdmin);
if (active) {
mDPM.wipeData(0);
}
}
});
builder.setNegativeButton("No way!", null);
builder.show();
}
};
} }
} }

View File

@@ -22,12 +22,20 @@ import com.example.android.apis.R;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Spinner1 extends Activity { public class Spinner1 extends Activity {
void showToast(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -38,11 +46,33 @@ public class Spinner1 extends Activity {
this, R.array.colors, android.R.layout.simple_spinner_item); this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter); s1.setAdapter(adapter);
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Spinner1: position=" + position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Spinner1: unselected");
}
});
Spinner s2 = (Spinner) findViewById(R.id.spinner2); Spinner s2 = (Spinner) findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(this, R.array.planets, adapter = ArrayAdapter.createFromResource(this, R.array.planets,
android.R.layout.simple_spinner_item); android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter); s2.setAdapter(adapter);
s2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Spinner2: position=" + position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Spinner2: unselected");
}
});
} }
} }