Initial sample code for writing a device policy.

This commit is contained in:
Dianne Hackborn
2010-01-12 18:17:18 -08:00
parent 1f93b7ef17
commit c86ba3199d
5 changed files with 252 additions and 0 deletions

View File

@@ -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 -->

View File

@@ -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>

View File

@@ -433,6 +433,18 @@
<string name="msg_launcher_shortcuts">This activity creates shortcuts for the launcher (home screen), and receives intents from those shortcuts. To try it, return to the launcher and long-press to create a shortcut.</string>
<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 -->
<!-- ============================== -->

View File

@@ -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)
-->

View File

@@ -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();
}
};
}
}