Back to project page android-memento.
The source code is released under:
Apache License
If you think the Android project android-memento listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * android-memento-lib https://github.com/twofortyfouram/android-memento * Copyright 2014 two forty four a.m. LLC */*from w ww .ja va 2 s . com*/ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package com.twofortyfouram.memento.debug.provider; import com.twofortyfouram.assertion.Assertions; import com.twofortyfouram.log.Lumberjack; import com.twofortyfouram.memento.provider.ImmutableUriMatcher; import com.twofortyfouram.memento.provider.sqlite.SqliteUriMatch; import com.twofortyfouram.memento.provider.sqlite.SqliteUriMatcher; import com.twofortyfouram.spackle.util.AndroidSdkVersion; import com.twofortyfouram.spackle.util.ContextUtil; import net.jcip.annotations.Immutable; import android.annotation.SuppressLint; import android.content.Context; import android.content.UriMatcher; import android.net.Uri; import android.os.Build; import android.support.annotation.NonNull; import android.util.ArrayMap; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; /** * Matcher for the Content Provider. */ @Immutable public final class SqliteUriMatcherImpl implements SqliteUriMatcher { private static final int MATCH_TABLE_ONE_DIR = 0; private static final int MATCH_TABLE_ONE_ITEM = 1; @NonNull private final ImmutableUriMatcher mUriMatcher; @NonNull private final SqliteUriMatch mTableOneDirMatch; @NonNull private final SqliteUriMatch mTableOneItemMatch; /** * Construct a new matcher. */ public SqliteUriMatcherImpl(@NonNull final Context context) { Assertions.assertNotNull(context, "context"); //$NON-NLS-1$ final Context ctx = ContextUtil.cleanContext(context); mUriMatcher = new ImmutableUriMatcher(newUriMatcher(ctx)); /* * Although a new match could be allocated each time #match(Uri) is called, let's avoid those * extra allocations for every call to the ContentProvider. */ mTableOneDirMatch = newTableOneDirMatch(ctx); mTableOneItemMatch = newTableOneItemMatch(ctx); } @NonNull @Override public SqliteUriMatch match(@NonNull final Uri uri) { switch (mUriMatcher.match(uri)) { case MATCH_TABLE_ONE_DIR: { return mTableOneDirMatch; } case MATCH_TABLE_ONE_ITEM: { return mTableOneItemMatch; } default: { throw new IllegalArgumentException(Lumberjack.formatMessage( "URI %s is unrecognized", uri)); //$NON-NLS-1$ } } } @NonNull private static UriMatcher newUriMatcher(@NonNull final Context context) { final String contentAuthority = SqliteContentProviderImpl.getContentAuthority(context); final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); matcher.addURI(contentAuthority, TableOneContract.TABLE_NAME, MATCH_TABLE_ONE_DIR); matcher.addURI(contentAuthority, TableOneContract.TABLE_NAME + "/#", MATCH_TABLE_ONE_ITEM); //$NON-NLS-1$ return matcher; } /** * @param context Application context. * @return A new match object for an item in {@link com.twofortyfouram.memento.debug.provider.TableOneContract}. */ @NonNull private static SqliteUriMatch newTableOneDirMatch(@NonNull final Context context) { final Uri baseUri = TableOneContract.getContentUri(context); final Collection<Uri> notifyUris = new ArrayList<Uri>(1); notifyUris.add(baseUri); final String tableName = TableOneContract.TABLE_NAME; final String mimeType = TableOneContract.MIMETYPE_DIR; final boolean isIdUri = false; final Map<String, String> projectionMap = newTableOneProjectionMap(); return new SqliteUriMatch(baseUri, notifyUris, tableName, mimeType, isIdUri, projectionMap); } /** * @param context Application context. * @return A new match object for an item in {@link com.twofortyfouram.memento.debug.provider.TableOneContract}. */ @NonNull private static SqliteUriMatch newTableOneItemMatch(@NonNull final Context context) { final Uri baseUri = TableOneContract.getContentUri(context); final Collection<Uri> notifyUris = new ArrayList<Uri>(1); notifyUris.add(baseUri); final String tableName = TableOneContract.TABLE_NAME; final String mimeType = TableOneContract.MIMETYPE_ITEM; final boolean isIdUri = true; final Map<String, String> projectionMap = newTableOneProjectionMap(); return new SqliteUriMatch(baseUri, notifyUris, tableName, mimeType, isIdUri, projectionMap); } /** * @return A new projection map for {@link com.twofortyfouram.memento.debug.provider.TableOneContract}. */ @SuppressLint("NewApi") @NonNull private static Map<String, String> newTableOneProjectionMap() { final Map<String, String> projectionMap; if (AndroidSdkVersion.isAtLeastSdk(Build.VERSION_CODES.KITKAT)) { projectionMap = new ArrayMap<String, String>(2); } else { projectionMap = new HashMap<String, String>(); } projectionMap.put(TableOneContract._ID, TableOneContract._ID); projectionMap.put(TableOneContract.COLUMN_STRING_COLUMN_ONE, TableOneContract.COLUMN_STRING_COLUMN_ONE); return projectionMap; } }