Example usage for android.util SparseArray put

List of usage examples for android.util SparseArray put

Introduction

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

Prototype

public void put(int key, E 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:Main.java

public static <T extends View> T hold(View view, int id) {
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();

    if (viewHolder == null) {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);//w w  w  . ja  v  a  2s .  c om
    }

    View childView = viewHolder.get(id);

    if (childView == null) {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }

    return (T) childView;
}

From source file:it.unicaradio.android.gui.Tabs.java

public static SparseArray<Tab> getTabs(Tabs tabsContainer) {
    SparseArray<Tab> tabs = new SparseArray<Tab>();

    for (int i = 0; i < tabsContainer.getChildCount(); i++) {
        View child = tabsContainer.getChildAt(i);
        if (child instanceof Tab) {
            Tab tab = (Tab) child;/*w  ww . j  a  v  a  2 s  .c  om*/
            tabs.put(tab.getType(), tab);
        }
    }

    return tabs;
}

From source file:Main.java

/**
 * Compare two dumps and get a list of all indices where
 * they differ from each other./*from ww  w .  j a va2  s . c o m*/
 * @param dump1 The first dump. The sector number is key and the
 * string array represents the blocks.
 * @param dump2 The second dump. The sector number is key and the
 * string array represents the blocks.
 * @return Indices where the two dumps differ. The key represents
 * the sector number. The first dimension of the value represents the
 * block number and the second is a list of indices where dump2 is
 * different from dump1. If the value is Integer[0][0] then the sector
 * exists only in dump1. If the value is Integer[1][0] then the sector
 * exists only in dump2.
 */
public static SparseArray<Integer[][]> diffIndices(SparseArray<String[]> dump1, SparseArray<String[]> dump2) {
    SparseArray<Integer[][]> ret = new SparseArray<Integer[][]>();
    // Walk through all sectors of dump1.
    for (int i = 0; i < dump1.size(); i++) {
        String[] sector1 = dump1.valueAt(i);
        int sectorNr = dump1.keyAt(i);
        String[] sector2 = dump2.get(sectorNr);

        // Check if dump2 has the current sector of dump1.
        if (sector2 == null) {
            ret.put(sectorNr, new Integer[0][0]);
            continue;
        }

        // Check the blocks.
        Integer[][] diffSector = new Integer[sector1.length][];
        // Walk through all blocks.
        for (int j = 0; j < sector1.length; j++) {
            ArrayList<Integer> diffIndices = new ArrayList<Integer>();
            // Walk through all symbols.
            for (int k = 0; k < sector1[j].length(); k++) {
                if (sector1[j].charAt(k) != sector2[j].charAt(k)) {
                    // Found different symbol at index k.
                    diffIndices.add(k);
                }
            }
            if (diffIndices.size() == 0) {
                // Block was identical.
                diffSector[j] = new Integer[0];
            } else {
                diffSector[j] = diffIndices.toArray(new Integer[diffIndices.size()]);
            }
        }
        ret.put(sectorNr, diffSector);
    }

    // Are there sectors that occur only in dump2?
    for (int i = 0; i < dump2.size(); i++) {
        int sectorNr = dump2.keyAt(i);
        if (dump1.get(sectorNr) == null) {
            // Sector only exists in dump2.
            ret.put(sectorNr, new Integer[1][0]);
        }
    }

    return ret;
}

From source file:dentex.youtube.downloader.YTD.java

public static CharSequence getListFilterConstraint(int c) {
    //0/*from w w  w  .  ja v  a 2s . c  om*/
    List<Integer> iMp4List = Arrays.asList(iMp4);
    //1
    List<Integer> iWebmList = Arrays.asList(iWebm);
    //2
    List<Integer> iFlvList = Arrays.asList(iFlv);
    //3
    List<Integer> i3gpList = Arrays.asList(i3gp);

    //4
    List<Integer> iHdList = Arrays.asList(iHd);
    //5
    List<Integer> iLdList = Arrays.asList(iLd);
    //6
    List<Integer> iMdList = Arrays.asList(iMd);
    //7
    List<Integer> iSdList = Arrays.asList(iSd);

    //8
    List<Integer> i3dList = Arrays.asList(i3d);

    //9
    List<Integer> iVoList = Arrays.asList(iVo);
    //10
    List<Integer> iAoList = Arrays.asList(iAo);

    SparseArray<List<Integer>> filtersMap = new SparseArray<List<Integer>>();

    filtersMap.put(MP4_FILTER, iMp4List);
    filtersMap.put(WEBM_FILTER, iWebmList);
    filtersMap.put(FLV_FILTER, iFlvList);
    filtersMap.put(_3GP_FILTER, i3gpList);
    filtersMap.put(HD_FILTER, iHdList);
    filtersMap.put(LD_FILTER, iLdList);
    filtersMap.put(MD_FILTER, iMdList);
    filtersMap.put(SD_FILTER, iSdList);
    filtersMap.put(_3D_FILTER, i3dList);
    filtersMap.put(VO_FILTER, iVoList);
    filtersMap.put(AO_FILTER, iAoList);

    if (c == -1)
        return VIEW_ALL_STRING;

    CharSequence constraint = null;
    List<Integer> selectedMap = filtersMap.get(c);

    for (int i = 0; i < selectedMap.size(); i++) {
        if (constraint == null) {
            constraint = String.valueOf(selectedMap.get(i));
        } else {
            constraint = constraint + "/" + selectedMap.get(i);
        }
    }
    //Utils.logger("i", "ListFilterConstraint: " + constraint, DEBUG_TAG);
    return constraint;
}

From source file:com.ruesga.rview.misc.NotificationsHelper.java

@SuppressWarnings("Convert2streamapi")
public static void recreateNotifications(Context ctx) {
    List<NotificationEntity> entities = NotificationEntity.getAllNotifications(ctx, true, true);
    SparseArray<Account> notifications = new SparseArray<>();
    for (NotificationEntity entity : entities) {
        if (notifications.indexOfKey(entity.mGroupId) < 0) {
            notifications.put(entity.mGroupId, ModelHelper.getAccountFromHash(ctx, entity.mAccountId));
        }//from  w ww  .java2 s.c  o  m
    }

    int count = notifications.size();
    for (int i = 0; i < count; i++) {
        int groupId = notifications.keyAt(i);
        Account account = notifications.valueAt(i);
        dismissNotification(ctx, groupId);
        createNotification(ctx, account, groupId, false);
    }
}

From source file:fr.outadev.android.timeo.TimeoRequestHandler.java

/**
 * Gets the list of the supported bus networks.
 *
 * @return an array containing the network names; the index is their code, and they're associated with their name
 *//*from  www  .  j  ava  2  s  . co  m*/
public static SparseArray<String> getNetworksList() {
    SparseArray<String> networks = new SparseArray<>();

    networks.put(105, "Le Mans");
    networks.put(117, "Pau");
    networks.put(120, "Soissons");
    networks.put(135, "Aix-en-Provence");
    networks.put(147, "Caen");
    networks.put(217, "Dijon");
    networks.put(297, "Brest");
    networks.put(402, "Pau-Agen");
    networks.put(416, "Blois");
    networks.put(422, "Saint-tienne");
    networks.put(440, "Nantes");
    networks.put(457, "Montargis");
    networks.put(497, "Angers");
    networks.put(691, "Macon-Villefranche");
    networks.put(910, "pinay-sur-Orge");
    networks.put(999, "Rennes");

    return networks;
}

From source file:com.svpino.longhorn.artifacts.StockTileProcessor.java

@TargetApi(11)
private static int createStandardRow(Fragment fragment, TableLayout tableLayout, List<Stock> stocks,
        SparseArray<View> tiles, int height, int margin, int index, int row) {

    Stock stock1 = stocks.get(index);/* w w  w . j  a  v a2  s .c o  m*/
    Stock stock2 = (index + 1 < stocks.size()) ? stocks.get(index + 1) : null;

    if (shouldUpdateTableRow(tableLayout, row, stock1, stock2)) {
        TableRow tableRow = new TableRow(fragment.getActivity());

        boolean shouldSpanFirstTile = row % 2 != 0;
        boolean shouldSpanSecondTile = !shouldSpanFirstTile;

        if (stock2 != null) {
            View tile1 = createTile(fragment, stock1, index, shouldSpanFirstTile);
            tiles.put(index, tile1);
            tableRow.addView(tile1, shouldSpanFirstTile ? getPartialSpannedLayoutParams(row, height, margin)
                    : getNotSpannedLayoutParams(row, height, margin));

            View tile2 = createTile(fragment, stock2, index + 1, shouldSpanSecondTile);
            tiles.put(index + 1, tile2);
            tableRow.addView(tile2,
                    shouldSpanSecondTile ? getLastPartialSpannedLayoutParams(row, height, margin)
                            : getLastNotSpannedLayoutParams(row, height, margin));
        } else {
            View tile1 = createTile(fragment, stock1, index, shouldSpanFirstTile);
            tiles.put(index, tile1);
            tableRow.addView(tile1, shouldSpanFirstTile ? getPartialSpannedLayoutParams(row, height, margin)
                    : getNotSpannedLayoutParams(row, height, margin));

            View tile2 = createTileForAddingNewStock(fragment);
            tiles.put(index + 1, tile2);
            tableRow.addView(tile2,
                    shouldSpanSecondTile ? getLastPartialSpannedLayoutParams(row, height, margin)
                            : getLastNotSpannedLayoutParams(row, height, margin));
        }

        if (row < tableLayout.getChildCount()) {
            tableLayout.removeViewAt(row);
        }

        tableLayout.addView(tableRow, row);
    }

    return index + 2;
}

From source file:org.akvo.caddisfly.test.helper.ResultsTest.java

@Test
public void testColorimetryResult() {

    TestInfo testInfo = TestConfigHelper.loadTestByUuid(SensorConstants.FLUORIDE_ID);
    assert testInfo != null;

    SparseArray<String> results = new SparseArray<>();
    results.put(1, "> 2.0");

    JSONObject resultJson = TestConfigHelper.getJsonResult(testInfo, results, -1, "", null);

    // Replace items that cannot be tested (e.g. currentTime)
    String json = resultJson.toString().replaceAll("(\"testDate\":\").*?\"", "$1today\"");
    json = json.replaceAll("(\"appVersion\":\").*?\"", "$1version\"");

    String expectedJson = "{\"type\":\"caddisfly\",\"name\":\"Water - Fluoride\",\"uuid\":\"f0f3c1dd-89af-49f1-83e7-bcc31c3006cf\",\"result\":[{\"name\":\"Fluoride\",\"unit\":\"ppm\",\"id\":1,\"value\":\"> 2.0\"}],\"testDate\":\"today\",\"user\":{\"backDropDetection\":true,\"language\":\"\"},\"app\":{\"appVersion\":\"version\",\"language\":\"en\"},\"device\":{\"model\":\"unknown\",\"product\":\"unknown\",\"manufacturer\":\"unknown\",\"os\":\"Android - 6.0.1_r3 (23)\",\"country\":\"US\",\"language\":\"en\"}}";

    assertEquals(expectedJson, json);//  w w w  .j  ava2  s  . c o  m
}

From source file:org.akvo.caddisfly.test.helper.ResultsTest.java

@Test
public void testEcSensorResult() {

    TestInfo testInfo = TestConfigHelper.loadTestByUuid("f88237b7-be3d-4fac-bbee-ab328eefcd14");
    assert testInfo != null;

    SparseArray<String> results = new SparseArray<>();
    results.put(1, "32432");
    results.put(2, "29.5");

    JSONObject resultJson = TestConfigHelper.getJsonResult(testInfo, results, -1, "", null);

    // Replace items that cannot be tested (e.g. currentTime)
    String json = resultJson.toString().replaceAll("(\"testDate\":\").*?\"", "$1today\"");
    json = json.replaceAll("(\"appVersion\":\").*?\"", "$1version\"");

    String expectedJson = "{\"type\":\"caddisfly\",\"name\":\"Water - Electrical Conductivity\",\"uuid\":\"f88237b7-be3d-4fac-bbee-ab328eefcd14\",\"result\":[{\"name\":\"Electrical Conductivity\",\"unit\":\"S\\/cm\",\"id\":1,\"value\":\"32432\"},{\"name\":\"Temperature\",\"unit\":\"Celsius\",\"id\":2,\"value\":\"29.5\"}],\"testDate\":\"today\",\"user\":{\"backDropDetection\":true,\"language\":\"\"},\"app\":{\"appVersion\":\"version\",\"language\":\"en\"},\"device\":{\"model\":\"unknown\",\"product\":\"unknown\",\"manufacturer\":\"unknown\",\"os\":\"Android - 6.0.1_r3 (23)\",\"country\":\"US\",\"language\":\"en\"}}";

    assertEquals(expectedJson, json);/*  ww w. j  a  v  a  2  s .  c o m*/
}

From source file:Main.java

/**
 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
 * @param bitmap The bitmap to scan/*from w w w .  j  a  v a 2 s.  c o m*/
 * @param samples The approximate max number of samples to use.
 */
static int findDominantColorByHue(Bitmap bitmap, int samples) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();
    int sampleStride = (int) Math.sqrt((height * width) / samples);
    if (sampleStride < 1) {
        sampleStride = 1;
    }

    // This is an out-param, for getting the hsv values for an rgb
    float[] hsv = new float[3];

    // First get the best hue, by creating a histogram over 360 hue buckets,
    // where each pixel contributes a score weighted by saturation, value, and alpha.
    float[] hueScoreHistogram = new float[360];
    float highScore = -1;
    int bestHue = -1;

    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int argb = bitmap.getPixel(x, y);
            int alpha = 0xFF & (argb >> 24);
            if (alpha < 0x80) {
                // Drop mostly-transparent pixels.
                continue;
            }
            // Remove the alpha channel.
            int rgb = argb | 0xFF000000;
            Color.colorToHSV(rgb, hsv);
            // Bucket colors by the 360 integer hues.
            int hue = (int) hsv[0];
            if (hue < 0 || hue >= hueScoreHistogram.length) {
                // Defensively avoid array bounds violations.
                continue;
            }
            float score = hsv[1] * hsv[2];
            hueScoreHistogram[hue] += score;
            if (hueScoreHistogram[hue] > highScore) {
                highScore = hueScoreHistogram[hue];
                bestHue = hue;
            }
        }
    }

    SparseArray<Float> rgbScores = new SparseArray<Float>();
    int bestColor = 0xff000000;
    highScore = -1;
    // Go back over the RGB colors that match the winning hue,
    // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
    // The highest-scoring RGB color wins.
    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int rgb = bitmap.getPixel(x, y) | 0xff000000;
            Color.colorToHSV(rgb, hsv);
            int hue = (int) hsv[0];
            if (hue == bestHue) {
                float s = hsv[1];
                float v = hsv[2];
                int bucket = (int) (s * 100) + (int) (v * 10000);
                // Score by cumulative saturation * value.
                float score = s * v;
                Float oldTotal = rgbScores.get(bucket);
                float newTotal = oldTotal == null ? score : oldTotal + score;
                rgbScores.put(bucket, newTotal);
                if (newTotal > highScore) {
                    highScore = newTotal;
                    // All the colors in the winning bucket are very similar. Last in wins.
                    bestColor = rgb;
                }
            }
        }
    }
    return bestColor;
}