public abstract class SmootherModelAdaptor implements SensorListener {

  private static final String TAG = MiscUtil.getTag(SmootherModelAdaptor.class);

  public void shutDown() {
    Log.d(TAG, this + " shutting down.");
    setLive(false);
  }

  public void start() {
    setLive(true);
  }

  protected synchronized void setLive(boolean b) {
    live = b;
  }

  protected synchronized boolean isLive() {
    return live;
  }

  private boolean live;

  @Override
  public void onAccuracyChanged(int sensor, int accuracy) {
    // Do nothing
  }
}
/** Provides search suggestions for a list of words and their definitions. */
public class SearchTermsProvider extends ContentProvider {
  public static class SearchTerm {
    public String origin;
    public String query;

    public SearchTerm(String query, String origin) {
      this.query = query;
      this.origin = origin;
    }
  }

  private static final String TAG = MiscUtil.getTag(SearchTermsProvider.class);
  public static String AUTHORITY = "com.yoidles.android.bronze.searchterms";
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
  private static final int SEARCH_SUGGEST = 0;
  private static final UriMatcher uriMatcher = buildUriMatcher();
  private LayerManager layerManager;

  /** The columns we'll include in our search suggestions. */
  private static final String[] COLUMNS = {
    "_id", // must include this column
    SearchManager.SUGGEST_COLUMN_QUERY,
    SearchManager.SUGGEST_COLUMN_TEXT_1,
    SearchManager.SUGGEST_COLUMN_TEXT_2
  };

  /** Sets up a uri matcher for search suggestion and shortcut refresh queries. */
  private static UriMatcher buildUriMatcher() {
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
    return matcher;
  }

  @Override
  public boolean onCreate() {
    Context context = getContext();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    layerManager =
        BronzeConstellationApplication.getLayerManager(
            context.getAssets(), preferences, context.getResources(), context);
    return true;
  }

  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Log.d(TAG, "Got query for " + uri);
    if (!TextUtils.isEmpty(selection)) {
      throw new IllegalArgumentException("selection not allowed for " + uri);
    }
    if (selectionArgs != null && selectionArgs.length != 0) {
      throw new IllegalArgumentException("selectionArgs not allowed for " + uri);
    }
    if (!TextUtils.isEmpty(sortOrder)) {
      throw new IllegalArgumentException("sortOrder not allowed for " + uri);
    }
    switch (uriMatcher.match(uri)) {
      case SEARCH_SUGGEST:
        String query = null;
        if (uri.getPathSegments().size() > 1) {
          query = uri.getLastPathSegment();
        }
        Log.d(TAG, "Got suggestions query for " + query);
        return getSuggestions(query);
      default:
        throw new IllegalArgumentException("Unknown URL " + uri);
    }
  }

  private Cursor getSuggestions(String query) {
    MatrixCursor cursor = new MatrixCursor(COLUMNS);
    if (query == null) {
      return cursor;
    }
    Set<SearchTerm> results = layerManager.getObjectNamesMatchingPrefix(query);
    Log.d("SearchTermsProvider", "Got results n=" + results.size());
    for (SearchTerm result : results) {
      cursor.addRow(columnValuesOfSuggestion(result));
    }
    return cursor;
  }

  static int s = 0;

  private Object[] columnValuesOfSuggestion(SearchTerm suggestion) {
    return new String[] {
      Integer.toString(s++), // _id
      suggestion.query, // query
      suggestion.query, // text1
      suggestion.origin, // text2
    };
  }

  /** All queries for this provider are for the search suggestion mime type. */
  @Override
  public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
      case SEARCH_SUGGEST:
        return SearchManager.SUGGEST_MIME_TYPE;
      default:
        throw new IllegalArgumentException("Unknown URL " + uri);
    }
  }

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

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

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