Example usage for android.os Bundle getShortArray

List of usage examples for android.os Bundle getShortArray

Introduction

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

Prototype

@Override
@Nullable
public short[] getShortArray(@Nullable String key) 

Source Link

Document

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Usage

From source file:com.facebook.LegacyTokenCacheTest.java

@Test
public void testAllTypes() {
    Bundle originalBundle = new Bundle();

    putBoolean(BOOLEAN_KEY, originalBundle);
    putBooleanArray(BOOLEAN_ARRAY_KEY, originalBundle);
    putByte(BYTE_KEY, originalBundle);//from  w  w  w  .j a va  2s.c o  m
    putByteArray(BYTE_ARRAY_KEY, originalBundle);
    putShort(SHORT_KEY, originalBundle);
    putShortArray(SHORT_ARRAY_KEY, originalBundle);
    putInt(INT_KEY, originalBundle);
    putIntArray(INT_ARRAY_KEY, originalBundle);
    putLong(LONG_KEY, originalBundle);
    putLongArray(LONG_ARRAY_KEY, originalBundle);
    putFloat(FLOAT_KEY, originalBundle);
    putFloatArray(FLOAT_ARRAY_KEY, originalBundle);
    putDouble(DOUBLE_KEY, originalBundle);
    putDoubleArray(DOUBLE_ARRAY_KEY, originalBundle);
    putChar(CHAR_KEY, originalBundle);
    putCharArray(CHAR_ARRAY_KEY, originalBundle);
    putString(STRING_KEY, originalBundle);
    putStringList(STRING_LIST_KEY, originalBundle);
    originalBundle.putSerializable(SERIALIZABLE_KEY, AccessTokenSource.FACEBOOK_APPLICATION_WEB);

    ensureApplicationContext();

    LegacyTokenHelper cache = new LegacyTokenHelper(RuntimeEnvironment.application);
    cache.save(originalBundle);

    LegacyTokenHelper cache2 = new LegacyTokenHelper(RuntimeEnvironment.application);
    Bundle cachedBundle = cache2.load();

    assertEquals(originalBundle.getBoolean(BOOLEAN_KEY), cachedBundle.getBoolean(BOOLEAN_KEY));
    assertArrayEquals(originalBundle.getBooleanArray(BOOLEAN_ARRAY_KEY),
            cachedBundle.getBooleanArray(BOOLEAN_ARRAY_KEY));
    assertEquals(originalBundle.getByte(BYTE_KEY), cachedBundle.getByte(BYTE_KEY));
    assertArrayEquals(originalBundle.getByteArray(BYTE_ARRAY_KEY), cachedBundle.getByteArray(BYTE_ARRAY_KEY));
    assertEquals(originalBundle.getShort(SHORT_KEY), cachedBundle.getShort(SHORT_KEY));
    assertArrayEquals(originalBundle.getShortArray(SHORT_ARRAY_KEY),
            cachedBundle.getShortArray(SHORT_ARRAY_KEY));
    assertEquals(originalBundle.getInt(INT_KEY), cachedBundle.getInt(INT_KEY));
    assertArrayEquals(originalBundle.getIntArray(INT_ARRAY_KEY), cachedBundle.getIntArray(INT_ARRAY_KEY));
    assertEquals(originalBundle.getLong(LONG_KEY), cachedBundle.getLong(LONG_KEY));
    assertArrayEquals(originalBundle.getLongArray(LONG_ARRAY_KEY), cachedBundle.getLongArray(LONG_ARRAY_KEY));
    assertEquals(originalBundle.getFloat(FLOAT_KEY), cachedBundle.getFloat(FLOAT_KEY),
            TestUtils.DOUBLE_EQUALS_DELTA);
    assertArrayEquals(originalBundle.getFloatArray(FLOAT_ARRAY_KEY),
            cachedBundle.getFloatArray(FLOAT_ARRAY_KEY));
    assertEquals(originalBundle.getDouble(DOUBLE_KEY), cachedBundle.getDouble(DOUBLE_KEY),
            TestUtils.DOUBLE_EQUALS_DELTA);
    assertArrayEquals(originalBundle.getDoubleArray(DOUBLE_ARRAY_KEY),
            cachedBundle.getDoubleArray(DOUBLE_ARRAY_KEY));
    assertEquals(originalBundle.getChar(CHAR_KEY), cachedBundle.getChar(CHAR_KEY));
    assertArrayEquals(originalBundle.getCharArray(CHAR_ARRAY_KEY), cachedBundle.getCharArray(CHAR_ARRAY_KEY));
    assertEquals(originalBundle.getString(STRING_KEY), cachedBundle.getString(STRING_KEY));
    assertListEquals(originalBundle.getStringArrayList(STRING_LIST_KEY),
            cachedBundle.getStringArrayList(STRING_LIST_KEY));
    assertEquals(originalBundle.getSerializable(SERIALIZABLE_KEY),
            cachedBundle.getSerializable(SERIALIZABLE_KEY));
}

From source file:com.fenlisproject.elf.core.framework.ElfBinder.java

public static void bindFragmentArgument(Fragment receiver) {
    Field[] fields = receiver.getClass().getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);/*  ww w.  j  a va2 s .c o  m*/
        FragmentArgument fragmentArgument = field.getAnnotation(FragmentArgument.class);
        if (fragmentArgument != null) {
            try {
                Bundle bundle = receiver.getArguments();
                if (bundle != null) {
                    Class<?> type = field.getType();
                    if (type == Boolean.class || type == boolean.class) {
                        field.set(receiver, bundle.getBoolean(fragmentArgument.value()));
                    } else if (type == Byte.class || type == byte.class) {
                        field.set(receiver, bundle.getByte(fragmentArgument.value()));
                    } else if (type == Character.class || type == char.class) {
                        field.set(receiver, bundle.getChar(fragmentArgument.value()));
                    } else if (type == Double.class || type == double.class) {
                        field.set(receiver, bundle.getDouble(fragmentArgument.value()));
                    } else if (type == Float.class || type == float.class) {
                        field.set(receiver, bundle.getFloat(fragmentArgument.value()));
                    } else if (type == Integer.class || type == int.class) {
                        field.set(receiver, bundle.getInt(fragmentArgument.value()));
                    } else if (type == Long.class || type == long.class) {
                        field.set(receiver, bundle.getLong(fragmentArgument.value()));
                    } else if (type == Short.class || type == short.class) {
                        field.set(receiver, bundle.getShort(fragmentArgument.value()));
                    } else if (type == String.class) {
                        field.set(receiver, bundle.getString(fragmentArgument.value()));
                    } else if (type == Boolean[].class || type == boolean[].class) {
                        field.set(receiver, bundle.getBooleanArray(fragmentArgument.value()));
                    } else if (type == Byte[].class || type == byte[].class) {
                        field.set(receiver, bundle.getByteArray(fragmentArgument.value()));
                    } else if (type == Character[].class || type == char[].class) {
                        field.set(receiver, bundle.getCharArray(fragmentArgument.value()));
                    } else if (type == Double[].class || type == double[].class) {
                        field.set(receiver, bundle.getDoubleArray(fragmentArgument.value()));
                    } else if (type == Float[].class || type == float[].class) {
                        field.set(receiver, bundle.getFloatArray(fragmentArgument.value()));
                    } else if (type == Integer[].class || type == int[].class) {
                        field.set(receiver, bundle.getIntArray(fragmentArgument.value()));
                    } else if (type == Long[].class || type == long[].class) {
                        field.set(receiver, bundle.getLongArray(fragmentArgument.value()));
                    } else if (type == Short[].class || type == short[].class) {
                        field.set(receiver, bundle.getShortArray(fragmentArgument.value()));
                    } else if (type == String[].class) {
                        field.set(receiver, bundle.getStringArray(fragmentArgument.value()));
                    } else if (Serializable.class.isAssignableFrom(type)) {
                        field.set(receiver, bundle.getSerializable(fragmentArgument.value()));
                    } else if (type == Bundle.class) {
                        field.set(receiver, bundle.getBundle(fragmentArgument.value()));
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

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

/**
 * Initializes this activity/*  w w w .j a  v a  2 s . c om*/
 * @param savedInstanceState if this parameter is not {@code null} the activity state is restored to the information
 *                           in this Bundle
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(myToolbar);
    // More info: http://codetheory.in/difference-between-setdisplayhomeasupenabled-sethomebuttonenabled-and-setdisplayshowhomeenabled/
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    newWorldView = findViewById(R.id.new_world_fragment_container);
    fpsOkColor = ContextCompat.getColor(this, R.color.fps_ok_color);
    fpsWarningColor = ContextCompat.getColor(this, R.color.fps_warning_color);
    desiredFpsSlider = (RangeSlider) findViewById(R.id.desired_fps);
    threadsSlider = (RangeSlider) findViewById(R.id.threads);
    desiredFpsSlider.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
            v.onTouchEvent(event);
            return true;
        }
    });
    desiredFpsSlider.setOnValueChangeListener(new RangeSlider.OnValueChangeListener() {
        @Override
        public void onValueChange(RangeSlider slider, int oldVal, int newVal, boolean fromUser) {
            synchronized (MainActivity.this) {
                if (newVal == 0) {
                    if (currentDrawFps != null) {
                        currentDrawFps.setVisibility(View.INVISIBLE);
                    }
                } else {
                    if (currentDrawFps != null) {
                        currentDrawFps.setVisibility(View.VISIBLE);
                    }
                }
                if (fromUser) {
                    if (simulatorRunnable.setTargetFps(newVal)) {
                        startSimulatorThread();
                    }
                }
            }
        }
    });
    threadsSlider.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
            v.onTouchEvent(event);
            return true;
        }
    });
    threadsSlider.setOnValueChangeListener(new RangeSlider.OnValueChangeListener() {
        @Override
        public void onValueChange(RangeSlider slider, int oldVal, int newVal, boolean fromUser) {
            simulatorRunnable.setThreadCount(newVal);
        }
    });

    handler = new Handler();
    updateFpsRunnable = new Runnable() {
        @Override
        public void run() {
            synchronized (MainActivity.this) {
                if (currentSimFps != null) {
                    if (simulatorRunnable.getTargetFps() == 0) {
                        currentSimFps.setText(getString(R.string.paused));
                        currentSimFps.setTextColor(fpsOkColor);
                    } else {
                        int fps = (int) simulatorRunnable.getAvgFps();
                        currentSimFps.setText(getString(R.string.current_simulation_fps, fps));
                        int newTextColor = fps < simulatorRunnable.getTargetFps() ? fpsWarningColor
                                : fpsOkColor;
                        currentSimFps.setTextColor(newTextColor);
                    }
                }
                if (currentDrawFps != null) {
                    float fps = drawingAverageTime.getAverage();
                    if (fps != 0f) {
                        fps = 1000 / fps;
                    }
                    currentDrawFps.setText(getString(R.string.current_drawing_fps, (int) fps));
                    int newTextColor = fps < simulatorRunnable.getTargetFps() ? fpsWarningColor : fpsOkColor;
                    currentDrawFps.setTextColor(newTextColor);
                }
            }
        }
    };

    if (savedInstanceState == null) {
        simulator = new Simulator(new WorldParameters());
    } else {
        WorldParameters parameters = new WorldParameters()
                .setWidth(savedInstanceState.getShort(WorldKeys.WORLD_WIDTH_KEY))
                .setHeight(savedInstanceState.getShort(WorldKeys.WORLD_HEIGHT_KEY))
                .setFishBreedTime(savedInstanceState.getShort(WorldKeys.FISH_BREED_TIME_KEY))
                .setSharkBreedTime(savedInstanceState.getShort(WorldKeys.SHARK_BREED_TIME_KEY))
                .setSharkStarveTime(savedInstanceState.getShort(WorldKeys.SHARK_STARVE_TIME_KEY))
                .setInitialFishCount(0).setInitialSharkCount(0);
        simulator = new Simulator(parameters);
        short[] fishAge = savedInstanceState.getShortArray(WorldKeys.FISH_AGE_KEY);
        if (fishAge != null) {
            short[] fishPosX = savedInstanceState.getShortArray(WorldKeys.FISH_POSITIONS_X_KEY);
            if (fishPosX != null) {
                short[] fishPosY = savedInstanceState.getShortArray(WorldKeys.FISH_POSITIONS_Y_KEY);
                if (fishPosY != null) {
                    for (int fishNo = 0; fishNo < fishAge.length; fishNo++) {
                        simulator.setFish(fishPosX[fishNo], fishPosY[fishNo], fishAge[fishNo]);
                    }
                }
            }
        }
        short[] sharkAge = savedInstanceState.getShortArray(WorldKeys.SHARK_AGE_KEY);
        if (sharkAge != null) {
            short[] sharkHunger = savedInstanceState.getShortArray(WorldKeys.SHARK_HUNGER_KEY);
            if (sharkHunger != null) {
                short[] sharkPosX = savedInstanceState.getShortArray(WorldKeys.SHARK_POSITIONS_X_KEY);
                if (sharkPosX != null) {
                    short[] sharkPosY = savedInstanceState.getShortArray(WorldKeys.SHARK_POSITIONS_Y_KEY);
                    if (sharkPosY != null) {
                        for (int sharkNo = 0; sharkNo < sharkAge.length; sharkNo++) {
                            simulator.setShark(sharkPosX[sharkNo], sharkPosY[sharkNo], sharkAge[sharkNo],
                                    sharkHunger[sharkNo]);
                        }
                    }
                }
            }
        }

        if (savedInstanceState.containsKey(WorldKeys.INITIAL_FISH_COUNT_KEY)
                || savedInstanceState.containsKey(WorldKeys.INITIAL_SHARK_COUNT_KEY)) {
            if (savedInstanceState.containsKey(WorldKeys.INITIAL_FISH_COUNT_KEY)) {
                parameters.setInitialFishCount(savedInstanceState.getInt(WorldKeys.INITIAL_FISH_COUNT_KEY));
            }
            if (savedInstanceState.containsKey(WorldKeys.INITIAL_SHARK_COUNT_KEY)) {
                parameters.setInitialSharkCount(savedInstanceState.getInt(WorldKeys.INITIAL_SHARK_COUNT_KEY));
            }
            previousWorldParameters = parameters;
        }

    }

    simulatorRunnable = new SimulatorRunnable(simulator);
    if (savedInstanceState != null && savedInstanceState.containsKey(WorldKeys.TARGET_FPS_KEY)) {
        simulatorRunnable.setTargetFps(savedInstanceState.getInt(WorldKeys.TARGET_FPS_KEY));
    }
    simulatorRunnable.registerSimulatorRunnableObserver(this);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    final ListView drawerList = (ListView) findViewById(R.id.drawer_commands);
    drawerList.setAdapter(new DrawerListAdapter(getDrawerCommands()));
    drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            DrawerCommandItem command = (DrawerCommandItem) drawerList.getItemAtPosition(position);
            if (command != null) {
                command.execute();
            }
        }
    });
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer_description,
            R.string.close_drawer_description) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            supportInvalidateOptionsMenu();
            synchronized (MainActivity.this) {
                currentSimFps = (TextView) findViewById(R.id.fps_simulator);
                currentDrawFps = (TextView) findViewById(R.id.fps_drawing);
            }
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            supportInvalidateOptionsMenu();
            synchronized (MainActivity.this) {
                currentSimFps = null;
                currentDrawFps = null;
            }
        }
    };
    drawerLayout.addDrawerListener(drawerToggle);
}