M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2006 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;
/**
* Base class for all checked exceptions thrown by the Android frameworks.
*/
public class AndroidException extends Exception {
public AndroidException() {
}
public AndroidException(String name) {
super(name);
}
public AndroidException(Exception cause) {
super(cause);
}
};
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2006 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;
/**
* Base class for all unchecked exceptions thrown by the Android frameworks.
*/
public class AndroidRuntimeException extends RuntimeException {
public AndroidRuntimeException() {
}
public AndroidRuntimeException(String name) {
super(name);
}
public AndroidRuntimeException(Exception cause) {
super(cause);
}
};
@@ -0,0 +1,269 @@
/*
* Copyright (C) 2006 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;
/**
* A collection of attributes, as found associated with a tag in an XML
* document. Often you will not want to use this interface directly, instead
* passing it to {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)
* Resources.Theme.obtainStyledAttributes()}
* which will take care of parsing the attributes for you. In particular,
* the Resources API will convert resource references (attribute values such as
* "@string/my_label" in the original XML) to the desired type
* for you; if you use AttributeSet directly then you will need to manually
* check for resource references
* (with {@link #getAttributeResourceValue(int, int)}) and do the resource
* lookup yourself if needed. Direct use of AttributeSet also prevents the
* application of themes and styles when retrieving attribute values.
*
* <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.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.asAttributeSet(parser);</pre>
*
* <p>The implementation returned here, unlike using
* the implementation on top of a generic XmlPullParser,
* is highly optimized by retrieving pre-computed information that was
* generated by aapt when compiling your resources. For example,
* the {@link #getAttributeFloatValue(int, float)} method returns a floating
* point number previous stored in the compiled resource instead of parsing
* at runtime the string originally in the XML file.
*
* <p>This interface also provides additional information contained in the
* compiled XML resource that is not available in a normal XML file, such
* as {@link #getAttributeNameResource(int)} which returns the resource
* identifier associated with a particular XML attribute name.
*/
public interface AttributeSet {
public int getAttributeCount();
public String getAttributeName(int index);
public String getAttributeValue(int index);
public String getAttributeValue(String namespace, String name);
public String getPositionDescription();
/**
* Return the resource ID associated with the given attribute name. This
* will be the identifier for an attribute resource, which can be used by
* styles. Returns 0 if there is no resource associated with this
* attribute.
*
* <p>Note that this is different than {@link #getAttributeResourceValue}
* in that it returns a resource identifier for the attribute name; the
* other method returns this attribute's value as a resource identifier.
*
* @param index Index of the desired attribute, 0...count-1.
*
* @return The resource identifier, 0 if none.
*/
public int getAttributeNameResource(int index);
/**
* Return the index of the value of 'attribute' in the list 'options'.
*
* @param attribute Name of attribute to retrieve.
* @param options List of strings whose values we are checking against.
* @param defaultValue Value returned if attribute doesn't exist or no
* match is found.
*
* @return Index in to 'options' or defaultValue.
*/
public int getAttributeListValue(String namespace, String attribute,
String[] options, int defaultValue);
/**
* Return the boolean value of 'attribute'.
*
* @param attribute The attribute to retrieve.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public boolean getAttributeBooleanValue(String namespace, String attribute,
boolean defaultValue);
/**
* Return the value of 'attribute' as a resource identifier.
*
* <p>Note that this is different than {@link #getAttributeNameResource}
* in that it returns a the value contained in this attribute as a
* resource identifier (i.e., a value originally of the form
* "@package:type/resource"); the other method returns a resource
* identifier that identifies the name of the attribute.
*
* @param attribute The attribute to retrieve.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeResourceValue(String namespace, String attribute,
int defaultValue);
/**
* Return the integer value of 'attribute'.
*
* @param attribute The attribute to retrieve.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeIntValue(String namespace, String attribute,
int defaultValue);
/**
* Return the boolean value of 'attribute' that is formatted as an
* unsigned value. In particular, the formats 0xn...n and #n...n are
* handled.
*
* @param attribute The attribute to retrieve.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeUnsignedIntValue(String namespace, String attribute,
int defaultValue);
/**
* Return the float value of 'attribute'.
*
* @param attribute The attribute to retrieve.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public float getAttributeFloatValue(String namespace, String attribute,
float defaultValue);
/**
* Return the index of the value of attribute at 'index' in the list
* 'options'.
*
* @param index Index of the desired attribute, 0...count-1.
* @param options List of strings whose values we are checking against.
* @param defaultValue Value returned if attribute doesn't exist or no
* match is found.
*
* @return Index in to 'options' or defaultValue.
*/
public int getAttributeListValue(int index,
String[] options, int defaultValue);
/**
* Return the boolean value of attribute at 'index'.
*
* @param index Index of the desired attribute, 0...count-1.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public boolean getAttributeBooleanValue(int index,
boolean defaultValue);
/**
* Return the value of attribute at 'index' as a resource identifier.
*
* <p>Note that this is different than {@link #getAttributeNameResource}
* in that it returns a the value contained in this attribute as a
* resource identifier (i.e., a value originally of the form
* "@package:type/resource"); the other method returns a resource
* identifier that identifies the name of the attribute.
*
* @param index Index of the desired attribute, 0...count-1.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeResourceValue(int index,
int defaultValue);
/**
* Return the integer value of attribute at 'index'.
*
* @param index Index of the desired attribute, 0...count-1.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeIntValue(int index,
int defaultValue);
/**
* Return the integer value of attribute at 'index' that is formatted as an
* unsigned value. In particular, the formats 0xn...n and #n...n are
* handled.
*
* @param index Index of the desired attribute, 0...count-1.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public int getAttributeUnsignedIntValue(int index,
int defaultValue);
/**
* Return the float value of attribute at 'index'.
*
* @param index Index of the desired attribute, 0...count-1.
* @param defaultValue What to return if the attribute isn't found.
*
* @return Resulting value.
*/
public float getAttributeFloatValue(int index,
float defaultValue);
/**
* Return the value of the "id" attribute or null if there is not one.
* Equivalent to getAttributeValue(null, "id").
*
* @return The id attribute's value or null.
*/
public String getIdAttribute();
/**
* Return the value of the "class" attribute or null if there is not one.
* Equivalent to getAttributeValue(null, "class").
*
* @return The class attribute's value or null.
*/
public String getClassAttribute();
/**
* Return the integer value of the "id" attribute or defaultValue if there
* is none.
* Equivalent to getAttributeResourceValue(null, "id", defaultValue);
*
* @param defaultValue What to return if the "id" attribute isn't found.
* @return int Resulting value.
*/
public int getIdAttributeResourceValue(int defaultValue);
/**
* Return the value of the "style" attribute or 0 if there is not one.
* Equivalent to getAttributeResourceValue(null, "style").
*
* @return The style attribute's resource identifier or 0.
*/
public int getStyleAttribute();
}
+741
View File
@@ -0,0 +1,741 @@
/*
* 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.
*/
package android.util;
import java.io.UnsupportedEncodingException;
/**
* Utilities for encoding and decoding the Base64 representation of
* binary data. See RFCs <a
* href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and <a
* href="http://www.ietf.org/rfc/rfc3548.txt">3548</a>.
*/
public class Base64 {
/**
* Default values for encoder/decoder flags.
*/
public static final int DEFAULT = 0;
/**
* Encoder flag bit to omit the padding '=' characters at the end
* of the output (if any).
*/
public static final int NO_PADDING = 1;
/**
* Encoder flag bit to omit all line terminators (i.e., the output
* will be on one long line).
*/
public static final int NO_WRAP = 2;
/**
* Encoder flag bit to indicate lines should be terminated with a
* CRLF pair instead of just an LF. Has no effect if {@code
* NO_WRAP} is specified as well.
*/
public static final int CRLF = 4;
/**
* Encoder/decoder flag bit to indicate using the "URL and
* filename safe" variant of Base64 (see RFC 3548 section 4) where
* {@code -} and {@code _} are used in place of {@code +} and
* {@code /}.
*/
public static final int URL_SAFE = 8;
/**
* Flag to pass to {@link Base64OutputStream} to indicate that it
* should not close the output stream it is wrapping when it
* itself is closed.
*/
public static final int NO_CLOSE = 16;
// --------------------------------------------------------
// shared code
// --------------------------------------------------------
/* package */ static abstract class Coder {
public byte[] output;
public int op;
/**
* Encode/decode another block of input data. this.output is
* provided by the caller, and must be big enough to hold all
* the coded data. On exit, this.opwill be set to the length
* of the coded data.
*
* @param finish true if this is the final call to process for
* this object. Will finalize the coder state and
* include any final bytes in the output.
*
* @return true if the input so far is good; false if some
* error has been detected in the input stream..
*/
public abstract boolean process(byte[] input, int offset, int len, boolean finish);
/**
* @return the maximum number of bytes a call to process()
* could produce for the given number of input bytes. This may
* be an overestimate.
*/
public abstract int maxOutputSize(int len);
}
// --------------------------------------------------------
// decoding
// --------------------------------------------------------
/**
* Decode the Base64-encoded data in input and return the data in
* a new byte array.
*
* <p>The padding '=' characters at the end are considered optional, but
* if any are present, there must be the correct number of them.
*
* @param str the input String to decode, which is converted to
* bytes using the default charset
* @param flags controls certain features of the decoded output.
* Pass {@code DEFAULT} to decode standard Base64.
*
* @throws IllegalArgumentException if the input contains
* incorrect padding
*/
public static byte[] decode(String str, int flags) {
return decode(str.getBytes(), flags);
}
/**
* Decode the Base64-encoded data in input and return the data in
* a new byte array.
*
* <p>The padding '=' characters at the end are considered optional, but
* if any are present, there must be the correct number of them.
*
* @param input the input array to decode
* @param flags controls certain features of the decoded output.
* Pass {@code DEFAULT} to decode standard Base64.
*
* @throws IllegalArgumentException if the input contains
* incorrect padding
*/
public static byte[] decode(byte[] input, int flags) {
return decode(input, 0, input.length, flags);
}
/**
* Decode the Base64-encoded data in input and return the data in
* a new byte array.
*
* <p>The padding '=' characters at the end are considered optional, but
* if any are present, there must be the correct number of them.
*
* @param input the data to decode
* @param offset the position within the input array at which to start
* @param len the number of bytes of input to decode
* @param flags controls certain features of the decoded output.
* Pass {@code DEFAULT} to decode standard Base64.
*
* @throws IllegalArgumentException if the input contains
* incorrect padding
*/
public static byte[] decode(byte[] input, int offset, int len, int flags) {
// Allocate space for the most data the input could represent.
// (It could contain less if it contains whitespace, etc.)
Decoder decoder = new Decoder(flags, new byte[len*3/4]);
if (!decoder.process(input, offset, len, true)) {
throw new IllegalArgumentException("bad base-64");
}
// Maybe we got lucky and allocated exactly enough output space.
if (decoder.op == decoder.output.length) {
return decoder.output;
}
// Need to shorten the array, so allocate a new one of the
// right size and copy.
byte[] temp = new byte[decoder.op];
System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
return temp;
}
/* package */ static class Decoder extends Coder {
/**
* Lookup table for turning bytes into their position in the
* Base64 alphabet.
*/
private static final int DECODE[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/**
* Decode lookup table for the "web safe" variant (RFC 3548
* sec. 4) where - and _ replace + and /.
*/
private static final int DECODE_WEBSAFE[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/** Non-data values in the DECODE arrays. */
private static final int SKIP = -1;
private static final int EQUALS = -2;
/**
* States 0-3 are reading through the next input tuple.
* State 4 is having read one '=' and expecting exactly
* one more.
* State 5 is expecting no more data or padding characters
* in the input.
* State 6 is the error state; an error has been detected
* in the input and no future input can "fix" it.
*/
private int state; // state number (0 to 6)
private int value;
final private int[] alphabet;
public Decoder(int flags, byte[] output) {
this.output = output;
alphabet = ((flags & URL_SAFE) == 0) ? DECODE : DECODE_WEBSAFE;
state = 0;
value = 0;
}
/**
* @return an overestimate for the number of bytes {@code
* len} bytes could decode to.
*/
public int maxOutputSize(int len) {
return len * 3/4 + 10;
}
/**
* Decode another block of input data.
*
* @return true if the state machine is still healthy. false if
* bad base-64 data has been detected in the input stream.
*/
public boolean process(byte[] input, int offset, int len, boolean finish) {
if (this.state == 6) return false;
int p = offset;
len += offset;
// Using local variables makes the decoder about 12%
// faster than if we manipulate the member variables in
// the loop. (Even alphabet makes a measurable
// difference, which is somewhat surprising to me since
// the member variable is final.)
int state = this.state;
int value = this.value;
int op = 0;
final byte[] output = this.output;
final int[] alphabet = this.alphabet;
while (p < len) {
// Try the fast path: we're starting a new tuple and the
// next four bytes of the input stream are all data
// bytes. This corresponds to going through states
// 0-1-2-3-0. We expect to use this method for most of
// the data.
//
// If any of the next four bytes of input are non-data
// (whitespace, etc.), value will end up negative. (All
// the non-data values in decode are small negative
// numbers, so shifting any of them up and or'ing them
// together will result in a value with its top bit set.)
//
// You can remove this whole block and the output should
// be the same, just slower.
if (state == 0) {
while (p+4 <= len &&
(value = ((alphabet[input[p] & 0xff] << 18) |
(alphabet[input[p+1] & 0xff] << 12) |
(alphabet[input[p+2] & 0xff] << 6) |
(alphabet[input[p+3] & 0xff]))) >= 0) {
output[op+2] = (byte) value;
output[op+1] = (byte) (value >> 8);
output[op] = (byte) (value >> 16);
op += 3;
p += 4;
}
if (p >= len) break;
}
// The fast path isn't available -- either we've read a
// partial tuple, or the next four input bytes aren't all
// data, or whatever. Fall back to the slower state
// machine implementation.
int d = alphabet[input[p++] & 0xff];
switch (state) {
case 0:
if (d >= 0) {
value = d;
++state;
} else if (d != SKIP) {
this.state = 6;
return false;
}
break;
case 1:
if (d >= 0) {
value = (value << 6) | d;
++state;
} else if (d != SKIP) {
this.state = 6;
return false;
}
break;
case 2:
if (d >= 0) {
value = (value << 6) | d;
++state;
} else if (d == EQUALS) {
// Emit the last (partial) output tuple;
// expect exactly one more padding character.
output[op++] = (byte) (value >> 4);
state = 4;
} else if (d != SKIP) {
this.state = 6;
return false;
}
break;
case 3:
if (d >= 0) {
// Emit the output triple and return to state 0.
value = (value << 6) | d;
output[op+2] = (byte) value;
output[op+1] = (byte) (value >> 8);
output[op] = (byte) (value >> 16);
op += 3;
state = 0;
} else if (d == EQUALS) {
// Emit the last (partial) output tuple;
// expect no further data or padding characters.
output[op+1] = (byte) (value >> 2);
output[op] = (byte) (value >> 10);
op += 2;
state = 5;
} else if (d != SKIP) {
this.state = 6;
return false;
}
break;
case 4:
if (d == EQUALS) {
++state;
} else if (d != SKIP) {
this.state = 6;
return false;
}
break;
case 5:
if (d != SKIP) {
this.state = 6;
return false;
}
break;
}
}
if (!finish) {
// We're out of input, but a future call could provide
// more.
this.state = state;
this.value = value;
this.op = op;
return true;
}
// Done reading input. Now figure out where we are left in
// the state machine and finish up.
switch (state) {
case 0:
// Output length is a multiple of three. Fine.
break;
case 1:
// Read one extra input byte, which isn't enough to
// make another output byte. Illegal.
this.state = 6;
return false;
case 2:
// Read two extra input bytes, enough to emit 1 more
// output byte. Fine.
output[op++] = (byte) (value >> 4);
break;
case 3:
// Read three extra input bytes, enough to emit 2 more
// output bytes. Fine.
output[op++] = (byte) (value >> 10);
output[op++] = (byte) (value >> 2);
break;
case 4:
// Read one padding '=' when we expected 2. Illegal.
this.state = 6;
return false;
case 5:
// Read all the padding '='s we expected and no more.
// Fine.
break;
}
this.state = state;
this.op = op;
return true;
}
}
// --------------------------------------------------------
// encoding
// --------------------------------------------------------
/**
* Base64-encode the given data and return a newly allocated
* String with the result.
*
* @param input the data to encode
* @param flags controls certain features of the encoded output.
* Passing {@code DEFAULT} results in output that
* adheres to RFC 2045.
*/
public static String encodeToString(byte[] input, int flags) {
try {
return new String(encode(input, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
}
/**
* Base64-encode the given data and return a newly allocated
* String with the result.
*
* @param input the data to encode
* @param offset the position within the input array at which to
* start
* @param len the number of bytes of input to encode
* @param flags controls certain features of the encoded output.
* Passing {@code DEFAULT} results in output that
* adheres to RFC 2045.
*/
public static String encodeToString(byte[] input, int offset, int len, int flags) {
try {
return new String(encode(input, offset, len, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
}
/**
* Base64-encode the given data and return a newly allocated
* byte[] with the result.
*
* @param input the data to encode
* @param flags controls certain features of the encoded output.
* Passing {@code DEFAULT} results in output that
* adheres to RFC 2045.
*/
public static byte[] encode(byte[] input, int flags) {
return encode(input, 0, input.length, flags);
}
/**
* Base64-encode the given data and return a newly allocated
* byte[] with the result.
*
* @param input the data to encode
* @param offset the position within the input array at which to
* start
* @param len the number of bytes of input to encode
* @param flags controls certain features of the encoded output.
* Passing {@code DEFAULT} results in output that
* adheres to RFC 2045.
*/
public static byte[] encode(byte[] input, int offset, int len, int flags) {
Encoder encoder = new Encoder(flags, null);
// Compute the exact length of the array we will produce.
int output_len = len / 3 * 4;
// Account for the tail of the data and the padding bytes, if any.
if (encoder.do_padding) {
if (len % 3 > 0) {
output_len += 4;
}
} else {
switch (len % 3) {
case 0: break;
case 1: output_len += 2; break;
case 2: output_len += 3; break;
}
}
// Account for the newlines, if any.
if (encoder.do_newline && len > 0) {
output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) *
(encoder.do_cr ? 2 : 1);
}
encoder.output = new byte[output_len];
encoder.process(input, offset, len, true);
assert encoder.op == output_len;
return encoder.output;
}
/* package */ static class Encoder extends Coder {
/**
* Emit a new line every this many output tuples. Corresponds to
* a 76-character line length (the maximum allowable according to
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>).
*/
public static final int LINE_GROUPS = 19;
/**
* Lookup table for turning Base64 alphabet positions (6 bits)
* into output bytes.
*/
private static final byte ENCODE[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
};
/**
* Lookup table for turning Base64 alphabet positions (6 bits)
* into output bytes.
*/
private static final byte ENCODE_WEBSAFE[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
};
final private byte[] tail;
/* package */ int tailLen;
private int count;
final public boolean do_padding;
final public boolean do_newline;
final public boolean do_cr;
final private byte[] alphabet;
public Encoder(int flags, byte[] output) {
this.output = output;
do_padding = (flags & NO_PADDING) == 0;
do_newline = (flags & NO_WRAP) == 0;
do_cr = (flags & CRLF) != 0;
alphabet = ((flags & URL_SAFE) == 0) ? ENCODE : ENCODE_WEBSAFE;
tail = new byte[2];
tailLen = 0;
count = do_newline ? LINE_GROUPS : -1;
}
/**
* @return an overestimate for the number of bytes {@code
* len} bytes could encode to.
*/
public int maxOutputSize(int len) {
return len * 8/5 + 10;
}
public boolean process(byte[] input, int offset, int len, boolean finish) {
// Using local variables makes the encoder about 9% faster.
final byte[] alphabet = this.alphabet;
final byte[] output = this.output;
int op = 0;
int count = this.count;
int p = offset;
len += offset;
int v = -1;
// First we need to concatenate the tail of the previous call
// with any input bytes available now and see if we can empty
// the tail.
switch (tailLen) {
case 0:
// There was no tail.
break;
case 1:
if (p+2 <= len) {
// A 1-byte tail with at least 2 bytes of
// input available now.
v = ((tail[0] & 0xff) << 16) |
((input[p++] & 0xff) << 8) |
(input[p++] & 0xff);
tailLen = 0;
};
break;
case 2:
if (p+1 <= len) {
// A 2-byte tail with at least 1 byte of input.
v = ((tail[0] & 0xff) << 16) |
((tail[1] & 0xff) << 8) |
(input[p++] & 0xff);
tailLen = 0;
}
break;
}
if (v != -1) {
output[op++] = alphabet[(v >> 18) & 0x3f];
output[op++] = alphabet[(v >> 12) & 0x3f];
output[op++] = alphabet[(v >> 6) & 0x3f];
output[op++] = alphabet[v & 0x3f];
if (--count == 0) {
if (do_cr) output[op++] = '\r';
output[op++] = '\n';
count = LINE_GROUPS;
}
}
// At this point either there is no tail, or there are fewer
// than 3 bytes of input available.
// The main loop, turning 3 input bytes into 4 output bytes on
// each iteration.
while (p+3 <= len) {
v = ((input[p] & 0xff) << 16) |
((input[p+1] & 0xff) << 8) |
(input[p+2] & 0xff);
output[op] = alphabet[(v >> 18) & 0x3f];
output[op+1] = alphabet[(v >> 12) & 0x3f];
output[op+2] = alphabet[(v >> 6) & 0x3f];
output[op+3] = alphabet[v & 0x3f];
p += 3;
op += 4;
if (--count == 0) {
if (do_cr) output[op++] = '\r';
output[op++] = '\n';
count = LINE_GROUPS;
}
}
if (finish) {
// Finish up the tail of the input. Note that we need to
// consume any bytes in tail before any bytes
// remaining in input; there should be at most two bytes
// total.
if (p-tailLen == len-1) {
int t = 0;
v = ((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 4;
tailLen -= t;
output[op++] = alphabet[(v >> 6) & 0x3f];
output[op++] = alphabet[v & 0x3f];
if (do_padding) {
output[op++] = '=';
output[op++] = '=';
}
if (do_newline) {
if (do_cr) output[op++] = '\r';
output[op++] = '\n';
}
} else if (p-tailLen == len-2) {
int t = 0;
v = (((tailLen > 1 ? tail[t++] : input[p++]) & 0xff) << 10) |
(((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 2);
tailLen -= t;
output[op++] = alphabet[(v >> 12) & 0x3f];
output[op++] = alphabet[(v >> 6) & 0x3f];
output[op++] = alphabet[v & 0x3f];
if (do_padding) {
output[op++] = '=';
}
if (do_newline) {
if (do_cr) output[op++] = '\r';
output[op++] = '\n';
}
} else if (do_newline && op > 0 && count != LINE_GROUPS) {
if (do_cr) output[op++] = '\r';
output[op++] = '\n';
}
assert tailLen == 0;
assert p == len;
} else {
// Save the leftovers in tail to be consumed on the next
// call to encodeInternal.
if (p == len-1) {
tail[tailLen++] = input[p];
} else if (p == len-2) {
tail[tailLen++] = input[p];
tail[tailLen++] = input[p+1];
}
}
this.op = op;
this.count = count;
return true;
}
}
private Base64() { } // don't instantiate
}
@@ -0,0 +1,153 @@
/*
* 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.
*/
package android.util;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* An InputStream that does Base64 decoding on the data read through
* it.
*/
public class Base64InputStream extends FilterInputStream {
private final Base64.Coder coder;
private static byte[] EMPTY = new byte[0];
private static final int BUFFER_SIZE = 2048;
private boolean eof;
private byte[] inputBuffer;
private int outputStart;
private int outputEnd;
/**
* An InputStream that performs Base64 decoding on the data read
* from the wrapped stream.
*
* @param in the InputStream to read the source data from
* @param flags bit flags for controlling the decoder; see the
* constants in {@link Base64}
*/
public Base64InputStream(InputStream in, int flags) {
this(in, flags, false);
}
/**
* Performs Base64 encoding or decoding on the data read from the
* wrapped InputStream.
*
* @param in the InputStream to read the source data from
* @param flags bit flags for controlling the decoder; see the
* constants in {@link Base64}
* @param encode true to encode, false to decode
*
* @hide
*/
public Base64InputStream(InputStream in, int flags, boolean encode) {
super(in);
eof = false;
inputBuffer = new byte[BUFFER_SIZE];
if (encode) {
coder = new Base64.Encoder(flags, null);
} else {
coder = new Base64.Decoder(flags, null);
}
coder.output = new byte[coder.maxOutputSize(BUFFER_SIZE)];
outputStart = 0;
outputEnd = 0;
}
public boolean markSupported() {
return false;
}
public void mark(int readlimit) {
throw new UnsupportedOperationException();
}
public void reset() {
throw new UnsupportedOperationException();
}
public void close() throws IOException {
in.close();
inputBuffer = null;
}
public int available() {
return outputEnd - outputStart;
}
public long skip(long n) throws IOException {
if (outputStart >= outputEnd) {
refill();
}
if (outputStart >= outputEnd) {
return 0;
}
long bytes = Math.min(n, outputEnd-outputStart);
outputStart += bytes;
return bytes;
}
public int read() throws IOException {
if (outputStart >= outputEnd) {
refill();
}
if (outputStart >= outputEnd) {
return -1;
} else {
return coder.output[outputStart++] & 0xff;
}
}
public int read(byte[] b, int off, int len) throws IOException {
if (outputStart >= outputEnd) {
refill();
}
if (outputStart >= outputEnd) {
return -1;
}
int bytes = Math.min(len, outputEnd-outputStart);
System.arraycopy(coder.output, outputStart, b, off, bytes);
outputStart += bytes;
return bytes;
}
/**
* Read data from the input stream into inputBuffer, then
* decode/encode it into the empty coder.output, and reset the
* outputStart and outputEnd pointers.
*/
private void refill() throws IOException {
if (eof) return;
int bytesRead = in.read(inputBuffer);
boolean success;
if (bytesRead == -1) {
eof = true;
success = coder.process(EMPTY, 0, 0, true);
} else {
success = coder.process(inputBuffer, 0, bytesRead, false);
}
if (!success) {
throw new IOException("bad base-64");
}
outputEnd = coder.op;
outputStart = 0;
}
}
@@ -0,0 +1,155 @@
/*
* 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.
*/
package android.util;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* An OutputStream that does Base64 encoding on the data written to
* it, writing the resulting data to another OutputStream.
*/
public class Base64OutputStream extends FilterOutputStream {
private final Base64.Coder coder;
private final int flags;
private byte[] buffer = null;
private int bpos = 0;
private static byte[] EMPTY = new byte[0];
/**
* Performs Base64 encoding on the data written to the stream,
* writing the encoded data to another OutputStream.
*
* @param out the OutputStream to write the encoded data to
* @param flags bit flags for controlling the encoder; see the
* constants in {@link Base64}
*/
public Base64OutputStream(OutputStream out, int flags) {
this(out, flags, true);
}
/**
* Performs Base64 encoding or decoding on the data written to the
* stream, writing the encoded/decoded data to another
* OutputStream.
*
* @param out the OutputStream to write the encoded data to
* @param flags bit flags for controlling the encoder; see the
* constants in {@link Base64}
* @param encode true to encode, false to decode
*
* @hide
*/
public Base64OutputStream(OutputStream out, int flags, boolean encode) {
super(out);
this.flags = flags;
if (encode) {
coder = new Base64.Encoder(flags, null);
} else {
coder = new Base64.Decoder(flags, null);
}
}
public void write(int b) throws IOException {
// To avoid invoking the encoder/decoder routines for single
// bytes, we buffer up calls to write(int) in an internal
// byte array to transform them into writes of decently-sized
// arrays.
if (buffer == null) {
buffer = new byte[1024];
}
if (bpos >= buffer.length) {
// internal buffer full; write it out.
internalWrite(buffer, 0, bpos, false);
bpos = 0;
}
buffer[bpos++] = (byte) b;
}
/**
* Flush any buffered data from calls to write(int). Needed
* before doing a write(byte[], int, int) or a close().
*/
private void flushBuffer() throws IOException {
if (bpos > 0) {
internalWrite(buffer, 0, bpos, false);
bpos = 0;
}
}
public void write(byte[] b, int off, int len) throws IOException {
if (len <= 0) return;
flushBuffer();
internalWrite(b, off, len, false);
}
public void close() throws IOException {
IOException thrown = null;
try {
flushBuffer();
internalWrite(EMPTY, 0, 0, true);
} catch (IOException e) {
thrown = e;
}
try {
if ((flags & Base64.NO_CLOSE) == 0) {
out.close();
} else {
out.flush();
}
} catch (IOException e) {
if (thrown != null) {
thrown = e;
}
}
if (thrown != null) {
throw thrown;
}
}
/**
* Write the given bytes to the encoder/decoder.
*
* @param finish true if this is the last batch of input, to cause
* encoder/decoder state to be finalized.
*/
private void internalWrite(byte[] b, int off, int len, boolean finish) throws IOException {
coder.output = embiggen(coder.output, coder.maxOutputSize(len));
if (!coder.process(b, off, len, finish)) {
throw new IOException("bad base-64");
}
out.write(coder.output, 0, coder.op);
}
/**
* If b.length is at least len, return b. Otherwise return a new
* byte array of length len.
*/
private byte[] embiggen(byte[] b, int len) {
if (b == null || b.length < len) {
return new byte[len];
} else {
return b;
}
}
}
@@ -0,0 +1,342 @@
/*
* 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.
*/
package android.util;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.Calendar.CalendarCache;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Time;
import java.util.Formatter;
import java.util.HashSet;
import java.util.Locale;
/**
* A class containing utility methods related to Calendar apps.
*
* @hide
*/
public class CalendarUtils {
private static final boolean DEBUG = false;
private static final String TAG = "CalendarUtils";
/**
* This class contains methods specific to reading and writing time zone
* values.
*/
public static class TimeZoneUtils {
private static final String[] TIMEZONE_TYPE_ARGS = { CalendarCache.TIMEZONE_KEY_TYPE };
private static final String[] TIMEZONE_INSTANCES_ARGS =
{ CalendarCache.TIMEZONE_KEY_INSTANCES };
private static StringBuilder mSB = new StringBuilder(50);
private static Formatter mF = new Formatter(mSB, Locale.getDefault());
private volatile static boolean mFirstTZRequest = true;
private volatile static boolean mTZQueryInProgress = false;
private volatile static boolean mUseHomeTZ = false;
private volatile static String mHomeTZ = Time.getCurrentTimezone();
private static HashSet<Runnable> mTZCallbacks = new HashSet<Runnable>();
private static int mToken = 1;
private static AsyncTZHandler mHandler;
// The name of the shared preferences file. This name must be maintained for historical
// reasons, as it's what PreferenceManager assigned the first time the file was created.
private final String mPrefsName;
/**
* This is the key used for writing whether or not a home time zone should
* be used in the Calendar app to the Calendar Preferences.
*/
public static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled";
/**
* This is the key used for writing the time zone that should be used if
* home time zones are enabled for the Calendar app.
*/
public static final String KEY_HOME_TZ = "preferences_home_tz";
/**
* This is a helper class for handling the async queries and updates for the
* time zone settings in Calendar.
*/
private class AsyncTZHandler extends AsyncQueryHandler {
public AsyncTZHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
synchronized (mTZCallbacks) {
boolean writePrefs = false;
// Check the values in the db
int keyColumn = cursor.getColumnIndexOrThrow(CalendarCache.KEY);
int valueColumn = cursor.getColumnIndexOrThrow(CalendarCache.VALUE);
while(cursor.moveToNext()) {
String key = cursor.getString(keyColumn);
String value = cursor.getString(valueColumn);
if (TextUtils.equals(key, CalendarCache.TIMEZONE_KEY_TYPE)) {
boolean useHomeTZ = !TextUtils.equals(
value, CalendarCache.TIMEZONE_TYPE_AUTO);
if (useHomeTZ != mUseHomeTZ) {
writePrefs = true;
mUseHomeTZ = useHomeTZ;
}
} else if (TextUtils.equals(
key, CalendarCache.TIMEZONE_KEY_INSTANCES_PREVIOUS)) {
if (!TextUtils.isEmpty(value) && !TextUtils.equals(mHomeTZ, value)) {
writePrefs = true;
mHomeTZ = value;
}
}
}
cursor.close();
if (writePrefs) {
SharedPreferences prefs = getSharedPreferences((Context)cookie, mPrefsName);
// Write the prefs
setSharedPreference(prefs, KEY_HOME_TZ_ENABLED, mUseHomeTZ);
setSharedPreference(prefs, KEY_HOME_TZ, mHomeTZ);
}
mTZQueryInProgress = false;
for (Runnable callback : mTZCallbacks) {
if (callback != null) {
callback.run();
}
}
mTZCallbacks.clear();
}
}
}
/**
* The name of the file where the shared prefs for Calendar are stored
* must be provided. All activities within an app should provide the
* same preferences name or behavior may become erratic.
*
* @param prefsName
*/
public TimeZoneUtils(String prefsName) {
mPrefsName = prefsName;
}
/**
* Formats a date or a time range according to the local conventions.
*
* This formats a date/time range using Calendar's time zone and the
* local conventions for the region of the device.
*
* If the {@link DateUtils#FORMAT_UTC} flag is used it will pass in
* the UTC time zone instead.
*
* @param context the context is required only if the time is shown
* @param startMillis the start time in UTC milliseconds
* @param endMillis the end time in UTC milliseconds
* @param flags a bit mask of options See
* {@link DateUtils#formatDateRange(Context, Formatter, long, long, int, String) formatDateRange}
* @return a string containing the formatted date/time range.
*/
public String formatDateRange(Context context, long startMillis,
long endMillis, int flags) {
String date;
String tz;
if ((flags & DateUtils.FORMAT_UTC) != 0) {
tz = Time.TIMEZONE_UTC;
} else {
tz = getTimeZone(context, null);
}
synchronized (mSB) {
mSB.setLength(0);
date = DateUtils.formatDateRange(context, mF, startMillis, endMillis, flags,
tz).toString();
}
return date;
}
/**
* Writes a new home time zone to the db.
*
* Updates the home time zone in the db asynchronously and updates
* the local cache. Sending a time zone of
* {@link CalendarCache#TIMEZONE_TYPE_AUTO} will cause it to be set
* to the device's time zone. null or empty tz will be ignored.
*
* @param context The calling activity
* @param timeZone The time zone to set Calendar to, or
* {@link CalendarCache#TIMEZONE_TYPE_AUTO}
*/
public void setTimeZone(Context context, String timeZone) {
if (TextUtils.isEmpty(timeZone)) {
if (DEBUG) {
Log.d(TAG, "Empty time zone, nothing to be done.");
}
return;
}
boolean updatePrefs = false;
synchronized (mTZCallbacks) {
if (CalendarCache.TIMEZONE_TYPE_AUTO.equals(timeZone)) {
if (mUseHomeTZ) {
updatePrefs = true;
}
mUseHomeTZ = false;
} else {
if (!mUseHomeTZ || !TextUtils.equals(mHomeTZ, timeZone)) {
updatePrefs = true;
}
mUseHomeTZ = true;
mHomeTZ = timeZone;
}
}
if (updatePrefs) {
// Write the prefs
SharedPreferences prefs = getSharedPreferences(context, mPrefsName);
setSharedPreference(prefs, KEY_HOME_TZ_ENABLED, mUseHomeTZ);
setSharedPreference(prefs, KEY_HOME_TZ, mHomeTZ);
// Update the db
ContentValues values = new ContentValues();
if (mHandler != null) {
mHandler.cancelOperation(mToken);
}
mHandler = new AsyncTZHandler(context.getContentResolver());
// skip 0 so query can use it
if (++mToken == 0) {
mToken = 1;
}
// Write the use home tz setting
values.put(CalendarCache.VALUE, mUseHomeTZ ? CalendarCache.TIMEZONE_TYPE_HOME
: CalendarCache.TIMEZONE_TYPE_AUTO);
mHandler.startUpdate(mToken, null, CalendarCache.URI, values, CalendarCache.WHERE,
TIMEZONE_TYPE_ARGS);
// If using a home tz write it to the db
if (mUseHomeTZ) {
ContentValues values2 = new ContentValues();
values2.put(CalendarCache.VALUE, mHomeTZ);
mHandler.startUpdate(mToken, null, CalendarCache.URI, values2,
CalendarCache.WHERE, TIMEZONE_INSTANCES_ARGS);
}
}
}
/**
* Gets the time zone that Calendar should be displayed in
*
* This is a helper method to get the appropriate time zone for Calendar. If this
* is the first time this method has been called it will initiate an asynchronous
* query to verify that the data in preferences is correct. The callback supplied
* will only be called if this query returns a value other than what is stored in
* preferences and should cause the calling activity to refresh anything that
* depends on calling this method.
*
* @param context The calling activity
* @param callback The runnable that should execute if a query returns new values
* @return The string value representing the time zone Calendar should display
*/
public String getTimeZone(Context context, Runnable callback) {
synchronized (mTZCallbacks){
if (mFirstTZRequest) {
mTZQueryInProgress = true;
mFirstTZRequest = false;
SharedPreferences prefs = getSharedPreferences(context, mPrefsName);
mUseHomeTZ = prefs.getBoolean(KEY_HOME_TZ_ENABLED, false);
mHomeTZ = prefs.getString(KEY_HOME_TZ, Time.getCurrentTimezone());
// When the async query returns it should synchronize on
// mTZCallbacks, update mUseHomeTZ, mHomeTZ, and the
// preferences, set mTZQueryInProgress to false, and call all
// the runnables in mTZCallbacks.
if (mHandler == null) {
mHandler = new AsyncTZHandler(context.getContentResolver());
}
mHandler.startQuery(0, context, CalendarCache.URI, CalendarCache.POJECTION,
null, null, null);
}
if (mTZQueryInProgress) {
mTZCallbacks.add(callback);
}
}
return mUseHomeTZ ? mHomeTZ : Time.getCurrentTimezone();
}
/**
* Forces a query of the database to check for changes to the time zone.
* This should be called if another app may have modified the db. If a
* query is already in progress the callback will be added to the list
* of callbacks to be called when it returns.
*
* @param context The calling activity
* @param callback The runnable that should execute if a query returns
* new values
*/
public void forceDBRequery(Context context, Runnable callback) {
synchronized (mTZCallbacks){
if (mTZQueryInProgress) {
mTZCallbacks.add(callback);
return;
}
mFirstTZRequest = true;
getTimeZone(context, callback);
}
}
}
/**
* A helper method for writing a String value to the preferences
* asynchronously.
*
* @param context A context with access to the correct preferences
* @param key The preference to write to
* @param value The value to write
*/
public static void setSharedPreference(SharedPreferences prefs, String key, String value) {
// SharedPreferences prefs = getSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.apply();
}
/**
* A helper method for writing a boolean value to the preferences
* asynchronously.
*
* @param context A context with access to the correct preferences
* @param key The preference to write to
* @param value The value to write
*/
public static void setSharedPreference(SharedPreferences prefs, String key, boolean value) {
// SharedPreferences prefs = getSharedPreferences(context, prefsName);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
/** Return a properly configured SharedPreferences instance */
public static SharedPreferences getSharedPreferences(Context context, String prefsName) {
return context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
}
}
@@ -0,0 +1,174 @@
/*
* 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 android.os.Build;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
/**
* A class containing utility methods related to character sets. This
* class is primarily useful for code that wishes to be vendor-aware
* in its interpretation of Japanese encoding names.
*
* <p>As of this writing, the only vendor that is recognized by this
* class is Docomo (identified case-insensitively as {@code "docomo"}).</p>
*
* <b>Note:</b> This class is hidden in Cupcake, with a plan to
* un-hide in Donut. This was done because the first deployment to use
* this code is based on Cupcake, but the API had to be introduced
* after the public API freeze for that release. The upshot is that
* only system applications can safely use this class until Donut is
* available.
*
* @hide
*/
public final class CharsetUtils {
/**
* name of the vendor "Docomo". <b>Note:</b> This isn't a public
* constant, in order to keep this class from becoming a de facto
* reference list of vendor names.
*/
private static final String VENDOR_DOCOMO = "docomo";
/**
* This class is uninstantiable.
*/
private CharsetUtils() {
// This space intentionally left blank.
}
/**
* Returns the name of the vendor-specific character set
* corresponding to the given original character set name and
* vendor. If there is no vendor-specific character set for the
* given name/vendor pair, this returns the original character set
* name. The vendor name is matched case-insensitively.
*
* @param charsetName the base character set name
* @param vendor the vendor to specialize for
* @return the specialized character set name, or {@code charsetName} if
* there is no specialized name
*/
public static String nameForVendor(String charsetName, String vendor) {
// TODO: Eventually, this may want to be table-driven.
if (vendor.equalsIgnoreCase(VENDOR_DOCOMO)
&& isShiftJis(charsetName)) {
return "docomo-shift_jis-2007";
}
return charsetName;
}
/**
* Returns the name of the vendor-specific character set
* corresponding to the given original character set name and the
* default vendor (that is, the targeted vendor of the device this
* code is running on). This method merely calls through to
* {@link #nameForVendor(String,String)}, passing the default vendor
* as the second argument.
*
* @param charsetName the base character set name
* @return the specialized character set name, or {@code charsetName} if
* there is no specialized name
*/
public static String nameForDefaultVendor(String charsetName) {
return nameForVendor(charsetName, getDefaultVendor());
}
/**
* Returns the vendor-specific character set corresponding to the
* given original character set name and vendor. If there is no
* vendor-specific character set for the given name/vendor pair,
* this returns the character set corresponding to the original
* name. The vendor name is matched case-insensitively. This
* method merely calls {@code Charset.forName()} on a name
* transformed by a call to {@link #nameForVendor(String,String)}.
*
* @param charsetName the base character set name
* @param vendor the vendor to specialize for
* @return the specialized character set, or the one corresponding
* directly to {@code charsetName} if there is no specialized
* variant
* @throws UnsupportedCharsetException thrown if the named character
* set is not supported by the system
* @throws IllegalCharsetNameException thrown if {@code charsetName}
* has invalid syntax
*/
public static Charset charsetForVendor(String charsetName, String vendor)
throws UnsupportedCharsetException, IllegalCharsetNameException {
charsetName = nameForVendor(charsetName, vendor);
return Charset.forName(charsetName);
}
/**
* Returns the vendor-specific character set corresponding to the
* given original character set name and default vendor (that is,
* the targeted vendor of the device this code is running on).
* This method merely calls through to {@link
* #charsetForVendor(String,String)}, passing the default vendor
* as the second argument.
*
* @param charsetName the base character set name
* @return the specialized character set, or the one corresponding
* directly to {@code charsetName} if there is no specialized
* variant
* @throws UnsupportedCharsetException thrown if the named character
* set is not supported by the system
* @throws IllegalCharsetNameException thrown if {@code charsetName}
* has invalid syntax
*/
public static Charset charsetForVendor(String charsetName)
throws UnsupportedCharsetException, IllegalCharsetNameException {
return charsetForVendor(charsetName, getDefaultVendor());
}
/**
* Returns whether the given character set name indicates the Shift-JIS
* encoding. Returns false if the name is null.
*
* @param charsetName the character set name
* @return {@code true} if the name corresponds to Shift-JIS or
* {@code false} if not
*/
private static boolean isShiftJis(String charsetName) {
// Bail quickly if the length doesn't match.
if (charsetName == null) {
return false;
}
int length = charsetName.length();
if (length != 4 && length != 9) {
return false;
}
return charsetName.equalsIgnoreCase("shift_jis")
|| charsetName.equalsIgnoreCase("shift-jis")
|| charsetName.equalsIgnoreCase("sjis");
}
/**
* Gets the default vendor for this build.
*
* @return the default vendor name
*/
private static String getDefaultVendor() {
return Build.BRAND;
}
}
+59
View File
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2006 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;
/**
* Build configuration. The constants in this class vary depending
* on release vs. debug build.
* {@more}
*/
public final class Config
{
/**
* If this is a debug build, this field will be true.
*/
public static final boolean DEBUG = ConfigBuildFlags.DEBUG;
/*
* Deprecated fields
* TODO: Remove platform references to these and @hide them.
*/
/**
* @deprecated Use {@link #DEBUG} instead.
*/
@Deprecated
public static final boolean RELEASE = !DEBUG;
/**
* @deprecated Always false.
*/
@Deprecated
public static final boolean PROFILE = false;
/**
* @deprecated Always false.
*/
@Deprecated
public static final boolean LOGV = false;
/**
* @deprecated Always true.
*/
@Deprecated
public static final boolean LOGD = true;
}
@@ -0,0 +1,187 @@
/*
* 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 android.util;
/**
* Helps control and display a month view of a calendar that has a current
* selected day.
* <ul>
* <li>Keeps track of current month, day, year</li>
* <li>Keeps track of current cursor position (row, column)</li>
* <li>Provides methods to help display the calendar</li>
* <li>Provides methods to move the cursor up / down / left / right.</li>
* </ul>
*
* This should be used by anyone who presents a month view to users and wishes
* to behave consistently with other widgets and apps; if we ever change our
* mind about when to flip the month, we can change it here only.
*
* @hide
*/
public class DayOfMonthCursor extends MonthDisplayHelper {
private int mRow;
private int mColumn;
/**
* @param year The initial year.
* @param month The initial month.
* @param dayOfMonth The initial dayOfMonth.
* @param weekStartDay What dayOfMonth of the week the week should start,
* in terms of {@link java.util.Calendar} constants such as
* {@link java.util.Calendar#SUNDAY}.
*/
public DayOfMonthCursor(int year, int month, int dayOfMonth, int weekStartDay) {
super(year, month, weekStartDay);
mRow = getRowOf(dayOfMonth);
mColumn = getColumnOf(dayOfMonth);
}
public int getSelectedRow() {
return mRow;
}
public int getSelectedColumn() {
return mColumn;
}
public void setSelectedRowColumn(int row, int col) {
mRow = row;
mColumn = col;
}
public int getSelectedDayOfMonth() {
return getDayAt(mRow, mColumn);
}
/**
* @return 0 if the selection is in the current month, otherwise -1 or +1
* depending on whether the selection is in the first or last row.
*/
public int getSelectedMonthOffset() {
if (isWithinCurrentMonth(mRow, mColumn)) {
return 0;
}
if (mRow == 0) {
return -1;
}
return 1;
}
public void setSelectedDayOfMonth(int dayOfMonth) {
mRow = getRowOf(dayOfMonth);
mColumn = getColumnOf(dayOfMonth);
}
public boolean isSelected(int row, int column) {
return (mRow == row) && (mColumn == column);
}
/**
* Move up one box, potentially flipping to the previous month.
* @return Whether the month was flipped to the previous month
* due to the move.
*/
public boolean up() {
if (isWithinCurrentMonth(mRow - 1, mColumn)) {
// within current month, just move up
mRow--;
return false;
}
// flip back to previous month, same column, first position within month
previousMonth();
mRow = 5;
while(!isWithinCurrentMonth(mRow, mColumn)) {
mRow--;
}
return true;
}
/**
* Move down one box, potentially flipping to the next month.
* @return Whether the month was flipped to the next month
* due to the move.
*/
public boolean down() {
if (isWithinCurrentMonth(mRow + 1, mColumn)) {
// within current month, just move down
mRow++;
return false;
}
// flip to next month, same column, first position within month
nextMonth();
mRow = 0;
while (!isWithinCurrentMonth(mRow, mColumn)) {
mRow++;
}
return true;
}
/**
* Move left one box, potentially flipping to the previous month.
* @return Whether the month was flipped to the previous month
* due to the move.
*/
public boolean left() {
if (mColumn == 0) {
mRow--;
mColumn = 6;
} else {
mColumn--;
}
if (isWithinCurrentMonth(mRow, mColumn)) {
return false;
}
// need to flip to last day of previous month
previousMonth();
int lastDay = getNumberOfDaysInMonth();
mRow = getRowOf(lastDay);
mColumn = getColumnOf(lastDay);
return true;
}
/**
* Move right one box, potentially flipping to the next month.
* @return Whether the month was flipped to the next month
* due to the move.
*/
public boolean right() {
if (mColumn == 6) {
mRow++;
mColumn = 0;
} else {
mColumn++;
}
if (isWithinCurrentMonth(mRow, mColumn)) {
return false;
}
// need to flip to first day of next month
nextMonth();
mRow = 0;
mColumn = 0;
while (!isWithinCurrentMonth(mRow, mColumn)) {
mColumn++;
}
return true;
}
}
+104
View File
@@ -0,0 +1,104 @@
/*
* 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 android.util;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* <p>Various utilities for debugging and logging.</p>
*/
public class DebugUtils {
/**
* <p>Filters objects against the <code>ANDROID_OBJECT_FILTER</code>
* environment variable. This environment variable can filter objects
* based on their class name and attribute values.</p>
*
* <p>Here is the syntax for <code>ANDROID_OBJECT_FILTER</code>:</p>
*
* <p><code>ClassName@attribute1=value1@attribute2=value2...</code></p>
*
* <p>Examples:</p>
* <ul>
* <li>Select TextView instances: <code>TextView</code></li>
* <li>Select TextView instances of text "Loading" and bottom offset of 22:
* <code>TextView@text=Loading.*@bottom=22</code></li>
* </ul>
*
* <p>The class name and the values are regular expressions.</p>
*
* <p>This class is useful for debugging and logging purpose:</p>
* <pre>
* if (DEBUG) {
* if (DebugUtils.isObjectSelected(childView) && LOGV_ENABLED) {
* Log.v(TAG, "Object " + childView + " logged!");
* }
* }
* </pre>
*
* <p><strong>NOTE</strong>: This method is very expensive as it relies
* heavily on regular expressions and reflection. Calls to this method
* should always be stripped out of the release binaries and avoided
* as much as possible in debug mode.</p>
*
* @param object any object to match against the ANDROID_OBJECT_FILTER
* environement variable
* @return true if object is selected by the ANDROID_OBJECT_FILTER
* environment variable, false otherwise
*/
public static boolean isObjectSelected(Object object) {
boolean match = false;
String s = System.getenv("ANDROID_OBJECT_FILTER");
if (s != null && s.length() > 0) {
String[] selectors = s.split("@");
// first selector == class name
if (object.getClass().getSimpleName().matches(selectors[0])) {
// check potential attributes
for (int i = 1; i < selectors.length; i++) {
String[] pair = selectors[i].split("=");
Class<?> klass = object.getClass();
try {
Method declaredMethod = null;
Class<?> parent = klass;
do {
declaredMethod = parent.getDeclaredMethod("get" +
pair[0].substring(0, 1).toUpperCase() +
pair[0].substring(1),
(Class[]) null);
} while ((parent = klass.getSuperclass()) != null &&
declaredMethod == null);
if (declaredMethod != null) {
Object value = declaredMethod
.invoke(object, (Object[])null);
match |= (value != null ?
value.toString() : "null").matches(pair[1]);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
return match;
}
}
@@ -0,0 +1,243 @@
/*
* Copyright (C) 2006 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 android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.os.*;
/**
* A structure describing general information about a display, such as its
* size, density, and font scaling.
* <p>To access the DisplayMetrics members, initialize an object like this:</p>
* <pre> DisplayMetrics metrics = new DisplayMetrics();
* getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
*/
public class DisplayMetrics {
/**
* Standard quantized DPI for low-density screens.
*/
public static final int DENSITY_LOW = 120;
/**
* Standard quantized DPI for medium-density screens.
*/
public static final int DENSITY_MEDIUM = 160;
/**
* Standard quantized DPI for high-density screens.
*/
public static final int DENSITY_HIGH = 240;
/**
* Standard quantized DPI for extra-high-density screens.
*/
public static final int DENSITY_XHIGH = 320;
/**
* The reference density used throughout the system.
*/
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
/**
* The device's density.
* @hide becase eventually this should be able to change while
* running, so shouldn't be a constant.
*/
public static final int DENSITY_DEVICE = getDeviceDensity();
/**
* The absolute width of the display in pixels.
*/
public int widthPixels;
/**
* The absolute height of the display in pixels.
*/
public int heightPixels;
/**
* The logical density of the display. This is a scaling factor for the
* 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 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
* the overall UI in steps based on gross changes in the display dpi. For
* example, a 240x320 screen will have a density of 1 even if its width is
* 1.8", 1.3", etc. However, if the screen resolution is increased to
* 320x480 but the screen size remained 1.5"x2" then the density would be
* increased (probably to 1.5).
*
* @see #DENSITY_DEFAULT
*/
public float density;
/**
* The screen density expressed as dots-per-inch. May be either
* {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
*/
public int densityDpi;
/**
* A scaling factor for fonts displayed on the display. This is the same
* as {@link #density}, except that it may be adjusted in smaller
* increments at runtime based on a user preference for the font size.
*/
public float scaledDensity;
/**
* The exact physical pixels per inch of the screen in the X dimension.
*/
public float xdpi;
/**
* The exact physical pixels per inch of the screen in the Y dimension.
*/
public float ydpi;
public DisplayMetrics() {
}
public void setTo(DisplayMetrics o) {
widthPixels = o.widthPixels;
heightPixels = o.heightPixels;
density = o.density;
densityDpi = o.densityDpi;
scaledDensity = o.scaledDensity;
xdpi = o.xdpi;
ydpi = o.ydpi;
}
public void setToDefaults() {
widthPixels = 0;
heightPixels = 0;
density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
densityDpi = DENSITY_DEVICE;
scaledDensity = density;
xdpi = DENSITY_DEVICE;
ydpi = DENSITY_DEVICE;
}
/**
* Update the display metrics based on the compatibility info and orientation
* NOTE: DO NOT EXPOSE THIS API! It is introducing a circular dependency
* with the higher-level android.res package.
* {@hide}
*/
public void updateMetrics(CompatibilityInfo compatibilityInfo, int orientation,
int screenLayout) {
boolean expandable = compatibilityInfo.isConfiguredExpandable();
boolean largeScreens = compatibilityInfo.isConfiguredLargeScreens();
boolean xlargeScreens = compatibilityInfo.isConfiguredXLargeScreens();
// Note: this assume that configuration is updated before calling
// updateMetrics method.
if (!expandable) {
if ((screenLayout&Configuration.SCREENLAYOUT_COMPAT_NEEDED) == 0) {
expandable = true;
// the current screen size is compatible with non-resizing apps.
compatibilityInfo.setExpandable(true);
} else {
compatibilityInfo.setExpandable(false);
}
}
if (!largeScreens) {
if ((screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
!= Configuration.SCREENLAYOUT_SIZE_LARGE) {
largeScreens = true;
// the current screen size is not large.
compatibilityInfo.setLargeScreens(true);
} else {
compatibilityInfo.setLargeScreens(false);
}
}
if (!xlargeScreens) {
if ((screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
!= Configuration.SCREENLAYOUT_SIZE_XLARGE) {
xlargeScreens = true;
// the current screen size is not large.
compatibilityInfo.setXLargeScreens(true);
} else {
compatibilityInfo.setXLargeScreens(false);
}
}
if (!expandable || (!largeScreens && !xlargeScreens)) {
// This is a larger screen device and the app is not
// compatible with large screens, so diddle it.
// Figure out the compatibility width and height of the screen.
int defaultWidth;
int defaultHeight;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE: {
defaultWidth = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_HEIGHT * density +
0.5f);
defaultHeight = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_WIDTH * density +
0.5f);
break;
}
case Configuration.ORIENTATION_PORTRAIT:
case Configuration.ORIENTATION_SQUARE:
default: {
defaultWidth = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_WIDTH * density +
0.5f);
defaultHeight = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_HEIGHT * density +
0.5f);
break;
}
case Configuration.ORIENTATION_UNDEFINED: {
// don't change
return;
}
}
if (defaultWidth < widthPixels) {
// content/window's x offset in original pixels
widthPixels = defaultWidth;
}
if (defaultHeight < heightPixels) {
heightPixels = defaultHeight;
}
}
if (compatibilityInfo.isScalingRequired()) {
float invertedRatio = compatibilityInfo.applicationInvertedScale;
density *= invertedRatio;
densityDpi = (int)((density*DisplayMetrics.DENSITY_DEFAULT)+.5f);
scaledDensity *= invertedRatio;
xdpi *= invertedRatio;
ydpi *= invertedRatio;
widthPixels = (int) (widthPixels * invertedRatio + 0.5f);
heightPixels = (int) (heightPixels * invertedRatio + 0.5f);
}
}
@Override
public String toString() {
return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
}
private static int getDeviceDensity() {
// qemu.sf.lcd_density can be used to override ro.sf.lcd_density
// when running in the emulator, allowing for dynamic configurations.
// The reason for this is that ro.sf.lcd_density is write-once and is
// set by the init process when it parses build.prop before anything else.
return SystemProperties.getInt("qemu.sf.lcd_density",
SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
}
}
+255
View File
@@ -0,0 +1,255 @@
/*
* 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 android.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Access to the system diagnostic event record. System diagnostic events are
* used to record certain system-level events (such as garbage collection,
* activity manager state, system watchdogs, and other low level activity),
* which may be automatically collected and analyzed during system development.
*
* <p>This is <b>not</b> the main "logcat" debugging log ({@link android.util.Log})!
* These diagnostic events are for system integrators, not application authors.
*
* <p>Events use integer tag codes corresponding to /system/etc/event-log-tags.
* They carry a payload of one or more int, long, or String values. The
* event-log-tags file defines the payload contents for each type code.
*/
public class EventLog {
private static final String TAG = "EventLog";
private static final String TAGS_FILE = "/system/etc/event-log-tags";
private static final String COMMENT_PATTERN = "^\\s*(#.*)?$";
private static final String TAG_PATTERN = "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$";
private static HashMap<String, Integer> sTagCodes = null;
private static HashMap<Integer, String> sTagNames = null;
/** A previously logged event read from the logs. */
public static final class Event {
private final ByteBuffer mBuffer;
// Layout of event log entry received from kernel.
private static final int LENGTH_OFFSET = 0;
private static final int PROCESS_OFFSET = 4;
private static final int THREAD_OFFSET = 8;
private static final int SECONDS_OFFSET = 12;
private static final int NANOSECONDS_OFFSET = 16;
private static final int PAYLOAD_START = 20;
private static final int TAG_OFFSET = 20;
private static final int DATA_START = 24;
// Value types
private static final byte INT_TYPE = 0;
private static final byte LONG_TYPE = 1;
private static final byte STRING_TYPE = 2;
private static final byte LIST_TYPE = 3;
/** @param data containing event, read from the system */
/*package*/ Event(byte[] data) {
mBuffer = ByteBuffer.wrap(data);
mBuffer.order(ByteOrder.nativeOrder());
}
/** @return the process ID which wrote the log entry */
public int getProcessId() {
return mBuffer.getInt(PROCESS_OFFSET);
}
/** @return the thread ID which wrote the log entry */
public int getThreadId() {
return mBuffer.getInt(THREAD_OFFSET);
}
/** @return the wall clock time when the entry was written */
public long getTimeNanos() {
return mBuffer.getInt(SECONDS_OFFSET) * 1000000000l
+ mBuffer.getInt(NANOSECONDS_OFFSET);
}
/** @return the type tag code of the entry */
public int getTag() {
return mBuffer.getInt(TAG_OFFSET);
}
/** @return one of Integer, Long, String, null, or Object[] of same. */
public synchronized Object getData() {
try {
mBuffer.limit(PAYLOAD_START + mBuffer.getShort(LENGTH_OFFSET));
mBuffer.position(DATA_START); // Just after the tag.
return decodeObject();
} catch (IllegalArgumentException e) {
Log.wtf(TAG, "Illegal entry payload: tag=" + getTag(), e);
return null;
} catch (BufferUnderflowException e) {
Log.wtf(TAG, "Truncated entry payload: tag=" + getTag(), e);
return null;
}
}
/** @return the loggable item at the current position in mBuffer. */
private Object decodeObject() {
byte type = mBuffer.get();
switch (type) {
case INT_TYPE:
return (Integer) mBuffer.getInt();
case LONG_TYPE:
return (Long) mBuffer.getLong();
case STRING_TYPE:
try {
int length = mBuffer.getInt();
int start = mBuffer.position();
mBuffer.position(start + length);
return new String(mBuffer.array(), start, length, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 is not supported", e);
return null;
}
case LIST_TYPE:
int length = mBuffer.get();
if (length < 0) length += 256; // treat as signed byte
Object[] array = new Object[length];
for (int i = 0; i < length; ++i) array[i] = decodeObject();
return array;
default:
throw new IllegalArgumentException("Unknown entry type: " + type);
}
}
}
// We assume that the native methods deal with any concurrency issues.
/**
* Record an event log message.
* @param tag The event type tag code
* @param value A value to log
* @return The number of bytes written
*/
public static native int writeEvent(int tag, int value);
/**
* Record an event log message.
* @param tag The event type tag code
* @param value A value to log
* @return The number of bytes written
*/
public static native int writeEvent(int tag, long value);
/**
* Record an event log message.
* @param tag The event type tag code
* @param str A value to log
* @return The number of bytes written
*/
public static native int writeEvent(int tag, String str);
/**
* Record an event log message.
* @param tag The event type tag code
* @param list A list of values to log
* @return The number of bytes written
*/
public static native int writeEvent(int tag, Object... list);
/**
* Read events from the log, filtered by type.
* @param tags to search for
* @param output container to add events into
* @throws IOException if something goes wrong reading events
*/
public static native void readEvents(int[] tags, Collection<Event> output)
throws IOException;
/**
* Get the name associated with an event type tag code.
* @param tag code to look up
* @return the name of the tag, or null if no tag has that number
*/
public static String getTagName(int tag) {
readTagsFile();
return sTagNames.get(tag);
}
/**
* Get the event type tag code associated with an event name.
* @param name of event to look up
* @return the tag code, or -1 if no tag has that name
*/
public static int getTagCode(String name) {
readTagsFile();
Integer code = sTagCodes.get(name);
return code != null ? code : -1;
}
/**
* Read TAGS_FILE, populating sTagCodes and sTagNames, if not already done.
*/
private static synchronized void readTagsFile() {
if (sTagCodes != null && sTagNames != null) return;
sTagCodes = new HashMap<String, Integer>();
sTagNames = new HashMap<Integer, String>();
Pattern comment = Pattern.compile(COMMENT_PATTERN);
Pattern tag = Pattern.compile(TAG_PATTERN);
BufferedReader reader = null;
String line;
try {
reader = new BufferedReader(new FileReader(TAGS_FILE), 256);
while ((line = reader.readLine()) != null) {
if (comment.matcher(line).matches()) continue;
Matcher m = tag.matcher(line);
if (!m.matches()) {
Log.wtf(TAG, "Bad entry in " + TAGS_FILE + ": " + line);
continue;
}
try {
int num = Integer.parseInt(m.group(1));
String name = m.group(2);
sTagCodes.put(name, num);
sTagNames.put(num, name);
} catch (NumberFormatException e) {
Log.wtf(TAG, "Error in " + TAGS_FILE + ": " + line, e);
}
}
} catch (IOException e) {
Log.wtf(TAG, "Error reading " + TAGS_FILE, e);
// Leave the maps existing but unpopulated
} finally {
try { if (reader != null) reader.close(); } catch (IOException e) {}
}
}
}
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2008 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 android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @deprecated This class is no longer functional.
* Use {@link android.util.EventLog} instead.
*/
@Deprecated
public class EventLogTags {
public static class Description {
public final int mTag;
public final String mName;
Description(int tag, String name) {
mTag = tag;
mName = name;
}
}
public EventLogTags() throws IOException {}
public EventLogTags(BufferedReader input) throws IOException {}
public Description get(String name) { return null; }
public Description get(int tag) { return null; }
}
@@ -0,0 +1,86 @@
/*
* 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;
/**
* @hide
*/
class FinitePool<T extends Poolable<T>> implements Pool<T> {
/**
* Factory used to create new pool objects
*/
private final PoolableManager<T> mManager;
/**
* Maximum number of objects in the pool
*/
private final int mLimit;
/**
* If true, mLimit is ignored
*/
private final boolean mInfinite;
/**
* Next object to acquire
*/
private T mRoot;
/**
* Number of objects in the pool
*/
private int mPoolCount;
FinitePool(PoolableManager<T> manager) {
mManager = manager;
mLimit = 0;
mInfinite = true;
}
FinitePool(PoolableManager<T> manager, int limit) {
if (limit <= 0) throw new IllegalArgumentException("The pool limit must be > 0");
mManager = manager;
mLimit = limit;
mInfinite = false;
}
public T acquire() {
T element;
if (mRoot != null) {
element = mRoot;
mRoot = element.getNextPoolable();
mPoolCount--;
} else {
element = mManager.newInstance();
}
if (element != null) {
element.setNextPoolable(null);
mManager.onAcquired(element);
}
return element;
}
public void release(T element) {
if (mInfinite || mPoolCount < mLimit) {
mPoolCount++;
element.setNextPoolable(mRoot);
mRoot = element;
}
mManager.onReleased(element);
}
}
@@ -0,0 +1,74 @@
/*
* 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 android.util;
/**
* Math routines similar to those found in {@link java.lang.Math}. Performs
* computations on {@code float} values directly without incurring the overhead
* of conversions to and from {@code double}.
*
* <p>On one platform, {@code FloatMath.sqrt(100)} executes in one third of the
* time required by {@code java.lang.Math.sqrt(100)}.</p>
*/
public class FloatMath {
/** Prevents instantiation. */
private FloatMath() {}
/**
* Returns the float conversion of the most positive (i.e. closest to
* positive infinity) integer value which is less than the argument.
*
* @param value to be converted
* @return the floor of value
*/
public static native float floor(float value);
/**
* Returns the float conversion of the most negative (i.e. closest to
* negative infinity) integer value which is greater than the argument.
*
* @param value to be converted
* @return the ceiling of value
*/
public static native float ceil(float value);
/**
* Returns the closest float approximation of the sine of the argument.
*
* @param angle to compute the cosine of, in radians
* @return the sine of angle
*/
public static native float sin(float angle);
/**
* Returns the closest float approximation of the cosine of the argument.
*
* @param angle to compute the cosine of, in radians
* @return the cosine of angle
*/
public static native float cos(float angle);
/**
* Returns the closest float approximation of the square root of the
* argument.
*
* @param value to compute sqrt of
* @return the square root of value
*/
public static native float sqrt(float value);
}
+330
View File
@@ -0,0 +1,330 @@
/*
* Copyright (C) 2006 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 com.android.internal.os.RuntimeInit;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* API for sending log output.
*
* <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
* methods.
*
* <p>The order in terms of verbosity, from least to most is
* ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled
* into an application except during development. Debug logs are compiled
* in but stripped at runtime. Error, warning and info logs are always kept.
*
* <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
* in your class:
*
* <pre>private static final String TAG = "MyActivity";</pre>
*
* and use that in subsequent calls to the log methods.
* </p>
*
* <p><b>Tip:</b> Don't forget that when you make a call like
* <pre>Log.v(TAG, "index=" + i);</pre>
* that when you're building the string to pass into Log.d, the compiler uses a
* StringBuilder and at least three allocations occur: the StringBuilder
* itself, the buffer, and the String object. Realistically, there is also
* another buffer allocation and copy, and even more pressure on the gc.
* That means that if your log message is filtered out, you might be doing
* significant work and incurring significant overhead.
*/
public final class Log {
/**
* Priority constant for the println method; use Log.v.
*/
public static final int VERBOSE = 2;
/**
* Priority constant for the println method; use Log.d.
*/
public static final int DEBUG = 3;
/**
* Priority constant for the println method; use Log.i.
*/
public static final int INFO = 4;
/**
* Priority constant for the println method; use Log.w.
*/
public static final int WARN = 5;
/**
* Priority constant for the println method; use Log.e.
*/
public static final int ERROR = 6;
/**
* Priority constant for the println method.
*/
public static final int ASSERT = 7;
/**
* Exception class used to capture a stack trace in {@link #wtf()}.
*/
private static class TerribleFailure extends Exception {
TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
}
/**
* Interface to handle terrible failures from {@link #wtf()}.
*
* @hide
*/
public interface TerribleFailureHandler {
void onTerribleFailure(String tag, TerribleFailure what);
}
private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
public void onTerribleFailure(String tag, TerribleFailure what) {
RuntimeInit.wtf(tag, what);
}
};
private Log() {
}
/**
* Send a {@link #VERBOSE} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int v(String tag, String msg) {
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
}
/**
* Send a {@link #VERBOSE} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int v(String tag, String msg, Throwable tr) {
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
}
/**
* Send a {@link #DEBUG} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int d(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
}
/**
* Send a {@link #DEBUG} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int d(String tag, String msg, Throwable tr) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
}
/**
* Send an {@link #INFO} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int i(String tag, String msg) {
return println_native(LOG_ID_MAIN, INFO, tag, msg);
}
/**
* Send a {@link #INFO} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int i(String tag, String msg, Throwable tr) {
return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
}
/**
* Send a {@link #WARN} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int w(String tag, String msg) {
return println_native(LOG_ID_MAIN, WARN, tag, msg);
}
/**
* Send a {@link #WARN} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int w(String tag, String msg, Throwable tr) {
return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
}
/**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
*
* The default level of any tag is set to INFO. This means that any level above and including
* INFO will be logged. Before you make any calls to a logging method you should check to see
* if your tag should be logged. You can change the default level by setting a system property:
* 'setprop log.tag.&lt;YOUR_LOG_TAG> &lt;LEVEL>'
* Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
* turn off all logging for your tag. You can also create a local.prop file that with the
* following in it:
* 'log.tag.&lt;YOUR_LOG_TAG>=&lt;LEVEL>'
* and place that in /data/local.prop.
*
* @param tag The tag to check.
* @param level The level to check.
* @return Whether or not that this is allowed to be logged.
* @throws IllegalArgumentException is thrown if the tag.length() > 23.
*/
public static native boolean isLoggable(String tag, int level);
/*
* Send a {@link #WARN} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param tr An exception to log
*/
public static int w(String tag, Throwable tr) {
return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
}
/**
* Send an {@link #ERROR} log message.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
*/
public static int e(String tag, String msg) {
return println_native(LOG_ID_MAIN, ERROR, tag, msg);
}
/**
* Send a {@link #ERROR} log message and log the exception.
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @param tr An exception to log
*/
public static int e(String tag, String msg, Throwable tr) {
return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
}
/**
* What a Terrible Failure: Report a condition that should never happen.
* The error will always be logged at level ASSERT with the call stack.
* Depending on system configuration, a report may be added to the
* {@link android.os.DropBoxManager} and/or the process may be terminated
* immediately with an error dialog.
* @param tag Used to identify the source of a log message.
* @param msg The message you would like logged.
*/
public static int wtf(String tag, String msg) {
return wtf(tag, msg, null);
}
/**
* What a Terrible Failure: Report an exception that should never happen.
* Similar to {@link #wtf(String, String)}, with an exception to log.
* @param tag Used to identify the source of a log message.
* @param tr An exception to log.
*/
public static int wtf(String tag, Throwable tr) {
return wtf(tag, tr.getMessage(), tr);
}
/**
* What a Terrible Failure: Report an exception that should never happen.
* Similar to {@link #wtf(String, Throwable)}, with a message as well.
* @param tag Used to identify the source of a log message.
* @param msg The message you would like logged.
* @param tr An exception to log. May be null.
*/
public static int wtf(String tag, String msg, Throwable tr) {
TerribleFailure what = new TerribleFailure(msg, tr);
int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, getStackTraceString(tr));
sWtfHandler.onTerribleFailure(tag, what);
return bytes;
}
/**
* Sets the terrible failure handler, for testing.
*
* @return the old handler
*
* @hide
*/
public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
if (handler == null) {
throw new NullPointerException("handler == null");
}
TerribleFailureHandler oldHandler = sWtfHandler;
sWtfHandler = handler;
return oldHandler;
}
/**
* Handy function to get a loggable stack trace from a Throwable
* @param tr An exception to log
*/
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return "";
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
return sw.toString();
}
/**
* Low-level logging call.
* @param priority The priority/type of this log message
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param msg The message you would like logged.
* @return The number of bytes written.
*/
public static int println(int priority, String tag, String msg) {
return println_native(LOG_ID_MAIN, priority, tag, msg);
}
/** @hide */ public static final int LOG_ID_MAIN = 0;
/** @hide */ public static final int LOG_ID_RADIO = 1;
/** @hide */ public static final int LOG_ID_EVENTS = 2;
/** @hide */ public static final int LOG_ID_SYSTEM = 3;
/** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg);
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2006 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;
/**
* Implementation of a {@link android.util.Printer} that sends its output
* to the system log.
*/
public class LogPrinter implements Printer {
private final int mPriority;
private final String mTag;
private final int mBuffer;
/**
* Create a new Printer that sends to the log with the given priority
* and tag.
*
* @param priority The desired log priority:
* {@link android.util.Log#VERBOSE Log.VERBOSE},
* {@link android.util.Log#DEBUG Log.DEBUG},
* {@link android.util.Log#INFO Log.INFO},
* {@link android.util.Log#WARN Log.WARN}, or
* {@link android.util.Log#ERROR Log.ERROR}.
* @param tag A string tag to associate with each printed log statement.
*/
public LogPrinter(int priority, String tag) {
mPriority = priority;
mTag = tag;
mBuffer = Log.LOG_ID_MAIN;
}
/**
* @hide
* Same as above, but buffer is one of the LOG_ID_ constants from android.util.Log.
*/
public LogPrinter(int priority, String tag, int buffer) {
mPriority = priority;
mTag = tag;
mBuffer = buffer;
}
public void println(String x) {
Log.println_native(mBuffer, mPriority, mTag, x);
}
}
@@ -0,0 +1,364 @@
/*
* 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 com.android.internal.util.ArrayUtils;
/**
* SparseArrays map longs to Objects. Unlike a normal array of Objects,
* there can be gaps in the indices. It is intended to be more efficient
* than using a HashMap to map Longs to Objects.
*
* @hide
*/
public class LongSparseArray<E> {
private static final Object DELETED = new Object();
private boolean mGarbage = false;
/**
* Creates a new SparseArray containing no mappings.
*/
public LongSparseArray() {
this(10);
}
/**
* Creates a new SparseArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings.
*/
public LongSparseArray(int initialCapacity) {
initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
mKeys = new long[initialCapacity];
mValues = new Object[initialCapacity];
mSize = 0;
}
/**
* @return A copy of all keys contained in the sparse array.
*/
public long[] getKeys() {
int length = mKeys.length;
long[] result = new long[length];
System.arraycopy(mKeys, 0, result, 0, length);
return result;
}
/**
* Sets all supplied keys to the given unique value.
* @param keys Keys to set
* @param uniqueValue Value to set all supplied keys to
*/
public void setValues(long[] keys, E uniqueValue) {
int length = keys.length;
for (int i = 0; i < length; i++) {
put(keys[i], uniqueValue);
}
}
/**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
*/
public E get(long key) {
return get(key, null);
}
/**
* Gets the Object mapped from the specified key, or the specified Object
* if no such mapping has been made.
*/
public E get(long key, E valueIfKeyNotFound) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}
/**
* Removes the mapping from the specified key, if there was any.
*/
public void delete(long key) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
mValues[i] = DELETED;
mGarbage = true;
}
}
}
/**
* Alias for {@link #delete(long)}.
*/
public void remove(long key) {
delete(key);
}
private void gc() {
// Log.e("SparseArray", "gc start with " + mSize);
int n = mSize;
int o = 0;
long[] keys = mKeys;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
Object val = values[i];
if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
}
o++;
}
}
mGarbage = false;
mSize = o;
// Log.e("SparseArray", "gc end with " + mSize);
}
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(long key, E value) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
mValues[i] = value;
} else {
i = ~i;
if (i < mSize && mValues[i] == DELETED) {
mKeys[i] = key;
mValues[i] = value;
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
// Search again because indices may have changed.
i = ~binarySearch(mKeys, 0, mSize, key);
}
if (mSize >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(mSize + 1);
long[] nkeys = new long[n];
Object[] nvalues = new Object[n];
// Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
if (mSize - i != 0) {
// Log.e("SparseArray", "move " + (mSize - i));
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
}
mKeys[i] = key;
mValues[i] = value;
mSize++;
}
}
/**
* Returns the number of key-value mappings that this SparseArray
* currently stores.
*/
public int size() {
if (mGarbage) {
gc();
}
return mSize;
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public long keyAt(int index) {
if (mGarbage) {
gc();
}
return mKeys[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public E valueAt(int index) {
if (mGarbage) {
gc();
}
return (E) mValues[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, sets a new
* value for the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
gc();
}
mValues[index] = value;
}
/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
*/
public int indexOfKey(long key) {
if (mGarbage) {
gc();
}
return binarySearch(mKeys, 0, mSize, key);
}
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
*/
public int indexOfValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
/**
* Removes all key-value mappings from this SparseArray.
*/
public void clear() {
int n = mSize;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
values[i] = null;
}
mSize = 0;
mGarbage = false;
}
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
public void append(long key, E value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
}
int pos = mSize;
if (pos >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(pos + 1);
long[] nkeys = new long[n];
Object[] nvalues = new Object[n];
// Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
mKeys[pos] = key;
mValues[pos] = value;
mSize = pos + 1;
}
private static int binarySearch(long[] a, int start, int len, long key) {
int high = start + len, low = start - 1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (a[guess] < key)
low = guess;
else
high = guess;
}
if (high == start + len)
return ~(start + len);
else if (a[high] == key)
return high;
else
return ~high;
}
private void checkIntegrity() {
for (int i = 1; i < mSize; i++) {
if (mKeys[i] <= mKeys[i - 1]) {
for (int j = 0; j < mSize; j++) {
Log.e("FAIL", j + ": " + mKeys[j] + " -> " + mValues[j]);
}
throw new RuntimeException();
}
}
}
private long[] mKeys;
private Object[] mValues;
private int mSize;
}
+176
View File
@@ -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);
}
}
@@ -0,0 +1,213 @@
/*
* 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 android.util;
import java.util.Calendar;
/**
* Helps answer common questions that come up when displaying a month in a
* 6 row calendar grid format.
*
* Not thread safe.
*/
public class MonthDisplayHelper {
// display pref
private final int mWeekStartDay;
// holds current month, year, helps compute display
private Calendar mCalendar;
// cached computed stuff that helps with display
private int mNumDaysInMonth;
private int mNumDaysInPrevMonth;
private int mOffset;
/**
* @param year The year.
* @param month The month.
* @param weekStartDay What day of the week the week should start.
*/
public MonthDisplayHelper(int year, int month, int weekStartDay) {
if (weekStartDay < Calendar.SUNDAY || weekStartDay > Calendar.SATURDAY) {
throw new IllegalArgumentException();
}
mWeekStartDay = weekStartDay;
mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, 1);
mCalendar.set(Calendar.HOUR_OF_DAY, 0);
mCalendar.set(Calendar.MINUTE, 0);
mCalendar.set(Calendar.SECOND, 0);
mCalendar.getTimeInMillis();
recalculate();
}
public MonthDisplayHelper(int year, int month) {
this(year, month, Calendar.SUNDAY);
}
public int getYear() {
return mCalendar.get(Calendar.YEAR);
}
public int getMonth() {
return mCalendar.get(Calendar.MONTH);
}
public int getWeekStartDay() {
return mWeekStartDay;
}
/**
* @return The first day of the month using a constants such as
* {@link java.util.Calendar#SUNDAY}.
*/
public int getFirstDayOfMonth() {
return mCalendar.get(Calendar.DAY_OF_WEEK);
}
/**
* @return The number of days in the month.
*/
public int getNumberOfDaysInMonth() {
return mNumDaysInMonth;
}
/**
* @return The offset from displaying everything starting on the very first
* box. For example, if the calendar is set to display the first day of
* the week as Sunday, and the month starts on a Wednesday, the offset is 3.
*/
public int getOffset() {
return mOffset;
}
/**
* @param row Which row (0-5).
* @return the digits of the month to display in one
* of the 6 rows of a calendar month display.
*/
public int[] getDigitsForRow(int row) {
if (row < 0 || row > 5) {
throw new IllegalArgumentException("row " + row
+ " out of range (0-5)");
}
int [] result = new int[7];
for (int column = 0; column < 7; column++) {
result[column] = getDayAt(row, column);
}
return result;
}
/**
* @param row The row, 0-5, starting from the top.
* @param column The column, 0-6, starting from the left.
* @return The day at a particular row, column
*/
public int getDayAt(int row, int column) {
if (row == 0 && column < mOffset) {
return mNumDaysInPrevMonth + column - mOffset + 1;
}
int day = 7 * row + column - mOffset + 1;
return (day > mNumDaysInMonth) ?
day - mNumDaysInMonth : day;
}
/**
* @return Which row day is in.
*/
public int getRowOf(int day) {
return (day + mOffset - 1) / 7;
}
/**
* @return Which column day is in.
*/
public int getColumnOf(int day) {
return (day + mOffset - 1) % 7;
}
/**
* Decrement the month.
*/
public void previousMonth() {
mCalendar.add(Calendar.MONTH, -1);
recalculate();
}
/**
* Increment the month.
*/
public void nextMonth() {
mCalendar.add(Calendar.MONTH, 1);
recalculate();
}
/**
* @return Whether the row and column fall within the month.
*/
public boolean isWithinCurrentMonth(int row, int column) {
if (row < 0 || column < 0 || row > 5 || column > 6) {
return false;
}
if (row == 0 && column < mOffset) {
return false;
}
int day = 7 * row + column - mOffset + 1;
if (day > mNumDaysInMonth) {
return false;
}
return true;
}
// helper method that recalculates cached values based on current month / year
private void recalculate() {
mNumDaysInMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
mCalendar.add(Calendar.MONTH, -1);
mNumDaysInPrevMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
mCalendar.add(Calendar.MONTH, 1);
int firstDayOfMonth = getFirstDayOfMonth();
int offset = firstDayOfMonth - mWeekStartDay;
if (offset < 0) {
offset += 7;
}
mOffset = offset;
}
}
+76
View File
@@ -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);
}
}
+232
View File
@@ -0,0 +1,232 @@
/*
* 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 android.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Commonly used regular expression patterns.
*/
public class Patterns {
/**
* Regular expression to match all IANA top-level domains.
* List accurate as of 2010/02/05. List taken from:
* http://data.iana.org/TLD/tlds-alpha-by-domain.txt
* This pattern is auto-generated by frameworks/base/common/tools/make-iana-tld-pattern.py
*/
public static final String TOP_LEVEL_DOMAIN_STR =
"((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
+ "|(biz|b[abdefghijmnorstvwyz])"
+ "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
+ "|d[ejkmoz]"
+ "|(edu|e[cegrstu])"
+ "|f[ijkmor]"
+ "|(gov|g[abdefghilmnpqrstuwy])"
+ "|h[kmnrtu]"
+ "|(info|int|i[delmnoqrst])"
+ "|(jobs|j[emop])"
+ "|k[eghimnprwyz]"
+ "|l[abcikrstuvy]"
+ "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
+ "|(name|net|n[acefgilopruz])"
+ "|(org|om)"
+ "|(pro|p[aefghklmnrstwy])"
+ "|qa"
+ "|r[eosuw]"
+ "|s[abcdeghijklmnortuvyz]"
+ "|(tel|travel|t[cdfghjklmnoprtvwz])"
+ "|u[agksyz]"
+ "|v[aceginu]"
+ "|w[fs]"
+ "|(xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-80akhbyknj4f|xn\\-\\-9t4b11yi5a|xn\\-\\-deba0ad|xn\\-\\-g6w251d|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-zckzah)"
+ "|y[etu]"
+ "|z[amw])";
/**
* Regular expression pattern to match all IANA top-level domains.
*/
public static final Pattern TOP_LEVEL_DOMAIN =
Pattern.compile(TOP_LEVEL_DOMAIN_STR);
/**
* Regular expression to match all IANA top-level domains for WEB_URL.
* List accurate as of 2010/02/05. List taken from:
* http://data.iana.org/TLD/tlds-alpha-by-domain.txt
* This pattern is auto-generated by frameworks/base/common/tools/make-iana-tld-pattern.py
*/
public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
"(?:"
+ "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
+ "|(?:biz|b[abdefghijmnorstvwyz])"
+ "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
+ "|d[ejkmoz]"
+ "|(?:edu|e[cegrstu])"
+ "|f[ijkmor]"
+ "|(?:gov|g[abdefghilmnpqrstuwy])"
+ "|h[kmnrtu]"
+ "|(?:info|int|i[delmnoqrst])"
+ "|(?:jobs|j[emop])"
+ "|k[eghimnprwyz]"
+ "|l[abcikrstuvy]"
+ "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
+ "|(?:name|net|n[acefgilopruz])"
+ "|(?:org|om)"
+ "|(?:pro|p[aefghklmnrstwy])"
+ "|qa"
+ "|r[eosuw]"
+ "|s[abcdeghijklmnortuvyz]"
+ "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
+ "|u[agksyz]"
+ "|v[aceginu]"
+ "|w[fs]"
+ "|(?:xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-80akhbyknj4f|xn\\-\\-9t4b11yi5a|xn\\-\\-deba0ad|xn\\-\\-g6w251d|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-zckzah)"
+ "|y[etu]"
+ "|z[amw]))";
/**
* Good characters for Internationalized Resource Identifiers (IRI).
* This comprises most common used Unicode characters allowed in IRI
* as detailed in RFC 3987.
* Specifically, those two byte Unicode characters are not included.
*/
public static final String GOOD_IRI_CHAR =
"a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
/**
* Regular expression pattern to match most part of RFC 3987
* Internationalized URLs, aka IRIs. Commonly used Unicode characters are
* added.
*/
public static final Pattern WEB_URL = Pattern.compile(
"((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
+ "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
+ "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
+ "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+" // named host
+ TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
+ "|(?:(?:25[0-5]|2[0-4]" // or ip address
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
+ "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9])))"
+ "(?:\\:\\d{1,5})?)" // plus option port number
+ "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
+ "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
+ "(?:\\b|$)"); // and finally, a word boundary or end of
// input. This is to stop foo.sure from
// matching as foo.su
public static final Pattern IP_ADDRESS
= Pattern.compile(
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9]))");
public static final Pattern DOMAIN_NAME
= Pattern.compile(
"(((([" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]*)*[" + GOOD_IRI_CHAR + "]\\.)+"
+ TOP_LEVEL_DOMAIN + ")|"
+ IP_ADDRESS + ")");
public static final Pattern EMAIL_ADDRESS
= Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
/**
* This pattern is intended for searching for things that look like they
* might be phone numbers in arbitrary text, not for validating whether
* something is in fact a phone number. It will miss many things that
* are legitimate phone numbers.
*
* <p> The pattern matches the following:
* <ul>
* <li>Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes
* may follow.
* <li>Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
* <li>A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
* </ul>
*/
public static final Pattern PHONE
= Pattern.compile( // sdd = space, dot, or dash
"(\\+[0-9]+[\\- \\.]*)?" // +<digits><sdd>*
+ "(\\([0-9]+\\)[\\- \\.]*)?" // (<digits>)<sdd>*
+ "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit>
/**
* Convenience method to take all of the non-null matching groups in a
* regex Matcher and return them as a concatenated string.
*
* @param matcher The Matcher object from which grouped text will
* be extracted
*
* @return A String comprising all of the non-null matched
* groups concatenated together
*/
public static final String concatGroups(Matcher matcher) {
StringBuilder b = new StringBuilder();
final int numGroups = matcher.groupCount();
for (int i = 1; i <= numGroups; i++) {
String s = matcher.group(i);
System.err.println("Group(" + i + ") : " + s);
if (s != null) {
b.append(s);
}
}
return b.toString();
}
/**
* Convenience method to return only the digits and plus signs
* in the matching string.
*
* @param matcher The Matcher object from which digits and plus will
* be extracted
*
* @return A String comprising all of the digits and plus in
* the match
*/
public static final String digitsAndPlusOnly(Matcher matcher) {
StringBuilder buffer = new StringBuilder();
String matchingRegion = matcher.group();
for (int i = 0, size = matchingRegion.length(); i < size; i++) {
char character = matchingRegion.charAt(i);
if (character == '+' || Character.isDigit(character)) {
buffer.append(character);
}
}
return buffer.toString();
}
/**
* Do not create this static utility class.
*/
private Patterns() {}
}
+25
View File
@@ -0,0 +1,25 @@
/*
* 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;
/**
* @hide
*/
public interface Pool<T extends Poolable<T>> {
public abstract T acquire();
public abstract void release(T element);
}
+25
View File
@@ -0,0 +1,25 @@
/*
* 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;
/**
* @hide
*/
public interface Poolable<T> {
void setNextPoolable(T element);
T getNextPoolable();
}
@@ -0,0 +1,27 @@
/*
* 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;
/**
* @hide
*/
public interface PoolableManager<T extends Poolable<T>> {
T newInstance();
void onAcquired(T element);
void onReleased(T element);
}
+41
View File
@@ -0,0 +1,41 @@
/*
* 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;
/**
* @hide
*/
public class Pools {
private Pools() {
}
public static <T extends Poolable<T>> Pool<T> simplePool(PoolableManager<T> manager) {
return new FinitePool<T>(manager);
}
public static <T extends Poolable<T>> Pool<T> finitePool(PoolableManager<T> manager, int limit) {
return new FinitePool<T>(manager, limit);
}
public static <T extends Poolable<T>> Pool<T> synchronizedPool(Pool<T> pool) {
return new SynchronizedPool<T>(pool);
}
public static <T extends Poolable<T>> Pool<T> synchronizedPool(Pool<T> pool, Object lock) {
return new SynchronizedPool<T>(pool, lock);
}
}
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2006 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.io.PrintStream;
/**
* Implementation of a {@link android.util.Printer} that sends its output
* to a {@link java.io.PrintStream}.
*/
public class PrintStreamPrinter implements Printer {
private final PrintStream mPS;
/**
* Create a new Printer that sends to a PrintWriter object.
*
* @param pw The PrintWriter where you would like output to go.
*/
public PrintStreamPrinter(PrintStream pw) {
mPS = pw;
}
public void println(String x) {
mPS.println(x);
}
}
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2006 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.io.PrintWriter;
/**
* Implementation of a {@link android.util.Printer} that sends its output
* to a {@link java.io.PrintWriter}.
*/
public class PrintWriterPrinter implements Printer {
private final PrintWriter mPW;
/**
* Create a new Printer that sends to a PrintWriter object.
*
* @param pw The PrintWriter where you would like output to go.
*/
public PrintWriterPrinter(PrintWriter pw) {
mPW = pw;
}
public void println(String x) {
mPW.println(x);
}
}
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2006 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;
/**
* Simple interface for printing text, allowing redirection to various
* targets. Standard implementations are {@link android.util.LogPrinter},
* {@link android.util.StringBuilderPrinter}, and
* {@link android.util.PrintWriterPrinter}.
*/
public interface Printer {
/**
* Write a line of text to the output. There is no need to terminate
* the given string with a newline.
*/
void println(String x);
}
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2006 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 com.android.internal.os.RuntimeInit;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* @hide
*/
public final class Slog {
private Slog() {
}
public static int v(String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.VERBOSE, tag, msg);
}
public static int v(String tag, String msg, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.VERBOSE, tag,
msg + '\n' + Log.getStackTraceString(tr));
}
public static int d(String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.DEBUG, tag, msg);
}
public static int d(String tag, String msg, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.DEBUG, tag,
msg + '\n' + Log.getStackTraceString(tr));
}
public static int i(String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.INFO, tag, msg);
}
public static int i(String tag, String msg, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.INFO, tag,
msg + '\n' + Log.getStackTraceString(tr));
}
public static int w(String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, msg);
}
public static int w(String tag, String msg, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag,
msg + '\n' + Log.getStackTraceString(tr));
}
public static int w(String tag, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, Log.getStackTraceString(tr));
}
public static int e(String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.ERROR, tag, msg);
}
public static int e(String tag, String msg, Throwable tr) {
return Log.println_native(Log.LOG_ID_SYSTEM, Log.ERROR, tag,
msg + '\n' + Log.getStackTraceString(tr));
}
public static int println(int priority, String tag, String msg) {
return Log.println_native(Log.LOG_ID_SYSTEM, priority, tag, msg);
}
}
@@ -0,0 +1,351 @@
/*
* Copyright (C) 2006 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 com.android.internal.util.ArrayUtils;
/**
* SparseArrays map integers to Objects. Unlike a normal array of Objects,
* there can be gaps in the indices. It is intended to be more efficient
* than using a HashMap to map Integers to Objects.
*/
public class SparseArray<E> {
private static final Object DELETED = new Object();
private boolean mGarbage = false;
/**
* Creates a new SparseArray containing no mappings.
*/
public SparseArray() {
this(10);
}
/**
* Creates a new SparseArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings.
*/
public SparseArray(int initialCapacity) {
initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
mKeys = new int[initialCapacity];
mValues = new Object[initialCapacity];
mSize = 0;
}
/**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
*/
public E get(int key) {
return get(key, null);
}
/**
* Gets the Object mapped from the specified key, or the specified Object
* if no such mapping has been made.
*/
public E get(int key, E valueIfKeyNotFound) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}
/**
* Removes the mapping from the specified key, if there was any.
*/
public void delete(int key) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
mValues[i] = DELETED;
mGarbage = true;
}
}
}
/**
* Alias for {@link #delete(int)}.
*/
public void remove(int key) {
delete(key);
}
/**
* Removes the mapping at the specified index.
* @hide
*/
public void removeAt(int index) {
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
}
}
private void gc() {
// Log.e("SparseArray", "gc start with " + mSize);
int n = mSize;
int o = 0;
int[] keys = mKeys;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
Object val = values[i];
if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
}
o++;
}
}
mGarbage = false;
mSize = o;
// Log.e("SparseArray", "gc end with " + mSize);
}
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(int key, E value) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
mValues[i] = value;
} else {
i = ~i;
if (i < mSize && mValues[i] == DELETED) {
mKeys[i] = key;
mValues[i] = value;
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
// Search again because indices may have changed.
i = ~binarySearch(mKeys, 0, mSize, key);
}
if (mSize >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(mSize + 1);
int[] nkeys = new int[n];
Object[] nvalues = new Object[n];
// Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
if (mSize - i != 0) {
// Log.e("SparseArray", "move " + (mSize - i));
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
}
mKeys[i] = key;
mValues[i] = value;
mSize++;
}
}
/**
* Returns the number of key-value mappings that this SparseArray
* currently stores.
*/
public int size() {
if (mGarbage) {
gc();
}
return mSize;
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public int keyAt(int index) {
if (mGarbage) {
gc();
}
return mKeys[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public E valueAt(int index) {
if (mGarbage) {
gc();
}
return (E) mValues[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, sets a new
* value for the <code>index</code>th key-value mapping that this
* SparseArray stores.
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
gc();
}
mValues[index] = value;
}
/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
*/
public int indexOfKey(int key) {
if (mGarbage) {
gc();
}
return binarySearch(mKeys, 0, mSize, key);
}
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
*/
public int indexOfValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
/**
* Removes all key-value mappings from this SparseArray.
*/
public void clear() {
int n = mSize;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
values[i] = null;
}
mSize = 0;
mGarbage = false;
}
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
public void append(int key, E value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
}
int pos = mSize;
if (pos >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(pos + 1);
int[] nkeys = new int[n];
Object[] nvalues = new Object[n];
// Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
mKeys[pos] = key;
mValues[pos] = value;
mSize = pos + 1;
}
private static int binarySearch(int[] a, int start, int len, int key) {
int high = start + len, low = start - 1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (a[guess] < key)
low = guess;
else
high = guess;
}
if (high == start + len)
return ~(start + len);
else if (a[high] == key)
return high;
else
return ~high;
}
private void checkIntegrity() {
for (int i = 1; i < mSize; i++) {
if (mKeys[i] <= mKeys[i - 1]) {
for (int j = 0; j < mSize; j++) {
Log.e("FAIL", j + ": " + mKeys[j] + " -> " + mValues[j]);
}
throw new RuntimeException();
}
}
}
private int[] mKeys;
private Object[] mValues;
private int mSize;
}
@@ -0,0 +1,245 @@
/*
* Copyright (C) 2006 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 com.android.internal.util.ArrayUtils;
/**
* SparseBooleanArrays map integers to booleans.
* Unlike a normal array of booleans
* there can be gaps in the indices. It is intended to be more efficient
* than using a HashMap to map Integers to Booleans.
*/
public class SparseBooleanArray {
/**
* Creates a new SparseBooleanArray containing no mappings.
*/
public SparseBooleanArray() {
this(10);
}
/**
* Creates a new SparseBooleanArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings.
*/
public SparseBooleanArray(int initialCapacity) {
initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
mKeys = new int[initialCapacity];
mValues = new boolean[initialCapacity];
mSize = 0;
}
/**
* Gets the boolean mapped from the specified key, or <code>false</code>
* if no such mapping has been made.
*/
public boolean get(int key) {
return get(key, false);
}
/**
* Gets the boolean mapped from the specified key, or the specified value
* if no such mapping has been made.
*/
public boolean get(int key, boolean valueIfKeyNotFound) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i < 0) {
return valueIfKeyNotFound;
} else {
return mValues[i];
}
}
/**
* Removes the mapping from the specified key, if there was any.
*/
public void delete(int key) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
System.arraycopy(mKeys, i + 1, mKeys, i, mSize - (i + 1));
System.arraycopy(mValues, i + 1, mValues, i, mSize - (i + 1));
mSize--;
}
}
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(int key, boolean value) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
mValues[i] = value;
} else {
i = ~i;
if (mSize >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(mSize + 1);
int[] nkeys = new int[n];
boolean[] nvalues = new boolean[n];
// Log.e("SparseBooleanArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
if (mSize - i != 0) {
// Log.e("SparseBooleanArray", "move " + (mSize - i));
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
}
mKeys[i] = key;
mValues[i] = value;
mSize++;
}
}
/**
* Returns the number of key-value mappings that this SparseBooleanArray
* currently stores.
*/
public int size() {
return mSize;
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseBooleanArray stores.
*/
public int keyAt(int index) {
return mKeys[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseBooleanArray stores.
*/
public boolean valueAt(int index) {
return mValues[index];
}
/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
*/
public int indexOfKey(int key) {
return binarySearch(mKeys, 0, mSize, key);
}
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
*/
public int indexOfValue(boolean value) {
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
/**
* Removes all key-value mappings from this SparseBooleanArray.
*/
public void clear() {
mSize = 0;
}
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
public void append(int key, boolean value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
int pos = mSize;
if (pos >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(pos + 1);
int[] nkeys = new int[n];
boolean[] nvalues = new boolean[n];
// Log.e("SparseBooleanArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
mKeys[pos] = key;
mValues[pos] = value;
mSize = pos + 1;
}
private static int binarySearch(int[] a, int start, int len, int key) {
int high = start + len, low = start - 1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (a[guess] < key)
low = guess;
else
high = guess;
}
if (high == start + len)
return ~(start + len);
else if (a[high] == key)
return high;
else
return ~high;
}
private void checkIntegrity() {
for (int i = 1; i < mSize; i++) {
if (mKeys[i] <= mKeys[i - 1]) {
for (int j = 0; j < mSize; j++) {
Log.e("FAIL", j + ": " + mKeys[j] + " -> " + mValues[j]);
}
throw new RuntimeException();
}
}
}
private int[] mKeys;
private boolean[] mValues;
private int mSize;
}
@@ -0,0 +1,251 @@
/*
* Copyright (C) 2006 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 com.android.internal.util.ArrayUtils;
/**
* SparseIntArrays map integers to integers. Unlike a normal array of integers,
* there can be gaps in the indices. It is intended to be more efficient
* than using a HashMap to map Integers to Integers.
*/
public class SparseIntArray {
/**
* Creates a new SparseIntArray containing no mappings.
*/
public SparseIntArray() {
this(10);
}
/**
* Creates a new SparseIntArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings.
*/
public SparseIntArray(int initialCapacity) {
initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
mKeys = new int[initialCapacity];
mValues = new int[initialCapacity];
mSize = 0;
}
/**
* Gets the int mapped from the specified key, or <code>0</code>
* if no such mapping has been made.
*/
public int get(int key) {
return get(key, 0);
}
/**
* Gets the int mapped from the specified key, or the specified value
* if no such mapping has been made.
*/
public int get(int key, int valueIfKeyNotFound) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i < 0) {
return valueIfKeyNotFound;
} else {
return mValues[i];
}
}
/**
* Removes the mapping from the specified key, if there was any.
*/
public void delete(int key) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
removeAt(i);
}
}
/**
* Removes the mapping at the given index.
*/
public void removeAt(int index) {
System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1));
System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1));
mSize--;
}
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(int key, int value) {
int i = binarySearch(mKeys, 0, mSize, key);
if (i >= 0) {
mValues[i] = value;
} else {
i = ~i;
if (mSize >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(mSize + 1);
int[] nkeys = new int[n];
int[] nvalues = new int[n];
// Log.e("SparseIntArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
if (mSize - i != 0) {
// Log.e("SparseIntArray", "move " + (mSize - i));
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
}
mKeys[i] = key;
mValues[i] = value;
mSize++;
}
}
/**
* Returns the number of key-value mappings that this SparseIntArray
* currently stores.
*/
public int size() {
return mSize;
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseIntArray stores.
*/
public int keyAt(int index) {
return mKeys[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseIntArray stores.
*/
public int valueAt(int index) {
return mValues[index];
}
/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
*/
public int indexOfKey(int key) {
return binarySearch(mKeys, 0, mSize, key);
}
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
*/
public int indexOfValue(int value) {
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
/**
* Removes all key-value mappings from this SparseIntArray.
*/
public void clear() {
mSize = 0;
}
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
public void append(int key, int value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
int pos = mSize;
if (pos >= mKeys.length) {
int n = ArrayUtils.idealIntArraySize(pos + 1);
int[] nkeys = new int[n];
int[] nvalues = new int[n];
// Log.e("SparseIntArray", "grow " + mKeys.length + " to " + n);
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
mKeys = nkeys;
mValues = nvalues;
}
mKeys[pos] = key;
mValues[pos] = value;
mSize = pos + 1;
}
private static int binarySearch(int[] a, int start, int len, int key) {
int high = start + len, low = start - 1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (a[guess] < key)
low = guess;
else
high = guess;
}
if (high == start + len)
return ~(start + len);
else if (a[high] == key)
return high;
else
return ~high;
}
private void checkIntegrity() {
for (int i = 1; i < mSize; i++) {
if (mKeys[i] <= mKeys[i - 1]) {
for (int j = 0; j < mSize; j++) {
Log.e("FAIL", j + ": " + mKeys[j] + " -> " + mValues[j]);
}
throw new RuntimeException();
}
}
}
private int[] mKeys;
private int[] mValues;
private int mSize;
}
+178
View File
@@ -0,0 +1,178 @@
/*
* 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 android.util;
import com.android.internal.R;
/**
* State sets are arrays of positive ints where each element
* represents the state of a {@link android.view.View} (e.g. focused,
* selected, visible, etc.). A {@link android.view.View} may be in
* one or more of those states.
*
* A state spec is an array of signed ints where each element
* represents a required (if positive) or an undesired (if negative)
* {@link android.view.View} state.
*
* Utils dealing with state sets.
*
* In theory we could encapsulate the state set and state spec arrays
* and not have static methods here but there is some concern about
* performance since these methods are called during view drawing.
*/
public class StateSet {
public static final int[] WILD_CARD = new int[0];
/**
* Return whether the stateSetOrSpec is matched by all StateSets.
*
* @param stateSetOrSpec a state set or state spec.
*/
public static boolean isWildCard(int[] stateSetOrSpec) {
return stateSetOrSpec.length == 0 || stateSetOrSpec[0] == 0;
}
/**
* Return whether the stateSet matches the desired stateSpec.
*
* @param stateSpec an array of required (if positive) or
* prohibited (if negative) {@link android.view.View} states.
* @param stateSet an array of {@link android.view.View} states
*/
public static boolean stateSetMatches(int[] stateSpec, int[] stateSet) {
if (stateSet == null) {
return (stateSpec == null || isWildCard(stateSpec));
}
int stateSpecSize = stateSpec.length;
int stateSetSize = stateSet.length;
for (int i = 0; i < stateSpecSize; i++) {
int stateSpecState = stateSpec[i];
if (stateSpecState == 0) {
// We've reached the end of the cases to match against.
return true;
}
final boolean mustMatch;
if (stateSpecState > 0) {
mustMatch = true;
} else {
// We use negative values to indicate must-NOT-match states.
mustMatch = false;
stateSpecState = -stateSpecState;
}
boolean found = false;
for (int j = 0; j < stateSetSize; j++) {
final int state = stateSet[j];
if (state == 0) {
// We've reached the end of states to match.
if (mustMatch) {
// We didn't find this must-match state.
return false;
} else {
// Continue checking other must-not-match states.
break;
}
}
if (state == stateSpecState) {
if (mustMatch) {
found = true;
// Continue checking other other must-match states.
break;
} else {
// Any match of a must-not-match state returns false.
return false;
}
}
}
if (mustMatch && !found) {
// We've reached the end of states to match and we didn't
// find a must-match state.
return false;
}
}
return true;
}
/**
* Return whether the state matches the desired stateSpec.
*
* @param stateSpec an array of required (if positive) or
* prohibited (if negative) {@link android.view.View} states.
* @param state a {@link android.view.View} state
*/
public static boolean stateSetMatches(int[] stateSpec, int state) {
int stateSpecSize = stateSpec.length;
for (int i = 0; i < stateSpecSize; i++) {
int stateSpecState = stateSpec[i];
if (stateSpecState == 0) {
// We've reached the end of the cases to match against.
return true;
}
if (stateSpecState > 0) {
if (state != stateSpecState) {
return false;
}
} else {
// We use negative values to indicate must-NOT-match states.
if (state == -stateSpecState) {
// We matched a must-not-match case.
return false;
}
}
}
return true;
}
public static int[] trimStateSet(int[] states, int newSize) {
if (states.length == newSize) {
return states;
}
int[] trimmedStates = new int[newSize];
System.arraycopy(states, 0, trimmedStates, 0, newSize);
return trimmedStates;
}
public static String dump(int[] states) {
StringBuilder sb = new StringBuilder();
int count = states.length;
for (int i = 0; i < count; i++) {
switch (states[i]) {
case R.attr.state_window_focused:
sb.append("W ");
break;
case R.attr.state_pressed:
sb.append("P ");
break;
case R.attr.state_selected:
sb.append("S ");
break;
case R.attr.state_focused:
sb.append("F ");
break;
case R.attr.state_enabled:
sb.append("E ");
break;
}
}
return sb.toString();
}
}
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2006 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;
/**
* Implementation of a {@link android.util.Printer} that sends its output
* to a {@link StringBuilder}.
*/
public class StringBuilderPrinter implements Printer {
private final StringBuilder mBuilder;
/**
* Create a new Printer that sends to a StringBuilder object.
*
* @param builder The StringBuilder where you would like output to go.
*/
public StringBuilderPrinter(StringBuilder builder) {
mBuilder = builder;
}
public void println(String x) {
mBuilder.append(x);
int len = x.length();
if (len <= 0 || x.charAt(len-1) != '\n') {
mBuilder.append('\n');
}
}
}
@@ -0,0 +1,48 @@
/*
* 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;
/**
*
* @hide
*/
class SynchronizedPool<T extends Poolable<T>> implements Pool<T> {
private final Pool<T> mPool;
private final Object mLock;
public SynchronizedPool(Pool<T> pool) {
mPool = pool;
mLock = this;
}
public SynchronizedPool(Pool<T> pool, Object lock) {
mPool = pool;
mLock = lock;
}
public T acquire() {
synchronized (mLock) {
return mPool.acquire();
}
}
public void release(T element) {
synchronized (mLock) {
mPool.release(element);
}
}
}
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2006 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;
public class TimeFormatException extends RuntimeException
{
TimeFormatException(String s)
{
super(s);
}
}
+282
View File
@@ -0,0 +1,282 @@
/*
* Copyright (C) 2006 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 android.content.res.Resources;
import android.content.res.XmlResourceParser;
import org.apache.harmony.luni.internal.util.ZoneInfoDB;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.TimeZone;
import java.util.Date;
import com.android.internal.util.XmlUtils;
/**
* A class containing utility methods related to time zones.
*/
public class TimeUtils {
private static final String TAG = "TimeUtils";
/**
* Tries to return a time zone that would have had the specified offset
* and DST value at the specified moment in the specified country.
* Returns null if no suitable zone could be found.
*/
public static TimeZone getTimeZone(int offset, boolean dst, long when, String country) {
if (country == null) {
return null;
}
TimeZone best = null;
Resources r = Resources.getSystem();
XmlResourceParser parser = r.getXml(com.android.internal.R.xml.time_zones_by_country);
Date d = new Date(when);
TimeZone current = TimeZone.getDefault();
String currentName = current.getID();
int currentOffset = current.getOffset(when);
boolean currentDst = current.inDaylightTime(d);
try {
XmlUtils.beginDocument(parser, "timezones");
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null || !(element.equals("timezone"))) {
break;
}
String code = parser.getAttributeValue(null, "code");
if (country.equals(code)) {
if (parser.next() == XmlPullParser.TEXT) {
String maybe = parser.getText();
// If the current time zone is from the right country
// and meets the other known properties, keep it
// instead of changing to another one.
if (maybe.equals(currentName)) {
if (currentOffset == offset && currentDst == dst) {
return current;
}
}
// Otherwise, take the first zone from the right
// country that has the correct current offset and DST.
// (Keep iterating instead of returning in case we
// haven't encountered the current time zone yet.)
if (best == null) {
TimeZone tz = TimeZone.getTimeZone(maybe);
if (tz.getOffset(when) == offset &&
tz.inDaylightTime(d) == dst) {
best = tz;
}
}
}
}
}
} catch (XmlPullParserException e) {
Log.e(TAG, "Got exception while getting preferred time zone.", e);
} catch (IOException e) {
Log.e(TAG, "Got exception while getting preferred time zone.", e);
} finally {
parser.close();
}
return best;
}
/**
* Returns a String indicating the version of the time zone database currently
* in use. The format of the string is dependent on the underlying time zone
* database implementation, but will typically contain the year in which the database
* was updated plus a letter from a to z indicating changes made within that year.
*
* <p>Time zone database updates should be expected to occur periodically due to
* political and legal changes that cannot be anticipated in advance. Therefore,
* when computing the UTC time for a future event, applications should be aware that
* the results may differ following a time zone database update. This method allows
* applications to detect that a database change has occurred, and to recalculate any
* cached times accordingly.
*
* <p>The time zone database may be assumed to change only when the device runtime
* is restarted. Therefore, it is not necessary to re-query the database version
* during the lifetime of an activity.
*/
public static String getTimeZoneDatabaseVersion() {
return ZoneInfoDB.getVersion();
}
/** @hide Field length that can hold 999 days of time */
public static final int HUNDRED_DAY_FIELD_LEN = 19;
private static final int SECONDS_PER_MINUTE = 60;
private static final int SECONDS_PER_HOUR = 60 * 60;
private static final int SECONDS_PER_DAY = 24 * 60 * 60;
private static final Object sFormatSync = new Object();
private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+5];
static private int accumField(int amt, int suffix, boolean always, int zeropad) {
if (amt > 99 || (always && zeropad >= 3)) {
return 3+suffix;
}
if (amt > 9 || (always && zeropad >= 2)) {
return 2+suffix;
}
if (always || amt > 0) {
return 1+suffix;
}
return 0;
}
static private int printField(char[] formatStr, int amt, char suffix, int pos,
boolean always, int zeropad) {
if (always || amt > 0) {
final int startPos = pos;
if ((always && zeropad >= 3) || amt > 99) {
int dig = amt/100;
formatStr[pos] = (char)(dig + '0');
pos++;
amt -= (dig*100);
}
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
int dig = amt/10;
formatStr[pos] = (char)(dig + '0');
pos++;
amt -= (dig*10);
}
formatStr[pos] = (char)(amt + '0');
pos++;
formatStr[pos] = suffix;
pos++;
}
return pos;
}
private static int formatDurationLocked(long duration, int fieldLen) {
if (sFormatStr.length < fieldLen) {
sFormatStr = new char[fieldLen];
}
char[] formatStr = sFormatStr;
if (duration == 0) {
int pos = 0;
fieldLen -= 1;
while (pos < fieldLen) {
formatStr[pos] = ' ';
}
formatStr[pos] = '0';
return pos+1;
}
char prefix;
if (duration > 0) {
prefix = '+';
} else {
prefix = '-';
duration = -duration;
}
int millis = (int)(duration%1000);
int seconds = (int) Math.floor(duration / 1000);
int days = 0, hours = 0, minutes = 0;
if (seconds > SECONDS_PER_DAY) {
days = seconds / SECONDS_PER_DAY;
seconds -= days * SECONDS_PER_DAY;
}
if (seconds > SECONDS_PER_HOUR) {
hours = seconds / SECONDS_PER_HOUR;
seconds -= hours * SECONDS_PER_HOUR;
}
if (seconds > SECONDS_PER_MINUTE) {
minutes = seconds / SECONDS_PER_MINUTE;
seconds -= minutes * SECONDS_PER_MINUTE;
}
int pos = 0;
if (fieldLen != 0) {
int myLen = accumField(days, 1, false, 0);
myLen += accumField(hours, 1, myLen > 0, 2);
myLen += accumField(minutes, 1, myLen > 0, 2);
myLen += accumField(seconds, 1, myLen > 0, 2);
myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1;
while (myLen < fieldLen) {
formatStr[pos] = ' ';
pos++;
myLen++;
}
}
formatStr[pos] = prefix;
pos++;
int start = pos;
boolean zeropad = fieldLen != 0;
pos = printField(formatStr, days, 'd', pos, false, 0);
pos = printField(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, millis, 'm', pos, true, (zeropad && pos != start) ? 3 : 0);
formatStr[pos] = 's';
return pos + 1;
}
/** @hide Just for debugging; not internationalized. */
public static void formatDuration(long duration, StringBuilder builder) {
synchronized (sFormatSync) {
int len = formatDurationLocked(duration, 0);
builder.append(sFormatStr, 0, len);
}
}
/** @hide Just for debugging; not internationalized. */
public static void formatDuration(long duration, PrintWriter pw, int fieldLen) {
synchronized (sFormatSync) {
int len = formatDurationLocked(duration, fieldLen);
pw.print(new String(sFormatStr, 0, len));
}
}
/** @hide Just for debugging; not internationalized. */
public static void formatDuration(long duration, PrintWriter pw) {
formatDuration(duration, pw, 0);
}
/** @hide Just for debugging; not internationalized. */
public static void formatDuration(long time, long now, PrintWriter pw) {
if (time == 0) {
pw.print("--");
return;
}
formatDuration(time-now, pw, 0);
}
}
@@ -0,0 +1,144 @@
/*
* 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 android.util;
import java.util.ArrayList;
import android.os.SystemClock;
/**
* A utility class to help log timings splits throughout a method call.
* Typical usage is:
*
* TimingLogger timings = new TimingLogger(TAG, "methodA");
* ... do some work A ...
* timings.addSplit("work A");
* ... do some work B ...
* timings.addSplit("work B");
* ... do some work C ...
* timings.addSplit("work C");
* timings.dumpToLog();
*
* The dumpToLog call would add the following to the log:
*
* D/TAG ( 3459): methodA: begin
* D/TAG ( 3459): methodA: 9 ms, work A
* D/TAG ( 3459): methodA: 1 ms, work B
* D/TAG ( 3459): methodA: 6 ms, work C
* D/TAG ( 3459): methodA: end, 16 ms
*/
public class TimingLogger {
/**
* The Log tag to use for checking Log.isLoggable and for
* logging the timings.
*/
private String mTag;
/** A label to be included in every log. */
private String mLabel;
/** Used to track whether Log.isLoggable was enabled at reset time. */
private boolean mDisabled;
/** Stores the time of each split. */
ArrayList<Long> mSplits;
/** Stores the labels for each split. */
ArrayList<String> mSplitLabels;
/**
* Create and initialize a TimingLogger object that will log using
* the specific tag. If the Log.isLoggable is not enabled to at
* least the Log.VERBOSE level for that tag at creation time then
* the addSplit and dumpToLog call will do nothing.
* @param tag the log tag to use while logging the timings
* @param label a string to be displayed with each log
*/
public TimingLogger(String tag, String label) {
reset(tag, label);
}
/**
* Clear and initialize a TimingLogger object that will log using
* the specific tag. If the Log.isLoggable is not enabled to at
* least the Log.VERBOSE level for that tag at creation time then
* the addSplit and dumpToLog call will do nothing.
* @param tag the log tag to use while logging the timings
* @param label a string to be displayed with each log
*/
public void reset(String tag, String label) {
mTag = tag;
mLabel = label;
reset();
}
/**
* Clear and initialize a TimingLogger object that will log using
* the tag and label that was specified previously, either via
* the constructor or a call to reset(tag, label). If the
* Log.isLoggable is not enabled to at least the Log.VERBOSE
* level for that tag at creation time then the addSplit and
* dumpToLog call will do nothing.
*/
public void reset() {
mDisabled = !Log.isLoggable(mTag, Log.VERBOSE);
if (mDisabled) return;
if (mSplits == null) {
mSplits = new ArrayList<Long>();
mSplitLabels = new ArrayList<String>();
} else {
mSplits.clear();
mSplitLabels.clear();
}
addSplit(null);
}
/**
* Add a split for the current time, labeled with splitLabel. If
* Log.isLoggable was not enabled to at least the Log.VERBOSE for
* the specified tag at construction or reset() time then this
* call does nothing.
* @param splitLabel a label to associate with this split.
*/
public void addSplit(String splitLabel) {
if (mDisabled) return;
long now = SystemClock.elapsedRealtime();
mSplits.add(now);
mSplitLabels.add(splitLabel);
}
/**
* Dumps the timings to the log using Log.d(). If Log.isLoggable was
* not enabled to at least the Log.VERBOSE for the specified tag at
* construction or reset() time then this call does nothing.
*/
public void dumpToLog() {
if (mDisabled) return;
Log.d(mTag, mLabel + ": begin");
final long first = mSplits.get(0);
long now = first;
for (int i = 1; i < mSplits.size(); i++) {
now = mSplits.get(i);
final String splitLabel = mSplitLabels.get(i);
final long prev = mSplits.get(i - 1);
Log.d(mTag, mLabel + ": " + (now - prev) + " ms, " + splitLabel);
}
Log.d(mTag, mLabel + ": end, " + (now - first) + " ms");
}
}
+494
View File
@@ -0,0 +1,494 @@
/*
* 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 android.util;
/**
* Container for a dynamically typed data value. Primarily used with
* {@link android.content.res.Resources} for holding resource values.
*/
public class TypedValue {
/** The value contains no data. */
public static final int TYPE_NULL = 0x00;
/** The <var>data</var> field holds a resource identifier. */
public static final int TYPE_REFERENCE = 0x01;
/** The <var>data</var> field holds an attribute resource
* identifier (referencing an attribute in the current theme
* style, not a resource entry). */
public static final int TYPE_ATTRIBUTE = 0x02;
/** The <var>string</var> field holds string data. In addition, if
* <var>data</var> is non-zero then it is the string block
* index of the string and <var>assetCookie</var> is the set of
* assets the string came from. */
public static final int TYPE_STRING = 0x03;
/** The <var>data</var> field holds an IEEE 754 floating point number. */
public static final int TYPE_FLOAT = 0x04;
/** The <var>data</var> field holds a complex number encoding a
* dimension value. */
public static final int TYPE_DIMENSION = 0x05;
/** The <var>data</var> field holds a complex number encoding a fraction
* of a container. */
public static final int TYPE_FRACTION = 0x06;
/** Identifies the start of plain integer values. Any type value
* from this to {@link #TYPE_LAST_INT} means the
* <var>data</var> field holds a generic integer value. */
public static final int TYPE_FIRST_INT = 0x10;
/** The <var>data</var> field holds a number that was
* originally specified in decimal. */
public static final int TYPE_INT_DEC = 0x10;
/** The <var>data</var> field holds a number that was
* originally specified in hexadecimal (0xn). */
public static final int TYPE_INT_HEX = 0x11;
/** The <var>data</var> field holds 0 or 1 that was originally
* specified as "false" or "true". */
public static final int TYPE_INT_BOOLEAN = 0x12;
/** Identifies the start of integer values that were specified as
* color constants (starting with '#'). */
public static final int TYPE_FIRST_COLOR_INT = 0x1c;
/** The <var>data</var> field holds a color that was originally
* specified as #aarrggbb. */
public static final int TYPE_INT_COLOR_ARGB8 = 0x1c;
/** The <var>data</var> field holds a color that was originally
* specified as #rrggbb. */
public static final int TYPE_INT_COLOR_RGB8 = 0x1d;
/** The <var>data</var> field holds a color that was originally
* specified as #argb. */
public static final int TYPE_INT_COLOR_ARGB4 = 0x1e;
/** The <var>data</var> field holds a color that was originally
* specified as #rgb. */
public static final int TYPE_INT_COLOR_RGB4 = 0x1f;
/** Identifies the end of integer values that were specified as color
* constants. */
public static final int TYPE_LAST_COLOR_INT = 0x1f;
/** Identifies the end of plain integer values. */
public static final int TYPE_LAST_INT = 0x1f;
/* ------------------------------------------------------------ */
/** Complex data: bit location of unit information. */
public static final int COMPLEX_UNIT_SHIFT = 0;
/** Complex data: mask to extract unit information (after shifting by
* {@link #COMPLEX_UNIT_SHIFT}). This gives us 16 possible types, as
* defined below. */
public static final int COMPLEX_UNIT_MASK = 0xf;
/** {@link #TYPE_DIMENSION} complex unit: Value is raw pixels. */
public static final int COMPLEX_UNIT_PX = 0;
/** {@link #TYPE_DIMENSION} complex unit: Value is Device Independent
* Pixels. */
public static final int COMPLEX_UNIT_DIP = 1;
/** {@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */
public static final int COMPLEX_UNIT_SP = 2;
/** {@link #TYPE_DIMENSION} complex unit: Value is in points. */
public static final int COMPLEX_UNIT_PT = 3;
/** {@link #TYPE_DIMENSION} complex unit: Value is in inches. */
public static final int COMPLEX_UNIT_IN = 4;
/** {@link #TYPE_DIMENSION} complex unit: Value is in millimeters. */
public static final int COMPLEX_UNIT_MM = 5;
/** {@link #TYPE_FRACTION} complex unit: A basic fraction of the overall
* size. */
public static final int COMPLEX_UNIT_FRACTION = 0;
/** {@link #TYPE_FRACTION} complex unit: A fraction of the parent size. */
public static final int COMPLEX_UNIT_FRACTION_PARENT = 1;
/** Complex data: where the radix information is, telling where the decimal
* place appears in the mantissa. */
public static final int COMPLEX_RADIX_SHIFT = 4;
/** Complex data: mask to extract radix information (after shifting by
* {@link #COMPLEX_RADIX_SHIFT}). This give us 4 possible fixed point
* representations as defined below. */
public static final int COMPLEX_RADIX_MASK = 0x3;
/** Complex data: the mantissa is an integral number -- i.e., 0xnnnnnn.0 */
public static final int COMPLEX_RADIX_23p0 = 0;
/** Complex data: the mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn */
public static final int COMPLEX_RADIX_16p7 = 1;
/** Complex data: the mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn */
public static final int COMPLEX_RADIX_8p15 = 2;
/** Complex data: the mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn */
public static final int COMPLEX_RADIX_0p23 = 3;
/** Complex data: bit location of mantissa information. */
public static final int COMPLEX_MANTISSA_SHIFT = 8;
/** Complex data: mask to extract mantissa information (after shifting by
* {@link #COMPLEX_MANTISSA_SHIFT}). This gives us 23 bits of precision;
* the top bit is the sign. */
public static final int COMPLEX_MANTISSA_MASK = 0xffffff;
/* ------------------------------------------------------------ */
/**
* If {@link #density} is equal to this value, then the density should be
* treated as the system's default density value: {@link DisplayMetrics#DENSITY_DEFAULT}.
*/
public static final int DENSITY_DEFAULT = 0;
/**
* If {@link #density} is equal to this value, then there is no density
* associated with the resource and it should not be scaled.
*/
public static final int DENSITY_NONE = 0xffff;
/* ------------------------------------------------------------ */
/** The type held by this value, as defined by the constants here.
* This tells you how to interpret the other fields in the object. */
public int type;
/** If the value holds a string, this is it. */
public CharSequence string;
/** Basic data in the value, interpreted according to {@link #type} */
public int data;
/** Additional information about where the value came from; only
* set for strings. */
public int assetCookie;
/** If Value came from a resource, this holds the corresponding resource id. */
public int resourceId;
/** If Value came from a resource, these are the configurations for which
* its contents can change. */
public int changingConfigurations = -1;
/**
* If the Value came from a resource, this holds the corresponding pixel density.
* */
public int density;
/* ------------------------------------------------------------ */
/** Return the data for this value as a float. Only use for values
* whose type is {@link #TYPE_FLOAT}. */
public final float getFloat() {
return Float.intBitsToFloat(data);
}
private static final float MANTISSA_MULT =
1.0f / (1<<TypedValue.COMPLEX_MANTISSA_SHIFT);
private static final float[] RADIX_MULTS = new float[] {
1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
};
/**
* Retrieve the base value from a complex data integer. This uses the
* {@link #COMPLEX_MANTISSA_MASK} and {@link #COMPLEX_RADIX_MASK} fields of
* the data to compute a floating point representation of the number they
* describe. The units are ignored.
*
* @param complex A complex data value.
*
* @return A floating point value corresponding to the complex data.
*/
public static float complexToFloat(int complex)
{
return (complex&(TypedValue.COMPLEX_MANTISSA_MASK
<<TypedValue.COMPLEX_MANTISSA_SHIFT))
* RADIX_MULTS[(complex>>TypedValue.COMPLEX_RADIX_SHIFT)
& TypedValue.COMPLEX_RADIX_MASK];
}
/**
* Converts a complex data value holding a dimension to its final floating
* point value. The given <var>data</var> must be structured as a
* {@link #TYPE_DIMENSION}.
*
* @param data A complex data value holding a unit, magnitude, and
* mantissa.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The complex floating point value multiplied by the appropriate
* metrics depending on its unit.
*/
public static float complexToDimension(int data, DisplayMetrics metrics)
{
return applyDimension(
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
complexToFloat(data),
metrics);
}
/**
* Converts a complex data value holding a dimension to its final value
* as an integer pixel offset. This is the same as
* {@link #complexToDimension}, except the raw floating point value is
* truncated to an integer (pixel) value.
* The given <var>data</var> must be structured as a
* {@link #TYPE_DIMENSION}.
*
* @param data A complex data value holding a unit, magnitude, and
* mantissa.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The number of pixels specified by the data and its desired
* multiplier and units.
*/
public static int complexToDimensionPixelOffset(int data,
DisplayMetrics metrics)
{
return (int)applyDimension(
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
complexToFloat(data),
metrics);
}
/**
* Converts a complex data value holding a dimension to its final value
* as an integer pixel size. This is the same as
* {@link #complexToDimension}, except the raw floating point value is
* converted to an integer (pixel) value for use as a size. A size
* conversion involves rounding the base value, and ensuring that a
* non-zero base value is at least one pixel in size.
* The given <var>data</var> must be structured as a
* {@link #TYPE_DIMENSION}.
*
* @param data A complex data value holding a unit, magnitude, and
* mantissa.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The number of pixels specified by the data and its desired
* multiplier and units.
*/
public static int complexToDimensionPixelSize(int data,
DisplayMetrics metrics)
{
final float value = complexToFloat(data);
final float f = applyDimension(
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
value,
metrics);
final int res = (int)(f+0.5f);
if (res != 0) return res;
if (value == 0) return 0;
if (value > 0) return 1;
return -1;
}
public static float complexToDimensionNoisy(int data, DisplayMetrics metrics)
{
float res = complexToDimension(data, metrics);
System.out.println(
"Dimension (0x" + ((data>>TypedValue.COMPLEX_MANTISSA_SHIFT)
& TypedValue.COMPLEX_MANTISSA_MASK)
+ "*" + (RADIX_MULTS[(data>>TypedValue.COMPLEX_RADIX_SHIFT)
& TypedValue.COMPLEX_RADIX_MASK] / MANTISSA_MULT)
+ ")" + DIMENSION_UNIT_STRS[(data>>COMPLEX_UNIT_SHIFT)
& COMPLEX_UNIT_MASK]
+ " = " + res);
return res;
}
/**
* Converts an unpacked complex data value holding a dimension to its final floating
* point value. The two parameters <var>unit</var> and <var>value</var>
* are as in {@link #TYPE_DIMENSION}.
*
* @param unit The unit to convert from.
* @param value The value to apply the unit to.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The complex floating point value multiplied by the appropriate
* metrics depending on its unit.
*/
public static float applyDimension(int unit, float value,
DisplayMetrics metrics)
{
switch (unit) {
case COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_DIP:
return value * metrics.density;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);
case COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
/**
* Return the data for this value as a dimension. Only use for values
* whose type is {@link #TYPE_DIMENSION}.
*
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The complex floating point value multiplied by the appropriate
* metrics depending on its unit.
*/
public float getDimension(DisplayMetrics metrics)
{
return complexToDimension(data, metrics);
}
/**
* Converts a complex data value holding a fraction to its final floating
* point value. The given <var>data</var> must be structured as a
* {@link #TYPE_FRACTION}.
*
* @param data A complex data value holding a unit, magnitude, and
* mantissa.
* @param base The base value of this fraction. In other words, a
* standard fraction is multiplied by this value.
* @param pbase The parent base value of this fraction. In other
* words, a parent fraction (nn%p) is multiplied by this
* value.
*
* @return The complex floating point value multiplied by the appropriate
* base value depending on its unit.
*/
public static float complexToFraction(int data, float base, float pbase)
{
switch ((data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK) {
case COMPLEX_UNIT_FRACTION:
return complexToFloat(data) * base;
case COMPLEX_UNIT_FRACTION_PARENT:
return complexToFloat(data) * pbase;
}
return 0;
}
/**
* Return the data for this value as a fraction. Only use for values whose
* type is {@link #TYPE_FRACTION}.
*
* @param base The base value of this fraction. In other words, a
* standard fraction is multiplied by this value.
* @param pbase The parent base value of this fraction. In other
* words, a parent fraction (nn%p) is multiplied by this
* value.
*
* @return The complex floating point value multiplied by the appropriate
* base value depending on its unit.
*/
public float getFraction(float base, float pbase)
{
return complexToFraction(data, base, pbase);
}
/**
* Regardless of the actual type of the value, try to convert it to a
* string value. For example, a color type will be converted to a
* string of the form #aarrggbb.
*
* @return CharSequence The coerced string value. If the value is
* null or the type is not known, null is returned.
*/
public final CharSequence coerceToString()
{
int t = type;
if (t == TYPE_STRING) {
return string;
}
return coerceToString(t, data);
}
private static final String[] DIMENSION_UNIT_STRS = new String[] {
"px", "dip", "sp", "pt", "in", "mm"
};
private static final String[] FRACTION_UNIT_STRS = new String[] {
"%", "%p"
};
/**
* Perform type conversion as per {@link #coerceToString()} on an
* explicitly supplied type and data.
*
* @param type The data type identifier.
* @param data The data value.
*
* @return String The coerced string value. If the value is
* null or the type is not known, null is returned.
*/
public static final String coerceToString(int type, int data)
{
switch (type) {
case TYPE_NULL:
return null;
case TYPE_REFERENCE:
return "@" + data;
case TYPE_ATTRIBUTE:
return "?" + data;
case TYPE_FLOAT:
return Float.toString(Float.intBitsToFloat(data));
case TYPE_DIMENSION:
return Float.toString(complexToFloat(data)) + DIMENSION_UNIT_STRS[
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
case TYPE_FRACTION:
return Float.toString(complexToFloat(data)*100) + FRACTION_UNIT_STRS[
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
case TYPE_INT_HEX:
return "0x" + Integer.toHexString(data);
case TYPE_INT_BOOLEAN:
return data != 0 ? "true" : "false";
}
if (type >= TYPE_FIRST_COLOR_INT && type <= TYPE_LAST_COLOR_INT) {
return "#" + Integer.toHexString(data);
} else if (type >= TYPE_FIRST_INT && type <= TYPE_LAST_INT) {
return Integer.toString(data);
}
return null;
}
public void setTo(TypedValue other)
{
type = other.type;
string = other.string;
data = other.data;
assetCookie = other.assetCookie;
resourceId = other.resourceId;
density = other.density;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("TypedValue{t=0x").append(Integer.toHexString(type));
sb.append("/d=0x").append(Integer.toHexString(data));
if (type == TYPE_STRING) {
sb.append(" \"").append(string != null ? string : "<null>").append("\"");
}
if (assetCookie != 0) {
sb.append(" a=").append(assetCookie);
}
if (resourceId != 0) {
sb.append(" r=0x").append(Integer.toHexString(resourceId));
}
sb.append("}");
return sb.toString();
}
};
+185
View File
@@ -0,0 +1,185 @@
/*
* 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 android.util;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import org.apache.harmony.xml.ExpatPullParser;
import org.apache.harmony.xml.ExpatReader;
/**
* XML utility methods.
*/
public class Xml {
/**
* {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name.
*
* @see <a href="http://xmlpull.org/v1/doc/features.html#relaxed">
* specification</a>
*/
public static String FEATURE_RELAXED = ExpatPullParser.FEATURE_RELAXED;
/**
* Parses the given xml string and fires events on the given SAX handler.
*/
public static void parse(String xml, ContentHandler contentHandler)
throws SAXException {
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(new StringReader(xml)));
}
catch (IOException e) {
throw new AssertionError(e);
}
}
/**
* Parses xml from the given reader and fires events on the given SAX
* handler.
*/
public static void parse(Reader in, ContentHandler contentHandler)
throws IOException, SAXException {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(in));
}
/**
* Parses xml from the given input stream and fires events on the given SAX
* handler.
*/
public static void parse(InputStream in, Encoding encoding,
ContentHandler contentHandler) throws IOException, SAXException {
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
InputSource source = new InputSource(in);
source.setEncoding(encoding.expatName);
reader.parse(source);
} catch (IOException e) {
throw new AssertionError(e);
}
}
/**
* Creates a new pull parser with namespace support.
*
* <p><b>Note:</b> This is actually slower than the SAX parser, and it's not
* fully implemented. If you need a fast, mostly implemented pull parser,
* use this. If you need a complete implementation, use KXML.
*/
public static XmlPullParser newPullParser() {
ExpatPullParser parser = new ExpatPullParser();
parser.setNamespaceProcessingEnabled(true);
return parser;
}
/**
* Creates a new xml serializer.
*/
public static XmlSerializer newSerializer() {
try {
return XmlSerializerFactory.instance.newSerializer();
} catch (XmlPullParserException e) {
throw new AssertionError(e);
}
}
/** Factory for xml serializers. Initialized on demand. */
static class XmlSerializerFactory {
static final String TYPE
= "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer";
static final XmlPullParserFactory instance;
static {
try {
instance = XmlPullParserFactory.newInstance(TYPE, null);
} catch (XmlPullParserException e) {
throw new AssertionError(e);
}
}
}
/**
* Supported character encodings.
*/
public enum Encoding {
US_ASCII("US-ASCII"),
UTF_8("UTF-8"),
UTF_16("UTF-16"),
ISO_8859_1("ISO-8859-1");
final String expatName;
Encoding(String expatName) {
this.expatName = expatName;
}
}
/**
* Finds an encoding by name. Returns UTF-8 if you pass {@code null}.
*/
public static Encoding findEncodingByName(String encodingName)
throws UnsupportedEncodingException {
if (encodingName == null) {
return Encoding.UTF_8;
}
for (Encoding encoding : Encoding.values()) {
if (encoding.expatName.equalsIgnoreCase(encodingName))
return encoding;
}
throw new UnsupportedEncodingException(encodingName);
}
/**
* Return an AttributeSet interface for use with the given XmlPullParser.
* If the given parser itself implements AttributeSet, that implementation
* is simply returned. Otherwise a wrapper class is
* instantiated on top of the XmlPullParser, as a proxy for retrieving its
* attributes, and returned to you.
*
* @param parser The existing parser for which you would like an
* AttributeSet.
*
* @return An AttributeSet you can use to retrieve the
* attribute values at each of the tags as the parser moves
* through its XML document.
*
* @see AttributeSet
*/
public static AttributeSet asAttributeSet(XmlPullParser parser) {
return (parser instanceof AttributeSet)
? (AttributeSet) parser
: new XmlPullAttributes(parser);
}
}
@@ -0,0 +1,147 @@
/*
* Copyright (C) 2006 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 org.xmlpull.v1.XmlPullParser;
import android.util.AttributeSet;
import com.android.internal.util.XmlUtils;
/**
* Provides an implementation of AttributeSet on top of an XmlPullParser.
*/
class XmlPullAttributes implements AttributeSet {
public XmlPullAttributes(XmlPullParser parser) {
mParser = parser;
}
public int getAttributeCount() {
return mParser.getAttributeCount();
}
public String getAttributeName(int index) {
return mParser.getAttributeName(index);
}
public String getAttributeValue(int index) {
return mParser.getAttributeValue(index);
}
public String getAttributeValue(String namespace, String name) {
return mParser.getAttributeValue(namespace, name);
}
public String getPositionDescription() {
return mParser.getPositionDescription();
}
public int getAttributeNameResource(int index) {
return 0;
}
public int getAttributeListValue(String namespace, String attribute,
String[] options, int defaultValue) {
return XmlUtils.convertValueToList(
getAttributeValue(namespace, attribute), options, defaultValue);
}
public boolean getAttributeBooleanValue(String namespace, String attribute,
boolean defaultValue) {
return XmlUtils.convertValueToBoolean(
getAttributeValue(namespace, attribute), defaultValue);
}
public int getAttributeResourceValue(String namespace, String attribute,
int defaultValue) {
return XmlUtils.convertValueToInt(
getAttributeValue(namespace, attribute), defaultValue);
}
public int getAttributeIntValue(String namespace, String attribute,
int defaultValue) {
return XmlUtils.convertValueToInt(
getAttributeValue(namespace, attribute), defaultValue);
}
public int getAttributeUnsignedIntValue(String namespace, String attribute,
int defaultValue) {
return XmlUtils.convertValueToUnsignedInt(
getAttributeValue(namespace, attribute), defaultValue);
}
public float getAttributeFloatValue(String namespace, String attribute,
float defaultValue) {
String s = getAttributeValue(namespace, attribute);
if (s != null) {
return Float.parseFloat(s);
}
return defaultValue;
}
public int getAttributeListValue(int index,
String[] options, int defaultValue) {
return XmlUtils.convertValueToList(
getAttributeValue(index), options, defaultValue);
}
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
return XmlUtils.convertValueToBoolean(
getAttributeValue(index), defaultValue);
}
public int getAttributeResourceValue(int index, int defaultValue) {
return XmlUtils.convertValueToInt(
getAttributeValue(index), defaultValue);
}
public int getAttributeIntValue(int index, int defaultValue) {
return XmlUtils.convertValueToInt(
getAttributeValue(index), defaultValue);
}
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
return XmlUtils.convertValueToUnsignedInt(
getAttributeValue(index), defaultValue);
}
public float getAttributeFloatValue(int index, float defaultValue) {
String s = getAttributeValue(index);
if (s != null) {
return Float.parseFloat(s);
}
return defaultValue;
}
public String getIdAttribute() {
return getAttributeValue(null, "id");
}
public String getClassAttribute() {
return getAttributeValue(null, "class");
}
public int getIdAttributeResourceValue(int defaultValue) {
return getAttributeResourceValue(null, "id", defaultValue);
}
public int getStyleAttribute() {
return getAttributeResourceValue(null, "style", 0);
}
private XmlPullParser mParser;
}
+6
View File
@@ -0,0 +1,6 @@
<HTML>
<BODY>
Provides common utility methods such as date/time manipulation, base64 encoders
and decoders, string and number conversion methods, and XML utilities.
</BODY>
</HTML>