下面列出了android.util.SparseArray#append ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a new view by inflating the associated XML layout.
*
* @param context The {@link Context} to use.
* @param parent The inflated view's parent.
* @return The {@link View} that was inflated from the layout.
*/
public final View createNewView(final Context context, final ViewGroup parent) {
View view = mLayoutInflater.inflate(mLayoutResourceId, parent, false);
SparseArray<Holder> holders = new SparseArray<Holder>();
int size = mViewIdsAndMetaCache.size();
for (int i = 0; i < size; i++) {
int viewId = mViewIdsAndMetaCache.keyAt(i);
Meta meta = mViewIdsAndMetaCache.get(viewId);
View viewFromLayout = view.findViewById(viewId);
if (viewFromLayout == null) {
String message = String.format("Cannot find View, check the 'viewId' " +
"attribute on method %s.%s()",
mDataType.getName(), meta.method.getName());
throw new IllegalStateException(message);
}
holders.append(viewId, new Holder(viewFromLayout, meta));
mAnnotatedViewIds.add(viewId);
}
view.setTag(mLayoutResourceId, holders);
return view;
}
public CommentsTimeLinePagerAdapter(CommentsTimeLineFragment fragment, ViewPager viewPager, FragmentManager fm,
SparseArray<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
fragmentList.append(CommentsTimeLineFragment.COMMENTS_TO_ME_CHILD_POSITION,
fragment.getCommentsToMeTimeLineFragment());
fragmentList.append(CommentsTimeLineFragment.COMMENTS_BY_ME_CHILD_POSITION,
fragment.getCommentsByMeTimeLineFragment());
FragmentTransaction transaction = fragment.getChildFragmentManager().beginTransaction();
if (!fragmentList.get(CommentsTimeLineFragment.COMMENTS_TO_ME_CHILD_POSITION).isAdded())
transaction.add(viewPager.getId(), fragmentList.get(CommentsTimeLineFragment.COMMENTS_TO_ME_CHILD_POSITION),
CommentsToMeTimeLineFragment.class.getName());
if (!fragmentList.get(CommentsTimeLineFragment.COMMENTS_BY_ME_CHILD_POSITION).isAdded())
transaction.add(viewPager.getId(), fragmentList.get(CommentsTimeLineFragment.COMMENTS_BY_ME_CHILD_POSITION),
CommentsByMeTimeLineFragment.class.getName());
if (!transaction.isEmpty()) {
transaction.commit();
fragment.getChildFragmentManager().executePendingTransactions();
}
}
private void executeViewHandlers(final SparseArray<Holder> holders,
final View parent, final View view, final T instance, final int position) {
int nViewHandlers = mViewHandlers.size();
for (int i = 0; i < nViewHandlers; i++) {
int viewId = mViewHandlers.keyAt(i);
ViewHandler<T> viewHandler = mViewHandlers.get(viewId);
if (viewHandler == null) continue;
if (viewId == mLayoutResourceId) {
viewHandler.handleView(mAdapter, parent, view, instance, position);
} else {
Holder holder = holders.get(viewId);
View viewWithId = null;
if (holder != null) {
viewWithId = holder.view;
} else {
viewWithId = view.findViewById(viewId);
holders.append(viewId, new Holder(viewWithId, null));
}
viewHandler.handleView(mAdapter, view, viewWithId, instance, position);
}
}
}
@NonNull
public Location require(int x, int y, Cluster cluster) {
if (x < 0 || y < 0)
throw new ArrayIndexOutOfBoundsException("x= " + String.valueOf(x) + " ; y= " + String.valueOf(y));
SparseArray<Location> col = super.get(x);
if (col == null) {
col = new SparseArray<>(cluster.height);
super.append(x, col);
col.append(y, new Location());
return col.get(y);
}
Location cel = col.get(y);
if (cel == null) {
cel = new Location();
col.append(y, cel);
}
return cel;
}
private static SparseArray<String[]> generatePermissionsMapping(
WindowAndroid windowAndroid, int[] contentSettingsTypes) {
SparseArray<String[]> permissionsToRequest = new SparseArray<>();
for (int i = 0; i < contentSettingsTypes.length; i++) {
String[] permissions = PrefServiceBridge.getAndroidPermissionsForContentSetting(
contentSettingsTypes[i]);
if (permissions == null) continue;
List<String> missingPermissions = new ArrayList<>();
for (int j = 0; j < permissions.length; j++) {
String permission = permissions[j];
if (!windowAndroid.hasPermission(permission)) missingPermissions.add(permission);
}
if (!missingPermissions.isEmpty()) {
permissionsToRequest.append(contentSettingsTypes[i],
missingPermissions.toArray(new String[missingPermissions.size()]));
}
}
return permissionsToRequest;
}
public static SparseArray<File> getDataFiles(final String folderSubPath){
if(folderSubPath == null || folderSubPath.trim().isEmpty()){
return null;
}
SparseArray<File> files = new SparseArray<>();
File file = new File(Environment.getExternalStorageDirectory() + folderSubPath);
if(!file.exists() || !file.isDirectory())
return null;
File[] tempFiles = file.listFiles();
if(tempFiles == null)
return null;
int count = 0;
for(File tempFile : tempFiles){
if(!tempFile.isDirectory()){
files.append(count++, tempFile);
}
}
return files;
}
private static SparseArray<Long> getInitialBitrateEstimatesForCountry(String countryCode) {
int[] groupIndices = getCountryGroupIndices(countryCode);
SparseArray<Long> result = new SparseArray<>(/* initialCapacity= */ 6);
result.append(C.NETWORK_TYPE_UNKNOWN, DEFAULT_INITIAL_BITRATE_ESTIMATE);
result.append(C.NETWORK_TYPE_WIFI, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
result.append(C.NETWORK_TYPE_2G, DEFAULT_INITIAL_BITRATE_ESTIMATES_2G[groupIndices[1]]);
result.append(C.NETWORK_TYPE_3G, DEFAULT_INITIAL_BITRATE_ESTIMATES_3G[groupIndices[2]]);
result.append(C.NETWORK_TYPE_4G, DEFAULT_INITIAL_BITRATE_ESTIMATES_4G[groupIndices[3]]);
// Assume default Wifi bitrate for Ethernet to prevent using the slower fallback bitrate.
result.append(
C.NETWORK_TYPE_ETHERNET, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
return result;
}
@Override
protected String getTag(int position) {
SparseArray<String> tagList = new SparseArray<String>();
tagList.append(CommentsTimeLineFragment.COMMENTS_TO_ME_CHILD_POSITION, CommentsToMeTimeLineFragment.class.getName());
tagList.append(CommentsTimeLineFragment.COMMENTS_BY_ME_CHILD_POSITION, CommentsByMeTimeLineFragment.class.getName());
return tagList.get(position);
}
@Test
public void testSparseArray(){
SparseArray<SubParcel> input = new SparseArray<SubParcel>();
input.append(1, new SubParcel("name"));
input.append(2, null);
SparseArray<SubParcel> exampleSparseArray = Parcels.unwrap(ParcelsTestUtil.wrap(input));
assertEquals(2, exampleSparseArray.size());
SubParcel output = exampleSparseArray.get(1);
assertEquals("name", output.getName());
assertNull(exampleSparseArray.get(2));
}
private void addTintListToCache(@NonNull Context context, @DrawableRes int resId,
@NonNull ColorStateList tintList) {
if (mTintLists == null) {
mTintLists = new WeakHashMap<>();
}
SparseArray<ColorStateList> themeTints = mTintLists.get(context);
if (themeTints == null) {
themeTints = new SparseArray<>();
mTintLists.put(context, themeTints);
}
themeTints.append(resId, tintList);
}
private void addTintListToCache(@NonNull Context context, @DrawableRes int resId,
@NonNull ColorStateList tintList) {
if (mTintLists == null) {
mTintLists = new WeakHashMap<>();
}
SparseArray<ColorStateList> themeTints = mTintLists.get(context);
if (themeTints == null) {
themeTints = new SparseArray<>();
mTintLists.put(context, themeTints);
}
themeTints.append(resId, tintList);
}
/**
* register a new device id or add feature to an already defined device
* <p>the change will affect only the node discover after this call</p>
* @param deviceId device type that will use the feature, it can be a new device id
* @param features array of feature that we will add, the index of the feature will be used
* as feature mask. the feature mask must have only one bit to 1
* @throws InvalidFeatureBitMaskException throw when a feature is a position that is not a
* power of 2
*/
public static void addFeatureToNode(byte deviceId,SparseArray<Class<? extends Feature>> features)
throws InvalidFeatureBitMaskException {
SparseArray<Class<? extends Feature>> updateMe;
if(!sFeatureMapDecoder.containsKey(deviceId)){
updateMe = BLENodeDefines.FeatureCharacteristics.DEFAULT_MASK_TO_FEATURE.clone();
sFeatureMapDecoder.put(deviceId,updateMe);
}else{
updateMe = sFeatureMapDecoder.get(deviceId);
}//if-else
SparseArray<Class<? extends Feature>> addMe = features.clone();
long mask=1;
//we test all the 32bit of the feature mask
for(int i=0; i<32; i++ ){
Class<? extends Feature> featureClass = addMe.get((int)mask);
if (featureClass != null) {
updateMe.append((int) mask, featureClass);
addMe.remove((int)mask);
}
mask=mask<<1;
}//for
if(addMe.size()!=0)
throw new InvalidFeatureBitMaskException("Not all elements have a single bit in " +
"as key");
}
public static SparseArray<String> getCalendarList(Context context) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
return null;
}
SparseArray<String> calendar = new SparseArray<>();
final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL
};
final ContentResolver cr = context.getContentResolver();
final Uri uri = CalendarContract.Calendars.CONTENT_URI;
Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);
if (cur == null)
return null;
while (cur.moveToNext()) {
if (cur.getInt(3) < 300)
continue;
Long id = cur.getLong(0);
String name = cur.getString(1);
calendar.append(id.intValue(), name);
}
cur.close();
return calendar;
}
private void setColumnScale(int x, float scale) {
SparseArray<Location> col = cluster.map.require(x, cluster);
for (int i = 0; i < cluster.height; i++) {
Location cel = col.get(i);
if (cel == null) {
cel = new Location();
col.append(i, cel);
}
cel.sizeX = scale;
}
}
/**
* インターフェースを取得する
* @param interface_id
* @param altsetting
* @return
* @throws IllegalStateException
*/
@SuppressLint("NewApi")
public synchronized UsbInterface getInterface(final int interface_id, final int altsetting)
throws IllegalStateException {
checkConnection();
SparseArray<UsbInterface> intfs = mInterfaces.get(interface_id);
if (intfs == null) {
intfs = new SparseArray<UsbInterface>();
mInterfaces.put(interface_id, intfs);
}
UsbInterface intf = intfs.get(altsetting);
if (intf == null) {
final UsbDevice device = mWeakDevice.get();
final int n = device.getInterfaceCount();
for (int i = 0; i < n; i++) {
final UsbInterface temp = device.getInterface(i);
if ((temp.getId() == interface_id) && (temp.getAlternateSetting() == altsetting)) {
intf = temp;
break;
}
}
if (intf != null) {
intfs.append(altsetting, intf);
}
}
return intf;
}
@Override
public SparseArray<Document> detect(Frame frame) {
SparseArray<Document> detections = new SparseArray<>();
if(frame.getBitmap() != null) {
Document doc = detectDocument(frame);
if (doc != null) detections.append(frame.getMetadata().getId(), doc);
}
return detections;
}
static SparseArray<Parcelable> readSparseArray(Parcel in, ClassLoader loader) {
int size = in.readInt();
if (size == -1) {
return null;
}
SparseArray<Parcelable> map = new SparseArray<>(size);
while (size != 0) {
int key = in.readInt();
Parcelable value = in.readParcelable(loader);
map.append(key, value);
size--;
}
return map;
}
@Test(expected = InvalidFeatureBitMaskException.class)
public void addInvalidFeatureMask() throws InvalidFeatureBitMaskException{
SparseArray<Class<? extends Feature>> invalidMask = new SparseArray<>(1);
invalidMask.append(3, Feature.class); //not a power of 2
Manager.addFeatureToNode((byte) 0x00, invalidMask);
}
private static synchronized void startRecord(int hash, SparseArray<Long> container) {
long startTimeMs = SystemClock.uptimeMillis();
if (container.get(hash) == null) {
container.append(hash, startTimeMs);
}
}
public static SparseArray<String> getCalendarListPrimary(Context context) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
return null;
}
SparseArray<String> calendar = new SparseArray<>();
final String[] EVENT_PROJECTION;
EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
CalendarContract.Calendars.IS_PRIMARY
};
final ContentResolver cr = context.getContentResolver();
final Uri uri = CalendarContract.Calendars.CONTENT_URI;
Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);
while (cur.moveToNext()) {
if (cur.getInt(3) < 300)
continue;
if (cur.getInt(4) != 1)
continue;
Long id = cur.getLong(0);
String name = cur.getString(1);
calendar.append(id.intValue(), name);
}
cur.close();
return calendar;
}