From 238e8d6350f13c4ad57367ee0cf68e28f9c64bc2 Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 27 Sep 2012 11:55:25 +0000 Subject: [PATCH] Try to work around devices whose calendar app only responds to EDIT, not INSERT Intent git-svn-id: https://zxing.googlecode.com/svn/trunk@2428 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../android/result/CalendarResultHandler.java | 15 ++++++++- .../client/android/result/ResultHandler.java | 33 +++++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java b/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java index 24585db91..b97029c85 100644 --- a/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/CalendarResultHandler.java @@ -16,6 +16,8 @@ package com.google.zxing.client.android.result; +import android.content.ActivityNotFoundException; +import android.util.Log; import com.google.zxing.client.android.R; import com.google.zxing.client.result.CalendarParsedResult; import com.google.zxing.client.result.ParsedResult; @@ -34,6 +36,8 @@ import java.util.Date; */ public final class CalendarResultHandler extends ResultHandler { + private static final String TAG = CalendarResultHandler.class.getSimpleName(); + private static final int[] buttons = { R.string.button_add_calendar }; @@ -122,7 +126,16 @@ public final class CalendarResultHandler extends ResultHandler { intent.putExtra(Intent.EXTRA_EMAIL, attendees); // Documentation says this is either a String[] or comma-separated String, which is right? } - launchIntent(intent); + + try { + // Do this manually at first + rawLaunchIntent(intent); + } catch (ActivityNotFoundException anfe) { + Log.w(TAG, "No calendar app available that responds to " + Intent.ACTION_INSERT); + // For calendar apps that don't like "INSERT": + intent.setAction(Intent.ACTION_EDIT); + launchIntent(intent); // Fail here for real if nothing can handle it + } } diff --git a/android/src/com/google/zxing/client/android/result/ResultHandler.java b/android/src/com/google/zxing/client/android/result/ResultHandler.java index f8822ac64..d7cabab06 100644 --- a/android/src/com/google/zxing/client/android/result/ResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/ResultHandler.java @@ -460,19 +460,32 @@ public abstract class ResultHandler { } } - void launchIntent(Intent intent) { + /** + * Like {@link #launchIntent(Intent)} but will tell you if it is not handle-able + * via {@link ActivityNotFoundException}. + * + * @throws ActivityNotFoundException + */ + void rawLaunchIntent(Intent intent) { if (intent != null) { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); Log.d(TAG, "Launching intent: " + intent + " with extras: " + intent.getExtras()); - try { - activity.startActivity(intent); - } catch (ActivityNotFoundException e) { - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(R.string.app_name); - builder.setMessage(R.string.msg_intent_failed); - builder.setPositiveButton(R.string.button_ok, null); - builder.show(); - } + activity.startActivity(intent); + } + } + + /** + * Like {@link #rawLaunchIntent(Intent)} but will show a user dialog if nothing is available to handle. + */ + void launchIntent(Intent intent) { + try { + rawLaunchIntent(intent); + } catch (ActivityNotFoundException e) { + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(R.string.app_name); + builder.setMessage(R.string.msg_intent_failed); + builder.setPositiveButton(R.string.button_ok, null); + builder.show(); } }