List of usage examples for android.util SparseArray put
public void put(int key, E value)
From source file:com.zenithed.core.widget.scaledimageview.ScaledImageView.java
/** * Once source image and view dimensions are known, creates a map of sample size to tile grid. *///from w w w . j ava 2 s . co m private static SparseArray<List<Tile>> initialiseTileMap(int fullImageSampleSize, int contentWidth, int contentHeight) { final SparseArray<List<Tile>> tileMap = new SparseArray<List<Tile>>(); int sampleSize = fullImageSampleSize; int tilesPerSide = 1; List<Tile> tileGrid = null; Tile tile = null; while (true) { int sTileWidth = contentWidth / tilesPerSide; int sTileHeight = contentHeight / tilesPerSide; int subTileWidth = sTileWidth / sampleSize; int subTileHeight = sTileHeight / sampleSize; while (subTileWidth > TILE_MAX_SIZE || subTileHeight > TILE_MAX_SIZE) { tilesPerSide *= 2; sTileWidth = contentWidth / tilesPerSide; sTileHeight = contentHeight / tilesPerSide; subTileWidth = sTileWidth / sampleSize; subTileHeight = sTileHeight / sampleSize; } tileGrid = new ArrayList<Tile>(tilesPerSide * tilesPerSide); for (int x = 0; x < tilesPerSide; x++) { for (int y = 0; y < tilesPerSide; y++) { tile = new Tile(); tile.sampleSize = sampleSize; tile.sRect = new Rect(x * sTileWidth, y * sTileHeight, (x + 1) * sTileWidth, (y + 1) * sTileHeight); tileGrid.add(tile); } } tileMap.put(sampleSize, tileGrid); tilesPerSide = (tilesPerSide == 1) ? 4 : tilesPerSide * 2; if (sampleSize == 1) { break; } else { sampleSize /= 2; } } return tileMap; }
From source file:fr.cph.chicago.task.GlobalConnectTask.java
@Override protected final Boolean doInBackground(final Void... connects) { mTrainBoolean = true;//from www . j a v a 2 s . c o m mBusBoolean = true; mBikeBoolean = true; mNetworkAvailable = Util.isNetworkAvailable(); if (mNetworkAvailable) { CtaConnect ctaConnect = CtaConnect.getInstance(); DivvyConnect divvyConnect = DivvyConnect.getInstance(); if (mLoadTrains) { try { for (Entry<String, Object> entry : mParams.entrySet()) { String key = entry.getKey(); if (key.equals("mapid")) { Object value = entry.getValue(); if (value instanceof String) { String xmlResult = ctaConnect.connect(mRequestType, mParams); this.mTrainArrivals = mXml.parseArrivals(xmlResult, mData); } else if (value instanceof List) { @SuppressWarnings("unchecked") List<String> list = (List<String>) value; if (list.size() < 5) { String xmlResult = ctaConnect.connect(mRequestType, mParams); this.mTrainArrivals = mXml.parseArrivals(xmlResult, mData); } else { int size = list.size(); SparseArray<TrainArrival> tempArrivals = new SparseArray<TrainArrival>(); int start = 0; int end = 4; while (end < size + 1) { List<String> subList = list.subList(start, end); MultiMap<String, String> paramsTemp = new MultiValueMap<String, String>(); for (String sub : subList) { paramsTemp.put(key, sub); } String xmlResult = ctaConnect.connect(mRequestType, paramsTemp); SparseArray<TrainArrival> temp = mXml.parseArrivals(xmlResult, mData); for (int j = 0; j < temp.size(); j++) { tempArrivals.put(temp.keyAt(j), temp.valueAt(j)); } start = end; if (end + 3 >= size - 1 && end != size) { end = size; } else { end = end + 3; } } this.mTrainArrivals = tempArrivals; } } } } // Apply filters int index = 0; while (index < mTrainArrivals.size()) { TrainArrival arri = mTrainArrivals.valueAt(index++); List<Eta> etas = arri.getEtas(); // Sort Eta by arriving time Collections.sort(etas); // Copy data into new list to be able to avoid looping on a list that we want to // modify List<Eta> etas2 = new ArrayList<Eta>(); etas2.addAll(etas); int j = 0; Eta eta = null; Station station = null; TrainLine line = null; TrainDirection direction = null; for (int i = 0; i < etas2.size(); i++) { eta = etas2.get(i); station = eta.getStation(); line = eta.getRouteName(); direction = eta.getStop().getDirection(); boolean toRemove = Preferences.getTrainFilter(station.getId(), line, direction); if (!toRemove) { etas.remove(i - j++); } } } } catch (ConnectException e) { mTrainBoolean = false; this.mTrackerTrainException = e; } catch (ParserException e) { mTrainBoolean = false; this.mTrackerTrainException = e; } } if (mLoadBuses) { try { List<String> rts = new ArrayList<String>(); List<String> stpids = new ArrayList<String>(); for (Entry<String, Object> entry : mParams2.entrySet()) { String key = entry.getKey(); StringBuilder str = new StringBuilder(); int i = 0; @SuppressWarnings("unchecked") List<String> values = (ArrayList<String>) entry.getValue(); for (String v : values) { str.append(v + ","); if (i == 9 || i == values.size() - 1) { if (key.equals("rt")) { rts.add(str.toString()); } else if (key.equals("stpid")) { stpids.add(str.toString()); } str = new StringBuilder(); i = -1; } i++; } } for (int i = 0; i < rts.size(); i++) { MultiMap<String, String> para = new MultiValueMap<String, String>(); para.put("rt", rts.get(i)); para.put("stpid", stpids.get(i)); String xmlResult = ctaConnect.connect(mRequestType2, para); this.mBusArrivals.addAll(mXml.parseBusArrivals(xmlResult)); } } catch (ConnectException e) { mBusBoolean = false; this.mTrackerBusException = e; } catch (ParserException e) { mBusBoolean = false; this.mTrackerBusException = e; } } if (mLoadBikes) { try { String bikeContent = divvyConnect.connect(); this.mBikeStations = mJson.parseStations(bikeContent); Collections.sort(this.mBikeStations, Util.BIKE_COMPARATOR_NAME); } catch (ParserException e) { mBikeBoolean = false; this.mTrackerBikeException = e; } catch (ConnectException e) { mBikeBoolean = false; this.mTrackerBikeException = e; } finally { if (!(mBusBoolean && mTrainBoolean)) { if (mParams2.size() == 0 && mBusBoolean) { mBusBoolean = false; } if (mParams.size() == 0 && mTrainBoolean) { mTrainBoolean = false; } } } } return mTrainBoolean || mBusBoolean || mBikeBoolean; } else { return mNetworkAvailable; } }
From source file:com.blestep.sportsbracelet.view.TimelineChartView.java
private void processData() { if (mCursor != null && !mCursor.isClosed() && mCursor.moveToFirst()) { // Load the cursor to memory double max = 0d; final SparseArray<Object[]> data = new SparseArray<>(mCursor.getCount()); do {// www .j a va 2 s .c o m int position = mCursor.getInt(0); String label = mCursor.getString(1); double stepCount = mCursor.getDouble(2); double stepDuration = mCursor.getDouble(3); double stepDistance = mCursor.getDouble(4); double stepCalorie = mCursor.getDouble(5); if (stepCount > max) { max = stepCount; } Object[] values = new Object[] { label, stepCount, stepDuration, stepDistance, stepCalorie }; data.put(position, values); } while (mCursor.moveToNext()); // Calculate the max available offset int size = data.size() - 1; float maxOffset = mBarWidth * size; //swap data synchronized (mLock) { mData = data; mMaxValue = max; mMaxOffset = maxOffset; mLastOffset = -1.f; mCurrentPosition = size; mCurrentOffset = computeOffsetForPosition(mCurrentPosition); setupTickLabels(); } } }
From source file:com.blestep.sportsbracelet.view.TimelineChartViewSleep.java
private void processData() { if (mCursor != null && !mCursor.isClosed() && mCursor.moveToFirst()) { // Load the cursor to memory double max = 0d; final SparseArray<Object[]> data = new SparseArray<>(mCursor.getCount()); do {/*w ww. j a v a 2 s .c o m*/ int position = mCursor.getInt(0); String label = mCursor.getString(1); String start = mCursor.getString(2); String end = mCursor.getString(3); double deep = mCursor.getDouble(4); double light = mCursor.getDouble(5); double awake = mCursor.getDouble(6); double asleep = mCursor.getDouble(7); if (asleep > max) { max = asleep; } Object[] values = new Object[] { label, start, end, deep, light, awake, asleep }; data.put(position, values); } while (mCursor.moveToNext()); // Calculate the max available offset int size = data.size() - 1; float maxOffset = mBarWidth * size; //swap data synchronized (mLock) { mData = data; mMaxValue = max; mMaxOffset = maxOffset; mLastOffset = -1.f; mCurrentPosition = size; mCurrentOffset = computeOffsetForPosition(mCurrentPosition); setupTickLabels(); } } }
From source file:ru.gkpromtech.exhibition.organizations.OrganizationsFragment.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle arguments = getArguments();//from ww w . j a va 2s .c o m if (arguments != null) { mGroupId = arguments.getString(ARG_GROUP_ID); mType = arguments.getInt(ARG_TYPE); //noinspection unchecked mItems = (List<Item>) arguments.getSerializable(ARG_ITEMS); mFilter = arguments.getString(ARG_FILTER); } if (mItems == null) { DbHelper db = DbHelper.getInstance(getActivity()); mItems = new ArrayList<>(); Table<Organization> organizationsTable = db.getTableFor(Organization.class); try { List<Pair<Entity[], Organization>> placesOrganizations; if (mType == GROUPED) { placesOrganizations = organizationsTable.selectJoined( new Table.Join[] { new Table.Join("id", PlacesOrganization.class, "organizationid"), new Table.Join(Place.class, "f1.id = f0.placeid"), new Table.Join(Group.class, "f2.id = f1.groupid") }, null, null, "f2.sortorder, t.fullname"); } else { placesOrganizations = organizationsTable .selectJoined( new Table.Join[] { new Table.Join("id", PlacesOrganization.class, "organizationid", "LEFT"), new Table.Join(Place.class, "f1.id = f0.placeid", "LEFT"), new Table.Join(Group.class, "f2.id = f1.groupid", "LEFT") }, null, null, "t.fullname"); } String lastTitle = null; int pos = 0; SparseArray<Item> itemsCache = new SparseArray<>(); for (Pair<Entity[], Organization> res : placesOrganizations) { Place place = (Place) res.first[1]; Organization organization = res.second; Group group = (Group) res.first[2]; Item item = null; if (mType == GROUPED) { if (!group.name.equals(lastTitle)) { if (mGroupId != null && mGroupPosition == -1 && mGroupId.equals(group.position)) mGroupPosition = pos; mItems.add(new Item(group.name)); ++pos; lastTitle = group.name; } } else { item = itemsCache.get(organization.id); } if (item == null) { item = new Item(group, place, organization); item.placesStr = place.name; mItems.add(item); itemsCache.put(organization.id, item); } else { if (item.addPlaces == null) item.addPlaces = new ArrayList<>(); item.addPlaces.add(place); item.placesStr += ", " + place.name; } ++pos; } } catch (Exception e) { e.printStackTrace(); } } mAdapter = new OrganizationsAdapter(getActivity()); if (mGroupPosition != -1) { new Handler().post(new Runnable() { @Override public void run() { mListView.setSelection(mGroupPosition); if (mFilter != null) onQueryTextChange(mFilter); } }); } }
From source file:ca.mimic.apphangar.Settings.java
protected void setUpSpinner(Spinner spinner) { String[] spinnerItems = getResources().getStringArray(R.array.entries_action_spinner); List<SparseArray<String>> items = new ArrayList<SparseArray<String>>(); for (int i = 0; i < spinnerItems.length; i++) { SparseArray<String> spinnerMap = new SparseArray<String>(); spinnerMap.put(i, spinnerItems[i]); items.add(spinnerMap);//www . j a va 2s . c o m } final List<SparseArray<String>> finalItems = items; SpinnerAdapter mSpinnerAdapter = new CustomArrayAdapter(mContext, android.R.layout.simple_spinner_dropdown_item, items); spinner.setAdapter(mSpinnerAdapter); Spinner.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { try { ((TextView) view).setText(finalItems.get(i).get(i)); } catch (NullPointerException e) { } switch (i) { case 0: return; case 1: startActivity(new Intent(mContext, AppsWidgetSettings.class)); break; case 2: startActivity(new Intent(mContext, StatsWidgetSettings.class)); break; } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }; spinner.setAdapter(mSpinnerAdapter); spinner.setOnItemSelectedListener(spinnerListener); }
From source file:android.support.transition.TransitionPort.java
/** * This method, essentially a wrapper around all calls to createAnimator for all * possible target views, is called with the entire set of start/end * values. The implementation in Transition iterates through these lists * and calls {@link #createAnimator(ViewGroup, TransitionValues, TransitionValues)} * with each set of start/end values on this transition. The * TransitionSet subclass overrides this method and delegates it to * each of its children in succession./* www .ja v a2s . c om*/ * * @hide */ @RestrictTo(GROUP_ID) protected void createAnimators(ViewGroup sceneRoot, TransitionValuesMaps startValues, TransitionValuesMaps endValues) { if (DBG) { Log.d(LOG_TAG, "createAnimators() for " + this); } ArrayMap<View, TransitionValues> endCopy = new ArrayMap<>(endValues.viewValues); SparseArray<TransitionValues> endIdCopy = new SparseArray<>(endValues.idValues.size()); for (int i = 0; i < endValues.idValues.size(); ++i) { int id = endValues.idValues.keyAt(i); endIdCopy.put(id, endValues.idValues.valueAt(i)); } LongSparseArray<TransitionValues> endItemIdCopy = new LongSparseArray<>(endValues.itemIdValues.size()); for (int i = 0; i < endValues.itemIdValues.size(); ++i) { long id = endValues.itemIdValues.keyAt(i); endItemIdCopy.put(id, endValues.itemIdValues.valueAt(i)); } // Walk through the start values, playing everything we find // Remove from the end set as we go ArrayList<TransitionValues> startValuesList = new ArrayList<>(); ArrayList<TransitionValues> endValuesList = new ArrayList<>(); for (View view : startValues.viewValues.keySet()) { TransitionValues start; TransitionValues end = null; boolean isInListView = false; if (view.getParent() instanceof ListView) { isInListView = true; } if (!isInListView) { int id = view.getId(); start = startValues.viewValues.get(view) != null ? startValues.viewValues.get(view) : startValues.idValues.get(id); if (endValues.viewValues.get(view) != null) { end = endValues.viewValues.get(view); endCopy.remove(view); } else if (id != View.NO_ID) { end = endValues.idValues.get(id); View removeView = null; for (View viewToRemove : endCopy.keySet()) { if (viewToRemove.getId() == id) { removeView = viewToRemove; } } if (removeView != null) { endCopy.remove(removeView); } } endIdCopy.remove(id); if (isValidTarget(view, id)) { startValuesList.add(start); endValuesList.add(end); } } else { ListView parent = (ListView) view.getParent(); if (parent.getAdapter().hasStableIds()) { int position = parent.getPositionForView(view); long itemId = parent.getItemIdAtPosition(position); start = startValues.itemIdValues.get(itemId); endItemIdCopy.remove(itemId); // TODO: deal with targetIDs for itemIDs for ListView items startValuesList.add(start); endValuesList.add(end); } } } int startItemIdCopySize = startValues.itemIdValues.size(); for (int i = 0; i < startItemIdCopySize; ++i) { long id = startValues.itemIdValues.keyAt(i); if (isValidTarget(null, id)) { TransitionValues start = startValues.itemIdValues.get(id); TransitionValues end = endValues.itemIdValues.get(id); endItemIdCopy.remove(id); startValuesList.add(start); endValuesList.add(end); } } // Now walk through the remains of the end set for (View view : endCopy.keySet()) { int id = view.getId(); if (isValidTarget(view, id)) { TransitionValues start = startValues.viewValues.get(view) != null ? startValues.viewValues.get(view) : startValues.idValues.get(id); TransitionValues end = endCopy.get(view); endIdCopy.remove(id); startValuesList.add(start); endValuesList.add(end); } } int endIdCopySize = endIdCopy.size(); for (int i = 0; i < endIdCopySize; ++i) { int id = endIdCopy.keyAt(i); if (isValidTarget(null, id)) { TransitionValues start = startValues.idValues.get(id); TransitionValues end = endIdCopy.get(id); startValuesList.add(start); endValuesList.add(end); } } int endItemIdCopySize = endItemIdCopy.size(); for (int i = 0; i < endItemIdCopySize; ++i) { long id = endItemIdCopy.keyAt(i); // TODO: Deal with targetIDs and itemIDs TransitionValues start = startValues.itemIdValues.get(id); TransitionValues end = endItemIdCopy.get(id); startValuesList.add(start); endValuesList.add(end); } ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators(); for (int i = 0; i < startValuesList.size(); ++i) { TransitionValues start = startValuesList.get(i); TransitionValues end = endValuesList.get(i); // Only bother trying to animate with values that differ between start/end if (start != null || end != null) { if (start == null || !start.equals(end)) { if (DBG) { View view = (end != null) ? end.view : start.view; Log.d(LOG_TAG, " differing start/end values for view " + view); if (start == null || end == null) { Log.d(LOG_TAG, " " + ((start == null) ? "start null, end non-null" : "start non-null, end null")); } else { for (String key : start.values.keySet()) { Object startValue = start.values.get(key); Object endValue = end.values.get(key); if (startValue != endValue && !startValue.equals(endValue)) { Log.d(LOG_TAG, " " + key + ": start(" + startValue + "), end(" + endValue + ")"); } } } } // TODO: what to do about targetIds and itemIds? Animator animator = createAnimator(sceneRoot, start, end); if (animator != null) { // Save animation info for future cancellation purposes View view; TransitionValues infoValues = null; if (end != null) { view = end.view; String[] properties = getTransitionProperties(); if (view != null && properties != null && properties.length > 0) { infoValues = new TransitionValues(); infoValues.view = view; TransitionValues newValues = endValues.viewValues.get(view); if (newValues != null) { for (int j = 0; j < properties.length; ++j) { infoValues.values.put(properties[j], newValues.values.get(properties[j])); } } int numExistingAnims = runningAnimators.size(); for (int j = 0; j < numExistingAnims; ++j) { Animator anim = runningAnimators.keyAt(j); AnimationInfo info = runningAnimators.get(anim); if (info.values != null && info.view == view && ((info.name == null && getName() == null) || info.name.equals(getName()))) { if (info.values.equals(infoValues)) { // Favor the old animator animator = null; break; } } } } } else { view = start.view; } if (animator != null) { AnimationInfo info = new AnimationInfo(view, getName(), WindowIdPort.getWindowId(sceneRoot), infoValues); runningAnimators.put(animator, info); mAnimators.add(animator); } } } } } }
From source file:org.indywidualni.centrumfm.util.ChangeLog.java
/** * Parse the {@code release} tag of a change log XML file. * * @param xml The {@code XmlPullParser} instance used to read the change log. * @param full If {@code true} the contents of the {@code release} tag are always added to * {@code changelog}. Otherwise only if the item's {@code versioncode} attribute is * higher than the last version code. * @param changelog The {@code SparseArray} to add a new {@link ReleaseItem} instance to. * @return {@code true} if the {@code release} element is describing changes of a version older * or equal to the last version. In that case {@code changelog} won't be modified and * {@link #readChangeLog(XmlPullParser, boolean)} will stop reading more elements from * the change log file./*w ww.j a v a 2s . c o m*/ * @throws XmlPullParserException * @throws IOException */ private boolean parseReleaseTag(XmlPullParser xml, boolean full, SparseArray<ReleaseItem> changelog) throws XmlPullParserException, IOException { String version = xml.getAttributeValue(null, ReleaseTag.ATTRIBUTE_VERSION); int versionCode; try { String versionCodeStr = xml.getAttributeValue(null, ReleaseTag.ATTRIBUTE_VERSION_CODE); versionCode = Integer.parseInt(versionCodeStr); } catch (NumberFormatException e) { versionCode = NO_VERSION; } if (!full && versionCode <= mLastVersionCode) { return true; } int eventType = xml.getEventType(); List<String> changes = new ArrayList<>(); while (eventType != XmlPullParser.END_TAG || xml.getName().equals(ChangeTag.NAME)) { if (eventType == XmlPullParser.START_TAG && xml.getName().equals(ChangeTag.NAME)) { xml.next(); changes.add(xml.getText()); } eventType = xml.next(); } ReleaseItem release = new ReleaseItem(versionCode, version, changes); changelog.put(versionCode, release); return false; }
From source file:org.telegram.ui.Components.NumberPicker.java
private void ensureCachedScrollSelectorValue(int selectorIndex) { SparseArray<String> cache = mSelectorIndexToStringCache; String scrollSelectorValue = cache.get(selectorIndex); if (scrollSelectorValue != null) { return;//w ww . j a va2 s. com } if (selectorIndex < mMinValue || selectorIndex > mMaxValue) { scrollSelectorValue = ""; } else { if (mDisplayedValues != null) { int displayedValueIndex = selectorIndex - mMinValue; scrollSelectorValue = mDisplayedValues[displayedValueIndex]; } else { scrollSelectorValue = formatNumber(selectorIndex); } } cache.put(selectorIndex, scrollSelectorValue); }
From source file:de.damdi.fitness.activity.settings.sync.WgerJSONParser.java
/** * A generic parsing method for parsing JSON to SportsEquipment, Muscle or Locale. *//*from w ww. j a v a2s . co m*/ private <T> SparseArray<T> parse(String jsonString, Class<T> c) throws JSONException { JSONObject mainObject = new JSONObject(jsonString); Log.d(TAG, "jsonString: " + mainObject.toString()); JSONArray mainArray = mainObject.getJSONArray("objects"); SparseArray<T> sparseArray = new SparseArray<T>(); // parse each exercise of the JSON Array for (int i = 0; i < mainArray.length(); i++) { JSONObject singleObject = mainArray.getJSONObject(i); Integer id = singleObject.getInt("id"); Object parsedObject; if (c.equals(Muscle.class)) { // handle Muscles String name = singleObject.getString("name"); parsedObject = mDataProvider.getMuscleByName(name); if (parsedObject == null) Log.e(TAG, "Could not find Muscle: " + name); } else if (c.equals(SportsEquipment.class)) { // handle SportsEquipment String name = singleObject.getString("name"); parsedObject = mDataProvider.getEquipmentByName(name); if (parsedObject == null) Log.e(TAG, "Could not find SportsEquipment: " + name); } else if (c.equals(Locale.class)) { // handle Locales String short_name = singleObject.getString("short_name"); parsedObject = new Locale(short_name); if (parsedObject == null) Log.e(TAG, "Could not find Locale.class: " + short_name); } else { throw new IllegalStateException( "parse(String, Class<T>) cannot be applied for class: " + c.toString()); } sparseArray.put(id, (T) parsedObject); } return sparseArray; }