@Override
 public boolean onOptionsItemSelected(MenuItem item) {
   int id = item.getItemId();
   if (id == R.id.menu_preferences) {
     doPreferences();
   } else if (id == R.id.menu_new_window) {
     doCreateNewWindow();
   } else if (id == R.id.menu_close_window) {
     confirmCloseWindow();
   } else if (id == R.id.menu_window_list) {
     startActivityForResult(new Intent(this, WindowList.class), REQUEST_CHOOSE_WINDOW);
   } else if (id == R.id.menu_reset) {
     doResetTerminal();
     Toast toast = Toast.makeText(this, R.string.reset_toast_notification, Toast.LENGTH_LONG);
     toast.setGravity(Gravity.CENTER, 0, 0);
     toast.show();
   } else if (id == R.id.menu_send_email) {
     doEmailTranscript();
   } else if (id == R.id.menu_special_keys) {
     doDocumentKeys();
   } else if (id == R.id.menu_toggle_soft_keyboard) {
     doToggleSoftKeyboard();
   } else if (id == R.id.menu_toggle_wakelock) {
     doToggleWakeLock();
   } else if (id == R.id.menu_toggle_wifilock) {
     doToggleWifiLock();
   }
   // Hide the action bar if appropriate
   if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES) {
     mActionBar.hide();
   }
   return super.onOptionsItemSelected(item);
 }
 public boolean onNavigationItemSelected(int position, long id) {
   int oldPosition = mViewFlipper.getDisplayedChild();
   if (position != oldPosition) {
     mViewFlipper.setDisplayedChild(position);
     if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES) {
       mActionBar.hide();
     }
   }
   return true;
 }
 private void doToggleActionBar() {
   ActionBarCompat bar = mActionBar;
   if (bar == null) {
     return;
   }
   if (bar.isShowing()) {
     bar.hide();
   } else {
     bar.show();
   }
 }
  private void updatePrefs() {
    mUseKeyboardShortcuts = mSettings.getUseKeyboardShortcutsFlag();

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    ColorScheme colorScheme = new ColorScheme(mSettings.getColorScheme());

    mViewFlipper.updatePrefs(mSettings);

    for (View v : mViewFlipper) {
      ((EmulatorView) v).setDensity(metrics);
      ((TermView) v).updatePrefs(mSettings);
    }

    if (mTermSessions != null) {
      for (TermSession session : mTermSessions) {
        ((ShellTermSession) session).updatePrefs(mSettings);
      }
    }

    {
      Window win = getWindow();
      WindowManager.LayoutParams params = win.getAttributes();
      final int FULLSCREEN = WindowManager.LayoutParams.FLAG_FULLSCREEN;
      int desiredFlag = mSettings.showStatusBar() ? 0 : FULLSCREEN;
      if (desiredFlag != (params.flags & FULLSCREEN)
          || (AndroidCompat.SDK >= 11 && mActionBarMode != mSettings.actionBarMode())) {
        if (mAlreadyStarted) {
          // Can't switch to/from fullscreen after
          // starting the activity.
          restart();
        } else {
          win.setFlags(desiredFlag, FULLSCREEN);
          if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES) {
            mActionBar.hide();
          }
        }
      }
    }

    int orientation = mSettings.getScreenOrientation();
    int o = 0;
    if (orientation == 0) {
      o = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    } else if (orientation == 1) {
      o = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    } else if (orientation == 2) {
      o = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    } else {
      /* Shouldn't be happened. */
    }
    setRequestedOrientation(o);
  }
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
   switch (keyCode) {
     case KeyEvent.KEYCODE_BACK:
       if (AndroidCompat.SDK < 5) {
         if (!mBackKeyPressed) {
           /* This key up event might correspond to a key down
           delivered to another activity -- ignore */
           return false;
         }
         mBackKeyPressed = false;
       }
       if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES && mActionBar.isShowing()) {
         mActionBar.hide();
         return true;
       }
       switch (mSettings.getBackKeyAction()) {
         case TermSettings.BACK_KEY_STOPS_SERVICE:
           mStopServiceOnFinish = true;
         case TermSettings.BACK_KEY_CLOSES_ACTIVITY:
           finish();
           return true;
         case TermSettings.BACK_KEY_CLOSES_WINDOW:
           doCloseWindow();
           return true;
         default:
           return false;
       }
     case KeyEvent.KEYCODE_MENU:
       if (mActionBar != null && !mActionBar.isShowing()) {
         mActionBar.show();
         return true;
       } else {
         return super.onKeyUp(keyCode, event);
       }
     default:
       return super.onKeyUp(keyCode, event);
   }
 }
  private void updatePrefs() {
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    ColorScheme colorScheme = new ColorScheme(mSettings.getColorScheme());

    mViewFlipper.updatePrefs(mSettings);

    for (View v : mViewFlipper) {
      ((EmulatorView) v).setDensity(metrics);
      ((TermView) v).updatePrefs(mSettings);
    }

    if (mTermSessions != null) {
      for (TermSession session : mTermSessions) {
        ((ShellTermSession) session).updatePrefs(mSettings);
      }
    }

    {
      Window win = getWindow();
      WindowManager.LayoutParams params = win.getAttributes();
      final int FULLSCREEN = WindowManager.LayoutParams.FLAG_FULLSCREEN;
      int desiredFlag = mSettings.showStatusBar() ? 0 : FULLSCREEN;
      if (desiredFlag != (params.flags & FULLSCREEN)
          || (AndroidCompat.SDK >= 11 && mActionBarMode != mSettings.actionBarMode())) {
        if (mAlreadyStarted) {
          // Can't switch to/from fullscreen after
          // starting the activity.
          restart();
        } else {
          win.setFlags(desiredFlag, FULLSCREEN);
          if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES) {
            mActionBar.hide();
          }
        }
      }
    }
  }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.e(TermDebug.LOG_TAG, "onCreate");
    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    mSettings = new TermSettings(getResources(), mPrefs);

    Intent broadcast = new Intent(ACTION_PATH_BROADCAST);
    if (AndroidCompat.SDK >= 12) {
      broadcast.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);
    }
    mPendingPathBroadcasts++;
    sendOrderedBroadcast(
        broadcast, PERMISSION_PATH_BROADCAST, mPathReceiver, null, RESULT_OK, null, null);

    broadcast = new Intent(broadcast);
    broadcast.setAction(ACTION_PATH_PREPEND_BROADCAST);
    mPendingPathBroadcasts++;
    sendOrderedBroadcast(
        broadcast, PERMISSION_PATH_PREPEND_BROADCAST, mPathReceiver, null, RESULT_OK, null, null);

    TSIntent = new Intent(this, TermService.class);
    startService(TSIntent);

    if (!bindService(TSIntent, mTSConnection, BIND_AUTO_CREATE)) {
      Log.w(TermDebug.LOG_TAG, "bind to service failed!");
    }

    if (AndroidCompat.SDK >= 11) {
      int actionBarMode = mSettings.actionBarMode();
      mActionBarMode = actionBarMode;
      switch (actionBarMode) {
        case TermSettings.ACTION_BAR_MODE_ALWAYS_VISIBLE:
          setTheme(R.style.Theme_Holo);
          break;
        case TermSettings.ACTION_BAR_MODE_HIDES:
          setTheme(R.style.Theme_Holo_ActionBarOverlay);
          break;
      }
    }

    setContentView(R.layout.term_activity);
    mViewFlipper = (TermViewFlipper) findViewById(VIEW_FLIPPER);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TermDebug.LOG_TAG);
    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    int wifiLockMode = WifiManager.WIFI_MODE_FULL;
    if (AndroidCompat.SDK >= 12) {
      wifiLockMode = WIFI_MODE_FULL_HIGH_PERF;
    }
    mWifiLock = wm.createWifiLock(wifiLockMode, TermDebug.LOG_TAG);

    ActionBarCompat actionBar = ActivityCompat.getActionBar(this);
    if (actionBar != null) {
      mActionBar = actionBar;
      actionBar.setNavigationMode(ActionBarCompat.NAVIGATION_MODE_LIST);
      actionBar.setDisplayOptions(0, ActionBarCompat.DISPLAY_SHOW_TITLE);
      if (mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES) {
        actionBar.hide();
      }
    }

    mHaveFullHwKeyboard = checkHaveFullHwKeyboard(getResources().getConfiguration());

    updatePrefs();
    mAlreadyStarted = true;
  }