Example usage for android.os Bundle putShortArray

List of usage examples for android.os Bundle putShortArray

Introduction

In this page you can find the example usage for android.os Bundle putShortArray.

Prototype

@Override
public void putShortArray(@Nullable String key, @Nullable short[] value) 

Source Link

Document

Inserts a short array value into the mapping of this Bundle, replacing any existing value for the given key.

Usage

From source file:com.facebook.LegacyTokenCacheTest.java

private static void putShortArray(String key, Bundle bundle) {
    int length = random.nextInt(50);
    short[] array = new short[length];
    for (int i = 0; i < length; i++) {
        array[i] = (short) random.nextInt();
    }/*from  ww w .  j a va 2 s  .  com*/
    bundle.putShortArray(key, array);
}

From source file:com.facebook.RequestTests.java

@LargeTest
public void testCantUseComplexParameterInGetRequest() {
    Bundle parameters = new Bundle();
    parameters.putShortArray("foo", new short[1]);

    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me", parameters,
            HttpMethod.GET, new ExpectFailureCallback());
    GraphResponse response = request.executeAndWait();

    FacebookRequestError error = response.getError();
    assertNotNull(error);/*  w ww. j av a  2s.co m*/
    FacebookException exception = error.getException();
    assertNotNull(exception);
    assertTrue(exception.getMessage().contains("short[]"));
}

From source file:me.xiaopan.android.inject.sample.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = new ListView(getBaseContext());
    setContentView(listView);/*ww w.j a v a2s.c  o m*/

    // SharedPreferences?
    PreferenceUtils.putBoolean(getBaseContext(), KEY_BOOLEAN, true);
    PreferenceUtils.putFloat(getBaseContext(), KEY_FLOAT, 10000f);
    PreferenceUtils.putInt(getBaseContext(), KEY_INT, 2000);
    PreferenceUtils.putLong(getBaseContext(), KEY_LONG, 50000);
    PreferenceUtils.putString(getBaseContext(), KEY_STRING, "Preference String");
    Set<String> stringSet = new HashSet<String>();
    stringSet.add("String Set 1");
    stringSet.add("String Set 2");
    stringSet.add("String Set 3");
    stringSet.add("String Set 4");
    PreferenceUtils.putStringSet(getBaseContext(), KEY_STRING_SET, stringSet);
    MyBean bean2 = new MyBean();
    bean2.setEmail("sky@xiaopan.me2");
    bean2.setName("?2");
    bean2.setSex("2");
    PreferenceUtils.putObject(getBaseContext(), KEY_JSON, bean2);
    PreferenceUtils.putString(getBaseContext(), KEY_ENUM, Sex.WOMAN.name());

    // ??
    String[] items = new String[] { "", "?", "FragmentDialog",
            "InjectAdapter", "InjectExpandableListAdapter" };
    listView.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,
            android.R.id.text1, items));
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position <= 2) {
                Bundle bundle = new Bundle();
                bundle.putBoolean(MainActivity.PARAM_BOOLEAN, true);
                bundle.putBooleanArray(MainActivity.PARAM_BOOLEAN_ARRAY, new boolean[] { true, false, true });
                bundle.putByte(MainActivity.PARAM_BYTE, (byte) 110);
                bundle.putByteArray(MainActivity.PARAM_BYTE_ARRAY, new byte[] { 111, 112, 113 });
                bundle.putChar(MainActivity.PARAM_CHAR, 'R');
                bundle.putCharArray(MainActivity.PARAM_CHAR_ARRAY, new char[] { 'c', 'h', 'a', 'r' });
                bundle.putCharSequence(MainActivity.PARAM_CHAR_SEQUENCE, "CharSequence");
                bundle.putCharSequenceArray(MainActivity.PARAM_CHAR_SEQUENCE_ARRAY,
                        new CharSequence[] { "Char", " ", "Sequence" });
                bundle.putDouble(MainActivity.PARAM_DOUBLE, 12.00d);
                bundle.putDoubleArray(MainActivity.PARAM_DOUBLE_ARRAY, new double[] { 12.01d, 12.02d, 12.03d });
                bundle.putFloat(MainActivity.PARAM_FLOAT, 13.00f);
                bundle.putFloatArray(MainActivity.PARAM_FLOAT_ARRAY, new float[] { 13.01f, 13.02f, 13.03f });
                bundle.putInt(MainActivity.PARAM_INT, 120);
                bundle.putIntArray(MainActivity.PARAM_INT_ARRAY, new int[] { 121, 122, 123, });
                bundle.putLong(MainActivity.PARAM_LONG, 12345);
                bundle.putLongArray(MainActivity.PARAM_LONG_ARRAY, new long[] { 12346, 12347, 12348 });
                bundle.putShort(MainActivity.PARAM_SHORT, (short) 2);
                bundle.putShortArray(MainActivity.PARAM_SHORT_ARRAY, new short[] { 3, 4, 5 });
                bundle.putString(MainActivity.PARAM_STRING, "String");
                bundle.putStringArray(MainActivity.PARAM_STRING_ARRAY,
                        new String[] { "String1", "String2", "String3" });

                // ??JSONBundle
                MyBean bean = new MyBean();
                bean.setEmail("sky@xiaopan.me");
                bean.setName("?");
                bean.setSex("");
                bundle.putString(PARAM_STRING_JSON, new Gson().toJson(bean));

                bundle.putString(MainActivity.PARAM_STRING_ENUM, Sex.WOMAN.name());

                // 
                ArrayList<String> stringList = new ArrayList<String>();
                stringList.add("ArrayList String 1");
                stringList.add("ArrayList String 2");
                stringList.add("ArrayList String 3");
                bundle.putStringArrayList(MainActivity.PARAM_STRING_ARRAY_LIST, stringList);
                switch (position) {
                case 0:
                    Second.SECOND_CHRONOGRAPH.lap();
                    Intent intent = new Intent(getBaseContext(), InjectTestActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 1:
                    Second.SECOND_CHRONOGRAPH.lap();
                    Intent intent2 = new Intent(getBaseContext(), NormalActivity.class);
                    intent2.putExtras(bundle);
                    startActivity(intent2);
                    break;
                case 2:
                    Second.SECOND_CHRONOGRAPH.lap();
                    new TestDialogFragment().show(getSupportFragmentManager(), "");
                    break;
                }
            } else {
                Class<?> targetClass = null;
                if (position == 3) {
                    targetClass = InjectAdapterActivity.class;
                } else if (position == 4) {
                    targetClass = InjectExpandableListAdapterActivity.class;
                }
                if (targetClass != null) {
                    startActivity(new Intent(getBaseContext(), targetClass));
                }
            }
        }
    });
}

From source file:com.facebook.SharedPreferencesTokenCache.java

private void deserializeKey(String key, Bundle bundle) throws JSONException {
    String jsonString = cache.getString(key, "{}");
    JSONObject json = new JSONObject(jsonString);

    String valueType = json.getString(JSON_VALUE_TYPE);

    if (valueType.equals(TYPE_BOOLEAN)) {
        bundle.putBoolean(key, json.getBoolean(JSON_VALUE));
    } else if (valueType.equals(TYPE_BOOLEAN_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        boolean[] array = new boolean[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getBoolean(i);
        }/* ww  w. j  a v  a 2 s . com*/
        bundle.putBooleanArray(key, array);
    } else if (valueType.equals(TYPE_BYTE)) {
        bundle.putByte(key, (byte) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_BYTE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        byte[] array = new byte[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (byte) jsonArray.getInt(i);
        }
        bundle.putByteArray(key, array);
    } else if (valueType.equals(TYPE_SHORT)) {
        bundle.putShort(key, (short) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_SHORT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        short[] array = new short[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (short) jsonArray.getInt(i);
        }
        bundle.putShortArray(key, array);
    } else if (valueType.equals(TYPE_INTEGER)) {
        bundle.putInt(key, json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_INTEGER_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int[] array = new int[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getInt(i);
        }
        bundle.putIntArray(key, array);
    } else if (valueType.equals(TYPE_LONG)) {
        bundle.putLong(key, json.getLong(JSON_VALUE));
    } else if (valueType.equals(TYPE_LONG_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        long[] array = new long[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getLong(i);
        }
        bundle.putLongArray(key, array);
    } else if (valueType.equals(TYPE_FLOAT)) {
        bundle.putFloat(key, (float) json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_FLOAT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        float[] array = new float[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (float) jsonArray.getDouble(i);
        }
        bundle.putFloatArray(key, array);
    } else if (valueType.equals(TYPE_DOUBLE)) {
        bundle.putDouble(key, json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_DOUBLE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        double[] array = new double[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getDouble(i);
        }
        bundle.putDoubleArray(key, array);
    } else if (valueType.equals(TYPE_CHAR)) {
        String charString = json.getString(JSON_VALUE);
        if (charString != null && charString.length() == 1) {
            bundle.putChar(key, charString.charAt(0));
        }
    } else if (valueType.equals(TYPE_CHAR_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        char[] array = new char[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            String charString = jsonArray.getString(i);
            if (charString != null && charString.length() == 1) {
                array[i] = charString.charAt(0);
            }
        }
        bundle.putCharArray(key, array);
    } else if (valueType.equals(TYPE_STRING)) {
        bundle.putString(key, json.getString(JSON_VALUE));
    } else if (valueType.equals(TYPE_STRING_LIST)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int numStrings = jsonArray.length();
        ArrayList<String> stringList = new ArrayList<String>(numStrings);
        for (int i = 0; i < numStrings; i++) {
            Object jsonStringValue = jsonArray.get(i);
            stringList.add(i, jsonStringValue == JSONObject.NULL ? null : (String) jsonStringValue);
        }
        bundle.putStringArrayList(key, stringList);
    }
}

From source file:com.facebook.LegacyTokenHelper.java

private void deserializeKey(String key, Bundle bundle) throws JSONException {
    String jsonString = cache.getString(key, "{}");
    JSONObject json = new JSONObject(jsonString);

    String valueType = json.getString(JSON_VALUE_TYPE);

    if (valueType.equals(TYPE_BOOLEAN)) {
        bundle.putBoolean(key, json.getBoolean(JSON_VALUE));
    } else if (valueType.equals(TYPE_BOOLEAN_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        boolean[] array = new boolean[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getBoolean(i);
        }//from  w ww .  ja  v a  2 s . com
        bundle.putBooleanArray(key, array);
    } else if (valueType.equals(TYPE_BYTE)) {
        bundle.putByte(key, (byte) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_BYTE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        byte[] array = new byte[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (byte) jsonArray.getInt(i);
        }
        bundle.putByteArray(key, array);
    } else if (valueType.equals(TYPE_SHORT)) {
        bundle.putShort(key, (short) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_SHORT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        short[] array = new short[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (short) jsonArray.getInt(i);
        }
        bundle.putShortArray(key, array);
    } else if (valueType.equals(TYPE_INTEGER)) {
        bundle.putInt(key, json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_INTEGER_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int[] array = new int[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getInt(i);
        }
        bundle.putIntArray(key, array);
    } else if (valueType.equals(TYPE_LONG)) {
        bundle.putLong(key, json.getLong(JSON_VALUE));
    } else if (valueType.equals(TYPE_LONG_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        long[] array = new long[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getLong(i);
        }
        bundle.putLongArray(key, array);
    } else if (valueType.equals(TYPE_FLOAT)) {
        bundle.putFloat(key, (float) json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_FLOAT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        float[] array = new float[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (float) jsonArray.getDouble(i);
        }
        bundle.putFloatArray(key, array);
    } else if (valueType.equals(TYPE_DOUBLE)) {
        bundle.putDouble(key, json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_DOUBLE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        double[] array = new double[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getDouble(i);
        }
        bundle.putDoubleArray(key, array);
    } else if (valueType.equals(TYPE_CHAR)) {
        String charString = json.getString(JSON_VALUE);
        if (charString != null && charString.length() == 1) {
            bundle.putChar(key, charString.charAt(0));
        }
    } else if (valueType.equals(TYPE_CHAR_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        char[] array = new char[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            String charString = jsonArray.getString(i);
            if (charString != null && charString.length() == 1) {
                array[i] = charString.charAt(0);
            }
        }
        bundle.putCharArray(key, array);
    } else if (valueType.equals(TYPE_STRING)) {
        bundle.putString(key, json.getString(JSON_VALUE));
    } else if (valueType.equals(TYPE_STRING_LIST)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int numStrings = jsonArray.length();
        ArrayList<String> stringList = new ArrayList<String>(numStrings);
        for (int i = 0; i < numStrings; i++) {
            Object jsonStringValue = jsonArray.get(i);
            stringList.add(i, jsonStringValue == JSONObject.NULL ? null : (String) jsonStringValue);
        }
        bundle.putStringArrayList(key, stringList);
    } else if (valueType.equals(TYPE_ENUM)) {
        try {
            String enumType = json.getString(JSON_VALUE_ENUM_TYPE);
            @SuppressWarnings({ "unchecked", "rawtypes" })
            Class<? extends Enum> enumClass = (Class<? extends Enum>) Class.forName(enumType);
            @SuppressWarnings("unchecked")
            Enum<?> enumValue = Enum.valueOf(enumClass, json.getString(JSON_VALUE));
            bundle.putSerializable(key, enumValue);
        } catch (ClassNotFoundException e) {
        } catch (IllegalArgumentException e) {
        }
    }
}

From source file:com.dirkgassen.wator.ui.activity.MainActivity.java

/**
 * Saves the state of this instance://w  w w .ja v a 2 s . c  o m
 * <ul>
 *     <li>current state of the simulator: fish positions and age, shark positions and age and hunger</li>
 *     <li>world parameters</li>
 *     <li>desired fps</li>
 * </ul>
 * @param outState Bundle to save the state to
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    Simulator.WorldInspector world = simulator.getWorldToPaint();
    try {
        final int fishCount = world.getFishCount();
        final int sharkCount = world.getSharkCount();
        final short[] fishAge;
        final short[] fishPosX;
        final short[] fishPosY;
        if (fishCount == 0) {
            fishAge = fishPosX = fishPosY = null;
        } else {
            fishAge = new short[fishCount];
            fishPosX = new short[fishCount];
            fishPosY = new short[fishCount];
        }
        final short[] sharkAge;
        final short[] sharkHunger;
        final short[] sharkPosX;
        final short[] sharkPosY;
        if (sharkCount == 0) {
            sharkAge = sharkHunger = sharkPosX = sharkPosY = null;
        } else {
            sharkAge = new short[sharkCount];
            sharkHunger = new short[sharkCount];
            sharkPosX = new short[sharkCount];
            sharkPosY = new short[sharkCount];
        }

        int fishNo = 0;
        int sharkNo = 0;
        do {
            if (world.isFish()) {
                //noinspection ConstantConditions
                fishAge[fishNo] = world.getFishAge();
                //noinspection ConstantConditions
                fishPosX[fishNo] = world.getCurrentX();
                //noinspection ConstantConditions
                fishPosY[fishNo++] = world.getCurrentY();
            } else if (world.isShark()) {
                //noinspection ConstantConditions
                sharkAge[sharkNo] = world.getSharkAge();
                //noinspection ConstantConditions
                sharkHunger[sharkNo] = world.getSharkHunger();
                //noinspection ConstantConditions
                sharkPosX[sharkNo] = world.getCurrentX();
                //noinspection ConstantConditions
                sharkPosY[sharkNo++] = world.getCurrentY();
            }
        } while (world.moveToNext() != Simulator.WorldInspector.RESET);
        if (fishCount > 0) {
            outState.putShortArray(WorldKeys.FISH_AGE_KEY, fishAge);
            outState.putShortArray(WorldKeys.FISH_POSITIONS_X_KEY, fishPosX);
            outState.putShortArray(WorldKeys.FISH_POSITIONS_Y_KEY, fishPosY);
        }
        if (sharkCount > 0) {
            outState.putShortArray(WorldKeys.SHARK_AGE_KEY, sharkAge);
            outState.putShortArray(WorldKeys.SHARK_HUNGER_KEY, sharkHunger);
            outState.putShortArray(WorldKeys.SHARK_POSITIONS_X_KEY, sharkPosX);
            outState.putShortArray(WorldKeys.SHARK_POSITIONS_Y_KEY, sharkPosY);
        }
        outState.putShort(WorldKeys.WORLD_WIDTH_KEY, world.getWorldWidth());
        outState.putShort(WorldKeys.WORLD_HEIGHT_KEY, world.getWorldHeight());
        outState.putShort(WorldKeys.FISH_BREED_TIME_KEY, world.getFishBreedTime());
        outState.putShort(WorldKeys.SHARK_BREED_TIME_KEY, world.getSharkBreedTime());
        outState.putShort(WorldKeys.SHARK_STARVE_TIME_KEY, world.getSharkStarveTime());

        if (previousWorldParameters == null) {
            previousWorldParameters = new WorldParameters();
        }
        outState.putInt(WorldKeys.INITIAL_FISH_COUNT_KEY, previousWorldParameters.getInitialFishCount());
        outState.putInt(WorldKeys.INITIAL_SHARK_COUNT_KEY, previousWorldParameters.getInitialSharkCount());

        if (simulatorRunnable != null) {
            outState.putInt(WorldKeys.TARGET_FPS_KEY, simulatorRunnable.getTargetFps());
        }
    } finally {
        world.release();
    }
    super.onSaveInstanceState(outState);
}