use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.
the class ParserFactory method readAndClose.
@NonNull
private static InputStream readAndClose(@NonNull InputStream stream, @Nullable String name, long size) throws XmlPullParserException {
// just a sanity check. It's doubtful we'll have such big files!
if (size > Integer.MAX_VALUE) {
throw new XmlPullParserException("File " + name + " is too big to be parsed");
}
int intSize = (int) size;
// create a buffered reader to facilitate reading.
BufferedInputStream bufferedStream = new BufferedInputStream(stream);
try {
int avail;
if (intSize != -1) {
avail = intSize;
} else {
// get the size to read.
avail = bufferedStream.available();
}
// create the initial buffer and read it.
byte[] buffer = new byte[avail];
int read = stream.read(buffer);
// this is the easy case.
if (read == intSize) {
return new ByteArrayInputStream(buffer);
}
// available() returned!)
while ((avail = bufferedStream.available()) > 0) {
if (read + avail > buffer.length) {
// just allocate what is needed. We're mostly reading small files
// so it shouldn't be too problematic.
byte[] moreBuffer = new byte[read + avail];
System.arraycopy(buffer, 0, moreBuffer, 0, read);
buffer = moreBuffer;
}
read += stream.read(buffer, read, avail);
}
// return a new stream encapsulating this buffer.
return new ByteArrayInputStream(buffer);
} catch (IOException e) {
throw new XmlPullParserException("Failed to read " + name, null, e);
} finally {
try {
bufferedStream.close();
} catch (IOException ignored) {
}
}
}
use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.
the class ImageUtils method scale.
/**
* Resize the given image
*
* @param source the image to be scaled
* @param xScale x scale
* @param yScale y scale
* @return the scaled image
*/
@NonNull
public static BufferedImage scale(@NonNull BufferedImage source, double xScale, double yScale) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
int destWidth = Math.max(1, (int) (xScale * sourceWidth));
int destHeight = Math.max(1, (int) (yScale * sourceHeight));
int imageType = source.getType();
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
if (xScale > 0.5 && yScale > 0.5) {
BufferedImage scaled = new BufferedImage(destWidth, destHeight, imageType);
Graphics2D g2 = scaled.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setColor(new Color(0, true));
g2.fillRect(0, 0, destWidth, destHeight);
if (xScale == 1 && yScale == 1) {
g2.drawImage(source, 0, 0, null);
} else {
setRenderingHints(g2);
g2.drawImage(source, 0, 0, destWidth, destHeight, 0, 0, sourceWidth, sourceHeight, null);
}
g2.dispose();
return scaled;
} else {
// When creating a thumbnail, using the above code doesn't work very well;
// you get some visible artifacts, especially for text. Instead use the
// technique of repeatedly scaling the image into half; this will cause
// proper averaging of neighboring pixels, and will typically (for the kinds
// of screen sizes used by this utility method in the layout editor) take
// about 3-4 iterations to get the result since we are logarithmically reducing
// the size. Besides, each successive pass in operating on much fewer pixels
// (a reduction of 4 in each pass).
//
// However, we may not be resizing to a size that can be reached exactly by
// successively diving in half. Therefore, once we're within a factor of 2 of
// the final size, we can do a resize to the exact target size.
// However, we can get even better results if we perform this final resize
// up front. Let's say we're going from width 1000 to a destination width of 85.
// The first approach would cause a resize from 1000 to 500 to 250 to 125, and
// then a resize from 125 to 85. That last resize can distort/blur a lot.
// Instead, we can start with the destination width, 85, and double it
// successfully until we're close to the initial size: 85, then 170,
// then 340, and finally 680. (The next one, 1360, is larger than 1000).
// So, now we *start* the thumbnail operation by resizing from width 1000 to
// width 680, which will preserve a lot of visual details such as text.
// Then we can successively resize the image in half, 680 to 340 to 170 to 85.
// We end up with the expected final size, but we've been doing an exact
// divide-in-half resizing operation at the end so there is less distortion.
// Number of halving operations to perform after the initial resize
int iterations = 0;
// Width closest to source width that = 2^x, x is integer
int nearestWidth = destWidth;
int nearestHeight = destHeight;
while (nearestWidth < sourceWidth / 2) {
nearestWidth *= 2;
nearestHeight *= 2;
iterations++;
}
BufferedImage scaled = new BufferedImage(nearestWidth, nearestHeight, imageType);
Graphics2D g2 = scaled.createGraphics();
setRenderingHints(g2);
g2.drawImage(source, 0, 0, nearestWidth, nearestHeight, 0, 0, sourceWidth, sourceHeight, null);
g2.dispose();
sourceWidth = nearestWidth;
sourceHeight = nearestHeight;
source = scaled;
for (int iteration = iterations - 1; iteration >= 0; iteration--) {
int halfWidth = sourceWidth / 2;
int halfHeight = sourceHeight / 2;
scaled = new BufferedImage(halfWidth, halfHeight, imageType);
g2 = scaled.createGraphics();
setRenderingHints(g2);
g2.drawImage(source, 0, 0, halfWidth, halfHeight, 0, 0, sourceWidth, sourceHeight, null);
g2.dispose();
sourceWidth = halfWidth;
sourceHeight = halfHeight;
source = scaled;
iterations--;
}
return scaled;
}
}
use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.
the class Layout method createStatusBar.
/**
* @param isRtl whether the current locale is an RTL locale.
* @param isRtlSupported whether the applications supports RTL (i.e. has supportsRtl=true
* in the manifest and targetSdkVersion >= 17.
*/
@NonNull
private StatusBar createStatusBar(BridgeContext context, Density density, boolean isRtl, boolean isRtlSupported, int simulatedPlatformVersion) {
StatusBar statusBar = new StatusBar(context, density, isRtl, isRtlSupported, simulatedPlatformVersion);
LayoutParams params = createLayoutParams(MATCH_PARENT, mBuilder.mStatusBarSize);
if (mBuilder.isNavBarVertical()) {
params.addRule(START_OF, getId(ID_NAV_BAR));
}
statusBar.setLayoutParams(params);
statusBar.setId(getId(ID_STATUS_BAR));
return statusBar;
}
use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.
the class Layout method createNavBar.
/**
* @param isRtl whether the current locale is an RTL locale.
* @param isRtlSupported whether the applications supports RTL (i.e. has supportsRtl=true
* in the manifest and targetSdkVersion >= 17.
*/
@NonNull
private NavigationBar createNavBar(BridgeContext context, Density density, boolean isRtl, boolean isRtlSupported, int simulatedPlatformVersion) {
int orientation = mBuilder.mNavBarOrientation;
int size = mBuilder.mNavBarSize;
NavigationBar navBar = new NavigationBar(context, density, orientation, isRtl, isRtlSupported, simulatedPlatformVersion);
boolean isVertical = mBuilder.isNavBarVertical();
int w = isVertical ? size : MATCH_PARENT;
int h = isVertical ? MATCH_PARENT : size;
LayoutParams params = createLayoutParams(w, h);
params.addRule(isVertical ? ALIGN_PARENT_END : ALIGN_PARENT_BOTTOM);
navBar.setLayoutParams(params);
navBar.setId(getId(ID_NAV_BAR));
return navBar;
}
use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.
the class TaskPersister method loadPersistedTaskIdsForUser.
@NonNull
SparseBooleanArray loadPersistedTaskIdsForUser(int userId) {
if (mTaskIdsInFile.get(userId) != null) {
return mTaskIdsInFile.get(userId).clone();
}
final SparseBooleanArray persistedTaskIds = new SparseBooleanArray();
synchronized (mIoLock) {
BufferedReader reader = null;
String line;
try {
reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
while ((line = reader.readLine()) != null) {
for (String taskIdString : line.split("\\s+")) {
int id = Integer.parseInt(taskIdString);
persistedTaskIds.put(id, true);
}
}
} catch (FileNotFoundException e) {
// File doesn't exist. Ignore.
} catch (Exception e) {
Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
} finally {
IoUtils.closeQuietly(reader);
}
}
mTaskIdsInFile.put(userId, persistedTaskIds);
return persistedTaskIds.clone();
}
Aggregations