Create Restrictions Receiver
Description
The following code shows how to Create Restrictions Receiver.
Code revised from
Android Recipes:A Problem-Solution Approach
http://www.apress.com/9781430234135
ISBN13: 978-1-4302-3413-5
Example
Register Permission
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidrecipes.restrictedprofiles"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".RestrictionsReceiver">
<intent-filter>
<action android:name="android.intent.action.GET_RESTRICTION_ENTRIES"/>
</intent-filter>
</receiver>
</application>
</manifest>
Main layout xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.androidrecipes.restrictedprofiles.DrawingView
android:id="@+id/drawing_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="$$$$"
android:onClick="onPurchaseClick"/>
<SeekBar
android:id="@+id/full_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:max="45"/>
<RadioGroup
android:id="@+id/simple_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<RadioButton
android:id="@+id/option_small"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#555"
android:text="Small" />
<RadioButton
android:id="@+id/option_medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#555"
android:text="Medium" />
<RadioButton
android:id="@+id/option_large"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#555"
android:text="Big" />
<RadioButton
android:id="@+id/option_xlarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#555"
android:text="Really Big" />
</RadioGroup>
</FrameLayout>
Main Activity Java code
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.UserManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
/*from ww w .j av a 2 s. c o m*/
@SuppressLint("NewApi")
public class MainActivity extends Activity implements
OnSeekBarChangeListener, OnCheckedChangeListener {
private Button mPurchaseButton;
private DrawingView mDrawingView;
private SeekBar mFullSlider;
private RadioGroup mSimpleSelector;
/* Profile Restriction Values */
private boolean mHasPurchases;
private int mMinAge;
/* Content Purchase Flags */
private boolean mHasCanvasColors = false;
private boolean mHasPaintColors = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPurchaseButton = (Button) findViewById(R.id.button_purchase);
mDrawingView = (DrawingView) findViewById(R.id.drawing_surface);
mFullSlider = (SeekBar) findViewById(R.id.full_slider);
mSimpleSelector = (RadioGroup) findViewById(R.id.simple_selector);
mFullSlider.setOnSeekBarChangeListener(this);
mSimpleSelector.setOnCheckedChangeListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
UserManager manager = (UserManager) getSystemService(USER_SERVICE);
//Check for system-level restrictions
Bundle restrictions = manager.getUserRestrictions();
if (restrictions != null && !restrictions.isEmpty()) {
showSystemRestrictionsDialog(restrictions);
}
}
}
@Override
protected void onStart() {
super.onStart();
/*
* Restrictions may change while the app is in the background so we need
* to check this each time we return
*/
updateRestrictions();
// Update UI based on restriction changes
updateDisplay();
}
public void onPurchaseClick(View v) {
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
builder.setTitle("Content Upgrades")
.setMessage(
"Tap any of the following items to add them.")
.setPositiveButton("Canvas Colors $2.99",
mPurchaseListener)
.setNeutralButton("Paint Colors $2.99",
mPurchaseListener)
.setNegativeButton("Both Items $4.99",
mPurchaseListener).show();
}
private DialogInterface.OnClickListener mPurchaseListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mHasCanvasColors = true;
break;
case DialogInterface.BUTTON_NEUTRAL:
mHasPaintColors = true;
break;
case DialogInterface.BUTTON_NEGATIVE:
mHasCanvasColors = true;
mHasPaintColors = true;
break;
}
Toast.makeText(getApplicationContext(), "Thank You For Your Purchase!",
Toast.LENGTH_SHORT).show();
updateDisplay();
}
};
private void showSystemRestrictionsDialog(Bundle restrictions) {
StringBuilder message = new StringBuilder();
for (String key : restrictions.keySet()) {
//Make sure the value of the restriction is true
if (restrictions.getBoolean(key)) {
message.append(RestrictionsReceiver.getNameForRestriction(key));
message.append("\n");
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("System Restrictions")
.setMessage(message.toString())
.setPositiveButton("OK", null)
.show();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
float width;
switch(checkedId) {
default:
case R.id.option_small:
width = 4f;
break;
case R.id.option_medium:
width = 12f;
break;
case R.id.option_large:
width = 25f;
break;
case R.id.option_xlarge:
width = 45f;
break;
}
mDrawingView.setStrokeWidth(width);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mDrawingView.setStrokeWidth(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
private void updateDisplay() {
//Show/hide purchase button
mPurchaseButton.setVisibility(
mHasPurchases ? View.VISIBLE : View.GONE);
//Update age-restricted content
mFullSlider.setVisibility(View.GONE);
mSimpleSelector.setVisibility(View.GONE);
switch (mMinAge) {
case 18:
//Full-range slider
mFullSlider.setVisibility(View.VISIBLE);
mFullSlider.setProgress(4);
break;
case 10:
//Four options
mSimpleSelector.setVisibility(View.VISIBLE);
findViewById(R.id.option_medium).setVisibility(View.VISIBLE);
findViewById(R.id.option_xlarge).setVisibility(View.VISIBLE);
mSimpleSelector.check(R.id.option_medium);
break;
case 5:
//Big/small option
mSimpleSelector.setVisibility(View.VISIBLE);
findViewById(R.id.option_medium).setVisibility(View.GONE);
findViewById(R.id.option_xlarge).setVisibility(View.GONE);
mSimpleSelector.check(R.id.option_small);
break;
case 3:
default:
//No selection
break;
}
//Update display with purchases
mDrawingView.setPaintColor(mHasPaintColors ? Color.BLUE : Color.GRAY);
mDrawingView.setCanvasColor(mHasCanvasColors ? Color.GREEN : Color.TRANSPARENT);
}
private void updateRestrictions() {
// Check for restrictions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
UserManager manager = (UserManager) getSystemService(USER_SERVICE);
Bundle restrictions = manager
.getApplicationRestrictions(getPackageName());
if (restrictions != null) {
// Read restriction settings
mHasPurchases = restrictions.getBoolean(
RestrictionsReceiver.RESTRICTION_PURCHASE, true);
try {
mMinAge = Integer.parseInt(restrictions.getString(
RestrictionsReceiver.RESTRICTION_AGERANGE, "18"));
} catch (NumberFormatException e) {
mMinAge = 0;
}
} else {
// We have no restrictions
mHasPurchases = true;
mMinAge = 18;
}
} else {
// We are not on a system that supports restrictions
mHasPurchases = true;
mMinAge = 18;
}
}
}
DrawingView Java code
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/* w ww . j a va 2 s. c om*/
public class DrawingView extends View {
private Paint mFingerPaint;
private Path mPath;
public DrawingView(Context context) {
super(context);
init();
}
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawingView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//Set up the paint brush
mFingerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mFingerPaint.setStyle(Style.STROKE);
mFingerPaint.setStrokeCap(Cap.ROUND);
mFingerPaint.setStrokeJoin(Join.ROUND);
//Default stroke width
mFingerPaint.setStrokeWidth(8f);
}
public void setPaintColor(int color) {
mFingerPaint.setColor(color);
}
public void setStrokeWidth(float width) {
mFingerPaint.setStrokeWidth(width);
}
public void setCanvasColor(int color) {
setBackgroundColor(color);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mPath = new Path();
//Start at the touch down
mPath.moveTo(event.getX(), event.getY());
//Re-draw
invalidate();
break;
case MotionEvent.ACTION_MOVE:
//Add all touch points between events
for (int i=0; i < event.getHistorySize(); i++) {
mPath.lineTo(event.getHistoricalX(i),
event.getHistoricalY(i) );
}
//Re-draw
invalidate();
break;
default:
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
//Draw the background
super.onDraw(canvas);
//Draw the paint stroke
if (mPath != null) {
canvas.drawPath(mPath, mFingerPaint);
}
}
}
RestrictionsReceiver Java code
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.RestrictionEntry;
import android.os.Bundle;
import android.os.UserManager;
/*from w ww . ja va 2 s .c o m*/
import java.util.ArrayList;
@SuppressLint("NewApi")
public class RestrictionsReceiver extends BroadcastReceiver {
public static final String RESTRICTION_PURCHASE = "purchases";
public static final String RESTRICTION_AGERANGE = "age_range";
private static final String[] AGES = {"3+", "5+", "10+", "18+"};
private static final String[] AGE_VALUES = {"3", "5", "10", "18"};
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<RestrictionEntry> restrictions = new ArrayList<RestrictionEntry>();
RestrictionEntry purchase = new RestrictionEntry(RESTRICTION_PURCHASE, false);
purchase.setTitle("Content Purchases");
purchase.setDescription("Allow purchasing of content in the application.");
restrictions.add(purchase);
RestrictionEntry ages = new RestrictionEntry(RESTRICTION_AGERANGE, AGE_VALUES[0]);
ages.setTitle("Age Level");
ages.setDescription("Difficulty level for application content.");
ages.setChoiceEntries(AGES);
ages.setChoiceValues(AGE_VALUES);
restrictions.add(ages);
Bundle result = new Bundle();
result.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, restrictions);
setResultExtras(result);
}
/*
* Utility to get readable strings from restriction keys
*/
public static String getNameForRestriction(String key) {
if (UserManager.DISALLOW_CONFIG_BLUETOOTH.equals(key)) {
return "Unable to configure Bluetooth";
}
if (UserManager.DISALLOW_CONFIG_CREDENTIALS.equals(key)) {
return "Unable to configure user credentials";
}
if (UserManager.DISALLOW_CONFIG_WIFI.equals(key)) {
return "Unable to configure Wifi";
}
if (UserManager.DISALLOW_INSTALL_APPS.equals(key)) {
return "Unable to install applications";
}
if (UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES.equals(key)) {
return "Unable to enable unknown sources";
}
if (UserManager.DISALLOW_MODIFY_ACCOUNTS.equals(key)) {
return "Unable to modify accounts";
}
if (UserManager.DISALLOW_REMOVE_USER.equals(key)) {
return "Unable to remove users";
}
if (UserManager.DISALLOW_SHARE_LOCATION.equals(key)) {
return "Unable to toggle location sharing";
}
if (UserManager.DISALLOW_UNINSTALL_APPS.equals(key)) {
return "Unable to uninstall applications";
}
if (UserManager.DISALLOW_USB_FILE_TRANSFER.equals(key)) {
return "Unable to transfer files";
}
return "Unknown Restriction: "+key;
}
}