fun sync(context: Context) { val cr: ContentResolver = cr() val proj = arrayOf<String>(_ID, Syncs.TYPE_ID, Syncs.OBJECT_ID, millis(Syncs.ACTION_ON)) val sel: String = Syncs.STATUS_ID + " = ?" val args = arrayOf<String>(java.lang.String.valueOf(ACTIVE.id)) val order: String = Syncs.ACTION_ON + " DESC" val c = EasyCursor(cr.query(Syncs.CONTENT_URI, proj, sel, args, order)) val count: Int = c.getCount() if (count > 0) { var users = 0 var reviews = 0 var review: Review? = null val lines: Set<CharSequence> = LinkedHashSet() var `when` = 0L var icon: Bitmap? = null val syncIds: Set<String> = HashSet(count) while (c.moveToNext()) { var photo: Uri? = null when (Sync.Type.get(c.getInt(Syncs.TYPE_ID))) { USER -> { photo = user(context, cr, c.getLong(Syncs.OBJECT_ID), lines, icon) if (photo != null) { users++ syncIds.add(java.lang.String.valueOf(c.getLong(_ID))) } } REVIEW -> { val (first, second) = review( context, cr, c.getLong(Syncs.OBJECT_ID), lines, icon ) photo = first if (second != null) { reviews++ syncIds.add(java.lang.String.valueOf(c.getLong(_ID))) if (review == null) { review = second } } } } if (`when` == 0L) { `when` = c.getLong(Syncs.ACTION_ON) } if (photo != null && photo !== EMPTY) { icon = photo(context, photo) } } val size: Int = lines.size() if (size > 0) { var bigText: CharSequence? = null var summary: CharSequence? = null val activity: Intent if (users > 0 && reviews == 0) { activity = Intent(context, FriendsActivity::class.java) } else if (users == 0 && (reviews == 1 || size == 1)) { bigText = ReviewAdapter.comments(review.comments) summary = context.getString(R.string.n_stars, review.rating) activity = Intent(context, RestaurantActivity::class.java).putExtra( EXTRA_ID, review.restaurantId ) if (review.type === GOOGLE) { activity.putExtra(EXTRA_TAB, TAB_PUBLIC) } } else { activity = Intent(context, NotificationsActivity::class.java) } notify(context, lines, bigText, summary, `when`, icon, users + reviews, activity) Prefs.putStringSet(context, APP, NEW_SYNC_IDS, syncIds) event("notification", "notify", "sync", size) } else { Managers.notification(context).cancel(TAG_SYNC, 0) context.startService(Intent(context, SyncsReadService::class.java)) } } c.close() }
Post a notification for any unread server changes .