From 34f820add414e08586dc4b008457acdfdf711fb2 Mon Sep 17 00:00:00 2001 From: ligi Date: Thu, 26 Dec 2013 01:15:16 +0100 Subject: [PATCH] special treatments for AirBerlin --- .../ligi/passandroid/model/PassLocation.java | 25 +++++++++-- .../org/ligi/passandroid/model/PassStore.java | 23 +++++++++-- .../org/ligi/passandroid/model/Passbook.java | 41 +++++++++++++++++-- .../ui/NavigateToLocationsDialog.java | 4 +- .../passandroid/ui/TicketListActivity.java | 1 + .../passandroid/LocationsMapFragment.java | 5 +-- 6 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/ligi/passandroid/model/PassLocation.java b/src/main/java/org/ligi/passandroid/model/PassLocation.java index 00048a7c..5e38db44 100644 --- a/src/main/java/org/ligi/passandroid/model/PassLocation.java +++ b/src/main/java/org/ligi/passandroid/model/PassLocation.java @@ -2,11 +2,30 @@ package org.ligi.passandroid.model; public class PassLocation { + private final Passbook connectedPass; + private String description; + + public PassLocation(Passbook connectedPass) { + this.connectedPass = connectedPass; + } + + public String getDescription() { + if (description == null) { + // fallback for passes with locations without description - e.g. AirBerlin + return connectedPass.getDescription(); + } + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LatLng latlng = new LatLng(); + + public class LatLng { public double lat; public double lon; } - - public LatLng latlng = new LatLng(); - public String description = ""; } diff --git a/src/main/java/org/ligi/passandroid/model/PassStore.java b/src/main/java/org/ligi/passandroid/model/PassStore.java index c0fe7c3b..f4951d59 100644 --- a/src/main/java/org/ligi/passandroid/model/PassStore.java +++ b/src/main/java/org/ligi/passandroid/model/PassStore.java @@ -42,13 +42,19 @@ public class PassStore { refreshPassesList(); } + public void deleteCache() { + + String[] passIdents = getPassesDirSafely().list(new DirectoryFileFilter()); + + for (String id : passIdents) { + new File(path + "/" + id + "/base_cache.obj").delete(); + } + } + public void refreshPassesList() { path = TicketDefinitions.getPassesDir(context); - File passes_dir = new File(TicketDefinitions.getPassesDir(context)); - if (!passes_dir.exists()) { - passes_dir.mkdirs(); - } + File passes_dir = getPassesDirSafely(); String[] passIdents = passes_dir.list(new DirectoryFileFilter()); reducedPassInformations = new ArrayList(); @@ -72,6 +78,15 @@ public class PassStore { } + private File getPassesDirSafely() { + File passes_dir = new File(TicketDefinitions.getPassesDir(context)); + + if (!passes_dir.exists()) { + passes_dir.mkdirs(); + } + return passes_dir; + } + public int passCount() { return reducedPassInformations.size(); } diff --git a/src/main/java/org/ligi/passandroid/model/Passbook.java b/src/main/java/org/ligi/passandroid/model/Passbook.java index 3a104355..b84d45ce 100644 --- a/src/main/java/org/ligi/passandroid/model/Passbook.java +++ b/src/main/java/org/ligi/passandroid/model/Passbook.java @@ -121,10 +121,14 @@ public class Passbook { for (int i = 0; i < locations_json.length(); i++) { JSONObject obj = locations_json.getJSONObject(i); - PassLocation location = new PassLocation(); + PassLocation location = new PassLocation(this); location.latlng.lat = obj.getDouble("latitude"); location.latlng.lon = obj.getDouble("longitude"); - location.description = obj.getString("relevantText"); + + if (obj.has("relevantText")) { + location.setDescription(obj.getString("relevantText")); + } + locations.add(location); } } catch (JSONException e) { @@ -183,6 +187,30 @@ public class Passbook { headerFields = getFieldListFromJsonArr(eventTicket, "headerFields"); + // for airberlin + if (description.equals("boardcard")) { + final String flight_regex = "\\b\\w{1,3}\\d{3,4}\\b"; + final String seat_regex = "\\b\\d\\d?\\w\\b "; + + for (Field f : headerFields) { + if (f.label.matches(flight_regex)) { + description = f.label + " " + f.value; + } + } + for (Field f : auxiliaryFields) { + if (f.key != null && f.key.equals("seat")) { + description += " | " + f.label + " " + f.value; + } + } + for (Field f : secondaryFields) { + if (f.key != null && f.key.equals("boardingGroup")) { + description += " | " + f.label + " " + f.value; + } + + + } + } + } public String findType(JSONObject obj) { @@ -264,8 +292,12 @@ public class Passbook { for (int i = 0; i < arr.length(); i++) { Field f = new Field(); try { - f.label = arr.getJSONObject(i).getString("label"); - f.value = arr.getJSONObject(i).getString("value"); + final JSONObject jsonObject = arr.getJSONObject(i); + f.label = jsonObject.getString("label"); + f.value = jsonObject.getString("value"); + if (jsonObject.has("key")) { + f.key = jsonObject.getString("key"); + } res.add(f); } catch (JSONException e) { e.printStackTrace(); @@ -409,6 +441,7 @@ public class Passbook { } public class Field { + public String key; public String label; public String value; } diff --git a/src/main/java/org/ligi/passandroid/ui/NavigateToLocationsDialog.java b/src/main/java/org/ligi/passandroid/ui/NavigateToLocationsDialog.java index 933792e3..31ffa181 100644 --- a/src/main/java/org/ligi/passandroid/ui/NavigateToLocationsDialog.java +++ b/src/main/java/org/ligi/passandroid/ui/NavigateToLocationsDialog.java @@ -30,7 +30,7 @@ public class NavigateToLocationsDialog { int i = 0; for (PassLocation loc : locations) { - locationDescriptions[i++] = loc.description; + locationDescriptions[i++] = loc.getDescription(); } new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.choose_location)).setItems(locationDescriptions, new DialogInterface.OnClickListener() { @Override @@ -58,7 +58,7 @@ public class NavigateToLocationsDialog { String description = ""; try { - description = URLEncoder.encode(location.description, "UTF-8"); + description = URLEncoder.encode(location.getDescription(), "UTF-8"); } catch (UnsupportedEncodingException e1) { // OK - no descripion } diff --git a/src/main/java/org/ligi/passandroid/ui/TicketListActivity.java b/src/main/java/org/ligi/passandroid/ui/TicketListActivity.java index 459e4339..ae8a7f6d 100644 --- a/src/main/java/org/ligi/passandroid/ui/TicketListActivity.java +++ b/src/main/java/org/ligi/passandroid/ui/TicketListActivity.java @@ -181,6 +181,7 @@ public class TicketListActivity extends ActionBarActivity { .listener(new OnRefreshListener() { @Override public void onRefreshStarted(View view) { + App.getPassStore().deleteCache(); new ScanForPassesTask().execute(); } }) diff --git a/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.java b/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.java index 1c0af39d..aebd5666 100644 --- a/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.java +++ b/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.java @@ -18,7 +18,6 @@ import com.google.android.gms.maps.model.MarkerOptions; import org.ligi.passandroid.model.PassLocation; import org.ligi.passandroid.ui.TicketViewActivityBase; -import org.ligi.tracedroid.logging.Log; import java.util.List; @@ -57,18 +56,16 @@ public class LocationsMapFragment extends SupportMapFragment { if (locations.size() > 0) { for (PassLocation l : locations) { - Log.i("adding marker" + l.latlng); // yea that looks stupid but need to split LatLng free/nonfree - google play services ^^ LatLng latLng = new LatLng(l.latlng.lat, l.latlng.lon); mMap.addMarker(new MarkerOptions() .position(latLng) - .title(l.description) + .title(l.getDescription()) //.icon(BitmapDescriptorFactory.fromBitmap(base_activity.passbook.getIconBitmap()))); ); boundser = boundser.include(latLng); - Log.i("added marker"); } mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {