Example usage for android.util SparseIntArray put

List of usage examples for android.util SparseIntArray put

Introduction

In this page you can find the example usage for android.util SparseIntArray put.

Prototype

public void put(int key, int value) 

Source Link

Document

Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key if there was one.

Usage

From source file:org.voidsink.anewjkuapp.fragment.CalendarFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.clear();/* w  w w  .java  2 s.com*/

    Account mAccount = AppUtils.getAccount(getContext());
    if (mAccount != null) {
        // fetch calendar colors
        final SparseIntArray mColors = new SparseIntArray();
        ContentResolver cr = getContext().getContentResolver();
        Cursor cursor = cr.query(CalendarContractWrapper.Calendars.CONTENT_URI(), new String[] {
                CalendarContractWrapper.Calendars._ID(), CalendarContractWrapper.Calendars.CALENDAR_COLOR() },
                null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                mColors.put(cursor.getInt(0), cursor.getInt(1));
            }
            cursor.close();
        }

        List<CalendarListEvent> mEvents = new ArrayList<>();

        if (data != null) {
            data.moveToFirst();
            data.moveToPrevious();
            while (data.moveToNext()) {
                mEvents.add(new CalendarListEvent(getContext(), data.getLong(CalendarUtils.COLUMN_EVENT_ID),
                        mColors.get(data.getInt(CalendarUtils.COLUMN_EVENT_CAL_ID)),
                        data.getString(CalendarUtils.COLUMN_EVENT_TITLE),
                        data.getString(CalendarUtils.COLUMN_EVENT_DESCRIPTION),
                        data.getString(CalendarUtils.COLUMN_EVENT_LOCATION),
                        data.getLong(CalendarUtils.COLUMN_EVENT_DTSTART),
                        data.getLong(CalendarUtils.COLUMN_EVENT_DTEND),
                        data.getInt(CalendarUtils.COLUMN_EVENT_ALL_DAY) == 1));
            }
        }

        mAdapter.addAll(mEvents);
    }

    mAdapter.notifyDataSetChanged();

    finishProgress();
}

From source file:net.naonedbus.manager.impl.FavoriManager.java

/**
 * Remplacer les favoris par ceux de la liste
 * /* ww w.j  a  v a 2 s . c o m*/
 * @param contentResolver
 * @param container
 */
private void fromList(final ContentResolver contentResolver, final FavoriContainer container) {
    final ArretManager arretManager = ArretManager.getInstance();
    final GroupeManager groupeManager = GroupeManager.getInstance();
    final SparseIntArray groupeMapping = new SparseIntArray();

    // Delete old items
    contentResolver.delete(FavoriProvider.CONTENT_URI, null, null);
    contentResolver.delete(GroupeProvider.CONTENT_URI, null, null);

    // Add new items
    for (final net.naonedbus.rest.container.FavoriContainer.Groupe g : container.groupes) {
        final Groupe groupe = new Groupe();
        groupe.setNom(g.nom);
        groupe.setOrdre(g.ordre);

        final int idLocal = groupeManager.add(contentResolver, groupe).intValue();
        groupeMapping.put(g.id, idLocal);
    }

    Integer itemId;
    final Favori.Builder builder = new Favori.Builder();
    for (final net.naonedbus.rest.container.FavoriContainer.Favori f : container.favoris) {
        builder.setCodeArret(f.codeArret);
        builder.setCodeSens(f.codeSens);
        builder.setCodeLigne(f.codeLigne);
        builder.setNomFavori(f.nomFavori);

        itemId = arretManager.getIdByFavori(contentResolver, builder.build());
        if (itemId != null) {
            builder.setId(itemId);
            addFavori(contentResolver, builder.build());

            // Associer aux groupes
            final List<Integer> favoriGroupes = f.idGroupes;
            if (favoriGroupes != null)
                for (final Integer idGroupe : favoriGroupes) {
                    if (groupeMapping.indexOfKey(idGroupe) > -1) {
                        groupeManager.addFavoriToGroup(contentResolver, groupeMapping.get(idGroupe), itemId);
                    }
                }
        }
    }
}

From source file:org.voidsink.anewjkuapp.fragment.CalendarFragment2.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    ArrayList<WeekViewEvent> events = mWeekViewLoader.getEvents(loader.getId());
    events.clear();//from  w w  w.j  a  v  a  2  s  .c  o m

    Account mAccount = AppUtils.getAccount(getContext());
    if (mAccount != null) {
        // fetch calendar colors
        final SparseIntArray mColors = new SparseIntArray();
        ContentResolver cr = getContext().getContentResolver();
        Cursor cursor = cr.query(CalendarContractWrapper.Calendars.CONTENT_URI(), new String[] {
                CalendarContractWrapper.Calendars._ID(), CalendarContractWrapper.Calendars.CALENDAR_COLOR() },
                null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int color = cursor.getInt(1);

                double lastContrast = ColorUtils.calculateContrast(color, mWeekView.getEventTextColor());
                //Log.d(TAG, String.format("color=%d %d %d, contrast=%f", Color.red(color), Color.green(color), Color.blue(color), lastContrast));

                while (lastContrast < 1.6) {
                    float[] hsv = new float[3];

                    Color.colorToHSV(color, hsv);
                    hsv[2] = Math.max(0f, hsv[2] - 0.033f); // darken
                    color = Color.HSVToColor(hsv);

                    lastContrast = ColorUtils.calculateContrast(color, mWeekView.getEventTextColor());
                    //Log.d(TAG, String.format("new color=%d %d %d, contrast=%f", Color.red(color), Color.green(color), Color.blue(color), lastContrast));

                    if (hsv[2] == 0)
                        break;
                }

                mColors.put(cursor.getInt(0), color);
            }
            cursor.close();
        }

        if (data != null) {
            data.moveToFirst();
            data.moveToPrevious();
            while (data.moveToNext()) {

                boolean allDay = data.getInt(CalendarUtils.COLUMN_EVENT_ALL_DAY) == 1;

                Calendar startTime = Calendar.getInstance();
                if (allDay) {
                    startTime.setTimeZone(TimeZone.getTimeZone("UTC"));
                }
                startTime.setTimeInMillis(data.getLong(CalendarUtils.COLUMN_EVENT_DTSTART));

                Calendar endTime = Calendar.getInstance();
                if (allDay) {
                    endTime.setTimeZone(TimeZone.getTimeZone("UTC"));
                }
                endTime.setTimeInMillis(data.getLong(CalendarUtils.COLUMN_EVENT_DTEND));
                if (allDay && endTime.getTimeInMillis() % DateUtils.DAY_IN_MILLIS == 0) {
                    endTime.add(Calendar.MILLISECOND, -1);
                }

                WeekViewEvent event = new WeekViewEvent(data.getLong(CalendarUtils.COLUMN_EVENT_ID),
                        data.getString(CalendarUtils.COLUMN_EVENT_TITLE),
                        data.getString(CalendarUtils.COLUMN_EVENT_LOCATION), startTime, endTime, allDay);

                event.setColor(mColors.get(data.getInt(CalendarUtils.COLUMN_EVENT_CAL_ID)));

                events.add(event);
            }
        }
    }

    mWeekView.notifyDatasetChanged();
}

From source file:org.chromium.chrome.browser.tabmodel.TabPersistentStore.java

private void restoreTab(TabRestoreDetails tabToRestore, TabState tabState, boolean setAsActive) {
    // If we don't have enough information about the Tab, bail out.
    boolean isIncognito = isIncognitoTabBeingRestored(tabToRestore, tabState);
    if (tabState == null) {
        if (tabToRestore.isIncognito == null) {
            Log.w(TAG, "Failed to restore tab: not enough info about its type was available.");
            return;
        } else if (isIncognito) {
            Log.i(TAG, "Failed to restore Incognito tab: its TabState could not be restored.");
            return;
        }//from   w w w .  j  a v a2 s . c o m
    }

    TabModel model = mTabModelSelector.getModel(isIncognito);
    SparseIntArray restoredTabs = isIncognito ? mIncognitoTabsRestored : mNormalTabsRestored;
    int restoredIndex = 0;
    if (tabToRestore.fromMerge) {
        // Put any tabs being merged into this list at the end.
        restoredIndex = mTabModelSelector.getModel(isIncognito).getCount();
    } else if (restoredTabs.size() > 0
            && tabToRestore.originalIndex > restoredTabs.keyAt(restoredTabs.size() - 1)) {
        // If the tab's index is too large, restore it at the end of the list.
        restoredIndex = restoredTabs.size();
    } else {
        // Otherwise try to find the tab we should restore before, if any.
        for (int i = 0; i < restoredTabs.size(); i++) {
            if (restoredTabs.keyAt(i) > tabToRestore.originalIndex) {
                Tab nextTabByIndex = TabModelUtils.getTabById(model, restoredTabs.valueAt(i));
                restoredIndex = nextTabByIndex != null ? model.indexOf(nextTabByIndex) : -1;
                break;
            }
        }
    }

    int tabId = tabToRestore.id;
    if (tabState != null) {
        mTabCreatorManager.getTabCreator(isIncognito).createFrozenTab(tabState, tabToRestore.id, restoredIndex);
    } else {
        Log.w(TAG, "Failed to restore TabState; creating Tab with last known URL.");
        Tab fallbackTab = mTabCreatorManager.getTabCreator(isIncognito)
                .createNewTab(new LoadUrlParams(tabToRestore.url), TabModel.TabLaunchType.FROM_RESTORE, null);
        tabId = fallbackTab.getId();
        model.moveTab(tabId, restoredIndex);
    }

    // If the tab is being restored from a merge and its index is 0, then the model being
    // merged into doesn't contain any tabs. Select the first tab to avoid having no tab
    // selected. TODO(twellington): The first tab will always be selected. Instead, the tab that
    // was selected in the other model before the merge should be selected after the merge.
    if (setAsActive || (tabToRestore.fromMerge && restoredIndex == 0)) {
        boolean wasIncognitoTabModelSelected = mTabModelSelector.isIncognitoSelected();
        int selectedModelTabCount = mTabModelSelector.getCurrentModel().getCount();

        TabModelUtils.setIndex(model, TabModelUtils.getTabIndexById(model, tabId));
        boolean isIncognitoTabModelSelected = mTabModelSelector.isIncognitoSelected();

        // Setting the index will cause the tab's model to be selected. Set it back to the model
        // that was selected before setting the index if the index is being set during a merge
        // unless the previously selected model is empty (e.g. showing the empty background
        // view on tablets).
        if (tabToRestore.fromMerge && wasIncognitoTabModelSelected != isIncognitoTabModelSelected
                && selectedModelTabCount != 0) {
            mTabModelSelector.selectModel(wasIncognitoTabModelSelected);
        }
    }
    restoredTabs.put(tabToRestore.originalIndex, tabId);
}