public GovernorFragment(
     GovernorFragmentCallback callback, IGovernorModel governor, boolean disableScript) {
   super(callback, governor);
   this.origThreshUp = governor.getGovernorThresholdUp() + "";
   this.origThreshDown = governor.getGovernorThresholdDown() + "";
   this.disableScript = disableScript;
   this.cpuHandler = CpuHandler.getInstance();
   this.availCpuGovs = cpuHandler.getAvailCpuGov();
 }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    FragmentActivity act = getActivity();
    if (cpuHandler == null) {
      this.cpuHandler = CpuHandler.getInstance();
      this.availCpuGovs = cpuHandler.getAvailCpuGov();
    }

    numberOfCpus = cpuHandler.getNumberOfCpus();
    if (cpuHandler instanceof CpuHandlerMulticore) {
      ArrayAdapter<Integer> cpuAdapter =
          new ArrayAdapter<Integer>(act, android.R.layout.simple_spinner_item);
      cpuAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      for (int i = numberOfCpus; i >= 1; i--) {
        cpuAdapter.add(i);
      }
      spUseCpus.setAdapter(cpuAdapter);
      spUseCpus.setOnItemSelectedListener(
          new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              int num = numberOfCpus - position;
              getGovernorModel().setUseNumberOfCpus(num);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
          });
    } else {
      llFragmentTop.removeView(act.findViewById(R.id.llUseCpus));
      spUseCpus = null;
    }

    ArrayAdapter<String> arrayAdapter =
        new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, availCpuGovs);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerSetGov.setAdapter(arrayAdapter);
    spinnerSetGov.setOnItemSelectedListener(
        new OnItemSelectedListener() {

          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (callback != null) {
              callback.updateModel();
            }
            String gov = parent.getItemAtPosition(pos).toString();
            getGovernorModel().setGov(gov);
            if (callback != null) {
              callback.updateView();
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {
            if (callback != null) {
              callback.updateView();
            }
          }
        });

    OnFocusChangeListener onFocusChangeListener =
        new OnFocusChangeListener() {

          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus && etGovTreshUp.getVisibility() == View.VISIBLE) {
              String upthresh = etGovTreshUp.getText().toString();
              String downthresh = etGovTreshDown.getText().toString();
              try {
                int up = Integer.parseInt(upthresh);
                int down = 0;
                if (etGovTreshDown.getVisibility() == View.VISIBLE) {
                  down = Integer.parseInt(downthresh);
                }
                if (up > 100 || up < 0) {
                  Toast.makeText(
                          getActivity(),
                          R.string.msg_up_threshhold_has_to_be_between_0_and_100,
                          Toast.LENGTH_LONG)
                      .show();
                  etGovTreshUp.setText(origThreshUp);
                }
                if (down > 100 || down < 0) {
                  Toast.makeText(
                          getActivity(),
                          R.string.msg_down_threshhold_has_to_be_between_0_and_100,
                          Toast.LENGTH_LONG)
                      .show();
                  etGovTreshDown.setText(origThreshDown);
                }
                if (up > down) {
                  // all OK
                  return;
                }
                Toast.makeText(
                        getActivity(),
                        R.string.msg_up_threshhold_smaler_than_the_down_threshold,
                        Toast.LENGTH_LONG)
                    .show();
                down = up - 10;
                etGovTreshDown.setText(down + "");
              } catch (Exception e) {
                Toast.makeText(getActivity(), R.string.msg_threshhold_NaN, Toast.LENGTH_LONG)
                    .show();
              }
            }
          }
        };
    etGovTreshUp.setOnFocusChangeListener(onFocusChangeListener);
    etGovTreshDown.setOnFocusChangeListener(onFocusChangeListener);
  }
 private void fixGovernors() {
   Cursor c = null;
   String[] availCpuGov = CpuHandler.getInstance().getAvailCpuGov();
   TreeMap<String, Boolean> availGovs = new TreeMap<String, Boolean>();
   for (String gov : availCpuGov) {
     availGovs.put(gov, Boolean.TRUE);
   }
   try {
     c =
         contentResolver.query(
             DB.VirtualGovernor.CONTENT_URI,
             DB.VirtualGovernor.PROJECTION_DEFAULT,
             null,
             null,
             null);
     while (c.moveToNext()) {
       String govs = c.getString(DB.VirtualGovernor.INDEX_REAL_GOVERNOR);
       String[] govitems = govs.split("\\|");
       boolean found = false;
       for (String gov : govitems) {
         Boolean avail = availGovs.get(gov);
         if (avail != null && avail) {
           Logger.i("Using " + gov);
           ContentValues values = new ContentValues(1);
           values.put(DB.VirtualGovernor.NAME_REAL_GOVERNOR, gov);
           // check for thresholds
           GovernorConfig governorConfig = GovernorConfigHelper.getGovernorConfig(gov);
           if (!governorConfig.hasThreshholdUpFeature()) {
             values.put(DB.VirtualGovernor.NAME_GOVERNOR_THRESHOLD_UP, -1);
           }
           if (!governorConfig.hasThreshholdDownFeature()) {
             values.put(DB.VirtualGovernor.NAME_GOVERNOR_THRESHOLD_DOWN, -1);
           }
           if (contentResolver.update(
                   DB.VirtualGovernor.CONTENT_URI,
                   values,
                   DB.SELECTION_BY_ID,
                   new String[] {Long.toString(c.getLong(DB.INDEX_ID))})
               > 0) {
             found = true;
             break;
           }
         }
       }
       if (!found) {
         // we did not find a compatible gov so use none
         ContentValues values = new ContentValues(1);
         values.put(DB.VirtualGovernor.NAME_REAL_GOVERNOR, RootHandler.NOT_AVAILABLE);
         contentResolver.update(
             DB.VirtualGovernor.CONTENT_URI,
             values,
             DB.SELECTION_BY_ID,
             new String[] {Long.toString(c.getLong(DB.INDEX_ID))});
       }
     }
   } finally {
     if (c != null) {
       c.close();
       c = null;
     }
   }
 }