From a034592003d8ff48bd1ca00a6a630811f9bdaca3 Mon Sep 17 00:00:00 2001
From: ligi <ligi@ligi.de>
Date: Sat, 23 Aug 2014 14:25:40 +0200
Subject: [PATCH] URL rewrithe for cathay and improve url rewriting on the go -
 with error handling and so

---
 src/main/AndroidManifest.xml                  |  14 ++-
 .../ui/quirk_fix/URLRewriteActivity.java      | 103 ++++++++++++++++++
 .../VirginAustraliaLoadActivity.java          |  28 -----
 3 files changed, 116 insertions(+), 29 deletions(-)
 create mode 100644 src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java
 delete mode 100644 src/main/java/org/ligi/passandroid/ui/quirk_fix/VirginAustraliaLoadActivity.java

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index d386b6a1..ffaedaa7 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -536,7 +536,7 @@
 
         </activity>
 
-        <activity android:name="org.ligi.passandroid.ui.quirk_fix.VirginAustraliaLoadActivity">
+        <activity android:name=".ui.quirk_fix.URLRewriteActivity">
         <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
 
@@ -559,6 +559,18 @@
                     android:pathPattern="/virginaustralia/.*"
                     android:scheme="http" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+
+                <data
+                    android:host="www.cathaypacific.com"
+                    android:pathPattern="/icheckin2/mbp.do.*"
+                    android:scheme="https" />
+            </intent-filter>
+
         </activity>
 
         <activity android:name=".ui.quirk_fix.USAirwaysLoadActivity">
diff --git a/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java b/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java
new file mode 100644
index 00000000..3459f909
--- /dev/null
+++ b/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java
@@ -0,0 +1,103 @@
+package org.ligi.passandroid.ui.quirk_fix;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+
+import org.ligi.axt.AXT;
+import org.ligi.passandroid.Tracker;
+import org.ligi.passandroid.ui.PassImportActivity;
+
+import java.net.URLEncoder;
+
+public class URLRewriteActivity extends Activity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        String url = null;
+
+        if (getIntent().getData().getHost().equals("mobile.virginaustralia.com")) {
+            url = getVirginAustraliaURL();
+        } else if (getIntent().getData().getHost().equals("www.cathaypacific.com")) {
+            url = getCathay();
+        }
+
+        if (url == null) {
+            new AlertDialog.Builder(this)
+                    .setTitle("Workaround failed")
+                    .setMessage("The URL PassAndroid tried to work around failed :-( some companies just send PassBooks to Apple Devices - this was an attempt to workaround this." +
+                            "Unfortunately it failed - perhaps there where changes on the serverside - you can open the site with your browser now - to see it in PassAndroid in future again it would help if you can send me the pass")
+                    .setPositiveButton("Browser", new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            Tracker.get().trackException("URLRewrite with invalid activity", false);
+                            AXT.at(URLRewriteActivity.this).rethrowIntentExcludingSelf();
+                        }
+                    })
+                    .setNeutralButton("send", new DialogInterface.OnClickListener() {
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+
+                                    Intent intent = new Intent(Intent.ACTION_SEND);
+                                    intent.putExtra(Intent.EXTRA_SUBJECT, "PassAndroid: URLRewrite Problem");
+                                    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"ligi@ligi.de"});
+                                    intent.putExtra(Intent.EXTRA_TEXT, "foo"+getIntent().getData());
+                                    intent.setType("text/plain");
+
+                                    startActivity(Intent.createChooser(intent, "How to send Link?"));
+                                    finish();
+                                }
+                            }
+                    )
+                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            URLRewriteActivity.this.finish();
+                        }
+                    })
+                    .show();
+
+            return;
+        }
+
+        final Intent intent = new Intent(this, PassImportActivity.class);
+        intent.setData(Uri.parse(url));
+        startActivity(intent);
+
+        finish();
+    }
+
+    private String getVirginAustraliaURL() {
+        final String passId = getIntent().getData().getQueryParameter("c");
+
+        Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "virgin_australia", null);
+
+        if (passId == null) {
+            return null;
+        }
+
+        Tracker.get().trackEvent("quirk_fix", "redirect", "virgin_australia", null);
+
+        return "https://mobile.virginaustralia.com/boarding/pass.pkpass?key=" + URLEncoder.encode(passId);
+    }
+
+    private String getCathay() {
+        final String passId = getIntent().getData().getQueryParameter("v");
+
+        Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "cathay", null);
+
+        if (passId == null) {
+            return null;
+        }
+
+        Tracker.get().trackEvent("quirk_fix", "redirect", "cathay", null);
+
+        return "https://www.cathaypacific.com/icheckin2/PassbookServlet?v=" + URLEncoder.encode(passId);
+    }
+
+}
diff --git a/src/main/java/org/ligi/passandroid/ui/quirk_fix/VirginAustraliaLoadActivity.java b/src/main/java/org/ligi/passandroid/ui/quirk_fix/VirginAustraliaLoadActivity.java
deleted file mode 100644
index acd9a89a..00000000
--- a/src/main/java/org/ligi/passandroid/ui/quirk_fix/VirginAustraliaLoadActivity.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package org.ligi.passandroid.ui.quirk_fix;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.net.Uri;
-import android.os.Bundle;
-
-import org.ligi.passandroid.Tracker;
-import org.ligi.passandroid.ui.PassImportActivity;
-
-import java.net.URLEncoder;
-
-public class VirginAustraliaLoadActivity extends Activity {
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        final String passId = getIntent().getData().getQueryParameter("c");
-        final String url = "https://mobile.virginaustralia.com/boarding/pass.pkpass?key=" + URLEncoder.encode(passId);
-
-        Tracker.get().trackEvent("quirk_fix", "redirect", "virgin_australia", null);
-        final Intent intent = new Intent(this, PassImportActivity.class);
-        intent.setData(Uri.parse(url));
-        startActivity(intent);
-        finish();
-    }
-}