Example usage for android.util SparseIntArray valueAt

List of usage examples for android.util SparseIntArray valueAt

Introduction

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

Prototype

public int valueAt(int index) 

Source Link

Document

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this SparseIntArray stores.

Usage

From source file:net.sf.sprockets.util.SparseArrays.java

/**
 * Get the values of the SparseIntArray.
 */// w  w w  . j  a v a 2s.  co  m
public static int[] values(SparseIntArray array) {
    int[] vals = new int[array.size()];
    for (int i = 0; i < vals.length; i++) {
        vals[i] = array.valueAt(i);
    }
    return vals;
}

From source file:Main.java

/**
 * internal method to handle the selections if items are added / removed
 *
 * @param positions     the positions map which should be adjusted
 * @param startPosition the global index of the first element modified
 * @param endPosition   the global index up to which the modification changed the indices (should be MAX_INT if we check til the end)
 * @param adjustBy      the value by which the data was shifted
 * @return the adjusted map//  w w w. j a v a2 s  .c o  m
 */
public static SparseIntArray adjustPosition(SparseIntArray positions, int startPosition, int endPosition,
        int adjustBy) {
    SparseIntArray newPositions = new SparseIntArray();

    for (int i = 0, size = positions.size(); i < size; i++) {
        int position = positions.keyAt(i);

        //if our current position is not within the bounds to check for we can add it
        if (position < startPosition || position > endPosition) {
            newPositions.put(position, positions.valueAt(i));
        } else if (adjustBy > 0) {
            //if we added items and we are within the bounds we can simply add the adjustBy to our entry
            newPositions.put(position + adjustBy, positions.valueAt(i));
        } else if (adjustBy < 0) {
            //if we removed items and we are within the bounds we have to check if the item was removed
            //adjustBy is negative in this case
            if (position > startPosition + adjustBy && position <= startPosition) {
                ;//we are within the removed items range we don't add this item anymore
            } else {
                //otherwise we adjust our position
                newPositions.put(position + adjustBy, positions.valueAt(i));
            }
        }
    }

    return newPositions;
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

public static boolean resolveAttribute(int attrId, TypedValue outValue) {
    SparseIntArray customAttrs = currentAttrs;
    if (customAttrs == null)
        return false;
    int index = customAttrs.indexOfKey(attrId);
    if (index < 0)
        return false;
    outValue.type = TypedValue.TYPE_INT_COLOR_ARGB8;
    outValue.data = customAttrs.valueAt(index);
    return true;/*from w w w. j ava2  s  .  c om*/
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

private static void processWindow(Context context, SparseIntArray attrs, int textColorPrimaryOriginal,
        int textColorPrimaryOverridden) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        return;// w  w  w.java 2 s .  c  o  m
    boolean isLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

    int materialPrimaryIndex = attrs.indexOfKey(R.attr.materialPrimary);
    int materialPrimaryDarkIndex = attrs.indexOfKey(R.attr.materialPrimaryDark);
    int materialNavigationBarIndex = attrs.indexOfKey(R.attr.materialNavigationBar);
    boolean overrideActionbarColor = materialPrimaryIndex >= 0;
    boolean overridePanelsColor = Math.max(materialPrimaryDarkIndex, materialNavigationBarIndex) >= 0;

    boolean overrideTextColor = textColorPrimaryOriginal != textColorPrimaryOverridden;
    if (!overrideTextColor && !overrideActionbarColor && !overridePanelsColor)
        return;

    Window window = ((Activity) context).getWindow();
    final View decorView = window.getDecorView();
    Resources resources = context.getResources();

    if (overrideActionbarColor) {
        try {
            Drawable background = new ColorDrawable(attrs.valueAt(materialPrimaryIndex));
            Object actionBar = context.getClass().getMethod("getActionBar").invoke(context);
            actionBar.getClass().getMethod("setBackgroundDrawable", Drawable.class).invoke(actionBar,
                    background);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (overrideTextColor) {
        int id = resources.getIdentifier("action_bar_title", "id", "android");
        if (id != 0) {
            View v = decorView.findViewById(id);
            if (v instanceof TextView)
                ((TextView) v).setTextColor(textColorPrimaryOverridden);
        }
    }

    if (isLollipop && overrideTextColor) {
        try {
            int id = resources.getIdentifier("action_bar", "id", "android");
            if (id == 0)
                throw new Exception("'android:id/action_bar' identifier not found");
            View v = decorView.findViewById(id);
            if (v == null)
                throw new Exception("view with id 'android:id/action_bar' not found");
            Class<?> toolbarClass = Class.forName("android.widget.Toolbar");
            if (!toolbarClass.isInstance(v))
                throw new Exception("view 'android:id/action_bar' is not instance of android.widget.Toolbar");
            toolbarClass.getMethod("setTitleTextColor", int.class).invoke(v, textColorPrimaryOverridden);
            setLollipopMenuOverflowIconColor((ViewGroup) v, textColorPrimaryOverridden);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (isLollipop && overridePanelsColor) {
        try {
            if (materialPrimaryDarkIndex >= 0) {
                window.getClass().getMethod("setStatusBarColor", int.class).invoke(window,
                        attrs.valueAt(materialPrimaryDarkIndex));
            }
            if (materialNavigationBarIndex >= 0) {
                window.getClass().getMethod("setNavigationBarColor", int.class).invoke(window,
                        attrs.valueAt(materialNavigationBarIndex));
            }
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
}

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  .ja v a  2 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);
}