diff options
| author | Joe Onorato <joeo@android.com> | 2009-12-02 21:13:17 -0800 |
|---|---|---|
| committer | Joe Onorato <joeo@android.com> | 2009-12-02 22:27:07 -0800 |
| commit | c7a63eea8d3d346addaaf892b5bbe0aa80651640 (patch) | |
| tree | 9c2e0aef948c4a8922b48220e0f2639eaa9ab73b /graphics | |
| parent | de5343290c4bac9b10b6fdd328b70da6f5e4d626 (diff) | |
| download | frameworks_base-c7a63eea8d3d346addaaf892b5bbe0aa80651640.zip frameworks_base-c7a63eea8d3d346addaaf892b5bbe0aa80651640.tar.gz frameworks_base-c7a63eea8d3d346addaaf892b5bbe0aa80651640.tar.bz2 | |
Add a new field to Intent that allows you to give a hint about what on screen caused the intent to
be sent.
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/java/android/graphics/Rect.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/graphics/java/android/graphics/Rect.java b/graphics/java/android/graphics/Rect.java index 42a14ce..98ffb8b 100644 --- a/graphics/java/android/graphics/Rect.java +++ b/graphics/java/android/graphics/Rect.java @@ -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. |
