billthefarmer / gurgle

Fairly simple android word game
https://billthefarmer.github.io/gurgle/
GNU General Public License v3.0
54 stars 18 forks source link

Compatibility issue when calling method #52

Closed PSDroid2022 closed 2 years ago

PSDroid2022 commented 2 years ago

We confirm a compatibility issue which might threaten the robustness of your app and give a detailed suggestion for you.

In ''org.billthefarmer.gurgle.Gurgle", you invoke the framework API "<android.os.BaseBundle: int getInt(java.lang.String)>" in "onRestoreInstanceState" method as shown in following. But actually, this method is introduced after API level 21 (https://developer.android.google.cn/reference/kotlin/android/os/BaseBundle?hl=en#getint_1).

   @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);

        row = savedInstanceState.getInt(ROW);
        word = savedInstanceState.getString(WORD);
        letter = savedInstanceState.getInt(LETTER);
        solved = savedInstanceState.getBoolean(SOLVED);

        List<String> letters =
            savedInstanceState.getStringArrayList(LETTERS);
        List<Integer> colours =
            savedInstanceState.getIntegerArrayList(COLOURS);

        for (int i = 0; i < letters.size(); i++)
        {
            TextView text = display[i / SIZE][i % SIZE];
            text.setText(letters.get(i));
            text.setTextColor(colours.get(i));
        }

        List<String> keys =
            savedInstanceState.getStringArrayList(KEYS);
        List<Integer> keyColours =
            savedInstanceState.getIntegerArrayList(KEY_COLOURS);

        for (int i = 0; i < keys.size(); i++)
        {
            TextView key = keyboard.get(keys.get(i));
            key.setTextColor(keyColours.get(i));
        }
    }

So when the app try to invoke this API on devices before API level 21, your app will run with an unpredictable results. So we suggest you add an "if(SDK_INT >= 21)" to fix this potential issue.

Android device

install app on devices with API 19, open Gurgle with crash. image

billthefarmer commented 2 years ago

This is clearly nonsense. The current docs show that android.os.Bundle (API level 1) inherits from android.os.BaseBundle (API level 21), which cannot be true for API levels less than 21. The answer is that Bundle contains getInt() and putint() in API levels less than 21, along with several other methods which the current docs say are in BaseBundle. Here are the sources for getInt() in Bundle.java for API level 19. If this were not so, the android build tools would pick it up.


    /**
     * Returns the value associated with the given key, or 0 if
     * no mapping of the desired type exists for the given key.
     *
     * @param key a String
     * @return an int value
     */
    public int getInt(String key) {
        unparcel();
        return getInt(key, 0);
    }

    /**
     * Returns the value associated with the given key, or defaultValue if
     * no mapping of the desired type exists for the given key.
     *
     * @param key a String
     * @param defaultValue Value to return if key does not exist
     * @return an int value
     */
    public int getInt(String key, int defaultValue) {
        unparcel();
        Object o = mMap.get(key);
        if (o == null) {
            return defaultValue;
        }
        try {
            return (Integer) o;
        } catch (ClassCastException e) {
            typeWarning(key, o, "Integer", defaultValue, e);
            return defaultValue;
        }
    }
PSDroid2022 commented 2 years ago

Thanks, it seems a false positive!