List of usage examples for android.util SparseArray valueAt
@SuppressWarnings("unchecked") public E valueAt(int index)
0...size()-1
, returns the value from the index
th key-value mapping that this SparseArray stores. From source file:android.transitions.everywhere.Transition.java
/** * Match start/end values by Adapter view ID. Adds matched values to mStartValuesList * and mEndValuesList and removes them from unmatchedStart and unmatchedEnd, using * startIds and endIds as a guide for which Views have unique IDs. */// w ww . j a v a 2 s . c om private void matchIds(ArrayMap<View, TransitionValues> unmatchedStart, ArrayMap<View, TransitionValues> unmatchedEnd, SparseArray<View> startIds, SparseArray<View> endIds) { int numStartIds = startIds.size(); for (int i = 0; i < numStartIds; i++) { View startView = startIds.valueAt(i); if (startView != null && isValidTarget(startView)) { View endView = endIds.get(startIds.keyAt(i)); if (endView != null && isValidTarget(endView)) { TransitionValues startValues = unmatchedStart.get(startView); TransitionValues endValues = unmatchedEnd.get(endView); if (startValues != null && endValues != null) { mStartValuesList.add(startValues); mEndValuesList.add(endValues); unmatchedStart.remove(startView); unmatchedEnd.remove(endView); } } } } }
From source file:edu.umich.flowfence.sandbox.ResolvedQM.java
@Override public void call(int flags, IQMCallback callback, List<CallParam> params) throws RemoteException { try {/*from w w w . j a v a 2 s . co m*/ if (localLOGD) { Log.d(TAG, String.format("Incoming sandbox call for %s, %d parameters:", mOriginalDescriptor, params.size())); for (CallParam param : params) { Log.d(TAG, param.toString(mContext.getClassLoader())); } } if (localLOGV) { Log.v(TAG, String.format("Callback %s, flags %d", callback, flags)); } // Sanity check. final int numParams = params.size(); if (numParams != mMemberData.countParameters()) { throw new IllegalArgumentException("Wrong number of arguments supplied"); } boolean hasReturn = (flags & CallFlags.NO_RETURN_VALUE) == 0; final ArrayList<Object> args = new ArrayList<>(); final SparseArray<IBinder> outs = new SparseArray<>(); mContext.beginQM(); try { if (hasReturn) { outs.append(CallResult.RETURN_VALUE, null); } for (int i = 0; i < numParams; i++) { CallParam param = params.get(i); int paramHeader = param.getHeader(); if (param.getType() == CallParam.TYPE_HANDLE && (paramHeader & CallParam.HANDLE_SYNC_ONLY) != 0) { Log.w(TAG, "HANDLE_SYNC_ONLY in sandbox for " + mOriginalDescriptor); continue; } // Deserialize argument, marshaling as necessary. Object arg = unpack(param); // TODO: FLAG_BY_REF args.add(arg); // Put together the out parameter for inout params. if ((paramHeader & CallParam.FLAG_RETURN) != 0) { if (localLOGV) { Log.v(TAG, String.format("Adding out param %d", i)); } outs.append(i, SandboxObject.binderForObject(this, arg)); } } // Actually do the call. Object[] argArray = args.toArray(); if (localLOGD) { Log.d(TAG, "Preparing to call " + mOriginalDescriptor.printCall(argArray)); } Object retval = mMemberData.call(argArray); if (localLOGD) { Log.d(TAG, "Call returned: " + Objects.toString(retval)); } // Bundle up handle for return value. if (hasReturn) { IBinder retvalObj = SandboxObject.binderForObject(this, retval); outs.put(CallResult.RETURN_VALUE, retvalObj); } // DEBUG: print out params if (localLOGV) { for (int i = 0; i < outs.size(); i++) { Log.v(TAG, String.format("out[%d] = %s", outs.keyAt(i), outs.valueAt(i))); } } // Post results to caller. if (localLOGD) { Log.d(TAG, "Posting results to caller"); } callback.onResult(new CallResult(outs)); } catch (InvocationTargetException ioe) { Throwable t = ioe.getTargetException(); if (t instanceof Exception) { throw ((Exception) t); } throw ioe; } finally { // Clear our ambient context. if (localLOGD) { Log.d(TAG, "Clearing call token"); } mContext.endQM(); } } catch (Exception e) { //Log.e(TAG, String.format("Error invoking %s", mOriginalDescriptor), e); callback.onResult(new CallResult(e)); } }
From source file:edu.umich.oasis.sandbox.ResolvedSoda.java
@Override public void call(int flags, ISodaCallback callback, List<CallParam> params) throws RemoteException { try {/* w w w .j a v a2s .c o m*/ if (localLOGD) { Log.d(TAG, String.format("Incoming sandbox call for %s, %d parameters:", mOriginalDescriptor, params.size())); for (CallParam param : params) { Log.d(TAG, param.toString(mContext.getClassLoader())); } } if (localLOGV) { Log.v(TAG, String.format("Callback %s, flags %d", callback, flags)); } // Sanity check. final int numParams = params.size(); if (numParams != mMemberData.countParameters()) { throw new IllegalArgumentException("Wrong number of arguments supplied"); } boolean hasReturn = (flags & CallFlags.NO_RETURN_VALUE) == 0; final ArrayList<Object> args = new ArrayList<>(); final SparseArray<IBinder> outs = new SparseArray<>(); mContext.beginSoda(); try { if (hasReturn) { outs.append(CallResult.RETURN_VALUE, null); } for (int i = 0; i < numParams; i++) { CallParam param = params.get(i); int paramHeader = param.getHeader(); if (param.getType() == CallParam.TYPE_HANDLE && (paramHeader & CallParam.HANDLE_SYNC_ONLY) != 0) { Log.w(TAG, "HANDLE_SYNC_ONLY in sandbox for " + mOriginalDescriptor); continue; } // Deserialize argument, marshaling as necessary. Object arg = unpack(param); // TODO: FLAG_BY_REF args.add(arg); // Put together the out parameter for inout params. if ((paramHeader & CallParam.FLAG_RETURN) != 0) { if (localLOGV) { Log.v(TAG, String.format("Adding out param %d", i)); } outs.append(i, SandboxObject.binderForObject(this, arg)); } } // Actually do the call. Object[] argArray = args.toArray(); if (localLOGD) { Log.d(TAG, "Preparing to call " + mOriginalDescriptor.printCall(argArray)); } Object retval = mMemberData.call(argArray); if (localLOGD) { Log.d(TAG, "Call returned: " + Objects.toString(retval)); } // Bundle up handle for return value. if (hasReturn) { IBinder retvalObj = SandboxObject.binderForObject(this, retval); outs.put(CallResult.RETURN_VALUE, retvalObj); } // DEBUG: print out params if (localLOGV) { for (int i = 0; i < outs.size(); i++) { Log.v(TAG, String.format("out[%d] = %s", outs.keyAt(i), outs.valueAt(i))); } } // Post results to caller. if (localLOGD) { Log.d(TAG, "Posting results to caller"); } callback.onResult(new CallResult(outs)); } catch (InvocationTargetException ioe) { Throwable t = ioe.getTargetException(); if (t instanceof Exception) { throw ((Exception) t); } throw ioe; } finally { // Clear our ambient context. if (localLOGD) { Log.d(TAG, "Clearing call token"); } mContext.endSoda(); } } catch (Exception e) { //Log.e(TAG, String.format("Error invoking %s", mOriginalDescriptor), e); callback.onResult(new CallResult(e)); } }
From source file:android.transitions.everywhere.Transition.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.//from w w w . j a v a2s .c o m * * @hide */ protected void createAnimators(ViewGroup sceneRoot, TransitionValuesMaps startValues, TransitionValuesMaps endValues, ArrayList<TransitionValues> startValuesList, ArrayList<TransitionValues> endValuesList) { if (DBG) { Log.d(LOG_TAG, "createAnimators() for " + this); } ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators(); long minStartDelay = Long.MAX_VALUE; int minAnimator = mAnimators.size(); SparseArray<Long> startDelays = new SparseArray<Long>(); int startValuesListCount = startValuesList.size(); for (int i = 0; i < startValuesListCount; ++i) { TransitionValues start = startValuesList.get(i); TransitionValues end = endValuesList.get(i); if (start != null && !start.targetedTransitions.contains(this)) { start = null; } if (end != null && !end.targetedTransitions.contains(this)) { end = null; } if (start == null && end == null) { continue; } // Only bother trying to animate with values that differ between start/end boolean isChanged = start == null || end == null || areValuesChanged(start, end); if (isChanged) { 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 = null; 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 != null) ? start.view : null; } if (animator != null) { if (mPropagation != null) { long delay = mPropagation.getStartDelay(sceneRoot, this, start, end); startDelays.put(mAnimators.size(), delay); minStartDelay = Math.min(delay, minStartDelay); } AnimationInfo info = new AnimationInfo(view, getName(), this, ViewUtils.getWindowId(sceneRoot), infoValues); runningAnimators.put(animator, info); mAnimators.add(animator); } } } } if (minStartDelay != 0) { for (int i = 0; i < startDelays.size(); i++) { int index = startDelays.keyAt(i); Animator animator = mAnimators.get(index); long delay = startDelays.valueAt(i) - minStartDelay + animator.getStartDelay(); animator.setStartDelay(delay); } } }
From source file:com.transitionseverywhere.Transition.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./*from www .j a v a 2s .co m*/ * * @hide */ protected void createAnimators(ViewGroup sceneRoot, TransitionValuesMaps startValues, TransitionValuesMaps endValues, ArrayList<TransitionValues> startValuesList, ArrayList<TransitionValues> endValuesList) { if (DBG) { Log.d(LOG_TAG, "createAnimators() for " + this); } ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators(); long minStartDelay = Long.MAX_VALUE; int minAnimator = mAnimators.size(); SparseArray<Long> startDelays = new SparseArray<Long>(); int startValuesListCount = startValuesList.size(); for (int i = 0; i < startValuesListCount; ++i) { TransitionValues start = startValuesList.get(i); TransitionValues end = endValuesList.get(i); if (start != null && !start.targetedTransitions.contains(this)) { start = null; } if (end != null && !end.targetedTransitions.contains(this)) { end = null; } if (start == null && end == null) { continue; } // Only bother trying to animate with values that differ between start/end boolean isChanged = start == null || end == null || isTransitionRequired(start, end); if (isChanged) { 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])); } } synchronized (sRunningAnimators) { 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 != null && info.name.equals(getName())))) { if (info.values.equals(infoValues)) { // Favor the old animator animator = null; break; } } } } } } else { view = start.view; } if (animator != null) { if (mPropagation != null) { long delay = mPropagation.getStartDelay(sceneRoot, this, start, end); startDelays.put(mAnimators.size(), delay); minStartDelay = Math.min(delay, minStartDelay); } AnimationInfo info = new AnimationInfo(view, getName(), this, ViewUtils.getWindowId(sceneRoot), infoValues); runningAnimators.put(animator, info); mAnimators.add(animator); } } } } if (startDelays.size() != 0) { for (int i = 0; i < startDelays.size(); i++) { int index = startDelays.keyAt(i); Animator animator = mAnimators.get(index); long delay = startDelays.valueAt(i) - minStartDelay + animator.getStartDelay(); animator.setStartDelay(delay); } } }
From source file:dev.ukanth.ufirewall.Api.java
/** * @param ctx application context (mandatory) * @return a list of applications// w w w. j a v a 2 s.c om */ public static List<PackageInfoData> getApps(Context ctx, GetAppList appList) { initSpecial(); if (applications != null && applications.size() > 0) { // return cached instance return applications; } final SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); final boolean enableVPN = defaultPrefs.getBoolean("enableVPN", false); final boolean enableLAN = defaultPrefs.getBoolean("enableLAN", false); final boolean enableRoam = defaultPrefs.getBoolean("enableRoam", true); final SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); final String savedPkg_wifi_uid = prefs.getString(PREF_WIFI_PKG_UIDS, ""); final String savedPkg_3g_uid = prefs.getString(PREF_3G_PKG_UIDS, ""); final String savedPkg_roam_uid = prefs.getString(PREF_ROAMING_PKG_UIDS, ""); final String savedPkg_vpn_uid = prefs.getString(PREF_VPN_PKG_UIDS, ""); final String savedPkg_lan_uid = prefs.getString(PREF_LAN_PKG_UIDS, ""); List<Integer> selected_wifi = new ArrayList<Integer>(); List<Integer> selected_3g = new ArrayList<Integer>(); List<Integer> selected_roam = new ArrayList<Integer>(); List<Integer> selected_vpn = new ArrayList<Integer>(); List<Integer> selected_lan = new ArrayList<Integer>(); selected_wifi = getListFromPref(savedPkg_wifi_uid); selected_3g = getListFromPref(savedPkg_3g_uid); if (enableRoam) { selected_roam = getListFromPref(savedPkg_roam_uid); } if (enableVPN) { selected_vpn = getListFromPref(savedPkg_vpn_uid); } if (enableLAN) { selected_lan = getListFromPref(savedPkg_lan_uid); } //revert back to old approach //always use the defaul preferences to store cache value - reduces the application usage size final SharedPreferences cachePrefs = ctx.getSharedPreferences("AFWallPrefs", Context.MODE_PRIVATE); int count = 0; try { final PackageManager pkgmanager = ctx.getPackageManager(); final List<ApplicationInfo> installed = pkgmanager .getInstalledApplications(PackageManager.GET_META_DATA); SparseArray<PackageInfoData> syncMap = new SparseArray<PackageInfoData>(); final Editor edit = cachePrefs.edit(); boolean changed = false; String name = null; String cachekey = null; final String cacheLabel = "cache.label."; PackageInfoData app = null; ApplicationInfo apinfo = null; for (int i = 0; i < installed.size(); i++) { //for (final ApplicationInfo apinfo : installed) { count = count + 1; apinfo = installed.get(i); if (appList != null) { appList.doProgress(count); } boolean firstseen = false; app = syncMap.get(apinfo.uid); // filter applications which are not allowed to access the Internet if (app == null && PackageManager.PERMISSION_GRANTED != pkgmanager .checkPermission(Manifest.permission.INTERNET, apinfo.packageName)) { continue; } // try to get the application label from our cache - getApplicationLabel() is horribly slow!!!! cachekey = cacheLabel + apinfo.packageName; name = prefs.getString(cachekey, ""); if (name.length() == 0) { // get label and put on cache name = pkgmanager.getApplicationLabel(apinfo).toString(); edit.putString(cachekey, name); changed = true; firstseen = true; } if (app == null) { app = new PackageInfoData(); app.uid = apinfo.uid; app.names = new ArrayList<String>(); app.names.add(name); app.appinfo = apinfo; app.pkgName = apinfo.packageName; syncMap.put(apinfo.uid, app); } else { app.names.add(name); } app.firstseen = firstseen; // check if this application is selected if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) { app.selected_wifi = true; } if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) { app.selected_3g = true; } if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) { app.selected_roam = true; } if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) { app.selected_vpn = true; } if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) { app.selected_lan = true; } } List<PackageInfoData> specialData = new ArrayList<PackageInfoData>(); specialData.add(new PackageInfoData(SPECIAL_UID_ANY, ctx.getString(R.string.all_item), "dev.afwall.special.any")); specialData.add(new PackageInfoData(SPECIAL_UID_KERNEL, ctx.getString(R.string.kernel_item), "dev.afwall.special.kernel")); specialData.add(new PackageInfoData(SPECIAL_UID_TETHER, ctx.getString(R.string.tethering_item), "dev.afwall.special.tether")); //specialData.add(new PackageInfoData(SPECIAL_UID_DNSPROXY, ctx.getString(R.string.dnsproxy_item), "dev.afwall.special.dnsproxy")); specialData.add(new PackageInfoData(SPECIAL_UID_NTP, ctx.getString(R.string.ntp_item), "dev.afwall.special.ntp")); specialData .add(new PackageInfoData("root", ctx.getString(R.string.root_item), "dev.afwall.special.root")); specialData.add(new PackageInfoData("media", "Media server", "dev.afwall.special.media")); specialData.add(new PackageInfoData("vpn", "VPN networking", "dev.afwall.special.vpn")); specialData.add(new PackageInfoData("shell", "Linux shell", "dev.afwall.special.shell")); specialData.add(new PackageInfoData("gps", "GPS", "dev.afwall.special.gps")); specialData.add(new PackageInfoData("adb", "ADB (Android Debug Bridge)", "dev.afwall.special.adb")); if (specialApps == null) { specialApps = new HashMap<String, Integer>(); } for (int i = 0; i < specialData.size(); i++) { app = specialData.get(i); specialApps.put(app.pkgName, app.uid); //default DNS/NTP if (app.uid != -1 && syncMap.get(app.uid) == null) { // check if this application is allowed if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) { app.selected_wifi = true; } if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) { app.selected_3g = true; } if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) { app.selected_roam = true; } if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) { app.selected_vpn = true; } if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) { app.selected_lan = true; } syncMap.put(app.uid, app); } } if (changed) { edit.commit(); } /* convert the map into an array */ applications = new ArrayList<PackageInfoData>(); for (int i = 0; i < syncMap.size(); i++) { applications.add(syncMap.valueAt(i)); } return applications; } catch (Exception e) { alert(ctx, ctx.getString(R.string.error_common) + e); } return null; }
From source file:fr.cph.chicago.xml.Xml.java
public final List<Eta> parseTrainsFollow(final String xml, final TrainData data) throws ParserException { InputStream is = new ByteArrayInputStream(xml.getBytes()); SparseArray<TrainArrival> arrivals = null; try {/* w w w .jav a2 s . c o m*/ parser.setInput(is, "UTF-8"); int eventType = parser.getEventType(); XmlArrivalTrainTag tag = null; Date tmst = null; Integer errCd = null; String errNum = null; String tagName = null; Integer staId = null; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_DOCUMENT) { arrivals = new SparseArray<TrainArrival>(); } else if (eventType == XmlPullParser.START_TAG) { tagName = parser.getName(); if (tagName.equals("tmst")) { tag = XmlArrivalTrainTag.TMST; } else if (tagName.equals("errCd")) { tag = XmlArrivalTrainTag.ERRCD; } else if (tagName.equals("errNm")) { tag = XmlArrivalTrainTag.ERRNM; } else if (tagName.equals("eta")) { tag = XmlArrivalTrainTag.ETA; } else { tag = XmlArrivalTrainTag.OTHER; } } else if (eventType == XmlPullParser.END_TAG) { tag = null; } else if (eventType == XmlPullParser.TEXT) { String text = parser.getText(); switch (tag) { case ETA: break; case OTHER: if (tagName.equals("staId")) { staId = Integer.valueOf(text); TrainArrival arri = arrivals.get(staId, new TrainArrival()); arri.setErrorCode(errCd); arri.setErrorMessage(errNum); arri.setTimeStamp(tmst); List<Eta> etas = arri.getEtas(); if (etas == null) { etas = new ArrayList<Eta>(); arri.setEtas(etas); } Eta eta = new Eta(); Station station = data.getStation(Integer.valueOf(text)); eta.setStation(station); etas.add(eta); arrivals.append(staId, arri); } else if (tagName.equals("stpId")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); Stop stop = data.getStop(Integer.valueOf(text)); currentEta.setStop(stop); } } else if (tagName.equals("staNm")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); Station station = currentEta.getStation(); station.setName(text); } } else if (tagName.equals("stpDe")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); Stop stop = currentEta.getStop(); stop.setDescription(text); } } else if (tagName.equals("rn")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setRunNumber(Integer.valueOf(text)); } } else if (tagName.equals("rt")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); TrainLine line = TrainLine.fromXmlString(text); currentEta.setRouteName(line); } } else if (tagName.equals("destSt")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); Integer i = Integer.valueOf(text); currentEta.setDestSt(i); } } else if (tagName.equals("destNm")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setDestName(text); } } else if (tagName.equals("trDr")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setTrainRouteDirectionCode(Integer.valueOf(text)); } } else if (tagName.equals("prdt")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setPredictionDate(dfTrain.parse(text)); } } else if (tagName.equals("arrT")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setArrivalDepartureDate(dfTrain.parse(text)); } } else if (tagName.equals("isApp")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setIsApp(BooleanUtils.toBoolean(Integer.valueOf(text))); } } else if (tagName.equals("isSch")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setIsSch(BooleanUtils.toBoolean(Integer.valueOf(text))); } } else if (tagName.equals("isDly")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setIsDly(BooleanUtils.toBoolean(Integer.valueOf(text))); } } else if (tagName.equals("isFlt")) { TrainArrival arri = arrivals.get(staId, null); if (arri != null) { Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setIsFlt(BooleanUtils.toBoolean(Integer.valueOf(text))); } } break; case TMST: tmst = dfTrain.parse(text); break; case ERRCD: errCd = Integer.valueOf(text); break; case ERRNM: errNum = text; break; default: break; } } eventType = parser.next(); } } catch (XmlPullParserException e) { throw new ParserException(TrackerException.ERROR, e); } catch (ParseException e) { throw new ParserException(TrackerException.ERROR, e); } catch (IOException e) { throw new ParserException(TrackerException.ERROR, e); } List<Eta> res = new ArrayList<Eta>(); int index = 0; while (index < arrivals.size()) { TrainArrival arri = arrivals.valueAt(index++); List<Eta> etas = arri.getEtas(); if (etas != null && etas.size() != 0) { res.add(etas.get(0)); } } Collections.sort(res); return res; }
From source file:fr.cph.chicago.parser.XmlParser.java
@NonNull public final synchronized List<Eta> parseTrainsFollow(@NonNull final InputStream is, @NonNull final TrainData data) throws ParserException { SparseArray<TrainArrival> arrivals = new SparseArray<>(); try {//from w w w . j a v a2s . c o m parser.setInput(is, "UTF-8"); int eventType = parser.getEventType(); XmlArrivalTrainTag tag = null; // Date tmst = null; // Integer errCd = null; // String errNum = null; String tagName = null; Integer staId = null; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { tagName = parser.getName(); switch (tagName) { case "tmst": tag = XmlArrivalTrainTag.TMST; break; case "errCd": tag = XmlArrivalTrainTag.ERRCD; break; case "errNm": tag = XmlArrivalTrainTag.ERRNM; break; case "eta": tag = XmlArrivalTrainTag.ETA; break; default: tag = XmlArrivalTrainTag.OTHER; break; } } else if (eventType == XmlPullParser.END_TAG) { tag = null; } else if (eventType == XmlPullParser.TEXT) { String text = parser.getText(); switch (tag) { case ETA: break; case OTHER: switch (tagName) { case "staId": { staId = Integer.parseInt(text); final TrainArrival arri = arrivals.get(staId, new TrainArrival()); //arri.setErrorCode(errCd); //arri.setErrorMessage(errNum); //arri.setTimeStamp(tmst); List<Eta> etas = arri.getEtas(); if (etas == null) { etas = new ArrayList<>(); arri.setEtas(etas); } final Eta eta = new Eta(); final Optional<Station> station = data.getStation(Integer.parseInt(text)); eta.setStation(station.orElse(new Station())); etas.add(eta); arrivals.append(staId, arri); break; } case "stpId": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); final Stop stop = data.getStop(Integer.parseInt(text)); currentEta.setStop(stop); } break; } case "staNm": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); final Station station = currentEta.getStation(); station.setName(text); } break; } case "stpDe": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); final Stop stop = currentEta.getStop(); stop.setDescription(text); } break; } case "rn": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setRunNumber(Integer.parseInt(text)); } break; } case "rt": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); final TrainLine line = TrainLine.fromXmlString(text); currentEta.setRouteName(line); } break; } case "destSt": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); final Integer i = Integer.parseInt(text); currentEta.setDestSt(i); } break; } case "destNm": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setDestName(text); } break; } case "trDr": { // final TrainArrival arri = arrivals.get(staId, null); // if (arri != null) { // final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); // currentEta.setTrainRouteDirectionCode(Integer.parseInt(text)); // } break; } case "prdt": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setPredictionDate(simpleDateFormatTrain.parse(text)); } break; } case "arrT": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setArrivalDepartureDate(simpleDateFormatTrain.parse(text)); } break; } case "isApp": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setApp(Util.textNumberToBoolean(text)); } break; } case "isSch": { // final TrainArrival arri = arrivals.get(staId, null); // if (arri != null) { // final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); // currentEta.setIsSch(Util.textNumberToBoolean(text)); // } break; } case "isDly": { final TrainArrival arri = arrivals.get(staId, null); if (arri != null) { final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); currentEta.setDly(Util.textNumberToBoolean(text)); } break; } case "isFlt": { // final TrainArrival arri = arrivals.get(staId, null); // if (arri != null) { // final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1); // currentEta.setIsFlt(Util.textNumberToBoolean(text)); // } break; } } break; case TMST: //tmst = simpleDateFormatTrain.parse(text); break; case ERRCD: //errCd = Integer.parseInt(text); break; case ERRNM: //errNum = text; break; default: break; } } eventType = parser.next(); } } catch (XmlPullParserException | ParseException | IOException e) { throw new ParserException(TrackerException.ERROR, e); } finally { IOUtils.closeQuietly(is); } final List<Eta> res = new ArrayList<>(); int index = 0; while (index < arrivals.size()) { final TrainArrival arri = arrivals.valueAt(index++); final List<Eta> etas = arri.getEtas(); if (etas != null && etas.size() != 0) { res.add(etas.get(0)); } } Collections.sort(res); return res; }