Android Open Source - campus Search Suggestion Provider






From Project

Back to project page campus.

License

The source code is released under:

GNU General Public License

If you think the Android project campus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lecho.app.campus.provider;
/* w ww .j a va 2  s  . c o m*/
import java.util.Arrays;

import lecho.app.campus.dao.DaoSession;
import lecho.app.campus.dao.FacultyDao;
import lecho.app.campus.dao.PlaceDao;
import lecho.app.campus.dao.PlaceUnitDao;
import lecho.app.campus.dao.UnitDao;
import lecho.app.campus.utils.Config;
import lecho.app.campus.utils.DatabaseHelper;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

/**
 * Provider for seach suggestions on {@link CampusMapActivity}.
 * 
 * @author lecho
 * 
 */
public class SearchSuggestionProvider extends ContentProvider {
  public static final String AUTHORITY = Config.SEARCH_SUGGESTION_AUTHORITY;
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
  public static final String DIR_CONTENT_TYPE = "vnd.android.cursor.dir/vnd.lecho.app.campus";
  public static final int CAMPUS_MAP_DIR = 1;
  private static final String MAX_SUGGESTIONS = "10";

  // Search for places by symbol, name or description.
  private static final String QUERY_SEARCH_BY_PLACE = "select distinct P." + PlaceDao.Properties.Id.columnName
      + ", P." + PlaceDao.Properties.Symbol.columnName + ", P." + PlaceDao.Properties.Name.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_TEXT_1 + ", P." + PlaceDao.Properties.Description.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_TEXT_2 + ", P." + PlaceDao.Properties.Id.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID + " from " + PlaceDao.TABLENAME + " P where P."
      + PlaceDao.Properties.Symbol.columnName + " like ? or P." + PlaceDao.Properties.Name.columnName
      + " like ? or P." + PlaceDao.Properties.Description.columnName + " like ?";

  // Search for place by units names and short names.DISTINCT
  private static final String QUERY_SEARCH_BY_UNIT = "select distinct P." + PlaceDao.Properties.Id.columnName
      + ", P." + PlaceDao.Properties.Symbol.columnName + ", U." + UnitDao.Properties.Name.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_TEXT_1 + ", F." + FacultyDao.Properties.ShortName.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_TEXT_2 + ", P." + PlaceDao.Properties.Id.columnName + " as "
      + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID + " from " + PlaceDao.TABLENAME + " P left join "
      + PlaceUnitDao.TABLENAME + " PU on PU." + PlaceUnitDao.Properties.PlaceId.columnName + "=P."
      + PlaceDao.Properties.Id.columnName + " left join " + UnitDao.TABLENAME + " U on PU."
      + PlaceUnitDao.Properties.UnitId.columnName + "=U." + UnitDao.Properties.Id.columnName + " left join "
      + FacultyDao.TABLENAME + " F on F." + FacultyDao.Properties.Id.columnName + "=U."
      + UnitDao.Properties.FacultyId.columnName + " where U." + UnitDao.Properties.Name.columnName
      + " like ? or U." + UnitDao.Properties.ShortName.columnName + " like ?";

  private static final UriMatcher sUriMatcher;
  private DaoSession mDaoSession;

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    throw new UnsupportedOperationException("Not implemented");
  }

  @Override
  public String getType(Uri uri) {
    switch (sUriMatcher.match(uri)) {
    case CAMPUS_MAP_DIR:
      return DIR_CONTENT_TYPE;
    default:
      throw new IllegalArgumentException("Unknown URI: " + uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    throw new UnsupportedOperationException("Not implemented");
  }

  @Override
  public boolean onCreate() {
    mDaoSession = DatabaseHelper.getDaoSession(getContext());
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = mDaoSession.getDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String query = qb.buildUnionQuery(new String[] { QUERY_SEARCH_BY_PLACE, QUERY_SEARCH_BY_UNIT }, null,
        MAX_SUGGESTIONS);
    String arg = new StringBuilder("%").append(selectionArgs[0]).append("%").toString();
    String[] args = new String[5];
    Arrays.fill(args, arg);
    Cursor c = db.rawQuery(query, args);
    return c;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    throw new UnsupportedOperationException("Not implemented");
  }

  @Override
  public int bulkInsert(Uri uri, ContentValues[] values) {
    throw new UnsupportedOperationException("Not implemented");
  }

  static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(Config.SEARCH_SUGGESTION_AUTHORITY, "", CAMPUS_MAP_DIR);
  }

}




Java Source Code List

lecho.app.campus.activity.AboutAppActivity.java
lecho.app.campus.activity.CampusMapActivity.java
lecho.app.campus.activity.GalleryActivity.java
lecho.app.campus.activity.LegalInfoActivity.java
lecho.app.campus.activity.PlaceDetailsActivity.java
lecho.app.campus.adapter.GalleryPagesFragmentAdapter.java
lecho.app.campus.adapter.MarkerInfoWindowAdapter.java
lecho.app.campus.adapter.NavigationDrawerAdapter.java
lecho.app.campus.adapter.PlaceDetailsFragmentAdapter.java
lecho.app.campus.adapter.SearchResultViewAdapter.java
lecho.app.campus.adapter.SearchSuggestionAdapter.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Main.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.fragment.GalleryPageFragment.java
lecho.app.campus.fragment.PlaceDetailsFragment.java
lecho.app.campus.fragment.dialog.NoInternetConnectionDialogFragment.java
lecho.app.campus.fragment.dialog.PlayServicesErrorDialogFragment.java
lecho.app.campus.loader.PlaceDetailsLoader.java
lecho.app.campus.loader.PlacesLoader.java
lecho.app.campus.provider.SearchSuggestionProvider.java
lecho.app.campus.service.PopulateDBIntentService.java
lecho.app.campus.utils.ABSMenuItemConverter.java
lecho.app.campus.utils.BitmapAsyncTask.java
lecho.app.campus.utils.Config.java
lecho.app.campus.utils.DataParser.java
lecho.app.campus.utils.DatabaseHelper.java
lecho.app.campus.utils.ImagesDirAsyncTask.java
lecho.app.campus.utils.NavigationDrawerItem.java
lecho.app.campus.utils.PlaceDetails.java
lecho.app.campus.utils.PlacesList.java
lecho.app.campus.utils.UnitsGroup.java
lecho.app.campus.utils.Utils.java
lecho.app.campus.view.CheckableDrawerItem.java
lecho.app.campus.view.UnitsGroupLayout.java
lecho.app.campus.view.ZoomImageView.java
lecho.app.campus.view.ZoomViewPager.java