@Override
  public void onEdit(Exercise exercise, int position) {
    if (position == -1) {
      for (Exercise e : workout.getExercises()) {
        String name = e.getName();
        if (name.equals(exercise.getName())) {
          Toast toast = Toast.makeText(this, "Exercise already exists!", Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.CENTER, 0, 0);
          toast.show();
          return;
        }
      }
    }
    ArrayList<Integer> positions = adapter.getCursorPositions();
    MatrixCursor newcursor = new MatrixCursor(new String[] {"_id", "name"});
    // int lastCursorPosition = cursor.getPosition();
    // copy over old cursor items to the new one.
    if (cursor != null) {
      cursor.moveToFirst();
      for (int i = 0; i < positions.size(); i++) {
        int cursorPosition = positions.get(i);
        int listPosition = adapter.getListPosition(positions.get(i));
        // dont add to new cursor if item has been removed.
        if (listPosition == DragSortCursorAdapter.REMOVED) continue;
        cursor.moveToPosition(cursorPosition);
        String c = cursor.getString(1);
        // if its not a new exercise, check to see if the current cursor list position mapping
        // matches the one we are working with. If yes, set c to be the new exercise.
        if (position >= 0) {
          if (position == listPosition) {
            c = exercise.getName();
          }
        }
        newcursor.newRow().add(listPosition).add(c);
      }
    }
    // add the new row
    if (position < 0 && exercise != null) {
      newcursor.newRow().add(newcursor.getCount()).add(exercise.getName());
    }

    ArrayList<Exercise> newExercises = new ArrayList<Exercise>();
    newcursor.moveToFirst();
    for (int i = 0; i < newcursor.getCount(); i++) {
      String ename = newcursor.getString(1);
      Exercise e = workout.getExercise(ename);
      if (e != null) newExercises.add(e);
      else if (i == newcursor.getCount() - 1) newExercises.add(exercise);
      if (!newcursor.moveToNext()) break;
    }

    adapter.changeCursor(newcursor);
    cursor = newcursor;

    // modify workout to reflect the change
    workout.setExercises(newExercises);
  }
  @Override
  public Cursor querySearchDocuments(String rootId, String query, String[] projection)
      throws FileNotFoundException {
    final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));

    final File parent;
    synchronized (mRootsLock) {
      parent = mRoots.get(rootId).path;
    }

    final LinkedList<File> pending = new LinkedList<File>();
    pending.add(parent);
    while (!pending.isEmpty() && result.getCount() < 24) {
      final File file = pending.removeFirst();
      if (file.isDirectory()) {
        for (File child : file.listFiles()) {
          pending.add(child);
        }
      }
      if (file.getName().toLowerCase().contains(query)) {
        includeFile(result, null, file);
      }
    }
    return result;
  }
Example #3
0
 @Override
 public int getItemCount() {
   if (mCursor != null) {
     return mCursor.getCount();
   }
   return 0;
 }
 private void createChallengesCursor(String[] response) {
   Log.d(TAG, "creating challenges array");
   Challenge[] challengeArr = ChallengeHelper.parseChallengeList(response, CHALLENGE_OPEN);
   // _id is CHALLENGE_ID_COL
   String[] columnNames = {
     "_id", CHALLENGE_SOURCE_ID_COL, CHALLENGE_SOURCE_HANDLE_COL, CHALLENGE_TARGET_COL
   };
   challenges = new MatrixCursor(columnNames, challengeArr.length);
   MatrixCursor.RowBuilder builder;
   Log.d(TAG, "creating challenges cursor");
   for (Challenge chal : challengeArr) {
     builder = challenges.newRow();
     builder.add(chal.challengeId());
     builder.add(chal.sourceId());
     builder.add(chal.sourceHandle());
     builder.add(chal.targetId());
   }
   Log.d(TAG, "challenges cursor created with " + challenges.getCount() + " row(s)");
 }
  @Override
  public Cursor queryRecentDocuments(String rootId, String[] projection)
      throws FileNotFoundException {
    final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));

    // Delegate to real provider
    final long token = Binder.clearCallingIdentity();
    Cursor cursor = null;
    try {
      cursor =
          mDm.query(
              new DownloadManager.Query()
                  .setOnlyIncludeVisibleInDownloadsUi(true)
                  .setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL));
      copyNotificationUri(result, cursor);
      while (cursor.moveToNext() && result.getCount() < 12) {
        final String mimeType =
            cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
        final String uri =
            cursor.getString(
                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIAPROVIDER_URI));

        // Skip images that have been inserted into the MediaStore so we
        // don't duplicate them in the recents list.
        if (mimeType == null || (mimeType.startsWith("image/") && !TextUtils.isEmpty(uri))) {
          continue;
        }

        includeTransferFromCursor(result, cursor);
      }
    } finally {
      IoUtils.closeQuietly(cursor);
      Binder.restoreCallingIdentity(token);
    }
    return result;
  }
  @Override
  public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
    // setNotificationUri call aren't really researched

    String limitStr = uri.getQueryParameter("limit");
    int limit = limitStr == null ? Integer.MAX_VALUE : Integer.parseInt(limitStr);

    // we create new matrix cursor and try to fill it
    // projection_all is too far away. better create it over here maybe
    MatrixCursor cursor = new MatrixCursor(Words.PROJECTION_ALL);
    int code = URI_MATCHER.match(uri);
    switch (URI_MATCHER.match(uri)) {
      case WORD:
        String word = uri.getQueryParameter("word");
        // look for position of the word
        if (word != null) {
          int position = Collections.binarySearch(words, word, String.CASE_INSENSITIVE_ORDER);
          if (position >= 0) {
            addWord(cursor, position);
          }
          break;
        }
        String id = uri.getQueryParameter("id");
        if (id != null) {
          int position = Integer.parseInt(id);
          addWord(cursor, position);
          break;
        }
        for (int i = 0; i < words.size() && cursor.getCount() < limit; ++i) {
          addWord(cursor, i);
        }
        cursor.setNotificationUri(getContext().getContentResolver(), Words.CONTENT_URI);
        break;
      case WORD_SUGGESTIONS_FREQUENT:
        // have to think how to implement it
        fillWordSuggestionsFrequent(cursor);
        break;
      case WORD_SUGGESTIONS:
        s = uri.getLastPathSegment();
        if (s.length() < 4) {
          fillWordSuggestionsFrequent(cursor);
          break;
        }

        int start = Collections.binarySearch(words, s, String.CASE_INSENSITIVE_ORDER);
        if (start < 0) {
          start = -(start + 1);
        }
        int i = start;
        while (i < words.size()
            && words.get(i).toLowerCase().startsWith(s)
            && cursor.getCount() < limit) {
          addWord(cursor, i);
          ++i;
        }
        cursor.setNotificationUri(getContext().getContentResolver(), Words.CONTENT_URI);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    return cursor;
  }