Fix issue with multiple devices and upgrade. The 'bare' registration will be removed if registrations with ID are found - which is not worse than

having a single device. Also fix few PersistentManager bugs - removal must be done with same PM, previous code was leaving one unclossed.
This commit is contained in:
costin
2010-09-18 06:03:10 +00:00
parent e9fa5e9759
commit 71684cb5cf

View File

@@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletResponse;
import com.google.android.c2dm.server.C2DMessaging;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
@SuppressWarnings("serial")
@@ -92,12 +93,28 @@ public class SendServlet extends HttpServlet {
PersistenceManager pm =
C2DMessaging.getPMF(getServletContext()).getPersistenceManager();
// delete will fail if the pm is different than the one used to
// load the object - we must close the object when we're done
List<DeviceInfo> registrations = null;
try {
registrations = DeviceInfo.getDeviceInfoForUser(C2DMessaging.getPMF(getServletContext())
.getPersistenceManager(), userAccount);
} finally {
pm.close();
registrations = DeviceInfo.getDeviceInfoForUser(pm, userAccount);
// Deal with upgrades and multi-device:
// If user has one device with an old version and few new ones -
// the old registration will be deleted.
if (registrations.size() > 1) {
// Make sure there is no 'bare' registration
// Keys are sorted - check the first
DeviceInfo first = registrations.get(0);
Key oldKey = first.getKey();
if (oldKey.toString().indexOf("#") < 0) {
log.warning("Removing old-style key " + oldKey.toString());
// multiple devices, first is old-style.
registrations.remove(0); // don't send to it
pm.deletePersistent(first);
}
}
int numSendAttempts = 0;
@@ -127,7 +144,7 @@ public class SendServlet extends HttpServlet {
if ("NotRegistered".equals(ex.getMessage()) ||
"InvalidRegistration".equals(ex.getMessage())) {
// Prune device, it no longer works
pruneDevice(deviceInfo);
pm.deletePersistent(deviceInfo);
} else {
throw ex;
}
@@ -146,14 +163,10 @@ public class SendServlet extends HttpServlet {
resp.getWriter().println(ERROR_STATUS + " (Unable to send link)");
return false;
}
}
private void pruneDevice(DeviceInfo deviceInfo) {
PersistenceManager pm =
C2DMessaging.getPMF(getServletContext()).getPersistenceManager();
pm.deletePersistent(deviceInfo);
} finally {
pm.close();
}
}
boolean doSendViaC2dm(String url, String title, String sel, C2DMessaging push,
String collapseKey, DeviceInfo deviceInfo) throws IOException {