コード例 #1
0
  private void refreshSpinners() {
    // reload the spinners to make sure all refs are in the right sequence
    m_spinnerFromAdapter.refreshFromSpinner(this);
    m_spinnerToAdapter.filterToSpinner(m_refFromName, this);
    // after we reloaded the spinners we need to reset the selections
    Spinner spinnerStatTypeFrom = (Spinner) findViewById(R.id.spinnerStatType);
    Spinner spinnerStatTypeTo = (Spinner) findViewById(R.id.spinnerStatSampleEnd);
    Log.i(
        TAG,
        "refreshSpinners: reset spinner selections: from='"
            + m_refFromName
            + "', to='"
            + m_refToName
            + "'");
    Log.i(
        TAG,
        "refreshSpinners Spinner values: SpinnerFrom="
            + m_spinnerFromAdapter.getNames()
            + " SpinnerTo="
            + m_spinnerToAdapter.getNames());
    Log.i(
        TAG,
        "refreshSpinners: request selections: from='"
            + m_spinnerFromAdapter.getPosition(m_refFromName)
            + "', to='"
            + m_spinnerToAdapter.getPosition(m_refToName)
            + "'");

    // restore positions
    spinnerStatTypeFrom.setSelection(m_spinnerFromAdapter.getPosition(m_refFromName), true);
    if (spinnerStatTypeTo.isShown()) {
      spinnerStatTypeTo.setSelection(m_spinnerToAdapter.getPosition(m_refToName), true);
    } else {
      spinnerStatTypeTo.setSelection(
          m_spinnerToAdapter.getPosition(Reference.CURRENT_REF_FILENAME), true);
    }
    Log.i(
        TAG,
        "refreshSpinners result positions: from='"
            + spinnerStatTypeFrom.getSelectedItemPosition()
            + "', to='"
            + spinnerStatTypeTo.getSelectedItemPosition()
            + "'");

    if ((spinnerStatTypeFrom.getSelectedItemPosition() == -1)
        || (spinnerStatTypeTo.getSelectedItemPosition() == -1)) {
      Toast.makeText(
              StatsActivity.this,
              "Selected 'from' or 'to' reference could not be loaded. Please refresh",
              Toast.LENGTH_LONG)
          .show();
    }
  }
コード例 #2
0
  /**
   * Take the change of selection from the spinners into account and refresh the ListView with the
   * right data
   */
  public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    // this method is fired even if nothing has changed so we nee to find that out
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    boolean bChanged = false;

    // id is in the order of the spinners, 0 is stat, 1 is stat_type
    if (parent == (Spinner) findViewById(R.id.spinnerStatType)) {
      // detect if something changed
      String newStat = (String) ((ReferencesAdapter) parent.getAdapter()).getItemName(position);
      if ((m_refFromName != null) && (!m_refFromName.equals(newStat))) {
        Log.i(TAG, "Spinner from changed from " + m_refFromName + " to " + newStat);
        m_refFromName = newStat;
        bChanged = true;
        // we need to update the second spinner
        m_spinnerToAdapter.filterToSpinner(newStat, this);
        m_spinnerToAdapter.notifyDataSetChanged();

        // select the right element
        Spinner spinnerStatSampleEnd = (Spinner) findViewById(R.id.spinnerStatSampleEnd);
        if (spinnerStatSampleEnd.isShown()) {
          spinnerStatSampleEnd.setSelection(m_spinnerToAdapter.getPosition(m_refToName));
        } else {
          spinnerStatSampleEnd.setSelection(
              m_spinnerToAdapter.getPosition(Reference.CURRENT_REF_FILENAME));
        }

      } else {
        return;
      }

    } else if (parent == (Spinner) findViewById(R.id.spinnerStatSampleEnd)) {
      String newStat = (String) ((ReferencesAdapter) parent.getAdapter()).getItemName(position);
      if ((m_refFromName != null) && (!m_refToName.equals(newStat))) {
        Log.i(TAG, "Spinner to changed from " + m_refToName + " to " + newStat);
        m_refToName = newStat;
        bChanged = true;
      } else {
        return;
      }

    } else if (parent == (Spinner) findViewById(R.id.spinnerStat)) {
      int iNewStat = position;
      if (m_iStat != iNewStat) {
        m_iStat = iNewStat;
        bChanged = true;
      } else {
        return;
      }

      // inform the user when he tries to use functions requiring root and he doesn't have root
      // enabled
      boolean rootEnabled = sharedPrefs.getBoolean("root_features", false);

      if (!rootEnabled) {
        if ((m_iStat == 4) || (m_iStat == 3)) {
          Toast.makeText(
                  this,
                  "This function requires root access. Check \"Advanced\" preferences",
                  Toast.LENGTH_LONG)
              .show();
        }
      }

    } else {
      Log.e(TAG, "ProcessStatsActivity.onItemSelected error. ID could not be resolved");
      Toast.makeText(this, "Error: could not resolve what changed", Toast.LENGTH_SHORT).show();
    }

    Reference myReferenceFrom = ReferenceStore.getReferenceByName(m_refFromName, this);
    Reference myReferenceTo = ReferenceStore.getReferenceByName(m_refToName, this);

    TextView tvSince = (TextView) findViewById(R.id.TextViewSince);
    //        long sinceMs = getSince();
    long sinceMs = StatsProvider.getInstance(this).getSince(myReferenceFrom, myReferenceTo);

    if (sinceMs != -1) {
      String sinceText = DateUtils.formatDuration(sinceMs);
      boolean bShowBatteryLevels = sharedPrefs.getBoolean("show_batt", true);
      if (bShowBatteryLevels) {

        sinceText +=
            " "
                + StatsProvider.getInstance(this)
                    .getBatteryLevelFromTo(myReferenceFrom, myReferenceTo);
      }
      tvSince.setText(sinceText);
      Log.i(TAG, "Since " + sinceText);
    } else {
      tvSince.setText("n/a ");
      Log.i(TAG, "Since: n/a ");
    }
    // @todo fix this: this method is called twice
    // m_listViewAdapter.notifyDataSetChanged();
    if (bChanged) {
      GoogleAnalytics.getInstance(this)
          .trackStats(
              this,
              GoogleAnalytics.ACTIVITY_STATS,
              m_iStat,
              m_refFromName,
              m_refToName,
              m_iSorting);
      // new LoadStatData().execute(this);
      // as the source changed fetch the data
      doRefresh(false);
    }
  }