Example usage for android.os Bundle getShort

List of usage examples for android.os Bundle getShort

Introduction

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

Prototype

@Override
public short getShort(String key) 

Source Link

Document

Returns the value associated with the given key, or (short) 0 if no mapping of the desired type exists for the given 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  v  a  2 s  .  c  om
    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.dirkgassen.wator.ui.activity.MainActivity.java

/**
 * Initializes this activity//from  ww w  .  j  a  v a  2s  . co m
 * @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);
}