/*
 ThetaFeatureActivity.java
 Copyright (c) 2015 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.android.deviceplugin.theta.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import org.deviceconnect.android.deviceplugin.theta.ThetaDeviceApplication;
import org.deviceconnect.android.deviceplugin.theta.fragment.ThetaShootingModeFragment;
import org.deviceconnect.android.deviceplugin.theta.fragment.ThetaVRModeFragment;

/**
 * Activity for the transition from the gallery to the function screen of THETA.
 * 
 * @author NTT DOCOMO, INC.
 */
public class ThetaFeatureActivity extends FragmentActivity {

    /**
     * Feature Mode.
     */
    public static final String FEATURE_MODE = "org.deviceconnect.android.feature.MODE";

    /**
     * Mode VR.
     */
    public static final int MODE_VR = 0;

    /**
     * Mode Shooting.
     */
    public static final int MODE_SHOOTING = 1;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int mode = getIntent().getIntExtra(FEATURE_MODE, -1);
        if (null == savedInstanceState) {
            startApp(mode, null);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

    }

    @Override
    public boolean dispatchKeyEvent(final KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_BACK:
                    finish();
                    break;
                default:
                    break;
            }
        }
        return super.dispatchKeyEvent(event);
    }
    /**
     * Move Page.
     * @param pageId pageId
     * @param serviceId TODO Theta Object?
     */
    public void startApp(final int pageId, final String serviceId) {
        if (pageId == MODE_SHOOTING) {
            ThetaShootingModeFragment f = new ThetaShootingModeFragment();
            moveFragment(f);
        } else if (pageId == MODE_VR) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            ThetaVRModeFragment f = new ThetaVRModeFragment();
            moveFragment(f);
        }
    }

    /**
     * Fragment の遷移.
     * @param f Fragment
     */
    private void moveFragment(final Fragment f) {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.setTransition(FragmentTransaction.TRANSIT_NONE);
        t.replace(android.R.id.content, f);
        t.addToBackStack(null);
        t.commit();

    }

    /**
     * Return ThetaDeviceApplication.
     * @return IRKitApplication
     */
    public ThetaDeviceApplication getIRKitApplication() {
        return (ThetaDeviceApplication) getApplication();
    }

}

@@ -1159,16 +1159,61 @@
 G = 1;%0A%0A
+    /** mode. */%0A    private int mMode = -1;%0A
     @Ove
@@ -1210,32 +1210,32 @@
 ;%0A    @Override%0A
-
     protected vo
@@ -1333,21 +1333,18 @@
         
-int 
 m
+M
 ode = ge
@@ -1388,54 +1388,8 @@
 1);%0A
-        if (null == savedInstanceState) %7B%0A    
     
@@ -1402,16 +1402,17 @@
 artApp(m
+M
 ode, nul
@@ -1415,26 +1415,16 @@
  null);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1574,17 +1574,16 @@
 ENSOR);%0A
-%0A
     %7D%0A%0A 
@@ -1979,24 +1979,223 @@
 ent);%0A    %7D%0A
+%0A    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        if (mMode == MODE_VR) %7B%0A            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);%0A        %7D%0A    %7D%0A%0A
     /**%0A    
@@ -2543,88 +2543,8 @@
 ) %7B%0A
-            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);%0A
     
@@ -2633,39 +2633,40 @@
 t(f);%0A        %7D%0A
-
     %7D%0A%0A
+%0A
     /**%0A     * F

Fix VR mode 2d portrait.