@Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (AndroidUtils.isCupcake()
        && keyCode == KeyEvent.KEYCODE_BACK
        && event.getRepeatCount() == 0) {
      // Take care of calling this method on earlier versions of
      // the platform where it doesn't exist.
      onBackPressed();
    }

    return super.onKeyDown(keyCode, event);
  }
 public static Intent createIntentForCurrentScreen(Activity activity, boolean showTitleBar) {
   Intent intent;
   if (AndroidUtils.isSmallNormalOrLargeScreen()) {
     intent = new Intent(activity, Fullscreen.class);
     if (showTitleBar) {
       intent.putExtra(JR_OPERATION_MODE, JR_FULLSCREEN);
     } else {
       intent.putExtra(JR_OPERATION_MODE, JR_FULLSCREEN_NO_TITLE);
     }
   } else { // Honeycomb (because the screen is large+)
     intent = new Intent(activity, JRFragmentHostActivity.class);
     intent.putExtra(JR_OPERATION_MODE, JR_DIALOG);
   }
   return intent;
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LogUtils.logd(TAG, "[onCreate]: " + getFragmentId());

    JRSession session = JRSession.getInstance();

    if (session == null || savedInstanceState != null) {
      /* This flow control path is reached when there's process death and restart */
      Log.e(TAG, "bailing out after a process kill/restart. mSession: " + session);

      // prevent fragment recreation error -- the system needs the fragment's container to exist
      // even if the Activity is finishing right away
      setContentView(R.layout.jr_fragment_host_activity);
      super.finish();
      return;
    }

    switch (getFragmentId()) {
      case JR_PROVIDER_LIST:
        mUiFragment = new JRProviderListFragment();
        break;
      case JR_LANDING:
        mUiFragment = new JRLandingFragment();
        break;
      case JR_WEBVIEW:
        mUiFragment = new JRWebViewFragment();
        break;
      case JR_PUBLISH:
        mUiFragment = new JRPublishFragment();
        break;
      case JR_OPENID_APPAUTH:
        mUiFragment = new JROpenIDAppAuthFragment();
        break;
      default:
        throw new IllegalFragmentIdException(getFragmentId());
    }

    Bundle fragArgs = new Bundle();
    fragArgs.putInt(JRUiFragment.JR_FRAGMENT_FLOW_MODE, getFlowMode());
    fragArgs.putAll(getIntent().getExtras());
    mUiFragment.setArguments(fragArgs);

    mUiFragment.onFragmentHostActivityCreate(this, session);

    if (shouldBeDialog()) {
      AndroidUtils.activitySetFinishOnTouchOutside(this, true);

      if (shouldBePhoneSizedDialog()) {
        getTheme().applyStyle(R.style.jr_dialog_phone_sized, true);
      } else {
        getTheme().applyStyle(R.style.jr_dialog_71_percent, true);
      }

      if (!mUiFragment.shouldShowTitleWhenDialog()) {
        getTheme().applyStyle(R.style.jr_disable_title_and_action_bar_style, true);
      }
    } else if (getOperationMode() == JR_FULLSCREEN_NO_TITLE) {
      getWindow().requestFeature(Window.FEATURE_NO_TITLE);
      getTheme().applyStyle(R.style.jr_disable_title_and_action_bar_style, true);
    } else if (getOperationMode() == JR_FULLSCREEN) {
      // noop
    }

    setContentView(R.layout.jr_fragment_host_activity);

    View fragmentContainer = findViewById(R.id.jr_fragment_container);
    if (fragmentContainer instanceof CustomMeasuringFrameLayout) {
      // CMFL -> dialog mode on a tablet
      if (shouldBePhoneSizedDialog()) {
        // Do the actual setting of the target size to achieve phone sized dialog.
        ((CustomMeasuringFrameLayout) fragmentContainer).setTargetSizeDip(320, 480);
        getWindow().makeActive();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(getWindow().getAttributes());
        lp.width = AndroidUtils.scaleDipToPixels(320);
        // After discussing it with Lilli we think it makes sense to let the height of the window
        // grow if the title is enabled
        // int targetHeight = mUiFragment.getCustomTitle() == null ? 480 : 560;
        // lp.height = AndroidUtils.scaleDipToPixels(targetHeight);
        getWindow().setAttributes(lp);
      }
    }

    getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.jr_fragment_container, mUiFragment)
        .setTransition(FragmentTransaction.TRANSIT_NONE)
        .commit();
  }
 private boolean shouldBeDialog() {
   return AndroidUtils.isXlarge();
 }