diff options
Diffstat (limited to 'core/java/android/util')
-rw-r--r-- | core/java/android/util/AttributeSet.java | 4 | ||||
-rw-r--r-- | core/java/android/util/Config.java | 8 | ||||
-rw-r--r-- | core/java/android/util/DisplayMetrics.java | 2 | ||||
-rw-r--r-- | core/java/android/util/EventLog.java | 25 | ||||
-rw-r--r-- | core/java/android/util/MathUtils.java | 176 | ||||
-rw-r--r-- | core/java/android/util/Pair.java | 76 |
6 files changed, 276 insertions, 15 deletions
diff --git a/core/java/android/util/AttributeSet.java b/core/java/android/util/AttributeSet.java index 01a7ad4..82592b9 100644 --- a/core/java/android/util/AttributeSet.java +++ b/core/java/android/util/AttributeSet.java @@ -34,13 +34,13 @@ package android.util; * <p>This interface provides an efficient mechanism for retrieving * data from compiled XML files, which can be retrieved for a particular * XmlPullParser through {@link Xml#asAttributeSet - * Xml.getAttributeSet()}. Normally this will return an implementation + * Xml.asAttributeSet()}. Normally this will return an implementation * of the interface that works on top of a generic XmlPullParser, however it * is more useful in conjunction with compiled XML resources: * * <pre> * XmlPullParser parser = resources.getXml(myResouce); - * AttributeSet attributes = Xml.getAttributeSet(parser);</pre> + * AttributeSet attributes = Xml.asAttributeSet(parser);</pre> * * <p>The implementation returned here, unlike using * the implementation on top of a generic XmlPullParser, diff --git a/core/java/android/util/Config.java b/core/java/android/util/Config.java index 9571041..924b49d 100644 --- a/core/java/android/util/Config.java +++ b/core/java/android/util/Config.java @@ -34,25 +34,25 @@ public final class Config */ /** - * Always the inverse of DEBUG. + * @deprecated Use {@link #DEBUG} instead. */ @Deprecated public static final boolean RELEASE = !DEBUG; /** - * Always false. + * @deprecated Always false. */ @Deprecated public static final boolean PROFILE = false; /** - * Always false. + * @deprecated Always false. */ @Deprecated public static final boolean LOGV = false; /** - * Always true. + * @deprecated Always true. */ @Deprecated public static final boolean LOGD = true; diff --git a/core/java/android/util/DisplayMetrics.java b/core/java/android/util/DisplayMetrics.java index 74f01cc..2628eb4 100644 --- a/core/java/android/util/DisplayMetrics.java +++ b/core/java/android/util/DisplayMetrics.java @@ -69,7 +69,7 @@ public class DisplayMetrics { * Density Independent Pixel unit, where one DIP is one pixel on an * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), * providing the baseline of the system's display. Thus on a 160dpi screen - * this density value will be 1; on a 106 dpi screen it would be .75; etc. + * this density value will be 1; on a 120 dpi screen it would be .75; etc. * * <p>This value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of diff --git a/core/java/android/util/EventLog.java b/core/java/android/util/EventLog.java index 24b4f73..81dd96e 100644 --- a/core/java/android/util/EventLog.java +++ b/core/java/android/util/EventLog.java @@ -73,7 +73,7 @@ import java.util.List; * </ul> * </li> * <li> '\n': 1 byte - an automatically generated newline, used to help detect and recover from log - * corruption and enable stansard unix tools like grep, tail and wc to operate + * corruption and enable standard unix tools like grep, tail and wc to operate * on event logs. </li> * </ul> * @@ -124,10 +124,6 @@ public class EventLog { "A List must have fewer than " + Byte.MAX_VALUE + " items in it."); } - if (items.length < 1) { - throw new IllegalArgumentException( - "A List must have at least one item in it."); - } for (int i = 0; i < items.length; i++) { final Object item = items[i]; if (item == null) { @@ -192,17 +188,21 @@ public class EventLog { return decodeObject(); } + public byte[] getRawData() { + return mBuffer.array(); + } + /** @return the loggable item at the current position in mBuffer. */ private Object decodeObject() { if (mBuffer.remaining() < 1) return null; switch (mBuffer.get()) { case INT: if (mBuffer.remaining() < 4) return null; - return mBuffer.getInt(); + return (Integer) mBuffer.getInt(); case LONG: if (mBuffer.remaining() < 8) return null; - return mBuffer.getLong(); + return (Long) mBuffer.getLong(); case STRING: try { @@ -219,7 +219,7 @@ public class EventLog { case LIST: if (mBuffer.remaining() < 1) return null; int length = mBuffer.get(); - if (length <= 0) return null; + if (length < 0) return null; Object[] array = new Object[length]; for (int i = 0; i < length; ++i) { array[i] = decodeObject(); @@ -285,4 +285,13 @@ public class EventLog { */ public static native void readEvents(int[] tags, Collection<Event> output) throws IOException; + + /** + * Read events from a file. + * @param path to read from + * @param output container to add events into + * @throws IOException if something goes wrong reading events + */ + public static native void readEvents(String path, Collection<Event> output) + throws IOException; } diff --git a/core/java/android/util/MathUtils.java b/core/java/android/util/MathUtils.java new file mode 100644 index 0000000..b35dd1e --- /dev/null +++ b/core/java/android/util/MathUtils.java @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2009 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 android.util; + +import java.util.Random; + +/** + * A class that contains utility methods related to numbers. + * + * @hide Pending API council approval + */ +public final class MathUtils { + private static final Random sRandom = new Random(); + private static final float DEG_TO_RAD = 3.1415926f / 180.0f; + private static final float RAD_TO_DEG = 180.0f / 3.1415926f; + + private MathUtils() { + } + + public static float abs(float v) { + return v > 0 ? v : -v; + } + + public static int constrain(int amount, int low, int high) { + return amount < low ? low : (amount > high ? high : amount); + } + + public static float constrain(float amount, float low, float high) { + return amount < low ? low : (amount > high ? high : amount); + } + + public static float log(float a) { + return (float) Math.log(a); + } + + public static float exp(float a) { + return (float) Math.exp(a); + } + + public static float pow(float a, float b) { + return (float) Math.pow(a, b); + } + + public static float max(float a, float b) { + return a > b ? a : b; + } + + public static float max(int a, int b) { + return a > b ? a : b; + } + + public static float max(float a, float b, float c) { + return a > b ? (a > c ? a : c) : (b > c ? b : c); + } + + public static float max(int a, int b, int c) { + return a > b ? (a > c ? a : c) : (b > c ? b : c); + } + + public static float min(float a, float b) { + return a < b ? a : b; + } + + public static float min(int a, int b) { + return a < b ? a : b; + } + + public static float min(float a, float b, float c) { + return a < b ? (a < c ? a : c) : (b < c ? b : c); + } + + public static float min(int a, int b, int c) { + return a < b ? (a < c ? a : c) : (b < c ? b : c); + } + + public static float dist(float x1, float y1, float x2, float y2) { + final float x = (x2 - x1); + final float y = (y2 - y1); + return (float) Math.sqrt(x * x + y * y); + } + + public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) { + final float x = (x2 - x1); + final float y = (y2 - y1); + final float z = (z2 - z1); + return (float) Math.sqrt(x * x + y * y + z * z); + } + + public static float mag(float a, float b) { + return (float) Math.sqrt(a * a + b * b); + } + + public static float mag(float a, float b, float c) { + return (float) Math.sqrt(a * a + b * b + c * c); + } + + public static float sq(float v) { + return v * v; + } + + public static float radians(float degrees) { + return degrees * DEG_TO_RAD; + } + + public static float degrees(float radians) { + return radians * RAD_TO_DEG; + } + + public static float acos(float value) { + return (float) Math.acos(value); + } + + public static float asin(float value) { + return (float) Math.asin(value); + } + + public static float atan(float value) { + return (float) Math.atan(value); + } + + public static float atan2(float a, float b) { + return (float) Math.atan2(a, b); + } + + public static float tan(float angle) { + return (float) Math.tan(angle); + } + + public static float lerp(float start, float stop, float amount) { + return start + (stop - start) * amount; + } + + public static float norm(float start, float stop, float value) { + return (value - start) / (stop - start); + } + + public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) { + return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart)); + } + + public static int random(int howbig) { + return (int) (sRandom.nextFloat() * howbig); + } + + public static int random(int howsmall, int howbig) { + if (howsmall >= howbig) return howsmall; + return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall); + } + + public static float random(float howbig) { + return sRandom.nextFloat() * howbig; + } + + public static float random(float howsmall, float howbig) { + if (howsmall >= howbig) return howsmall; + return sRandom.nextFloat() * (howbig - howsmall) + howsmall; + } + + public static void randomSeed(long seed) { + sRandom.setSeed(seed); + } +} diff --git a/core/java/android/util/Pair.java b/core/java/android/util/Pair.java new file mode 100644 index 0000000..bf25306 --- /dev/null +++ b/core/java/android/util/Pair.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2009 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 android.util; + +/** + * Container to ease passing around a tuple of two objects. This object provides a sensible + * implementation of equals(), returning true if equals() is true on each of the contained + * objects. + */ +public class Pair<F, S> { + public final F first; + public final S second; + + /** + * Constructor for a Pair. If either are null then equals() and hashCode() will throw + * a NullPointerException. + * @param first the first object in the Pair + * @param second the second object in the pair + */ + public Pair(F first, S second) { + this.first = first; + this.second = second; + } + + /** + * Checks the two objects for equality by delegating to their respective equals() methods. + * @param o the Pair to which this one is to be checked for equality + * @return true if the underlying objects of the Pair are both considered equals() + */ + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof Pair)) return false; + final Pair<F, S> other; + try { + other = (Pair<F, S>) o; + } catch (ClassCastException e) { + return false; + } + return first.equals(other.first) && second.equals(other.second); + } + + /** + * Compute a hash code using the hash codes of the underlying objects + * @return a hashcode of the Pair + */ + public int hashCode() { + int result = 17; + result = 31 * result + first.hashCode(); + result = 31 * result + second.hashCode(); + return result; + } + + /** + * Convenience method for creating an appropriately typed pair. + * @param a the first object in the Pair + * @param b the second object in the pair + * @return a Pair that is templatized with the types of a and b + */ + public static <A, B> Pair <A, B> create(A a, B b) { + return new Pair<A, B>(a, b); + } +} |