Add a new field to Intent that allows you to give a hint about what on screen caused the intent to

be sent.
This commit is contained in:
Joe Onorato
2009-12-02 21:13:17 -08:00
parent de5343290c
commit c7a63eea8d
3 changed files with 169 additions and 1 deletions

View File

@@ -34021,6 +34021,17 @@
<parameter name="defaultValue" type="short">
</parameter>
</method>
<method name="getSourceBounds"
return="android.graphics.Rect"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
</method>
<method name="getStringArrayExtra"
return="java.lang.String[]"
abstract="false"
@@ -34832,6 +34843,19 @@
<parameter name="packageName" type="java.lang.String">
</parameter>
</method>
<method name="setSourceBounds"
return="void"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
<parameter name="r" type="android.graphics.Rect">
</parameter>
</method>
<method name="setType"
return="android.content.Intent"
abstract="false"
@@ -36412,6 +36436,17 @@
visibility="public"
>
</field>
<field name="FILL_IN_SOURCE_BOUNDS"
type="int"
transient="false"
volatile="false"
value="32"
static="true"
final="true"
deprecated="not deprecated"
visibility="public"
>
</field>
<field name="FLAG_ACTIVITY_BROUGHT_TO_FRONT"
type="int"
transient="false"
@@ -62074,6 +62109,17 @@
visibility="public"
>
</method>
<method name="flattenToString"
return="java.lang.String"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
</method>
<method name="height"
return="int"
abstract="false"
@@ -62300,6 +62346,19 @@
visibility="public"
>
</method>
<method name="unflattenFromString"
return="android.graphics.Rect"
abstract="false"
native="false"
synchronized="false"
static="true"
final="false"
deprecated="not deprecated"
visibility="public"
>
<parameter name="str" type="java.lang.String">
</parameter>
</method>
<method name="union"
return="void"
abstract="false"

View File

@@ -26,6 +26,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
@@ -2417,6 +2418,7 @@ public class Intent implements Parcelable {
private int mFlags;
private HashSet<String> mCategories;
private Bundle mExtras;
private Rect mSourceBounds;
// ---------------------------------------------------------------------
@@ -2442,6 +2444,9 @@ public class Intent implements Parcelable {
if (o.mExtras != null) {
this.mExtras = new Bundle(o.mExtras);
}
if (o.mSourceBounds != null) {
this.mSourceBounds = new Rect(o.mSourceBounds);
}
}
@Override
@@ -2635,7 +2640,7 @@ public class Intent implements Parcelable {
intent.mType = value;
}
// launch flags
// launch flags
else if (uri.startsWith("launchFlags=", i)) {
intent.mFlags = Integer.decode(value).intValue();
}
@@ -2655,6 +2660,11 @@ public class Intent implements Parcelable {
scheme = value;
}
// source bounds
else if (uri.startsWith("sourceBounds=", i)) {
intent.mSourceBounds = Rect.unflattenFromString(value);
}
// extra
else {
String key = Uri.decode(uri.substring(i + 2, eq));
@@ -3546,6 +3556,15 @@ public class Intent implements Parcelable {
return mComponent;
}
/**
* Get the bounds of the sender of this intent, in screen coordinates. This can be
* used as a hint to the receiver for animations and the like. Null means that there
* is no source bounds.
*/
public Rect getSourceBounds() {
return mSourceBounds;
}
/**
* Return the Activity component that should be used to handle this intent.
* The appropriate component is determined based on the information in the
@@ -4647,6 +4666,19 @@ public class Intent implements Parcelable {
return this;
}
/**
* Set the bounds of the sender of this intent, in screen coordinates. This can be
* used as a hint to the receiver for animations and the like. Null means that there
* is no source bounds.
*/
public void setSourceBounds(Rect r) {
if (r != null) {
mSourceBounds = new Rect(r);
} else {
r = null;
}
}
/**
* Use with {@link #fillIn} to allow the current action value to be
* overwritten, even if it is already set.
@@ -4677,6 +4709,12 @@ public class Intent implements Parcelable {
*/
public static final int FILL_IN_PACKAGE = 1<<4;
/**
* Use with {@link #fillIn} to allow the current package value to be
* overwritten, even if it is already set.
*/
public static final int FILL_IN_SOURCE_BOUNDS = 1<<5;
/**
* Copy the contents of <var>other</var> in to this object, but only
* where fields are not defined by this object. For purposes of a field
@@ -4691,6 +4729,7 @@ public class Intent implements Parcelable {
* <li> package, as set by {@link #setPackage}.
* <li> component, as set by {@link #setComponent(ComponentName)} or
* related methods.
* <li> source bounds, as set by {@link #setSourceBounds}
* <li> each top-level name in the associated extras.
* </ul>
*
@@ -4752,6 +4791,11 @@ public class Intent implements Parcelable {
changes |= FILL_IN_COMPONENT;
}
mFlags |= other.mFlags;
if (other.mSourceBounds != null
&& (mSourceBounds == null || (flags&FILL_IN_SOURCE_BOUNDS) != 0)) {
mSourceBounds = new Rect(other.mSourceBounds);
changes |= FILL_IN_SOURCE_BOUNDS;
}
if (mExtras == null) {
if (other.mExtras != null) {
mExtras = new Bundle(other.mExtras);
@@ -5005,6 +5049,13 @@ public class Intent implements Parcelable {
first = false;
b.append("cmp=").append(mComponent.flattenToShortString());
}
if (mSourceBounds != null) {
if (!first) {
b.append(' ');
}
first = false;
b.append("bnds=").append(mSourceBounds.toShortString());
}
if (extras && mExtras != null) {
if (!first) {
b.append(' ');
@@ -5096,6 +5147,11 @@ public class Intent implements Parcelable {
uri.append("component=").append(Uri.encode(
mComponent.flattenToShortString(), "/")).append(';');
}
if (mSourceBounds != null) {
uri.append("sourceBounds=")
.append(Uri.encode(mSourceBounds.flattenToString()))
.append(';');
}
if (mExtras != null) {
for (String key : mExtras.keySet()) {
final Object value = mExtras.get(key);
@@ -5139,6 +5195,13 @@ public class Intent implements Parcelable {
out.writeString(mPackage);
ComponentName.writeToParcel(mComponent, out);
if (mSourceBounds != null) {
out.writeInt(1);
mSourceBounds.writeToParcel(out, flags);
} else {
out.writeInt(0);
}
if (mCategories != null) {
out.writeInt(mCategories.size());
for (String category : mCategories) {
@@ -5174,6 +5237,10 @@ public class Intent implements Parcelable {
mPackage = in.readString();
mComponent = ComponentName.readFromParcel(in);
if (in.readInt() != 0) {
mSourceBounds = Rect.CREATOR.createFromParcel(in);
}
int N = in.readInt();
if (N > 0) {
mCategories = new HashSet<String>();

View File

@@ -20,6 +20,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Rect holds four integer coordinates for a rectangle. The rectangle is
@@ -34,6 +36,9 @@ public final class Rect implements Parcelable {
public int right;
public int bottom;
private static final Pattern FLATTENED_PATTERN = Pattern.compile(
"(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)");
/**
* Create a new empty Rect. All coordinates are initialized to 0.
*/
@@ -105,6 +110,43 @@ public final class Rect implements Parcelable {
sb.append(','); sb.append(bottom); sb.append(']');
return sb.toString();
}
/**
* Return a string representation of the rectangle in a well-defined format.
*
* <p>You can later recover the Rect from this string through
* {@link #unflattenFromString(String)}.
*
* @return Returns a new String of the form "left top right bottom"
*/
public String flattenToString() {
StringBuilder sb = new StringBuilder(32);
// WARNING: Do not change the format of this string, it must be
// preserved because Rects are saved in this flattened format.
sb.append(left);
sb.append(' ');
sb.append(top);
sb.append(' ');
sb.append(right);
sb.append(' ');
sb.append(bottom);
return sb.toString();
}
/**
* Returns a Rect from a string of the form returned by {@link #flattenToString},
* or null if the string is not of that form.
*/
public static Rect unflattenFromString(String str) {
Matcher matcher = FLATTENED_PATTERN.matcher(str);
if (!matcher.matches()) {
return null;
}
return new Rect(Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)));
}
/**
* Print short representation to given writer.