Java Code Examples for com.j256.ormlite.android.AndroidDatabaseResults

The following examples show how to use com.j256.ormlite.android.AndroidDatabaseResults. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 6 votes vote down vote up
private Cursor makePeopleNameCursor(CharSequence name) throws SQLException {
    if (name == null) {
        name = "";
    }
    return ((AndroidDatabaseResults) app
            .getDao(Person.class)
            .queryRaw(
                    "SELECT rowid _id, * FROM people WHERE "
                            + Person.ISBOT_FIELD + " = 0 AND "
                            + Person.ISACTIVE_FIELD + " = 1 AND "
                            + Person.NAME_FIELD
                            + " LIKE ? ESCAPE '\\' ORDER BY "
                            + Person.NAME_FIELD + " COLLATE NOCASE",
                    DatabaseHelper.likeEscape(name.toString()) + "%")
            .closeableIterator().getRawResults()).getRawCursor();
}
 
Example 2
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a cursor to get the streams saved in the database
 *
 * @param streamName Filter out streams name containing this string
 */
private Cursor makeStreamCursor(CharSequence streamName)
        throws SQLException {
    if (streamName == null) {
        streamName = "";
    }

    return ((AndroidDatabaseResults) app
            .getDao(Stream.class)
            .queryRaw(
                    "SELECT rowid _id, * FROM streams WHERE "
                            + Stream.SUBSCRIBED_FIELD + " = 1 AND "
                            + Stream.NAME_FIELD
                            + " LIKE ? ESCAPE '\\' ORDER BY "
                            + Stream.NAME_FIELD + " COLLATE NOCASE",
                    DatabaseHelper.likeEscape(streamName.toString()) + "%")
            .closeableIterator().getRawResults()).getRawCursor();
}
 
Example 3
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a cursor to get the topics in the stream in
 *
 * @param stream  from which topics similar to {@param subject} are selected
 * @param subject Filter out subject containing this string
 */
private Cursor makeSubjectCursor(CharSequence stream, CharSequence subject)
        throws SQLException {
    if (subject == null) {
        subject = "";
    }
    if (stream == null) {
        stream = "";
    }

    AndroidDatabaseResults results = (AndroidDatabaseResults) app
            .getDao(Message.class)
            .queryRaw(
                    "SELECT DISTINCT "
                            + Message.SUBJECT_FIELD
                            + ", 1 AS _id FROM messages JOIN streams ON streams."
                            + Stream.ID_FIELD + " = messages."
                            + Message.STREAM_FIELD + " WHERE "
                            + Message.SUBJECT_FIELD
                            + " LIKE ? ESCAPE '\\' AND "
                            + Stream.NAME_FIELD + " = ? ORDER BY "
                            + Message.SUBJECT_FIELD + " COLLATE NOCASE",
                    DatabaseHelper.likeEscape(subject.toString()) + "%",
                    stream.toString()).closeableIterator().getRawResults();
    return results.getRawCursor();
}
 
Example 4
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a cursor to get the E-Mails stored in the database
 *
 * @param email Filter out emails containing this string
 */
private Cursor makePeopleCursor(CharSequence email) throws SQLException {
    if (email == null) {
        email = "";
    }
    String[] pieces = TextUtils.split(email.toString(), ",");
    String piece;
    if (pieces.length == 0) {
        piece = "";
    } else {
        piece = pieces[pieces.length - 1].trim();
    }
    return ((AndroidDatabaseResults) app
            .getDao(Person.class)
            .queryRaw(
                    "SELECT rowid _id, * FROM people WHERE "
                            + Person.ISBOT_FIELD + " = 0 AND "
                            + Person.ISACTIVE_FIELD + " = 1 AND "
                            + Person.EMAIL_FIELD
                            + " LIKE ? ESCAPE '\\' ORDER BY "
                            + Person.NAME_FIELD + " COLLATE NOCASE",
                    DatabaseHelper.likeEscape(piece) + "%")
            .closeableIterator().getRawResults()).getRawCursor();
}
 
Example 5
Source Project: mage-android   Source File: ObservationFeedFragment.java    License: Apache License 2.0 6 votes vote down vote up
private Cursor obtainCursor(PreparedQuery<Observation> query, Dao<Observation, Long> oDao) throws SQLException {
	Cursor c = null;
	CloseableIterator<Observation> iterator = oDao.iterator(query);

	// get the raw results which can be cast under Android
	AndroidDatabaseResults results = (AndroidDatabaseResults) iterator.getRawResults();
	c = results.getRawCursor();
	if (c.moveToLast()) {
		long oldestTime = c.getLong(c.getColumnIndex("last_modified"));
		Log.i(LOG_NAME, "last modified is: " + c.getLong(c.getColumnIndex("last_modified")));
		Log.i(LOG_NAME, "querying again in: " + (oldestTime - requeryTime)/60000 + " minutes");
		if (queryUpdateHandle != null) {
			queryUpdateHandle.cancel(true);
		}
		queryUpdateHandle = scheduler.schedule(new Runnable() {
			public void run() {
				updateFilter();
			}
		}, oldestTime - requeryTime, TimeUnit.MILLISECONDS);
		c.moveToFirst();
	}
	return c;
}
 
Example 6
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 5 votes vote down vote up
private Callable<Cursor> getSteamCursorGenerator() {

        Callable<Cursor> steamCursorGenerator = new Callable<Cursor>() {
            @Override
            public Cursor call() throws Exception {
                String query = "SELECT s.id as _id,  s.name, s.color"
                        + " FROM streams as s LEFT JOIN messages as m ON s.id=m.stream ";
                String selectArg = null;
                if (!etSearchStream.getText().toString().equals("") && !etSearchStream.getText().toString().isEmpty()) {
                    //append where clause
                    selectArg = etSearchStream.getText().toString() + '%';
                    query += " WHERE s.name LIKE ? " + " and s." + Stream.SUBSCRIBED_FIELD + " = " + "1 ";
                    //set visibility of this image false
                    ivSearchStreamCancel.setVisibility(View.VISIBLE);
                } else {
                    //set visibility of this image false
                    query += " WHERE s." + Stream.SUBSCRIBED_FIELD + " = " + "1 ";
                    ivSearchStreamCancel.setVisibility(View.GONE);
                }
                //append group by
                query += " group by s.name order by s.name COLLATE NOCASE";

                if (selectArg != null) {
                    return ((AndroidDatabaseResults) app.getDao(Stream.class).queryRaw(query, selectArg).closeableIterator().getRawResults()).getRawCursor();
                } else {
                    return ((AndroidDatabaseResults) app.getDao(Stream.class).queryRaw(query).closeableIterator().getRawResults()).getRawCursor();
                }
            }
        };
        return steamCursorGenerator;
    }
 
Example 7
Source Project: zulip-android   Source File: ZulipActivity.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a cursor for the combinedAdapter used to suggest Emoji when ':' is typed in the {@link #messageEt}
 *
 * @param emoji A string to search in the existing database
 */
private Cursor makeEmojiCursor(CharSequence emoji)
        throws SQLException {
    if (emoji == null) {
        emoji = "";
    }
    return ((AndroidDatabaseResults) app
            .getDao(Emoji.class)
            .queryRaw("SELECT  rowid _id,name FROM emoji WHERE name LIKE '%" + emoji + "%'")
            .closeableIterator().getRawResults()).getRawCursor();
}
 
Example 8
Source Project: mage-android   Source File: PeopleFeedFragment.java    License: Apache License 2.0 5 votes vote down vote up
/**
    * Zoom to map.
    * 
    */
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
	HeaderViewListAdapter headerAdapter = (HeaderViewListAdapter)adapter.getAdapter();
	Cursor c = ((PeopleCursorAdapter) headerAdapter.getWrappedAdapter()).getCursor();
	c.moveToPosition(position);
	try {
		Location l = query.mapRow(new AndroidDatabaseResults(c, null, false));
		Intent profileView = new Intent(context, ProfileActivity.class);
		profileView.putExtra(ProfileActivity.USER_ID, l.getUser().getRemoteId());
		getActivity().startActivity(profileView);
	} catch (Exception e) {
		Log.e(LOG_NAME, "Problem.", e);
	}
}
 
Example 9
Source Project: Man-Man   Source File: OrmLiteCursorAdapter.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public T getItem(int position) {
    try {
        Cursor cur = getRawCursor();
        cur.moveToPosition(position);
        return mQuery.mapRow(new AndroidDatabaseResults(cur, mCursor.getRawResults().getObjectCacheForRetrieve(), false));
    } catch (SQLException e) {
        return null;
    }
}
 
Example 10
Source Project: mage-android   Source File: PeopleCursorAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void bindView(View v, final Context context, Cursor cursor) {
	try {
		Location location = query.mapRow(new AndroidDatabaseResults(cursor, null, false));
		User user = location.getUser();
		if (user == null) {
			return;
		}

		ImageView avatarView = v.findViewById(R.id.avatarImageView);
		Drawable defaultPersonIcon = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_person_white_24dp));
		DrawableCompat.setTint(defaultPersonIcon, ContextCompat.getColor(context, R.color.icon));
		DrawableCompat.setTintMode(defaultPersonIcon, PorterDuff.Mode.SRC_ATOP);
		avatarView.setImageDrawable(defaultPersonIcon);

		GlideApp.with(context)
				.load(Avatar.Companion.forUser(user))
				.fallback(defaultPersonIcon)
				.error(defaultPersonIcon)
				.circleCrop()
				.into(avatarView);

		final ImageView iconView = v.findViewById(R.id.iconImageView);
		GlideApp.with(context)
				.load(user.getUserLocal().getLocalIconPath())
				.centerCrop()
				.into(iconView);

		TextView name = v.findViewById(R.id.name);
		name.setText(user.getDisplayName());

		TextView date = v.findViewById(R.id.date);
		String timeText = new PrettyTime().format(location.getTimestamp());
		date.setText(timeText);

		Collection<Team> userTeams = teamHelper.getTeamsByUser(user);
		userTeams.retainAll(eventTeams);
		Collection<String> teamNames = Collections2.transform(userTeams, new Function<Team, String>() {
			@Override
			public String apply(Team team) {
				return team.getName();
			}
		});

		TextView teamsView = v.findViewById(R.id.teams);
		teamsView.setText(StringUtils.join(teamNames, ", "));

	} catch (SQLException sqle) {
		Log.e(LOG_NAME, "Could not set location view information.", sqle);
	}
}
 
Example 11
Source Project: mage-android   Source File: ObservationListAdapter.java    License: Apache License 2.0 4 votes vote down vote up
private void bindObservationViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (cursor == null) return;

    cursor.moveToPosition(position);
    ObservationViewHolder vh = (ObservationViewHolder) holder;

    try {
        final Observation observation = query.mapRow(new AndroidDatabaseResults(cursor, null, false));
        vh.bind(observation, observationActionListener);

        Drawable markerPlaceholder = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_place_white_48dp));
        DrawableCompat.setTint(markerPlaceholder, ContextCompat.getColor(context, R.color.icon));
        DrawableCompat.setTintMode(markerPlaceholder, PorterDuff.Mode.SRC_IN);

        vh.markerView.setImageDrawable(markerPlaceholder);
        vh.iconTask = new IconTask(vh.markerView);
        vh.iconTask.execute(observation);

        vh.primaryView.setText("");
        vh.primaryPropertyTask = new PropertyTask(PropertyTask.Type.PRIMARY, vh.primaryView);
        vh.primaryPropertyTask.execute(observation);

        vh.secondaryView.setText("");
        vh.secondaryPropertyTask = new PropertyTask(PropertyTask.Type.SECONDARY, vh.secondaryView);
        vh.secondaryPropertyTask.execute(observation);

        Date timestamp = observation.getTimestamp();
        String pattern = DateUtils.isToday(timestamp.getTime()) ? SHORT_TIME_PATTERN : SHORT_DATE_PATTERN;
        DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        vh.timeView.setText(dateFormat.format(timestamp));

        vh.userView.setText("");
        vh.userTask = new UserTask(vh.userView);
        vh.userTask.execute(observation);

        vh.importantButton.setOnClickListener(v -> observationActionListener.onObservationImportant(observation));
        setImportantView(observation.getImportant(), vh);

        ObservationError error = observation.getError();
        if (error != null) {
            boolean hasValidationError = error.getStatusCode() != null;
            vh.syncBadge.setVisibility(hasValidationError ? View.GONE : View.VISIBLE);
            vh.errorBadge.setVisibility(hasValidationError ? View.VISIBLE : View.GONE);
        } else {
            vh.syncBadge.setVisibility(View.GONE);
            vh.errorBadge.setVisibility(View.GONE);
        }

        vh.attachmentLayout.removeAllViews();
        if (observation.getAttachments().size() == 0) {
            vh.attachmentLayout.setVisibility(View.GONE);
        } else {
            vh.attachmentLayout.setVisibility(View.VISIBLE);
            attachmentGallery.addAttachments(vh.attachmentLayout, observation.getAttachments());
        }

        vh.favoriteButton.setOnClickListener(v -> toggleFavorite(observation, vh));
        setFavoriteImage(observation.getFavorites(), vh, isFavorite(observation));

        vh.directionsButton.setOnClickListener(v -> getDirections(observation));
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 12
Source Project: Man-Man   Source File: OrmLiteCursorAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public int getCount() {
    return ((AndroidDatabaseResults) mCursor.getRawResults()).getCount();
}
 
Example 13
Source Project: Man-Man   Source File: OrmLiteCursorAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
public Cursor getRawCursor() {
    return ((AndroidDatabaseResults) mCursor.getRawResults()).getRawCursor();
}
 
Example 14
Source Project: ormlite-android   Source File: OrmLiteCursorAdapter.java    License: ISC License 4 votes vote down vote up
/**
 * Map a single row to our cursor object.
 */
protected T cursorToObject(Cursor cursor) throws SQLException {
	return preparedQuery.mapRow(new AndroidDatabaseResults(cursor, null, true));
}