diff options
| author | Dianne Hackborn <hackbod@google.com> | 2010-01-12 18:17:18 -0800 |
|---|---|---|
| committer | Dianne Hackborn <hackbod@google.com> | 2010-01-16 18:01:37 -0800 |
| commit | c86ba3199dda4ce270c92feb3c19eabb913de44a (patch) | |
| tree | daaebc29b18dbbd5c9f1b2bab275508b3dc6d5ea /samples | |
| parent | 1f93b7ef17851f0025db1abafeb5d77bc4313ca3 (diff) | |
| download | android_development-c86ba3199dda4ce270c92feb3c19eabb913de44a.tar.gz android_development-c86ba3199dda4ce270c92feb3c19eabb913de44a.tar.bz2 android_development-c86ba3199dda4ce270c92feb3c19eabb913de44a.zip | |
Initial sample code for writing a device policy.
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/ApiDemos/AndroidManifest.xml | 25 | ||||
| -rw-r--r-- | samples/ApiDemos/res/layout/sample_device_admin.xml | 42 | ||||
| -rw-r--r-- | samples/ApiDemos/res/values/strings.xml | 12 | ||||
| -rw-r--r-- | samples/ApiDemos/res/xml/sample_device_admin.xml | 24 | ||||
| -rw-r--r-- | samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java | 149 |
5 files changed, 252 insertions, 0 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index b0cda301f..5e50a40c8 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -543,6 +543,31 @@ </intent-filter> </activity> + <!-- Device Admin Samples --> + + <activity android:name=".app.SampleDeviceAdmin$Controller" + android:label="@string/activity_sample_device_admin"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.SAMPLE_CODE" /> + </intent-filter> + </activity> + +<!-- +// BEGIN_INCLUDE(device_admin_declaration) +--> + <receiver android:name=".app.SampleDeviceAdmin" + android:label="@string/sample_device_admin" + android:permission="android.permission.BIND_DEVICE_ADMIN"> + <meta-data android:name="android.app.device_admin" + android:resource="@xml/sample_device_admin" /> + <intent-filter> + <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> + </intent-filter> + </receiver> +<!-- +// END_INCLUDE(device_admin_declaration) +--> <!-- Voice Recognition Samples --> diff --git a/samples/ApiDemos/res/layout/sample_device_admin.xml b/samples/ApiDemos/res/layout/sample_device_admin.xml new file mode 100644 index 000000000..e6c7e9a8d --- /dev/null +++ b/samples/ApiDemos/res/layout/sample_device_admin.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 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. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<!-- Demonstrates starting and stopping a local service. + 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" + android:gravity="center_horizontal" + android:layout_width="match_parent" android:layout_height="match_parent"> + + <TextView + android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_weight="0" + android:paddingBottom="4dip" + android:text="@string/sample_device_admin_summary"/> + + <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> + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index bdcb7a74f..63ac249f9 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -434,6 +434,18 @@ <string name="label_intent">Intent:</string> <!-- ============================== --> + <!-- app/device policies examples strings --> + <!-- ============================== --> + + <string name="activity_sample_device_admin">App/Device Admin</string> + <string name="sample_device_admin">Sample Device Admin</string> + + <string name="sample_device_admin_summary">Demonstration of a DeviceAdmin + class for administering the user\'s device.</string> + <string name="enable_admin">Enable Admin</string> + <string name="disable_admin">Disable Admin</string> + + <!-- ============================== --> <!-- app/voice recognition examples strings --> <!-- ============================== --> diff --git a/samples/ApiDemos/res/xml/sample_device_admin.xml b/samples/ApiDemos/res/xml/sample_device_admin.xml new file mode 100644 index 000000000..26306ca12 --- /dev/null +++ b/samples/ApiDemos/res/xml/sample_device_admin.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<!-- +// BEGIN_INCLUDE(meta_data) +--> +<device-admin xmlns:android="http://schemas.android.com/apk/res/android" +/> +<!-- +// END_INCLUDE(meta_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 new file mode 100644 index 000000000..57dae9c53 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/app/SampleDeviceAdmin.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.apis.app; + +import com.example.android.apis.R; + +import android.app.Activity; +import android.app.DeviceAdmin; +import android.app.DevicePolicyManager; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.Toast; + +/** + * Example of a do-nothing admin class. When enabled, it lets you control + * some of its policy and reports when there is interesting activity. + */ +public class SampleDeviceAdmin extends DeviceAdmin { + + void showToast(Context context, CharSequence msg) { + Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onEnabled(Context context, Intent intent) { + showToast(context, "Sample Device Admin: enabled"); + } + + @Override + public void onDisabled(Context context, Intent intent) { + showToast(context, "Sample Device Admin: disabled"); + } + + @Override + public void onPasswordChanged(Context context, Intent intent) { + showToast(context, "Sample Device Admin: pw changed"); + } + + @Override + public void onPasswordFailed(Context context, Intent intent) { + showToast(context, "Sample Device Admin: pw failed"); + } + + /** + * <p>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.</p> + * + * <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. + */ + public static class Controller extends Activity { + static final int RESULT_ENABLE = 1; + + DevicePolicyManager mDPM; + ComponentName mSampleDeviceAdmin; + + Button mEnableButton; + Button mDisableButton; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); + mSampleDeviceAdmin = new ComponentName(Controller.this, SampleDeviceAdmin.class); + + setContentView(R.layout.sample_device_admin); + + // Watch for button clicks. + mEnableButton = (Button)findViewById(R.id.enable); + mEnableButton.setOnClickListener(mEnableListener); + mDisableButton = (Button)findViewById(R.id.disable); + mDisableButton.setOnClickListener(mDisableListener); + } + + void updateButtonStates() { + boolean active = mDPM.isAdminActive(mSampleDeviceAdmin); + if (active) { + mEnableButton.setEnabled(false); + mDisableButton.setEnabled(true); + } else { + mEnableButton.setEnabled(true); + mDisableButton.setEnabled(false); + } + } + + @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("SampleDeviceAdmin", "Admin enabled!"); + } else { + Log.i("SampleDeviceAdmin", "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, + mSampleDeviceAdmin); + startActivityForResult(intent, RESULT_ENABLE); + } + }; + + private OnClickListener mDisableListener = new OnClickListener() { + public void onClick(View v) { + mDPM.removeActiveAdmin(mSampleDeviceAdmin); + updateButtonStates(); + } + }; + } + + +} |
