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

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

Introduction

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

Prototype

public long keyAt(int i) 

Source Link

Usage

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

/**
 * @return A copy of all keys contained in the sparse array.
 *//*from ww  w. j a  v a 2s  .  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: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);
    }/*ww w  .ja  va 2 s .c om*/
    return result;
}

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  .  j  a v  a 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//  w w  w .  jav a  2s.co 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 ww  .  ja  v a2s.  c  om
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:es.ugr.swad.swadroid.modules.groups.EnrollmentExpandableListAdapter.java

public EnrollmentExpandableListAdapter(Context context, ArrayList<Model> groups,
        LongSparseArray<ArrayList<Group>> children, int layoutGroup, int layoutChild, int currentRole) {
    super();//from ww  w. j a  v a 2 s. c o  m
    this.context = context;
    this.groups = groups;
    this.children = children;
    this.layoutGroup = layoutGroup;
    this.layoutChild = layoutChild;
    this.role = currentRole;

    //Initialize real inscription
    for (int i = 0; i < children.size(); i++) {
        Long groupTypeCode = children.keyAt(i);
        ArrayList<Group> groupsChildren = children.get(groupTypeCode);
        realMembership.put(groupTypeCode, new boolean[groupsChildren.size()]);
    }

    setRealInscription();
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

From source file:org.mariotaku.twidere.util.DataStoreUtils.java

@WorkerThread
public static void updateActivity(ContentResolver cr, Uri uri, String where, String[] whereArgs,
        UpdateActivityAction action) {/*from   w w w .  j  a  v  a 2s  .c  o  m*/
    final Cursor c = cr.query(uri, Activities.COLUMNS, where, whereArgs, null);
    if (c == null)
        return;
    LongSparseArray<ContentValues> values = new LongSparseArray<>();
    try {
        ParcelableActivityCursorIndices ci = new ParcelableActivityCursorIndices(c);
        c.moveToFirst();
        while (!c.isAfterLast()) {
            final ParcelableActivity activity = ci.newObject(c);
            action.process(activity);
            values.put(activity._id, ParcelableActivityValuesCreator.create(activity));
            c.moveToNext();
        }
    } finally {
        c.close();
    }
    String updateWhere = Expression.equalsArgs(Activities._ID).getSQL();
    String[] updateWhereArgs = new String[1];
    for (int i = 0, j = values.size(); i < j; i++) {
        updateWhereArgs[0] = String.valueOf(values.keyAt(i));
        cr.update(uri, values.valueAt(i), updateWhere, updateWhereArgs);
    }
}

From source file:com.timekeeping.app.activities.MainActivity.java

@Override
public boolean onActionItemClicked(final android.support.v7.view.ActionMode actionMode, MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case R.id.action_delete: {
        new ParallelTask<Void, Void, LongSparseArray<Time>>() {
            @Override/* w ww .  j  a va 2 s. c  o  m*/
            protected LongSparseArray<Time> doInBackground(Void... params) {
                DB db = DB.getInstance(getApplication());
                long key;
                Time item;
                LongSparseArray<Time> removedItems = mAdp.removeItems();
                for (int i = 0; removedItems != null && i < removedItems.size(); i++) {
                    key = removedItems.keyAt(i);
                    item = removedItems.get(key);
                    db.removeTime(item);
                }
                return removedItems;
            }

            @Override
            protected void onPostExecute(LongSparseArray<Time> result) {
                super.onPostExecute(result);
                if (result == null) {
                    if (mAdp != null) {
                        mAdp.notifyDataSetChanged();
                    }
                }
                mActionMode.finish();
                mActionMode = null;

                EventBus.getDefault().post(new AfterDeleteEvent());
            }
        }.executeParallel();
        break;
    }
    default:
        return false;
    }
    return true;
}

From source file:freed.utils.AppSettingsManager.java

public void saveDngProfiles(LongSparseArray<DngProfile> dngProfileList) {
    BufferedWriter writer = null;
    try {/*w w w. j  ava  2 s  . c  o m*/

        File configFile = new File(StringUtils.GetFreeDcamConfigFolder + "dngprofiles.xml");
        Log.d(TAG, configFile.getAbsolutePath() + " exists:" + configFile.exists());
        Log.d(TAG, configFile.getParentFile().getAbsolutePath() + " exists:"
                + configFile.getParentFile().exists());
        if (!configFile.getParentFile().exists())
            configFile.getParentFile().mkdirs();
        Log.d(TAG, configFile.getParentFile().getAbsolutePath() + " exists:"
                + configFile.getParentFile().exists());
        configFile.createNewFile();
        writer = new BufferedWriter(new FileWriter(configFile));
        writer.write("<devices>" + "\r\n");
        writer.write("<device name = \"" + mDevice + "\">\r\n");

        for (int i = 0; i < dngProfileList.size(); i++) {
            long t = dngProfileList.keyAt(i);
            Log.d(TAG, "Write Profile: " + t);
            writer.write(dngProfileList.get(t).getXmlString(t));
        }

        writer.write("</device>" + "\r\n");
        writer.write("</devices>" + "\r\n");
        writer.flush();

    } catch (IOException e) {
        Log.WriteEx(e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

From source file:com.facebook.litho.MountState.java

private static boolean shouldUpdateMountItem(LayoutOutput layoutOutput, MountItem currentMountItem,
        boolean useUpdateValueFromLayoutOutput, LongSparseArray<MountItem> indexToItemMap,
        long[] layoutOutputsIds, ComponentsLogger logger) {
    final @LayoutOutput.UpdateState int updateState = layoutOutput.getUpdateState();
    final Component currentComponent = currentMountItem.getComponent();
    final ComponentLifecycle currentLifecycle = currentComponent.getLifecycle();
    final Component nextComponent = layoutOutput.getComponent();
    final ComponentLifecycle nextLifecycle = nextComponent.getLifecycle();

    // If the two components have different sizes and the mounted content depends on the size we
    // just return true immediately.
    if (!sameSize(layoutOutput, currentMountItem) && nextLifecycle.isMountSizeDependent()) {
        return true;
    }//from   w  w w  .ja v  a  2  s . c  om

    if (useUpdateValueFromLayoutOutput) {
        if (updateState == LayoutOutput.STATE_UPDATED) {

            // Check for incompatible ReferenceLifecycle.
            if (currentLifecycle instanceof DrawableComponent && nextLifecycle instanceof DrawableComponent
                    && currentLifecycle.shouldComponentUpdate(currentComponent, nextComponent)) {

                if (logger != null) {
                    LayoutOutputLog logObj = new LayoutOutputLog();

                    logObj.currentId = indexToItemMap.keyAt(indexToItemMap.indexOfValue(currentMountItem));
                    logObj.currentLifecycle = currentLifecycle.toString();

                    logObj.nextId = layoutOutput.getId();
                    logObj.nextLifecycle = nextLifecycle.toString();

                    for (int i = 0; i < layoutOutputsIds.length; i++) {
                        if (layoutOutputsIds[i] == logObj.currentId) {
                            if (logObj.currentIndex == -1) {
                                logObj.currentIndex = i;
                            }

                            logObj.currentLastDuplicatedIdIndex = i;
                        }
                    }

                    if (logObj.nextId == logObj.currentId) {
                        logObj.nextIndex = logObj.currentIndex;
                        logObj.nextLastDuplicatedIdIndex = logObj.currentLastDuplicatedIdIndex;
                    } else {
                        for (int i = 0; i < layoutOutputsIds.length; i++) {
                            if (layoutOutputsIds[i] == logObj.nextId) {
                                if (logObj.nextIndex == -1) {
                                    logObj.nextIndex = i;
                                }

                                logObj.nextLastDuplicatedIdIndex = i;
                            }
                        }
                    }

                    final LogEvent mismatchEvent = logger
                            .newEvent(EVENT_SHOULD_UPDATE_REFERENCE_LAYOUT_MISMATCH);
                    mismatchEvent.addParam(PARAM_MESSAGE, logObj.toString());
                    logger.log(mismatchEvent);
                }

                return true;
            }

            return false;
        } else if (updateState == LayoutOutput.STATE_DIRTY) {
            return true;
        }
    }

    if (!currentLifecycle.callsShouldUpdateOnMount()) {
        return true;
    }

    return currentLifecycle.shouldComponentUpdate(currentComponent, nextComponent);
}