Example usage for android.support.v4.util LongSparseArray size

List of usage examples for android.support.v4.util LongSparseArray size

Introduction

In this page you can find the example usage for android.support.v4.util LongSparseArray size.

Prototype

public int size() 

Source Link

Usage

From source file:de.vanita5.twittnuker.util.LongSparseArrayUtils.java

/**
 * @return A copy of all keys contained in the sparse array.
 *//*  w w  w.  j ava2  s  .  c  o  m*/
public static <E> long[] getKeys(final LongSparseArray<E> array) {
    final int length = array.size();
    final long[] result = new long[length];
    for (int i = 0, j = length; i < j; i++) {
        result[i] = array.keyAt(i);
    }
    return result;
}

From source file:com.wei.c.im.test.msg.client.MsgListHelper.java

private static <T> void addAll(List<T> list, LongSparseArray<T> map) {
    for (int i = 0; i < map.size(); i++) {
        list.add(map.valueAt(i));/*from   w w w. j  a  va2 s.co  m*/
    }
}

From source file:de.vanita5.twittnuker.util.TwitterWrapper.java

public static int removeUnreadCounts(final Context context, final int position,
        final LongSparseArray<Set<Long>> counts) {
    if (context == null || position < 0 || counts == null)
        return 0;
    int result = 0;
    for (int i = 0, j = counts.size(); i < j; i++) {
        final long key = counts.keyAt(i);
        final Set<Long> value = counts.valueAt(i);
        final Uri.Builder builder = UnreadCounts.CONTENT_URI.buildUpon();
        builder.appendPath(String.valueOf(position));
        builder.appendPath(String.valueOf(key));
        builder.appendPath(CollectionUtils.toString(value, ',', false));
        result += context.getContentResolver().delete(builder.build(), null, null);
    }/*from www.jav  a 2s.c  o  m*/
    return result;
}

From source file:com.bmd.android.collection.internal.SupportLongSparseArrayReverseIterator.java

public SupportLongSparseArrayReverseIterator(final LongSparseArray<E> array) {

    super(array.size());

    mSparseArray = array;
}

From source file:com.bmd.android.collection.internal.SupportLongSparseArrayIterator.java

public SupportLongSparseArrayIterator(final LongSparseArray<V> array) {

    super(array.size());

    mSparseArray = array;
}

From source file:nz.ac.otago.psyanlab.common.model.util.LongSparseArrayGsonAdapter.java

@Override
public LongSparseArray<T> read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//from  w w w . ja va  2s  . c o m
        return null;
    }
    LongSparseArray<Object> temp = mGson.fromJson(in, mTypeOfLongSparseArrayOfObject);
    LongSparseArray<T> result = new LongSparseArray<T>(temp.size());
    long key;
    JsonElement tElement;
    for (int i = 0; i < temp.size(); i++) {
        key = temp.keyAt(i);
        tElement = mGson.toJsonTree(temp.get(key), new TypeToken<T>() {
        }.getType());
        result.put(key, mGson.fromJson(tElement, mTClazz));
    }
    return result;
}

From source file:nz.ac.otago.psyanlab.common.designer.util.LongSparseArrayAdapter.java

private Long[] sortKeys(Context context, LongSparseArray<T> items) {
    Locale locale = context.getResources().getConfiguration().locale;
    final Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);

    SortedSet<Long> sortedKeys = new TreeSet<Long>(new Comparator<Long>() {
        @Override//from  w  w w  .j av  a  2 s . c  o  m
        public int compare(Long lhs, Long rhs) {
            return collator.compare(mItems.get(lhs).toString(), mItems.get(rhs).toString());
        }
    });

    for (int i = 0; i < items.size(); i++) {
        sortedKeys.add(items.keyAt(i));
    }

    return sortedKeys.toArray(new Long[sortedKeys.size()]);
}

From source file:com.android.exchange.eas.EasSync.java

/**
 * @return Number of messages successfully synced, or a negative response code from
 *         {@link EasOperation} if we encountered any errors.
 *///w w  w. jav  a2 s .  c o m
public final int upsync() {
    final List<MessageStateChange> changes = MessageStateChange.getChanges(mContext, getAccountId(),
            getProtocolVersion() < Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE);
    if (changes == null) {
        return 0;
    }
    final LongSparseArray<List<MessageStateChange>> allData = MessageStateChange.convertToChangesMap(changes);
    if (allData == null) {
        return 0;
    }

    final long[][] messageIds = new long[2][changes.size()];
    final int[] counts = new int[2];
    int result = 0;

    for (int i = 0; i < allData.size(); ++i) {
        mMailboxId = allData.keyAt(i);
        mStateChanges = allData.valueAt(i);
        boolean retryMailbox = true;
        // If we've already encountered a fatal error, don't even try to upsync subsequent
        // mailboxes.
        if (result >= 0) {
            final Cursor mailboxCursor = mContext.getContentResolver().query(
                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailboxId),
                    Mailbox.ProjectionSyncData.PROJECTION, null, null, null);
            if (mailboxCursor != null) {
                try {
                    if (mailboxCursor.moveToFirst()) {
                        mMailboxServerId = mailboxCursor.getString(Mailbox.ProjectionSyncData.COLUMN_SERVER_ID);
                        mMailboxSyncKey = mailboxCursor.getString(Mailbox.ProjectionSyncData.COLUMN_SYNC_KEY);
                        if (TextUtils.isEmpty(mMailboxSyncKey) || mMailboxSyncKey.equals("0")) {
                            // For some reason we can get here without a valid mailbox sync key
                            // b/10797675
                            // TODO: figure out why and clean this up
                            LogUtils.d(LOG_TAG, "Tried to sync mailbox %d with invalid mailbox sync key",
                                    mMailboxId);
                        } else {
                            result = performOperation();
                            if (result >= 0) {
                                // Our request gave us back a legitimate answer; this is the
                                // only case in which we don't retry this mailbox.
                                retryMailbox = false;
                                if (result == RESULT_OK) {
                                    handleMessageUpdateStatus(mMessageUpdateStatus, messageIds, counts);
                                } else if (result == RESULT_NO_MAILBOX) {
                                    // A retry here is pointless -- the message's mailbox (and
                                    // therefore the message) is gone, so mark as success so
                                    // that these entries get wiped from the change list.
                                    for (final MessageStateChange msc : mStateChanges) {
                                        messageIds[0][counts[0]] = msc.getMessageId();
                                        ++counts[0];
                                    }
                                } else {
                                    LogUtils.wtf(LOG_TAG, "Unrecognized result code: %d", result);
                                }
                            }
                        }
                    }
                } finally {
                    mailboxCursor.close();
                }
            }
        }
        if (retryMailbox) {
            for (final MessageStateChange msc : mStateChanges) {
                messageIds[1][counts[1]] = msc.getMessageId();
                ++counts[1];
            }
        }
    }

    final ContentResolver cr = mContext.getContentResolver();
    MessageStateChange.upsyncSuccessful(cr, messageIds[0], counts[0]);
    MessageStateChange.upsyncRetry(cr, messageIds[1], counts[1]);

    if (result < 0) {
        return result;
    }
    return counts[0];
}

From source file:com.snda.mymarket.providers.downloads.DownloadNotifier.java

private void updateWithLocked(LongSparseArray<DownloadInfo> downloads) {
    final Resources res = mContext.getResources();
    // Cluster downloads together
    final Map<String, Collection<DownloadInfo>> clustered = new HashMap<String, Collection<DownloadInfo>>();
    for (int index = 0; index < downloads.size(); index++) {
        final String tag = buildNotificationTag(downloads.valueAt(index));
        if (tag != null) {
            Collection<DownloadInfo> arrDownloads = clustered.get(tag);
            if (arrDownloads == null) {
                arrDownloads = new ArrayList<DownloadInfo>();
                clustered.put(tag, arrDownloads);
            }//from  w w w . ja  v a  2 s  . c o  m
            arrDownloads.add(downloads.valueAt(index));
        }
    }
    // Build notification for each cluster
    for (String tag : clustered.keySet()) {
        final int type = getNotificationTagType(tag);
        final Collection<DownloadInfo> cluster = clustered.get(tag);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
        // Use time when cluster was first shown to avoid shuffling
        final long firstShown;
        if (mActiveNotifs.containsKey(tag)) {
            firstShown = mActiveNotifs.get(tag);
        } else {
            firstShown = System.currentTimeMillis();
            mActiveNotifs.put(tag, firstShown);
        }
        builder.setWhen(firstShown);
        // Show relevant icon
        if (type == TYPE_ACTIVE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download);
        } else if (type == TYPE_WAITING) {
            builder.setSmallIcon(android.R.drawable.stat_sys_warning);
        } else if (type == TYPE_COMPLETE) {
            builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        }
        // Build action intents
        if (type == TYPE_ACTIVE || type == TYPE_WAITING) {
            // build a synthetic uri for intent identification purposes
            final Uri uri = new Uri.Builder().scheme("active-dl").appendPath(tag).build();
            final Intent intent = new Intent(Constants.ACTION_LIST, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.setOngoing(true);
        } else if (type == TYPE_COMPLETE) {
            final DownloadInfo info = cluster.iterator().next();
            final Uri uri = ContentUris.withAppendedId(Downloads.ALL_DOWNLOADS_CONTENT_URI, info.mId);
            builder.setAutoCancel(true);
            final String action;
            if (Downloads.isStatusError(info.mStatus)) {
                action = Constants.ACTION_LIST;
            } else {
                if (info.mDestination != Downloads.DESTINATION_SYSTEMCACHE_PARTITION) {
                    action = Constants.ACTION_OPEN;
                } else {
                    action = Constants.ACTION_LIST;
                }
            }
            final Intent intent = new Intent(action, uri, mContext, DownloadReceiver.class);
            intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, getDownloadIds(cluster));
            builder.setContentIntent(
                    PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
            final Intent hideIntent = new Intent(Constants.ACTION_HIDE, uri, mContext, DownloadReceiver.class);
            builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, hideIntent, 0));
        }
        // Calculate and show progress
        String remainingText = null;
        String percentText = null;
        if (type == TYPE_ACTIVE) {
            long current = 0;
            long total = 0;
            long speed = 0;
            synchronized (mDownloadSpeed) {
                for (DownloadInfo info : cluster) {
                    if (info.mTotalBytes != -1) {
                        current += info.mCurrentBytes;
                        total += info.mTotalBytes;
                        speed += mDownloadSpeed.get(info.mId) == null ? 0 : mDownloadSpeed.get(info.mId);
                    }
                }
            }

            if (total > 0) {
                final int percent = (int) ((current * 100) / total);
                percentText = res.getString(R.string.download_percent, percent);
                if (speed > 0) {
                    final long remainingMillis = (total - current) / speed;
                    remainingText = res.getString(R.string.download_remaining,
                            DateUtils.formatDuration(remainingMillis));
                }
                builder.setProgress(100, percent, false);
            } else {
                builder.setProgress(100, 0, true);
            }
        }
        // Build titles and description
        final Notification notif;
        if (cluster.size() == 1) {
            final DownloadInfo info = cluster.iterator().next();
            builder.setContentTitle(getDownloadTitle(res, info));
            if (type == TYPE_ACTIVE) {
                if (!TextUtils.isEmpty(info.mDescription)) {
                    builder.setContentText(info.mDescription);
                } else {
                    builder.setContentText(remainingText);
                }
                builder.setContentInfo(percentText);
            } else if (type == TYPE_WAITING) {
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
            } else if (type == TYPE_COMPLETE) {
                if (Downloads.isStatusError(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_failed));
                } else if (Downloads.isStatusSuccess(info.mStatus)) {
                    builder.setContentText(res.getText(R.string.notification_download_complete));
                }
            }
            notif = builder.build();
        } else {
            final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);
            for (DownloadInfo info : cluster) {
                inboxStyle.addLine(getDownloadTitle(res, info));
            }
            if (type == TYPE_ACTIVE) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_active, cluster.size(), cluster.size()));
                builder.setContentText(remainingText);
                builder.setContentInfo(percentText);
                inboxStyle.setSummaryText(remainingText);
            } else if (type == TYPE_WAITING) {
                builder.setContentTitle(
                        res.getQuantityString(R.plurals.notif_summary_waiting, cluster.size(), cluster.size()));
                builder.setContentText(res.getString(R.string.notification_need_wifi_for_size));
                inboxStyle.setSummaryText(res.getString(R.string.notification_need_wifi_for_size));
            }
            notif = inboxStyle.build();
        }
        mNotifManager.notify(tag, 0, notif);
    }
    // Remove stale tags that weren't renewed
    final Iterator<String> it = mActiveNotifs.keySet().iterator();
    while (it.hasNext()) {
        final String tag = it.next();
        if (!clustered.containsKey(tag)) {
            mNotifManager.cancel(tag, 0);
            it.remove();
        }
    }
}

From source file:com.android.messaging.datamodel.action.SyncMessagesAction.java

/**
 * Batch loading MMS sender for the messages in current batch
 *///  w w  w  .j av  a 2s  .c  o  m
private void setMmsSenders(final LongSparseArray<MmsMessage> mmses, final ThreadInfoCache cache) {
    // Store all the MMS messages
    for (int i = 0; i < mmses.size(); i++) {
        final MmsMessage mms = mmses.valueAt(i);

        final boolean isOutgoing = mms.mType != Mms.MESSAGE_BOX_INBOX;
        String senderId = null;
        if (!isOutgoing) {
            // We only need to find out sender phone number for received message
            senderId = getMmsSender(mms, cache);
            if (senderId == null) {
                LogUtil.w(TAG, "SyncMessagesAction: Could not find sender of incoming MMS " + "message "
                        + mms.getUri() + "; using 'unknown sender' instead");
                senderId = ParticipantData.getUnknownSenderDestination();
            }
        }
        mms.setSender(senderId);
    }
}