From cc4ee91892be32a358964f2bebbf5bc7924b1657 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Wed, 20 Jan 2010 13:43:01 -0800 Subject: [PATCH] Start implementing UI for changing requested policies. --- .../res/layout/sample_device_admin.xml | 92 +++++++-- samples/ApiDemos/res/values/arrays.xml | 8 + samples/ApiDemos/res/values/strings.xml | 7 + .../android/apis/app/SampleDeviceAdmin.java | 187 +++++++++++++++++- .../example/android/apis/view/Spinner1.java | 30 +++ 5 files changed, 304 insertions(+), 20 deletions(-) diff --git a/samples/ApiDemos/res/layout/sample_device_admin.xml b/samples/ApiDemos/res/layout/sample_device_admin.xml index e6c7e9a8d..f357eaf89 100644 --- a/samples/ApiDemos/res/layout/sample_device_admin.xml +++ b/samples/ApiDemos/res/layout/sample_device_admin.xml @@ -1,5 +1,5 @@ - - + - @@ -27,16 +27,82 @@ android:paddingBottom="4dip" android:text="@string/sample_device_admin_summary"/> - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/values/arrays.xml b/samples/ApiDemos/res/values/arrays.xml index ca3003f2b..a40212d0e 100644 --- a/samples/ApiDemos/res/values/arrays.xml +++ b/samples/ApiDemos/res/values/arrays.xml @@ -85,4 +85,12 @@ charlie + + + Unspecified + Something + Numeric + Alphanumeric + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 63ac249f9..25774d8aa 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -444,6 +444,13 @@ class for administering the user\'s device. Enable Admin Disable Admin + Password Mode + Minimum Length + Set Password + Password + Reset Password + Force Lock + Wipe Data diff --git a/samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java b/samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java index 57dae9c53..819d85bf0 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java @@ -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"); * 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 android.app.Activity; +import android.app.AlertDialog; import android.app.DeviceAdmin; import android.app.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; import android.widget.Toast; +import android.widget.AdapterView.OnItemSelectedListener; /** * 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 { + 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) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } @@ -61,11 +78,15 @@ public class SampleDeviceAdmin extends DeviceAdmin { showToast(context, "Sample Device Admin: pw failed"); } + @Override + public void onPasswordSucceeded(Context context, Intent intent) { + showToast(context, "Sample Device Admin: pw succeeded"); + } + /** - *

Example of explicitly starting and stopping the local service. - * This demonstrates the implementation of a service that runs in the same - * process as the rest of the application, which is explicitly started and stopped - * as desired.

+ *

UI control for the sample device admin. This provides an interface + * to enable, disable, and perform other operations with it to see + * their effect.

* *

Note that this is implemented as an inner class only keep the sample * all together; typically this code would appear in some separate class. @@ -79,6 +100,24 @@ public class SampleDeviceAdmin extends DeviceAdmin { Button mEnableButton; 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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -93,6 +132,47 @@ public class SampleDeviceAdmin extends DeviceAdmin { mEnableButton.setOnClickListener(mEnableListener); mDisableButton = (Button)findViewById(R.id.disable); mDisableButton.setOnClickListener(mDisableListener); + + mPasswordMode = (Spinner)findViewById(R.id.password_mode); + ArrayAdapter 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() { @@ -100,12 +180,64 @@ public class SampleDeviceAdmin extends DeviceAdmin { if (active) { mEnableButton.setEnabled(false); 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 { mEnableButton.setEnabled(true); 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 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); adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 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"); + } + }); } }