/** Sets up a notification shortcut in the system bar */
  public static void setupNotification() {
    Context context = ApplicationContext.getInstance();
    String notificationService = Context.NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) context.getSystemService(notificationService);
    Intent notificationIntent = new Intent(context, MainFragmentActivity.class);
    PendingIntent contentIntent =
        PendingIntent.getActivity(
            context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
      mBuilder =
          new NotificationCompat.Builder(ApplicationContext.getInstance())
              .setSmallIcon(R.drawable.ic_stat_notification)
              .setContentTitle(context.getResources().getText(R.string.app_name))
              .setContentText(context.getResources().getText(R.string.go_to_app))
              .setWhen(0);
    else
      mBuilder =
          new NotificationCompat.Builder(ApplicationContext.getInstance())
              .setSmallIcon(R.drawable.ic_stat_notification)
              .setContentTitle(context.getResources().getText(R.string.app_name))
              .setContentText(context.getResources().getText(R.string.go_to_app));

    mBuilder.setContentIntent(contentIntent);
    // mId allows you to update the notification later on.
    int mId = 1;
    Notification shortcutNotification = mBuilder.build();
    shortcutNotification.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(mId, shortcutNotification);

    ApplicationContext.getInstance().notificationIsShowing = true;
  }
    /**
     * Toggles the Hybrid keyboard with the appropriate language
     *
     * @param sFragmentManager FragmentManager that is responsible for the transaction
     */
    public void handleHybridShiftButton(FragmentManager sFragmentManager) {
      BugSenseHandler.sendEvent("Hybrid shift button was pressed");
      Crashlytics.log("Hybrid shift button was pressed");

      SearchFragmentTab.myVib.vibrate(20);
      // ---Figure out what type of hybrid keyboard should be shown, and save it in shared prefs---
      if (ApplicationContext.getInstance().getHybridKeyboardMode().equals("")
          || ApplicationContext.getInstance()
              .getHybridKeyboardMode()
              .equals("Hybrid_English_To_Russ")) {
        ApplicationContext.getInstance().setHybridKeyboardMode("Hybrid_Russian_To_Eng");
      } else {
        ApplicationContext.getInstance().setHybridKeyboardMode("Hybrid_English_To_Russ");
      }
      ApplicationContext.getInstance()
          .sharedPreferencesEditor
          .putString("HYBRID_TYPE", ApplicationContext.getInstance().getHybridKeyboardMode());
      ApplicationContext.getInstance().sharedPreferencesEditor.commit();
      if (Build.VERSION.SDK_INT > 7) ApplicationContext.getInstance().backupManager.dataChanged();

      // ---refresh the search tab_layout---
      SearchFragmentTab searchFragment = new SearchFragmentTab();
      sFragmentManager
          .beginTransaction()
          .replace(sFragmentManager.findFragmentByTag("search").getId(), searchFragment, "search")
          .commit();

      mapKeyBindings(ApplicationContext.getInstance());
    }
    public void handleLetterPress(
        KeyEvent event, int keyCode, Device.DeviceType mDeviceType, IBinder windowToken) {
      if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
        // ---Done/Enter button was pressed---
        InputMethodManager keyboard =
            (InputMethodManager)
                ApplicationContext.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (keyboard != null) {
          try {
            keyboard.hideSoftInputFromWindow(windowToken, 0);
          } catch (Exception e) {
            BugSenseHandler.sendExceptionMessage(
                "KeyBindings", "KeyEventHandlers.handleLetterPress", e);
            Crashlytics.setString("KeyBindings", "KeyEventHandlers.handleLetterPress");
            Crashlytics.logException(e);

            if (ApplicationContext.DEBUG_MODE) e.printStackTrace();
          }
        }
      } else if (event.getKeyCode() != KeyEvent.KEYCODE_MENU) {
        if (!SearchFragmentTab.ENGLISH_KBD_BACKSPACE) {
          if (mDeviceType == Device.DeviceType.Google_TV) {
            // *---Goog TV custom keys mappings!---
            handleGoogTvKeyPress(event, keyCode);
          } else {
            handleNonGoogTvKeyPress(event, keyCode);
          }
        } else {
          SearchFragmentTab.ENGLISH_KBD_BACKSPACE = false;
        }
      }
    }
  public static void dismissNotification() {
    if (ApplicationContext.getInstance().notificationIsShowing)
      mNotificationManager.cancel(NOTIFY_ID);

    ApplicationContext.getInstance().notificationIsShowing = false;
  }
 public void handleBackspaceButton() {
   if (!ApplicationContext.getInstance().keyboardType.equals("English")) {
     ApplicationContext.getInstance().readyToSearch = true;
     SearchFragmentTab.backSpace(false);
   }
 }
    private void handleGoogTvKeyPress(KeyEvent event, int keyCode) {
      int hash;
      String charLabel = "";

      if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
          || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
          || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
          || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
        // ---Navigate with DPad---
        Log.i("KeyBindings", event.toString());
      } else {
        if (event.getKeyCode()
            == KeyEvent.KEYCODE_UNKNOWN) { // key was pressed on soft (on-screen) keyboard
          hash = BasicOnKeyboardActionListener.KEY_PRESSED;
          SearchFragmentTab.PHYSICAL_KEY = false;
        } else { // key was pressed on physical keyboard for Goog TV
          SearchFragmentTab.PHYSICAL_KEY = true;
          hash = event.getKeyCode();

          if (event.isLongPress() && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            SearchFragmentTab.backSpaceLong();
          }
        }

        switch (hash) {
          case 0: // A letter was pressed on the English keyboard
            SearchFragmentTab.retainSearch();
            break;
        }

        ApplicationContext.getInstance().readyToSearch = true;

        // Russian Keyboard
        if (ApplicationContext.getInstance().keyboardType.equals("Russian")) {
          if (event.getKeyCode()
              == KeyEvent.KEYCODE_UNKNOWN) { // key was pressed on soft (on-screen) keyboard
            charLabel = sparseArrRussian.get(hash);
          } else { // key was pressed on the physical keyboard (for GoogTV)
            charLabel = sparseArrRussian_GoogTV.get(hash);
          }

          // Hybrid Keyboard
        } else if (ApplicationContext.getInstance()
            .keyboardType
            .equals("Hybrid")) { // Hybrid keyboard
          if (ApplicationContext.getInstance()
              .getHybridKeyboardMode()
              .equals("Hybrid_English_To_Russ")) {
            if (event.getKeyCode()
                != KeyEvent
                    .KEYCODE_UNKNOWN) { // key was pressed on the physical keyboard (for GoogTV)
              charLabel = sparseArrHybridEngToRuss_GoogTV.get(hash);
            } else { // key was pressed on the soft (on-screen) keyboard (for GoogTV)
              ApplicationContext.getInstance().readyToSearch = true;
              charLabel = sparseArrHybrid_EngToRuss.get(hash);
            }
          } else if (ApplicationContext.getInstance()
              .getHybridKeyboardMode()
              .equals("Hybrid_Russian_To_Eng")) {
            if (event.getKeyCode()
                != KeyEvent
                    .KEYCODE_UNKNOWN) { // key was pressed on the physical keyboard (for GoogTV)
              charLabel = sparseArrHybrid_RussToEng_GoogTV.get(hash);
            } else { // key was pressed on the soft (on-screen) keyboard (for GoogTV)
              ApplicationContext.getInstance().readyToSearch = true;
              charLabel = sparseArrHybrid_RussToEng.get(hash);
            }
          }
        }

        // English Keyboard
        else if (ApplicationContext.getInstance().keyboardType.equals("English")) {
          try {
            charLabel = sparseArrEnglish.get(hash);
          } catch (Exception e) {
            BugSenseHandler.addCrashExtraData(
                "KeyBindings", "Exception in dispatchKeyEvent: " + e.getMessage());
            BugSenseHandler.leaveBreadcrumb("KeyBindings.keyboardType.equals(\"English\")");
            BugSenseHandler.sendException(e);
            Crashlytics.setString("KeyBindings", "keyboardType.equals(\"English\")");
            Crashlytics.logException(e);
          }
        }

        if (event.getKeyCode()
            != KeyEvent.KEYCODE_UNKNOWN) { // physical key was pressed on physical GoogTV keyboard
          if (event.getAction() == KeyEvent.ACTION_UP) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
              SearchFragmentTab.backSpace(true);
            } else {
              if (event.getAction() == KeyEvent.ACTION_UP)
                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                  Intent intent = new Intent("key_bindings_event");
                  intent.putExtra("message", "onBackPressed");
                  LocalBroadcastManager.getInstance(ApplicationContext.getInstance())
                      .sendBroadcast(intent);
                } else {
                  SearchFragmentTab.typeText(charLabel);
                  // ---keep track of key presses to avoid double-typing on Goog TV---
                  SearchFragmentTab.ENGLISH_KBD_BACKSPACE = false;
                }
            }
          }
        } else { // soft input key was pressed on IMM keyboard
          SearchFragmentTab.typeText(charLabel);
          SearchFragmentTab.ENGLISH_KBD_BACKSPACE = false;
        }
      }
    }
    /** Keyboard event processing for non-Google TV devices */
    private void handleNonGoogTvKeyPress(KeyEvent event, int keyCode) {
      int hash;
      String charLabel = "";

      hash = BasicOnKeyboardActionListener.KEY_PRESSED;
      if (hash == 3) {
        // Space key
        try {
          SearchFragmentTab.typeText(" ");
          SearchFragmentTab.ENGLISH_KBD_BACKSPACE = false;
        } catch (Exception e) {
          BugSenseHandler.addCrashExtraData(
              "KeyBindings", "dispatchKeyEvent (not google tv)" + e.getMessage());
          BugSenseHandler.leaveBreadcrumb("KeyBindings.handleNonGoogTvKeyPress");
          BugSenseHandler.sendException(e);
          Crashlytics.setString("KeyBindings", "dispatchKeyEvent (not google tv)");
          Crashlytics.logException(e);
        }
      } else if (hash == 591) {
        // Shift button was pressed on the Russian keyboard
        // do what?
      } else if (hash == 0) {
        // ---A letter was pressed on the English keyboard---
        SearchFragmentTab.retainSearch();
      } else {
        // ---Regular key was pressed---
        ApplicationContext.getInstance().readyToSearch = true;

        if (ApplicationContext.getInstance().keyboardType.equals("Russian")) {
          charLabel = sparseArrRussian.get(hash);
        } else if (ApplicationContext.getInstance().keyboardType.equals("Hybrid")) {
          if (ApplicationContext.getInstance()
              .getHybridKeyboardMode()
              .equals("Hybrid_English_To_Russ")) {
            charLabel = sparseArrHybrid_EngToRuss.get(hash);
          } else if (ApplicationContext.getInstance()
              .getHybridKeyboardMode()
              .equals("Hybrid_Russian_To_Eng")) {
            charLabel = sparseArrHybrid_RussToEng.get(hash);
          }
        } else if (ApplicationContext.getInstance().keyboardType.equals("English")) {
          try {
            if (sparseArrEnglish != null) charLabel = sparseArrEnglish.get(hash);
          } catch (Exception e) {
            BugSenseHandler.addCrashExtraData(
                "KeyBindings", "regular key was pressed" + e.getMessage());
            BugSenseHandler.leaveBreadcrumb("KeyBindings.handleNonGoogTvKeyPress");
            BugSenseHandler.sendException(e);
            Crashlytics.setString("KeyBindings", "regular key was pressed");
            Crashlytics.logException(e);
          }
        }

        if (keyCode != KeyEvent.KEYCODE_BACK
            && keyCode != KeyEvent.KEYCODE_POWER
            && keyCode != KeyEvent.KEYCODE_VOLUME_UP
            && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) {
          if (keyCode == KeyEvent.KEYCODE_DEL) {
            SearchFragmentTab.backSpace(true);
          } else {
            SearchFragmentTab.typeText(charLabel);
          }
        }
        SearchFragmentTab.ENGLISH_KBD_BACKSPACE = false;
      }
    }
  public static void mapKeyBindings(Context context) {
    ApplicationContext aContext = ApplicationContext.getInstance();
    KeyBindings keyBindings;

    if (sDeviceType == Device.DeviceType.Google_TV) {
      // ---Google TV devices---
      KeyBindings keyBindings_GoogTV;
      if (ApplicationContext.getInstance().keyboardType.equals("Hybrid")) {
        if (aContext.getHybridKeyboardMode().equals("Hybrid_English_To_Russ")
            || aContext.getHybridKeyboardMode().equals("")) {
          keyBindings_GoogTV = new KeyBindings("Hybrid_English_To_Russ_GoogTV");
          sparseArrHybridEngToRuss_GoogTV = keyBindings_GoogTV.getHybridEngToRussHashes_GoogTV();

          keyBindings = new KeyBindings("Hybrid_English_To_Russ");
          sparseArrHybrid_EngToRuss = keyBindings.getHybridEngToRussHashes();
        } else if (ApplicationContext.getInstance()
            .getHybridKeyboardMode()
            .equals("Hybrid_Russian_To_Eng")) {
          keyBindings_GoogTV = new KeyBindings("Hybrid_Russian_To_Eng_GoogTV");
          sparseArrHybrid_RussToEng_GoogTV = keyBindings_GoogTV.getHybridRussToEngHashes_GoogTV();

          keyBindings = new KeyBindings("Hybrid_Russian_To_Eng");
          sparseArrHybrid_RussToEng = keyBindings.getHybridRussToEngHashes();
        }
      } else if (ApplicationContext.getInstance().keyboardType.equals("Russian")) {
        keyBindings_GoogTV = new KeyBindings("Russian_GoogTV");
        sparseArrRussian_GoogTV = keyBindings_GoogTV.getRussianHashes_GoogTV();

        keyBindings = new KeyBindings("Russian");
        sparseArrRussian = keyBindings.getRussianHashes();
      } else if (ApplicationContext.getInstance().keyboardType.equals("English")) {
        keyBindings_GoogTV = new KeyBindings("English");
        sparseArrEnglish = keyBindings_GoogTV.getEnglishHashes_GoogTV();
      }

    } else {
      // ---Normal phones---
      if (ApplicationContext.getInstance().keyboardType.equals("Hybrid")) {
        if (ApplicationContext.getInstance().getHybridKeyboardMode() == null
            || ApplicationContext.getInstance()
                .getHybridKeyboardMode()
                .equals("Hybrid_English_To_Russ")
            || ApplicationContext.getInstance().getHybridKeyboardMode().equals("")) {

          keyBindings =
              new KeyBindings(
                  "Hybrid_English_To_Russ"); // Bind Hybrid keyboard's English keys to output
          // Russian letters
          sparseArrHybrid_EngToRuss = keyBindings.getHybridEngToRussHashes();

        } else if (ApplicationContext.getInstance()
            .getHybridKeyboardMode()
            .equals("Hybrid_Russian_To_Eng")) {

          keyBindings =
              new KeyBindings(
                  "Hybrid_Russian_To_Eng"); // Bind Hybrid keyboard's Russian keys to output English
          // letters
          sparseArrHybrid_RussToEng = keyBindings.getHybridRussToEngHashes();
        }
      } else if (ApplicationContext.getInstance().keyboardType.equals("Russian")) {

        keyBindings = new KeyBindings("Russian");
        sparseArrRussian = keyBindings.getRussianHashes();
      }
    }
  }
 /** Removes all activities from the stack */
 public void doPositiveClick() {
   ApplicationContext.getInstance().dataSource.deleteSearch(model);
 }