protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case DAY_PICK:
          selectedDays = data.getIntegerArrayListExtra("selectedDays");
          dayStrings.clear();
          days[0] = days[1] = days[2] = days[3] = days[4] = 0;
          for (int n : selectedDays) {
            days[n] = 1;
            switch (n) {
              case Loc8Preference.MONDAY:
                dayStrings.add("Monday");
                break;
              case Loc8Preference.TUESDAY:
                dayStrings.add("Tuesday");
                break;
              case Loc8Preference.WEDNESDAY:
                dayStrings.add("Wednesday");
                break;
              case Loc8Preference.THURSDAY:
                dayStrings.add("Thursday");
                break;
              case Loc8Preference.FRIDAY:
                dayStrings.add("Friday");
                break;
              case Loc8Preference.SATURDAY:
                dayStrings.add("Saturday");
                break;
              case Loc8Preference.SUNDAY:
                dayStrings.add("Sunday");
                break;
            }
          }
          if (dayStrings.size() > 5) {
            tSelectDaysOfWeekHeader.setText(
                dayStrings.get(0).toString()
                    + ", "
                    + dayStrings.get(1).toString()
                    + ", "
                    + dayStrings.get(2).toString()
                    + " and "
                    + String.valueOf(dayStrings.size() - 3)
                    + " other(s)");
          } else if (dayStrings.size() == 0) {
            tSelectDaysOfWeekHeader.setText("");
            selectedDays = null;
          } else {
            tSelectDaysOfWeekHeader.setText(
                dayStrings.toString().replace("[", "").replace("]", ""));
          }
          break;

        case LOCATION_PICK:

          // clear arraylist to prevent previous selections to be added
          locs.clear();

          int count_l = 0;
          int loc;

          String found = "";

          ArrayList<Integer> selection = data.getIntegerArrayListExtra("selectedLoc");

          Cursor locsCursor = mDbHelper.getAllLocations("");

          ArrayList<String> locArrayList = new ArrayList<String>();

          for (locsCursor.moveToFirst(); !locsCursor.isAfterLast(); locsCursor.moveToNext()) {
            locArrayList.add(locsCursor.getString(1));
          }

          for (int n : selection) {
            for (locsCursor.moveToFirst(); !locsCursor.isAfterLast(); locsCursor.moveToNext()) {
              if (n == locsCursor.getInt(0)) {

                Log.d("locationSelect", "Found : " + selection);
                loc = n;
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Set radius (meters) for ");
                alert.setMessage(
                    locsCursor.getString(locsCursor.getColumnIndex(mDbHelper.KEY_LOCATIONNAME)));

                final double tempLat =
                    locsCursor.getDouble(locsCursor.getColumnIndex(mDbHelper.KEY_LATITUDE));
                final double tempLon =
                    locsCursor.getDouble(locsCursor.getColumnIndex(mDbHelper.KEY_LONGITUDE));
                final String tempName =
                    locsCursor.getString(locsCursor.getColumnIndex(mDbHelper.KEY_LOCATIONNAME));

                // Set an EditText view to get user input
                final EditText input = new EditText(this);
                int maxLength = 6;
                InputFilter[] filter = new InputFilter[1];
                filter[0] = new InputFilter.LengthFilter(maxLength);
                input.setFilters(filter);
                alert.setView(input);

                alert.setPositiveButton(
                    "Set",
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString();
                        float radius;
                        if ((value != null) && (value.trim().length() == 0)) {
                          radius = Loc8Task.RADIUS_DEFAULT;
                        } else {
                          radius = Float.parseFloat(value);
                        }

                        Loc8Location d =
                            new Loc8Location(
                                new GeoPoint(
                                    (int) ((tempLat) * 1000000), (int) ((tempLon) * 1000000)),
                                radius,
                                tempName);

                        locs.add(d);
                      }
                    });
                // if back button pressed then default radius is used
                alert.setOnCancelListener(
                    new OnCancelListener() {
                      @Override
                      public void onCancel(DialogInterface di) {
                        Loc8Location d =
                            new Loc8Location(
                                new GeoPoint(
                                    (int) ((tempLat) * 1000000), (int) ((tempLon) * 1000000)),
                                Loc8Task.RADIUS_DEFAULT,
                                tempName);
                        locs.add(d);
                      }
                    });
                alert.show();

                count_l++;
                found = locsCursor.getString(1);
              }
            }
          }

          if (count_l == 1) {
            tSelectLocationHeaderPref.setText(found);

          } else if (count_l > 1) {
            tSelectLocationHeaderPref.setText(count_l + " locations selected");
          } else {
            tSelectLocationHeaderPref.setText("");
            Log.d("locationSelectPreference", "Nothing found");
          }
          break;
        case PREF_PICK:
          // This thing is super hacky now that I've removed a bunch of stuff.
          //
          prefsArrayList = data.getIntegerArrayListExtra("prefs");
          ArrayList<String> blockNames = data.getStringArrayListExtra("blockNames");
          for (String n : blockNames) {
            /*if(n.equals("All callers")) {
            	blockList.add(Loc8ContactList.ALL);
            	break;
            }*/
            blockList.add(Loc8ContactList.getContactFromName(n));
          }
          prefs = prefsArrayList.toArray(new Integer[prefsArrayList.size()]);
          int prefsCount = 0;
          int prefIndex = -1;
          for (int i = 0; i < prefsArrayList.size(); i++) {
            if (prefsArrayList.get(i) == 1) {
              prefsCount++;
              prefIndex = i;
            }
          }
          if (prefsCount == 1) {
            switch (prefIndex) {
              case Loc8Preference.SILENT:
                tSetPreferencesHeader.setText("Set phone to silent");
                break;
              case Loc8Preference.ALARM:
                tSetPreferencesHeader.setText("Turn off alarms");
                break;
              case Loc8Preference.VIBRATE:
                tSetPreferencesHeader.setText("Set ringtone to vibrate");
                break;
              case Loc8Preference.NORMAL:
                tSetPreferencesHeader.setText("Normal ringtone");
                break;
            }
          } else if (prefsCount == 0) {
            prefsArrayList = null;
            tSetPreferencesHeader.setText("");
          } else {
            tSetPreferencesHeader.setText(String.valueOf(prefsCount) + " preferences selected");
          }
          break;
      }
    }
  }
  public ArrayList<Loc8Preference> constructAllPrefsFromDb() {
    ArrayList<Loc8Preference> prefList = new ArrayList<Loc8Preference>();

    DBAdapter mDbHelper = new DBAdapter(GlobalContext.getContext());
    mDbHelper.open();
    Cursor pc = mDbHelper.fetchAllPreferences();
    pc.moveToFirst();
    for (int i = 0; i < pc.getCount(); i++) {
      Integer[] prefs = {0, 0, 0, 0, 0, 0};
      Integer[] days = {0, 0, 0, 0, 0, 0, 0};
      String id;
      String name;
      List<List<Integer>> times = new ArrayList<List<Integer>>();
      List<Loc8Contact> blockList = new ArrayList<Loc8Contact>();
      List<Loc8Location> locs = new ArrayList<Loc8Location>();
      List<Integer> times1 = new ArrayList<Integer>();
      List<Integer> times2 = new ArrayList<Integer>();
      int enabledNum;
      boolean enabled = false;
      prefs[Loc8Preference.ALARM] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ALARM));
      prefs[Loc8Preference.BLOCK] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_BLOCKCALLS));
      prefs[Loc8Preference.NOTIFICATION] =
          pc.getInt(pc.getColumnIndex(DBAdapter.KEY_NOTIFICATIONS));
      prefs[Loc8Preference.SILENT] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_RINGER));
      prefs[Loc8Preference.SMS] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_TEXTMESSAGES));
      prefs[Loc8Preference.VIBRATE] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_VIBRATE));
      days[Loc8Preference.MONDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_MONDAY));
      days[Loc8Preference.TUESDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_TUESDAY));
      days[Loc8Preference.WEDNESDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_WEDNESDAY));
      days[Loc8Preference.THURSDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_THURSDAY));
      days[Loc8Preference.FRIDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_FRIDAY));
      days[Loc8Preference.SATURDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_SATURDAY));
      days[Loc8Preference.SUNDAY] = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_SUNDAY));
      enabledNum = pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENABLED));
      if (enabledNum != 0) {
        enabled = true;
      }
      times1.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_STARTYEAR)));
      times1.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_STARTMONTH)));
      times1.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_STARTDAY)));
      times1.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_STARTHOUR)));
      times1.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_STARTMINUTE)));
      times2.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENDYEAR)));
      times2.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENDMONTH)));
      times2.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENDDAY)));
      times2.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENDHOUR)));
      times2.add(pc.getInt(pc.getColumnIndex(DBAdapter.KEY_ENDMINUTE)));
      times.add(times1);
      times.add(times2);
      id = pc.getString(pc.getColumnIndex(DBAdapter.KEY_ROWID));
      name = pc.getString(pc.getColumnIndex(DBAdapter.KEY_NAME));
      Cursor c = mDbHelper.getPreferenceContacts(id);
      Cursor l = mDbHelper.getPreferenceLocations(id);
      for (l.moveToFirst(); !l.isAfterLast(); l.moveToNext()) {
        // These values are specific to preferences. Don't try to use them in another context!
        Loc8Location temp =
            new Loc8Location(
                new GeoPoint((int) (l.getDouble(32) * 1000000), (int) (l.getDouble(31) * 1000000)),
                l.getFloat(30),
                l.getString(29));
        locs.add(temp);
      }
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        String phoneNum = c.getString(c.getColumnIndex(DBAdapter.KEY_PHONENUMBER));
        String phoneNumNoDash = phoneNum.replace("-", "").replace("\\s", "");
        long phoneNumber = Long.parseLong(phoneNumNoDash);
        Loc8Contact temp = Loc8ContactList.getContactFromNumber(phoneNumber);
        blockList.add(temp);
      }
      prefList.add(new Loc8Preference(prefs, blockList, times, days, locs, name, id, enabled));
      pc.moveToNext();
    }
    mDbHelper.close();
    return prefList;
  }