List of usage examples for android.util SparseArray keyAt
public int keyAt(int index)
0...size()-1
, returns the key from the index
th key-value mapping that this SparseArray stores. From source file:com.bilibili.lib.pageradapter.IDFragmentStatePagerAdapter.java
@Override @CallSuper/*w w w. j a va 2 s.c o m*/ public void restoreState(Parcelable state, ClassLoader loader) { if (state != null) { Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); mSavedState.clear(); mFragments.clear(); SparseArray<Fragment.SavedState> fss = bundle.getSparseParcelableArray(KEY_SAVED_STATES); if (fss != null) { for (int i = 0; i < fss.size(); i++) { mSavedState.put(fss.keyAt(i), fss.valueAt(i)); } } Iterable<String> keys = bundle.keySet(); for (String key : keys) { if (key.startsWith(KEY_PREFIX)) { Fragment f = mFragmentManager.getFragment(bundle, key); int id; try { id = Integer.parseInt(key.substring(KEY_PREFIX.length())); } catch (NumberFormatException e) { // should not happen! throw new IllegalStateException("Can't find id at key " + key); } if (f != null) { f.setMenuVisibility(false); mFragments.put(id, f); } else { Log.w(TAG, "Bad fragment at key " + key); } } } } }
From source file:bolts.AppLinkNavigation.java
/** * Gets a JSONObject-compatible value for the given object. *//*from w ww .j ava 2 s . c o m*/ private Object getJSONValue(Object value) throws JSONException { if (value instanceof Bundle) { return getJSONForBundle((Bundle) value); } else if (value instanceof CharSequence) { return value.toString(); } else if (value instanceof List) { JSONArray array = new JSONArray(); for (Object listValue : (List<?>) value) { array.put(getJSONValue(listValue)); } return array; } else if (value instanceof SparseArray) { JSONArray array = new JSONArray(); SparseArray<?> sparseValue = (SparseArray<?>) value; for (int i = 0; i < sparseValue.size(); i++) { array.put(sparseValue.keyAt(i), getJSONValue(sparseValue.valueAt(i))); } return array; } else if (value instanceof Character) { return value.toString(); } else if (value instanceof Boolean) { return value; } else if (value instanceof Number) { if (value instanceof Double || value instanceof Float) { return ((Number) value).doubleValue(); } else { return ((Number) value).longValue(); } } else if (value instanceof boolean[]) { JSONArray array = new JSONArray(); for (boolean arrValue : (boolean[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof char[]) { JSONArray array = new JSONArray(); for (char arrValue : (char[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof CharSequence[]) { JSONArray array = new JSONArray(); for (CharSequence arrValue : (CharSequence[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof double[]) { JSONArray array = new JSONArray(); for (double arrValue : (double[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof float[]) { JSONArray array = new JSONArray(); for (float arrValue : (float[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof int[]) { JSONArray array = new JSONArray(); for (int arrValue : (int[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof long[]) { JSONArray array = new JSONArray(); for (long arrValue : (long[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof short[]) { JSONArray array = new JSONArray(); for (short arrValue : (short[]) value) { array.put(getJSONValue(arrValue)); } return array; } else if (value instanceof String[]) { JSONArray array = new JSONArray(); for (String arrValue : (String[]) value) { array.put(getJSONValue(arrValue)); } return array; } return null; }
From source file:fr.cph.chicago.task.GlobalConnectTask.java
@Override protected final Boolean doInBackground(final Void... connects) { mTrainBoolean = true;//from w w w .j av a 2 s .com 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.sonymobile.androidapp.gridcomputing.fragments.ReportChartFragment.java
/** * Creates a line data set to plot the chart. * @param setPosition this dataset position. * @param label The label of the data set. * @param array The sparse array containing the data to plot. * @param startValue the value at which the x axis starts. * @return a dataset ready to plot./* w ww .j a va 2s .com*/ */ private LineDataSet getData(final int setPosition, final String label, final SparseArray<Double> array, final int startValue) { final List<Entry> values = new ArrayList<>(); final int startIndex = startValue == 0 ? 0 : array.indexOfKey(startValue); for (int i = startIndex; i < array.size(); i++) { int key = array.keyAt(i); int xValue = startValue == 0 ? key : values.size() + startValue; values.add(new Entry(xValue, array.get(key).floatValue())); } for (int i = 0; i < startIndex; i++) { try { int key = array.keyAt(i); int xValue = startValue == 0 ? key : values.size() + startValue; values.add(new Entry(xValue, array.get(key).floatValue())); } catch (Exception e) { Log.e("Entry at index " + i + " does not exist"); } } LineDataSet d = new LineDataSet(values, label); d.setLineWidth(2.5f); d.setCircleRadius(4f); d.setDrawValues(false); int color = CHART_COLORS[setPosition]; d.setColor(color); d.setCircleColor(color); d.setDrawFilled(true); return d; }
From source file:fr.cph.chicago.core.fragment.NearbyFragment.java
private SparseArray<TrainArrival> loadAroundTrainArrivals(@NonNull final List<Station> trainStations) { try {// w w w .java2 s . c om final SparseArray<TrainArrival> trainArrivals = new SparseArray<>(); if (isAdded()) { final CtaConnect cta = CtaConnect.getInstance(getContext()); for (final Station station : trainStations) { final MultiValuedMap<String, String> reqParams = new ArrayListValuedHashMap<>(1, 1); reqParams.put(requestMapId, Integer.toString(station.getId())); final InputStream xmlRes = cta.connect(TRAIN_ARRIVALS, reqParams); final XmlParser xml = XmlParser.getInstance(); final SparseArray<TrainArrival> temp = xml.parseArrivals(xmlRes, DataHolder.getInstance().getTrainData()); for (int j = 0; j < temp.size(); j++) { trainArrivals.put(temp.keyAt(j), temp.valueAt(j)); } trackWithGoogleAnalytics(activity, R.string.analytics_category_req, R.string.analytics_action_get_train, TRAINS_ARRIVALS_URL, 0); } } return trainArrivals; } catch (final Throwable throwable) { throw Exceptions.propagate(throwable); } }
From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java
/** * Destroys the loader with the specified ID. * * @param context the context./*from ww w. j av a2s .co m*/ * @param loaderId the loader ID. */ static void purgeLoader(@Nonnull final Object context, final int loaderId) { final SparseArray<WeakReference<RoutineLoaderCallbacks<?>>> callbackArray = sCallbackMap.get(context); if (callbackArray == null) { return; } final LoaderManager loaderManager; if (context instanceof FragmentActivity) { final FragmentActivity activity = (FragmentActivity) context; loaderManager = activity.getSupportLoaderManager(); } else if (context instanceof Fragment) { final Fragment fragment = (Fragment) context; loaderManager = fragment.getLoaderManager(); } else { throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName()); } int i = 0; while (i < callbackArray.size()) { final RoutineLoaderCallbacks<?> callbacks = callbackArray.valueAt(i).get(); if (callbacks == null) { callbackArray.remove(callbackArray.keyAt(i)); continue; } final RoutineLoader<?, ?> loader = callbacks.mLoader; if ((loaderId == callbackArray.keyAt(i)) && (loader.getInvocationCount() == 0)) { loaderManager.destroyLoader(loaderId); callbackArray.remove(loaderId); continue; } ++i; } if (callbackArray.size() == 0) { sCallbackMap.remove(context); } }
From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java
/** * Destroys the loader with the specified ID and the specified inputs. * * @param context the context.//from w ww .jav a 2 s . co m * @param loaderId the loader ID. * @param inputs the invocation inputs. */ @SuppressWarnings("unchecked") static void purgeLoader(@Nonnull final Object context, final int loaderId, @Nonnull final List<?> inputs) { final SparseArray<WeakReference<RoutineLoaderCallbacks<?>>> callbackArray = sCallbackMap.get(context); if (callbackArray == null) { return; } final LoaderManager loaderManager; if (context instanceof FragmentActivity) { final FragmentActivity activity = (FragmentActivity) context; loaderManager = activity.getSupportLoaderManager(); } else if (context instanceof Fragment) { final Fragment fragment = (Fragment) context; loaderManager = fragment.getLoaderManager(); } else { throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName()); } int i = 0; while (i < callbackArray.size()) { final RoutineLoaderCallbacks<?> callbacks = callbackArray.valueAt(i).get(); if (callbacks == null) { callbackArray.remove(callbackArray.keyAt(i)); continue; } final RoutineLoader<Object, Object> loader = (RoutineLoader<Object, Object>) callbacks.mLoader; if ((loader.getInvocationCount() == 0) && (loaderId == callbackArray.keyAt(i)) && loader.areSameInputs(inputs)) { loaderManager.destroyLoader(loaderId); callbackArray.remove(loaderId); continue; } ++i; } if (callbackArray.size() == 0) { sCallbackMap.remove(context); } }
From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java
/** * Destroys all loaders with the specified invocation class. * * @param context the context.//from w w w . j a v a 2s . co m * @param loaderId the loader ID. * @param invocationClass the invocation class. * @param invocationArgs the invocation constructor arguments. */ static void purgeLoaders(@Nonnull final Object context, final int loaderId, @Nonnull final Class<?> invocationClass, @Nonnull final Object[] invocationArgs) { final SparseArray<WeakReference<RoutineLoaderCallbacks<?>>> callbackArray = sCallbackMap.get(context); if (callbackArray == null) { return; } final LoaderManager loaderManager; if (context instanceof FragmentActivity) { final FragmentActivity activity = (FragmentActivity) context; loaderManager = activity.getSupportLoaderManager(); } else if (context instanceof Fragment) { final Fragment fragment = (Fragment) context; loaderManager = fragment.getLoaderManager(); } else { throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName()); } int i = 0; while (i < callbackArray.size()) { final RoutineLoaderCallbacks<?> callbacks = callbackArray.valueAt(i).get(); if (callbacks == null) { callbackArray.remove(callbackArray.keyAt(i)); continue; } final RoutineLoader<?, ?> loader = callbacks.mLoader; if ((loader.getInvocationType() == invocationClass) && Arrays.equals(loader.getInvocationArgs(), invocationArgs) && (loader.getInvocationCount() == 0)) { final int id = callbackArray.keyAt(i); if ((loaderId == ContextRoutineBuilder.AUTO) || (loaderId == id)) { loaderManager.destroyLoader(id); callbackArray.remove(id); continue; } } ++i; } if (callbackArray.size() == 0) { sCallbackMap.remove(context); } }
From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java
/** * Destroys all loaders with the specified invocation class and the specified inputs. * * @param context the context.//from ww w .ja v a 2 s . c o m * @param loaderId the loader ID. * @param invocationClass the invocation class. * @param invocationArgs the invocation constructor arguments. * @param inputs the invocation inputs. */ @SuppressWarnings("unchecked") static void purgeLoader(@Nonnull final Object context, final int loaderId, @Nonnull final Class<?> invocationClass, @Nonnull final Object[] invocationArgs, @Nonnull final List<?> inputs) { final SparseArray<WeakReference<RoutineLoaderCallbacks<?>>> callbackArray = sCallbackMap.get(context); if (callbackArray == null) { return; } final LoaderManager loaderManager; if (context instanceof FragmentActivity) { final FragmentActivity activity = (FragmentActivity) context; loaderManager = activity.getSupportLoaderManager(); } else if (context instanceof Fragment) { final Fragment fragment = (Fragment) context; loaderManager = fragment.getLoaderManager(); } else { throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName()); } int i = 0; while (i < callbackArray.size()) { final RoutineLoaderCallbacks<?> callbacks = callbackArray.valueAt(i).get(); if (callbacks == null) { callbackArray.remove(callbackArray.keyAt(i)); continue; } final RoutineLoader<Object, Object> loader = (RoutineLoader<Object, Object>) callbacks.mLoader; if ((loader.getInvocationType() == invocationClass) && Arrays.equals(loader.getInvocationArgs(), invocationArgs) && (loader.getInvocationCount() == 0)) { final int id = callbackArray.keyAt(i); if (((loaderId == ContextRoutineBuilder.AUTO) || (loaderId == id)) && loader.areSameInputs(inputs)) { loaderManager.destroyLoader(id); callbackArray.remove(id); continue; } } ++i; } if (callbackArray.size() == 0) { sCallbackMap.remove(context); } }
From source file:com.tmall.wireless.tangram3.dataparser.concrete.PojoGroupBasicAdapter.java
@Override protected void diffGroup(SparseArray<Card> added, SparseArray<Card> removed) { for (int i = 0, size = removed.size(); i < size; i++) { int key = removed.keyAt(i); Card card = removed.get(key);//from w w w. j av a 2s. co m if (card != null) { try { card.removed(); } catch (Exception e) { if (card.serviceManager != null) { CellSupport cellSupport = card.serviceManager.getService(CellSupport.class); if (card.extras != null) { cellSupport.onException(card.extras.toString(), e); } else { cellSupport.onException(card.stringType, e); } } } } } for (int i = 0, size = added.size(); i < size; i++) { int key = added.keyAt(i); Card card = added.get(key); if (card != null) { try { card.added(); } catch (Exception e) { if (card.serviceManager != null) { CellSupport cellSupport = card.serviceManager.getService(CellSupport.class); if (card.extras != null) { cellSupport.onException(card.extras.toString(), e); } else { cellSupport.onException(card.stringType, e); } } } } } }