Clean up launch logic: (1) Clipboard (non-numbers) always generates a notification with follow-on intent; (2) Links, maps, numbers now generate proper follow-on intents when user elects to not launch automatically.

This commit is contained in:
burke.davey
2010-07-13 14:56:38 +00:00
parent d924f06c8f
commit 0c3c62c7d0
2 changed files with 42 additions and 59 deletions

View File

@@ -19,7 +19,6 @@
<string name="link_action_pref_text">Link action preference:</string> <string name="link_action_pref_text">Link action preference:</string>
<string name="disconnect">Disconnect phone</string> <string name="disconnect">Disconnect phone</string>
<string name="disconnect_error_text">Error! Unable to disconnect.</string> <string name="disconnect_error_text">Error! Unable to disconnect.</string>
<string name="clipboard_copied">Clipboard copied from desktop</string>
<string name="help">Help</string> <string name="help">Help</string>
<string name="intro_text"> <string name="intro_text">
Chrome to Phone lets you easily share links, maps, and selected phone numbers and text Chrome to Phone lets you easily share links, maps, and selected phone numbers and text

View File

@@ -25,21 +25,19 @@ import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import android.util.Log;
import com.google.android.c2dm.C2DMBaseReceiver; import com.google.android.c2dm.C2DMBaseReceiver;
public class C2DMReceiver extends C2DMBaseReceiver { public class C2DMReceiver extends C2DMBaseReceiver {
private static final String TAG = "C2DMReceiver";
public C2DMReceiver() { public C2DMReceiver() {
super(DeviceRegistrar.SENDER_ID); super(DeviceRegistrar.SENDER_ID);
@@ -70,6 +68,7 @@ public class C2DMReceiver extends C2DMBaseReceiver {
String title = (String) extras.get("title"); String title = (String) extras.get("title");
String sel = (String) extras.get("sel"); String sel = (String) extras.get("sel");
String debug = (String) extras.get("debug"); String debug = (String) extras.get("debug");
if (debug != null) { if (debug != null) {
// server-controlled debug - the server wants to know // server-controlled debug - the server wants to know
// we received the message, and when. This is not user-controllable, // we received the message, and when. This is not user-controllable,
@@ -90,69 +89,54 @@ public class C2DMReceiver extends C2DMBaseReceiver {
} }
} }
if (url != null && title != null) { if (title != null && url != null && url.startsWith("http")) {
if (url.startsWith("http")) { SharedPreferences settings = Prefs.get(context);
SharedPreferences settings = Prefs.get(context); Intent launchIntent = getLaunchIntent(context, url, title, sel);
if (sel != null && sel.length() > 0) { // user selected text if (settings.getBoolean("launchBrowserOrMaps", false) && launchIntent != null) {
ClipboardManager cm = playNotificationSound(context);
(ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE); context.startActivity(launchIntent);
cm.setText(sel);
// Special handling for launching phone numbers
String number = parseTelephoneNumber(sel);
if (number != null && settings.getBoolean("launchBrowserOrMaps", false)) {
try {
Intent dialerIntent = new Intent("android.intent.action.DIAL",
Uri.parse("tel:" + number));
dialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialerIntent);
return; // done
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Dialer did not recognize " + sel);
}
} else { // otherwise generate a notification with clipboard content
generateNotification(context, sel,
context.getString(R.string.clipboard_copied), null);
}
} else { // user didn't select text - launch app or create notification
if (settings.getBoolean("launchBrowserOrMaps", false)) {
launchApp(context, url, title, sel);
} else {
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
generateNotification(context, url, title, urlIntent);
}
}
} else { } else {
Log.w(TAG, "Invalid URL: " + url); generateNotification(context, sel != null && sel.length() > 0 ? sel : url,
title, launchIntent);
} }
} }
} }
} }
private void launchApp(Context context, String url, String title, String sel) { private Intent getLaunchIntent(Context context, String url, String title, String sel) {
playNotificationSound(context); Intent intent = null;
String number = parseTelephoneNumber(sel);
if (number != null) {
intent = new Intent("android.intent.action.DIAL",
Uri.parse("tel:" + number));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (sel != null && sel.length() > 0) {
// No intent for selection - just copy to clipboard
ClipboardManager cm =
(ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
cm.setText(sel);
} else {
final String GMM_PACKAGE_NAME = "com.google.android.apps.maps";
final String GMM_CLASS_NAME = "com.google.android.maps.MapsActivity";
boolean isMapsURL = url.startsWith("http://maps.google.") ||
url.matches("^http://www\\.google\\.[a-z\\.]+/maps");
final String GMM_PACKAGE_NAME = "com.google.android.apps.maps"; intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
final String GMM_CLASS_NAME = "com.google.android.maps.MapsActivity"; intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
boolean isMapsURL = url.startsWith("http://maps.google.") || if (isMapsURL) {
url.matches("^http://www\\.google\\.[a-z\\.]+/maps"); intent.setClassName(GMM_PACKAGE_NAME, GMM_CLASS_NAME);
try { }
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Fall back if we can't resolve intent (i.e. maps missing)
if (isMapsURL) { PackageManager pm = context.getPackageManager();
intent.setClassName(GMM_PACKAGE_NAME, GMM_CLASS_NAME); if (null == intent.resolveActivity(pm)) {
} intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(intent); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (ActivityNotFoundException e) { }
if (isMapsURL) { // try again without GMM }
Log.w(TAG, "Maps not found, falling back to browser"); return intent;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); }
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
private void generateNotification(Context context, String msg, String title, Intent intent) { private void generateNotification(Context context, String msg, String title, Intent intent) {
int icon = R.drawable.status_icon; int icon = R.drawable.status_icon;