use of android.util.SparseArray in project AndroidDevelop by 7449.
the class SuperViewHolder method getView.
public <T extends View> T getView(int id) {
SparseArray<View> viewSparseArray = (SparseArray<View>) itemView.getTag();
if (null == viewSparseArray) {
viewSparseArray = new SparseArray<>();
itemView.setTag(viewSparseArray);
}
View childView = viewSparseArray.get(id);
if (null == childView) {
childView = itemView.findViewById(id);
viewSparseArray.put(id, childView);
}
return (T) childView;
}
use of android.util.SparseArray in project greedo-layout-for-android by 500px.
the class GreedoLayoutManager method preFillGrid.
/**
* Find first visible position, scrap all children, and then layout all visible views returning
* the number of pixels laid out, which could be greater than the entire view (useful for scroll
* functions).
* @param direction The direction we are filling the grid in
* @param dy Vertical offset, creating a gap that we need to fill
* @param emptyTop Offset we begin filling at
* @return Number of vertical pixels laid out
*/
private int preFillGrid(Direction direction, int dy, int emptyTop, RecyclerView.Recycler recycler, RecyclerView.State state) {
int newFirstVisiblePosition = firstChildPositionForRow(mFirstVisibleRow);
// First, detach all existing views from the layout. detachView() is a lightweight operation
// that we can use to quickly reorder views without a full add/remove.
SparseArray<View> viewCache = new SparseArray<>(getChildCount());
int startLeftOffset = getPaddingLeft();
int startTopOffset = getPaddingTop() + emptyTop;
if (getChildCount() != 0) {
startTopOffset = getDecoratedTop(getChildAt(0));
if (mFirstVisiblePosition != newFirstVisiblePosition) {
switch(direction) {
case // new row above may be shown
UP:
double previousTopRowHeight = sizeForChildAtPosition(mFirstVisiblePosition - 1).getHeight();
startTopOffset -= previousTopRowHeight;
break;
case // row may have gone off screen
DOWN:
double topRowHeight = sizeForChildAtPosition(mFirstVisiblePosition).getHeight();
startTopOffset += topRowHeight;
break;
}
}
// Cache all views by their existing position, before updating counts
for (int i = 0; i < getChildCount(); i++) {
int position = mFirstVisiblePosition + i;
final View child = getChildAt(i);
viewCache.put(position, child);
}
// Temporarily detach all cached views. Views we still need will be added back at the proper index
for (int i = 0; i < viewCache.size(); i++) {
final View cachedView = viewCache.valueAt(i);
detachView(cachedView);
}
}
mFirstVisiblePosition = newFirstVisiblePosition;
// Next, supply the grid of items that are deemed visible. If they were previously there,
// they will simply be re-attached. New views that must be created are obtained from
// the Recycler and added.
int leftOffset = startLeftOffset;
int topOffset = startTopOffset + mPendingScrollPositionOffset;
int nextPosition = mFirstVisiblePosition;
while (nextPosition >= 0 && nextPosition < state.getItemCount()) {
boolean isViewCached = true;
View view = viewCache.get(nextPosition);
if (view == null) {
view = recycler.getViewForPosition(nextPosition);
isViewCached = false;
}
if (mIsFirstViewHeader && nextPosition == HEADER_POSITION) {
measureChildWithMargins(view, 0, 0);
mHeaderViewSize = new Size(view.getMeasuredWidth(), view.getMeasuredHeight());
}
// Overflow to next row if we don't fit
Size viewSize = sizeForChildAtPosition(nextPosition);
if ((leftOffset + viewSize.getWidth()) > getContentWidth()) {
leftOffset = startLeftOffset;
Size previousViewSize = sizeForChildAtPosition(nextPosition - 1);
topOffset += previousViewSize.getHeight();
}
// These next children would no longer be visible, stop here
boolean isAtEndOfContent;
switch(direction) {
case DOWN:
isAtEndOfContent = topOffset >= getContentHeight() + dy;
break;
default:
isAtEndOfContent = topOffset >= getContentHeight();
break;
}
if (isAtEndOfContent)
break;
if (isViewCached) {
// Re-attach the cached view at its new index
attachView(view);
viewCache.remove(nextPosition);
} else {
addView(view);
measureChildWithMargins(view, 0, 0);
int right = leftOffset + viewSize.getWidth();
int bottom = topOffset + viewSize.getHeight();
layoutDecorated(view, leftOffset, topOffset, right, bottom);
}
leftOffset += viewSize.getWidth();
nextPosition++;
}
// Scrap and store views that were not re-attached (no longer visible).
for (int i = 0; i < viewCache.size(); i++) {
final View removingView = viewCache.valueAt(i);
recycler.recycleView(removingView);
}
// Calculate pixels laid out during fill
int pixelsFilled = 0;
if (getChildCount() > 0) {
pixelsFilled = getChildAt(getChildCount() - 1).getBottom();
}
return pixelsFilled;
}
use of android.util.SparseArray in project android_frameworks_base by DirtyUnicorns.
the class SecurityControllerImpl method updateState.
private void updateState() {
// Find all users with an active VPN
SparseArray<VpnConfig> vpns = new SparseArray<>();
try {
for (UserInfo user : mUserManager.getUsers()) {
VpnConfig cfg = mConnectivityManagerService.getVpnConfig(user.id);
if (cfg == null) {
continue;
} else if (cfg.legacy) {
// Legacy VPNs should do nothing if the network is disconnected. Third-party
// VPN warnings need to continue as traffic can still go to the app.
LegacyVpnInfo legacyVpn = mConnectivityManagerService.getLegacyVpnInfo(user.id);
if (legacyVpn == null || legacyVpn.state != LegacyVpnInfo.STATE_CONNECTED) {
continue;
}
}
vpns.put(user.id, cfg);
}
} catch (RemoteException rme) {
// Roll back to previous state
Log.e(TAG, "Unable to list active VPNs", rme);
return;
}
mCurrentVpns = vpns;
}
use of android.util.SparseArray in project android_frameworks_base by DirtyUnicorns.
the class MenuBuilder method dispatchSaveInstanceState.
private void dispatchSaveInstanceState(Bundle outState) {
if (mPresenters.isEmpty())
return;
SparseArray<Parcelable> presenterStates = new SparseArray<Parcelable>();
for (WeakReference<MenuPresenter> ref : mPresenters) {
final MenuPresenter presenter = ref.get();
if (presenter == null) {
mPresenters.remove(ref);
} else {
final int id = presenter.getId();
if (id > 0) {
final Parcelable state = presenter.onSaveInstanceState();
if (state != null) {
presenterStates.put(id, state);
}
}
}
}
outState.putSparseParcelableArray(PRESENTER_KEY, presenterStates);
}
use of android.util.SparseArray in project android_frameworks_base by DirtyUnicorns.
the class ScanRecord method parseFromBytes.
/**
* Parse scan record bytes to {@link ScanRecord}.
* <p>
* The format is defined in Bluetooth 4.1 specification, Volume 3, Part C, Section 11 and 18.
* <p>
* All numerical multi-byte entities and values shall use little-endian <strong>byte</strong>
* order.
*
* @param scanRecord The scan record of Bluetooth LE advertisement and/or scan response.
* @hide
*/
public static ScanRecord parseFromBytes(byte[] scanRecord) {
if (scanRecord == null) {
return null;
}
int currentPos = 0;
int advertiseFlag = -1;
List<ParcelUuid> serviceUuids = new ArrayList<ParcelUuid>();
String localName = null;
int txPowerLevel = Integer.MIN_VALUE;
SparseArray<byte[]> manufacturerData = new SparseArray<byte[]>();
Map<ParcelUuid, byte[]> serviceData = new ArrayMap<ParcelUuid, byte[]>();
try {
while (currentPos < scanRecord.length) {
// length is unsigned int.
int length = scanRecord[currentPos++] & 0xFF;
if (length == 0) {
break;
}
// Note the length includes the length of the field type itself.
int dataLength = length - 1;
// fieldType is unsigned int.
int fieldType = scanRecord[currentPos++] & 0xFF;
switch(fieldType) {
case DATA_TYPE_FLAGS:
advertiseFlag = scanRecord[currentPos] & 0xFF;
break;
case DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL:
case DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE:
parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_16_BIT, serviceUuids);
break;
case DATA_TYPE_SERVICE_UUIDS_32_BIT_PARTIAL:
case DATA_TYPE_SERVICE_UUIDS_32_BIT_COMPLETE:
parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_32_BIT, serviceUuids);
break;
case DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL:
case DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE:
parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_128_BIT, serviceUuids);
break;
case DATA_TYPE_LOCAL_NAME_SHORT:
case DATA_TYPE_LOCAL_NAME_COMPLETE:
localName = new String(extractBytes(scanRecord, currentPos, dataLength));
break;
case DATA_TYPE_TX_POWER_LEVEL:
txPowerLevel = scanRecord[currentPos];
break;
case DATA_TYPE_SERVICE_DATA:
// The first two bytes of the service data are service data UUID in little
// endian. The rest bytes are service data.
int serviceUuidLength = BluetoothUuid.UUID_BYTES_16_BIT;
byte[] serviceDataUuidBytes = extractBytes(scanRecord, currentPos, serviceUuidLength);
ParcelUuid serviceDataUuid = BluetoothUuid.parseUuidFrom(serviceDataUuidBytes);
byte[] serviceDataArray = extractBytes(scanRecord, currentPos + serviceUuidLength, dataLength - serviceUuidLength);
serviceData.put(serviceDataUuid, serviceDataArray);
break;
case DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
// The first two bytes of the manufacturer specific data are
// manufacturer ids in little endian.
int manufacturerId = ((scanRecord[currentPos + 1] & 0xFF) << 8) + (scanRecord[currentPos] & 0xFF);
byte[] manufacturerDataBytes = extractBytes(scanRecord, currentPos + 2, dataLength - 2);
manufacturerData.put(manufacturerId, manufacturerDataBytes);
break;
default:
// Just ignore, we don't handle such data type.
break;
}
currentPos += dataLength;
}
if (serviceUuids.isEmpty()) {
serviceUuids = null;
}
return new ScanRecord(serviceUuids, manufacturerData, serviceData, advertiseFlag, txPowerLevel, localName, scanRecord);
} catch (Exception e) {
Log.e(TAG, "unable to parse scan record: " + Arrays.toString(scanRecord));
// and return an empty record with raw scanRecord bytes in results
return new ScanRecord(null, null, null, -1, Integer.MIN_VALUE, null, scanRecord);
}
}
Aggregations