List of usage examples for android.support.v4.util CircularArray addLast
public final void addLast(E e)
From source file:Main.java
public static <T> void addAll(CircularArray<T> target, CircularArray<T> source) { int N = source.size(); for (int i = 0; i < N; i++) { target.addLast(source.get(i)); }/*from w w w .ja v a2s . c o m*/ }
From source file:Main.java
@SafeVarargs public static <T> CircularArray<T> asArray(T... items) { CircularArray<T> result = new CircularArray<>(); if (items != null) { for (T t : items) { result.addLast(t); }//from ww w . j a v a 2 s . c o m } return result; }
From source file:com.appsimobile.util.ArrayUtils.java
public static <T> void sort(CircularArray<T> list, Comparator<? super T> comparator) { if (list.isEmpty()) return;//from w w w .j a v a 2 s .c o m T first = list.getFirst(); T[] array = (T[]) Array.newInstance(first.getClass(), list.size()); array = toArray(list, array); Arrays.sort(array, comparator); list.clear(); for (T item : array) { list.addLast(item); } }
From source file:com.appsimobile.appsii.module.HotspotLoader.java
public static CircularArray<HotspotItem> loadHotspots(Context c) { ContentResolver r = c.getContentResolver(); Cursor cursor = r.query(HomeContract.Hotspots.CONTENT_URI, HotspotQuery.PROJECTION, null, null, null); if (cursor != null) { CircularArray<HotspotItem> result = new CircularArray<>(cursor.getCount()); try {/*from w w w. j a v a2 s . c om*/ while (cursor.moveToNext()) { long id = cursor.getLong(HotspotQuery.ID); float height = cursor.getFloat(HotspotQuery.HEIGHT); float ypos = cursor.getFloat(HotspotQuery.Y_POSITION); boolean left = cursor.getInt(HotspotQuery.LEFT_BORDER) == 1; boolean needsConfiguration = cursor.getInt(HotspotQuery.NEEDS_CONFIGURATION) == 1; long defaultPageId = cursor.isNull(HotspotQuery._DEFAULT_PAGE) ? -1L : cursor.getLong(HotspotQuery._DEFAULT_PAGE); String name = cursor.getString(HotspotQuery.NAME); HotspotItem conf = new HotspotItem(); conf.init(id, name, height, ypos, left, needsConfiguration, defaultPageId); result.addLast(conf); } } finally { cursor.close(); } return result; } return CollectionUtils.emptyArray(); }
From source file:com.appsimobile.appsii.module.weather.loader.WeatherDataParser.java
public static void parseWeatherData(CircularArray<WeatherData> result, String jsonString, CircularArray<String> woeids) throws JSONException, ResponseParserException, ParseException { if (woeids.isEmpty()) return;// www .java 2 s .c o m SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy", Locale.US); simpleDateFormat.setTimeZone(TimeZone.getTimeZone(Time.TIMEZONE_UTC)); SimpleJson json = new SimpleJson(jsonString); // when only a single woeid was requested, this is an object, otherwise it is an array. // So we need to check if we got an array; if so, handle each of the objects. // Otherwise get it as an object JSONArray resultsArray = json.getJsonArray("query.results.channel"); if (resultsArray == null) { JSONObject weatherObject = json.optJsonObject("query.results.channel"); if (weatherObject == null) return; String woeid = woeids.get(0); WeatherData weatherData = parseWeatherData(woeid, simpleDateFormat, weatherObject); if (weatherData != null) { result.addLast(weatherData); } return; } int length = resultsArray.length(); for (int i = 0; i < length; i++) { JSONObject weatherJson = resultsArray.getJSONObject(i); WeatherData weatherData = parseWeatherData(woeids.get(i), simpleDateFormat, weatherJson); if (weatherData == null) continue; result.addLast(weatherData); } }
From source file:com.appsimobile.appsii.module.weather.loader.YahooWeatherApiClient.java
@VisibleForTesting static LocationInfo parseLocationInfo(LocationInfo li, InputStream in) throws XmlPullParserException, IOException, CantGetWeatherException { CircularArray<Pair<String, String>> alternateWoeids = new CircularArray<>(); String primaryWoeid = null;//w w w . j a va 2 s . com XmlPullParser xpp = sXmlPullParserFactory.newPullParser(); xpp.setInput(new InputStreamReader(in)); boolean inWoe = false; boolean inTown = false; boolean inCountry = false; boolean inTimezone = false; int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String tagName = xpp.getName(); if (eventType == XmlPullParser.START_TAG && "woeid".equals(tagName)) { inWoe = true; } else if (eventType == XmlPullParser.TEXT && inWoe) { primaryWoeid = xpp.getText(); } else if (eventType == XmlPullParser.START_TAG && tagName.startsWith("timezone")) { inTimezone = true; } else if (eventType == XmlPullParser.TEXT && inTimezone) { li.timezone = xpp.getText(); } else if (eventType == XmlPullParser.START_TAG && (tagName.startsWith("locality") || tagName.startsWith("admin") || tagName.startsWith("country"))) { for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) { String attrName = xpp.getAttributeName(i); if ("type".equals(attrName) && "Town".equals(xpp.getAttributeValue(i))) { inTown = true; } else if ("type".equals(attrName) && "Country".equals(xpp.getAttributeValue(i))) { inCountry = true; } else if ("woeid".equals(attrName)) { String woeid = xpp.getAttributeValue(i); if (!TextUtils.isEmpty(woeid)) { alternateWoeids.addLast(new Pair<>(tagName, woeid)); } } } } else if (eventType == XmlPullParser.TEXT && inTown) { li.town = xpp.getText(); } else if (eventType == XmlPullParser.TEXT && inCountry) { li.country = xpp.getText(); } if (eventType == XmlPullParser.END_TAG) { inWoe = false; inTown = false; inCountry = false; inTimezone = false; } eventType = xpp.next(); } // Add the primary woeid if it was found. if (!TextUtils.isEmpty(primaryWoeid)) { li.woeids.addLast(primaryWoeid); } // Sort by descending tag name to order by decreasing precision // (locality3, locality2, locality1, admin3, admin2, admin1, etc.) ArrayUtils.sort(alternateWoeids, new Comparator<Pair<String, String>>() { @Override public int compare(Pair<String, String> pair1, Pair<String, String> pair2) { return pair1.first.compareTo(pair2.first); } }); int N = alternateWoeids.size(); for (int i = 0; i < N; i++) { Pair<String, String> pair = alternateWoeids.get(i); li.woeids.addLast(pair.second); } if (li.woeids.size() > 0) { return li; } throw new CantGetWeatherException(true, R.string.no_weather_data, "No WOEIDs found nearby."); }
From source file:com.appsimobile.appsii.SidebarHotspot.java
void reloadHotspotData() { Log.d("SidebarHotspot", "reloading hotspots"); if (mHotspotItem == null) return;// w w w . java 2 s . com final long hotspotId = mHotspotItem.mId; final Context context = getContext(); if (mLoadDataTask != null) mLoadDataTask.cancel(true); mLoadDataTask = new AsyncTask<Void, Void, CircularArray<HotspotPageEntry>>() { @Override protected CircularArray<HotspotPageEntry> doInBackground(Void... params) { Cursor c = context.getContentResolver().query(HotspotPagesQuery.createUri(hotspotId), HotspotPagesQuery.PROJECTION, null, null, HomeContract.HotspotDetails.POSITION + " ASC"); CircularArray<HotspotPageEntry> result = new CircularArray<>(c.getCount()); while (c.moveToNext()) { HotspotPageEntry entry = new HotspotPageEntry(); entry.mEnabled = c.getInt(HotspotPagesQuery.ENABLED) == 1; entry.mPageId = c.getLong(HotspotPagesQuery.PAGE_ID); entry.mHotspotId = c.getLong(HotspotPagesQuery.HOTSPOT_ID); entry.mPageName = c.getString(HotspotPagesQuery.PAGE_NAME); entry.mHotspotName = c.getString(HotspotPagesQuery.HOTSPOT_NAME); entry.mPosition = c.getInt(HotspotPagesQuery.POSITION); entry.mPageType = c.getInt(HotspotPagesQuery.PAGE_TYPE); result.addLast(entry); } c.close(); return result; } @Override protected void onPostExecute(CircularArray<HotspotPageEntry> hotspotPageEntries) { onHotspotEntriesLoaded(hotspotPageEntries); } }; mLoadDataTask.execute(); }
From source file:com.appsimobile.appsii.module.weather.loader.YahooWeatherApiClient.java
@VisibleForTesting static void parseLocationSearchResults(CircularArray<LocationSearchResult> results, InputStream inputStream) throws XmlPullParserException, IOException { XmlPullParser xpp = sXmlPullParserFactory.newPullParser(); xpp.setInput(new InputStreamReader(inputStream)); LocationSearchResult result = null;/*from w w w .j a v a2 s . c o m*/ String name = null, country = null, admin1 = null; String timezone = null; StringBuilder sb = new StringBuilder(); int state = PARSE_STATE_NONE; int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String tagName = xpp.getName(); if (eventType == XmlPullParser.START_TAG) { switch (state) { case PARSE_STATE_NONE: if ("place".equals(tagName)) { state = PARSE_STATE_PLACE; result = new LocationSearchResult(); name = country = admin1 = null; } break; case PARSE_STATE_PLACE: if ("name".equals(tagName)) { state = PARSE_STATE_NAME; } else if ("woeid".equals(tagName)) { state = PARSE_STATE_WOEID; } else if ("country".equals(tagName)) { state = PARSE_STATE_COUNTRY; } else if ("admin1".equals(tagName)) { state = PARSE_STATE_ADMIN1; } else if ("timezone".equals(tagName)) { state = PARSE_STATE_TIMEZONE; } break; } } else if (eventType == XmlPullParser.TEXT) { switch (state) { case PARSE_STATE_WOEID: result.woeid = xpp.getText(); break; case PARSE_STATE_NAME: name = xpp.getText(); break; case PARSE_STATE_COUNTRY: country = xpp.getText(); break; case PARSE_STATE_ADMIN1: admin1 = xpp.getText(); break; case PARSE_STATE_TIMEZONE: timezone = xpp.getText(); break; } } else if (eventType == XmlPullParser.END_TAG) { if ("place".equals(tagName)) { // // Sort by descending tag name to order by decreasing precision // // (locality3, locality2, locality1, admin3, admin2, admin1, etc.) // Collections.sort(alternateWoeids, new Comparator<Pair<String, String>>() { // @Override // public int compare(Pair<String, String> pair1, // Pair<String, String> pair2) { // return pair1.first.compareTo(pair2.first); // } // }); sb.setLength(0); if (!TextUtils.isEmpty(name)) { sb.append(name); } if (!TextUtils.isEmpty(admin1)) { if (sb.length() > 0) { sb.append(", "); } sb.append(admin1); } result.displayName = sb.toString(); result.country = country; result.timezone = timezone; results.addLast(result); state = PARSE_STATE_NONE; } else if (state != PARSE_STATE_NONE) { state = PARSE_STATE_PLACE; } } eventType = xpp.next(); } }