Example usage for android.util SparseIntArray indexOfKey

List of usage examples for android.util SparseIntArray indexOfKey

Introduction

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

Prototype

public int indexOfKey(int key) 

Source Link

Document

Returns the index for which #keyAt would return the specified key, or a negative number if the specified key is not mapped.

Usage

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;/*  w w  w . j av  a2 s . c o m*/
}

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;/*from   w  w w. ja v a  2 s  .  c  om*/
    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:net.naonedbus.manager.impl.FavoriManager.java

/**
 * Remplacer les favoris par ceux de la liste
 * /* w  w  w.jav  a  2s  .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);
                    }
                }
        }
    }
}