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
This commit is contained in:
srowen 2012-09-27 11:55:25 +00:00
parent 54d32c794d
commit 238e8d6350
2 changed files with 37 additions and 11 deletions

View file

@ -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
}
}

View file

@ -460,12 +460,26 @@ 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);
}
}
/**
* 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);
@ -474,7 +488,6 @@ public abstract class ResultHandler {
builder.show();
}
}
}
private static void putExtra(Intent intent, String key, String value) {
if (value != null && value.length() > 0) {