|
34 | 34 | import android.support.v4.view.GravityCompat;
|
35 | 35 | import android.support.v4.widget.DrawerLayout;
|
36 | 36 | import android.support.v4.widget.SimpleCursorAdapter;
|
| 37 | +import android.text.TextUtils; |
37 | 38 | import android.util.Log;
|
38 | 39 | import android.view.Menu;
|
39 | 40 | import android.view.MenuInflater;
|
|
43 | 44 | import android.widget.AdapterView.OnItemClickListener;
|
44 | 45 | import android.widget.AutoCompleteTextView;
|
45 | 46 | import android.widget.EditText;
|
| 47 | +import android.widget.FilterQueryProvider; |
46 | 48 | import android.widget.ImageView;
|
47 | 49 | import android.widget.ListView;
|
48 | 50 | import android.widget.SearchView;
|
49 | 51 | import android.widget.TextView;
|
50 | 52 |
|
51 | 53 | import com.j256.ormlite.android.AndroidDatabaseResults;
|
| 54 | +import com.zulip.android.database.DatabaseHelper; |
52 | 55 | import com.zulip.android.models.Message;
|
53 | 56 | import com.zulip.android.models.MessageType;
|
54 | 57 | import com.zulip.android.filters.NarrowFilter;
|
@@ -109,6 +112,9 @@ public class ZulipActivity extends FragmentActivity implements
|
109 | 112 | private ImageView sendBtn;
|
110 | 113 | private ImageView togglePrivateStreamBtn;
|
111 | 114 | Notifications notifications;
|
| 115 | + SimpleCursorAdapter streamActvAdapter; |
| 116 | + SimpleCursorAdapter subjectActvAdapter; |
| 117 | + SimpleCursorAdapter emailActvAdapter; |
112 | 118 |
|
113 | 119 | private BroadcastReceiver onGcmMessage = new BroadcastReceiver() {
|
114 | 120 | public void onReceive(Context contenxt, Intent intent) {
|
@@ -423,6 +429,157 @@ public void onClick(View v) {
|
423 | 429 | switchView();
|
424 | 430 | }
|
425 | 431 | });
|
| 432 | + setUpAdapter(); |
| 433 | + streamActv.setAdapter(streamActvAdapter); |
| 434 | + topicActv.setAdapter(subjectActvAdapter); |
| 435 | + |
| 436 | + public void setUpAdapter() { |
| 437 | + streamActvAdapter = new SimpleCursorAdapter( |
| 438 | + that, R.layout.stream_tile, null, |
| 439 | + new String[]{Stream.NAME_FIELD}, |
| 440 | + new int[]{R.id.name}, 0); |
| 441 | + streamActvAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { |
| 442 | + @Override |
| 443 | + public CharSequence convertToString(Cursor cursor) { |
| 444 | + int index = cursor.getColumnIndex(Stream.NAME_FIELD); |
| 445 | + return cursor.getString(index); |
| 446 | + } |
| 447 | + }); |
| 448 | + streamActvAdapter.setFilterQueryProvider(new FilterQueryProvider() { |
| 449 | + @Override |
| 450 | + public Cursor runQuery(CharSequence charSequence) { |
| 451 | + try { |
| 452 | + return makeStreamCursor(charSequence); |
| 453 | + } catch (SQLException e) { |
| 454 | + Log.e("SQLException", "SQL not correct", e); |
| 455 | + return null; |
| 456 | + } |
| 457 | + } |
| 458 | + }); |
| 459 | + subjectActvAdapter = new SimpleCursorAdapter( |
| 460 | + that, R.layout.stream_tile, null, |
| 461 | + new String[]{Message.SUBJECT_FIELD}, |
| 462 | + new int[]{R.id.name}, 0); |
| 463 | + subjectActvAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { |
| 464 | + @Override |
| 465 | + public CharSequence convertToString(Cursor cursor) { |
| 466 | + int index = cursor.getColumnIndex(Message.SUBJECT_FIELD); |
| 467 | + return cursor.getString(index); |
| 468 | + } |
| 469 | + }); |
| 470 | + subjectActvAdapter.setFilterQueryProvider(new FilterQueryProvider() { |
| 471 | + @Override |
| 472 | + public Cursor runQuery(CharSequence charSequence) { |
| 473 | + try { |
| 474 | + return makeSubjectCursor(streamActv.getText().toString(), charSequence); |
| 475 | + } catch (SQLException e) { |
| 476 | + Log.e("SQLException", "SQL not correct", e); |
| 477 | + return null; |
| 478 | + } |
| 479 | + } |
| 480 | + }); |
| 481 | + |
| 482 | + emailActvAdapter = new SimpleCursorAdapter( |
| 483 | + that, R.layout.stream_tile, null, |
| 484 | + new String[]{Person.EMAIL_FIELD}, |
| 485 | + new int[]{R.id.name}, 0); |
| 486 | + emailActvAdapter |
| 487 | + .setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { |
| 488 | + @Override |
| 489 | + public CharSequence convertToString(Cursor cursor) { |
| 490 | + String text = topicActv.getText().toString(); |
| 491 | + String prefix; |
| 492 | + int lastIndex = text.lastIndexOf(","); |
| 493 | + if (lastIndex != -1) { |
| 494 | + prefix = text.substring(0, lastIndex + 1); |
| 495 | + } else { |
| 496 | + prefix = ""; |
| 497 | + } |
| 498 | + int index = cursor.getColumnIndex(Person.EMAIL_FIELD); |
| 499 | + return prefix + cursor.getString(index); |
| 500 | + } |
| 501 | + }); |
| 502 | + emailActvAdapter.setFilterQueryProvider(new FilterQueryProvider() { |
| 503 | + @Override |
| 504 | + public Cursor runQuery(CharSequence charSequence) { |
| 505 | + try { |
| 506 | + return makePeopleCursor(charSequence); |
| 507 | + } catch (SQLException e) { |
| 508 | + Log.e("SQLException", "SQL not correct", e); |
| 509 | + return null; |
| 510 | + } |
| 511 | + } |
| 512 | + }); |
| 513 | + |
| 514 | + } |
| 515 | + |
| 516 | + private Cursor makeStreamCursor(CharSequence streamName) |
| 517 | + throws SQLException { |
| 518 | + if (streamName == null) { |
| 519 | + streamName = ""; |
| 520 | + } |
| 521 | + |
| 522 | + return ((AndroidDatabaseResults) app |
| 523 | + .getDao(Stream.class) |
| 524 | + .queryRaw( |
| 525 | + "SELECT rowid _id, * FROM streams WHERE " |
| 526 | + + Stream.SUBSCRIBED_FIELD + " = 1 AND " |
| 527 | + + Stream.NAME_FIELD |
| 528 | + + " LIKE ? ESCAPE '\\' ORDER BY " |
| 529 | + + Stream.NAME_FIELD + " COLLATE NOCASE", |
| 530 | + DatabaseHelper.likeEscape(streamName.toString()) + "%") |
| 531 | + .closeableIterator().getRawResults()).getRawCursor(); |
| 532 | + } |
| 533 | + |
| 534 | + private Cursor makeSubjectCursor(CharSequence stream, CharSequence subject) |
| 535 | + throws SQLException { |
| 536 | + if (subject == null) { |
| 537 | + subject = ""; |
| 538 | + } |
| 539 | + if (stream == null) { |
| 540 | + stream = ""; |
| 541 | + } |
| 542 | + |
| 543 | + AndroidDatabaseResults results = (AndroidDatabaseResults) app |
| 544 | + .getDao(Message.class) |
| 545 | + .queryRaw( |
| 546 | + "SELECT DISTINCT " |
| 547 | + + Message.SUBJECT_FIELD |
| 548 | + + ", 1 AS _id FROM messages JOIN streams ON streams." |
| 549 | + + Stream.ID_FIELD + " = messages." |
| 550 | + + Message.STREAM_FIELD + " WHERE " |
| 551 | + + Message.SUBJECT_FIELD |
| 552 | + + " LIKE ? ESCAPE '\\' AND " |
| 553 | + + Stream.NAME_FIELD + " = ? ORDER BY " |
| 554 | + + Message.SUBJECT_FIELD + " COLLATE NOCASE", |
| 555 | + DatabaseHelper.likeEscape(subject.toString()) + "%", |
| 556 | + stream.toString()).closeableIterator().getRawResults(); |
| 557 | + return results.getRawCursor(); |
| 558 | + } |
| 559 | + |
| 560 | + private Cursor makePeopleCursor(CharSequence email) throws SQLException { |
| 561 | + if (email == null) { |
| 562 | + email = ""; |
| 563 | + } |
| 564 | + String[] pieces = TextUtils.split(email.toString(), ","); |
| 565 | + String piece; |
| 566 | + if (pieces.length == 0) { |
| 567 | + piece = ""; |
| 568 | + } else { |
| 569 | + piece = pieces[pieces.length - 1].trim(); |
| 570 | + } |
| 571 | + return ((AndroidDatabaseResults) app |
| 572 | + .getDao(Person.class) |
| 573 | + .queryRaw( |
| 574 | + "SELECT rowid _id, * FROM people WHERE " |
| 575 | + + Person.ISBOT_FIELD + " = 0 AND " |
| 576 | + + Person.ISACTIVE_FIELD + " = 1 AND " |
| 577 | + + Person.EMAIL_FIELD |
| 578 | + + " LIKE ? ESCAPE '\\' ORDER BY " |
| 579 | + + Person.NAME_FIELD + " COLLATE NOCASE", |
| 580 | + DatabaseHelper.likeEscape(piece) + "%") |
| 581 | + .closeableIterator().getRawResults()).getRawCursor(); |
| 582 | + } |
426 | 583 | public void switchView() {
|
427 | 584 | if (isCurrentModeStream()) { //Person
|
428 | 585 | togglePrivateStreamBtn.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_bullhorn));
|
|
0 commit comments