alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (UserDictionary.java)

This example Android source code file (UserDictionary.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, app, authority, content, content_item_type, content_uri, contentvalues, default_sort_order, desc, frequency, get, internet, locale_type_current, net, network, string, uri, userdictionary, util, words

The UserDictionary.java Android example source code

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * 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 android.provider;

import java.util.Locale;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

/**
 * A provider of user defined words for input methods to use for predictive text input.
 * Applications and input methods may add words into the dictionary. Words can have associated
 * frequency information and locale information.
 */
public class UserDictionary {

    /** Authority string for this provider. */
    public static final String AUTHORITY = "user_dictionary";

    /**
     * The content:// style URL for this provider
     */
    public static final Uri CONTENT_URI =
        Uri.parse("content://" + AUTHORITY);

    /**
     * Contains the user defined words.
     */
    public static class Words implements BaseColumns {
        /**
         * The content:// style URL for this table
         */
        public static final Uri CONTENT_URI =
                Uri.parse("content://" + AUTHORITY + "/words");

        /**
         * The MIME type of {@link #CONTENT_URI} providing a directory of words.
         */
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.userword";

        /**
         * The MIME type of a {@link #CONTENT_URI} sub-directory of a single word.
         */
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.userword";

        public static final String _ID = BaseColumns._ID;

        /**
         * The word column.
         * <p>TYPE: TEXT

*/ public static final String WORD = "word"; /** * The frequency column. A value between 1 and 255. Higher values imply higher frequency. * <p>TYPE: INTEGER

*/ public static final String FREQUENCY = "frequency"; /** * The locale that this word belongs to. Null if it pertains to all * locales. Locale is as defined by the string returned by Locale.toString(). * <p>TYPE: TEXT

*/ public static final String LOCALE = "locale"; /** * The uid of the application that inserted the word. * <p>TYPE: INTEGER

*/ public static final String APP_ID = "appid"; /** The locale type to specify that the word is common to all locales. */ public static final int LOCALE_TYPE_ALL = 0; /** The locale type to specify that the word is for the current locale. */ public static final int LOCALE_TYPE_CURRENT = 1; /** * Sort by descending order of frequency. */ public static final String DEFAULT_SORT_ORDER = FREQUENCY + " DESC"; /** Adds a word to the dictionary, with the given frequency and the specified * specified locale type. * @param context the current application context * @param word the word to add to the dictionary. This should not be null or * empty. * @param localeType the locale type for this word. It should be one of * {@link #LOCALE_TYPE_ALL} or {@link #LOCALE_TYPE_CURRENT}. */ public static void addWord(Context context, String word, int frequency, int localeType) { final ContentResolver resolver = context.getContentResolver(); if (TextUtils.isEmpty(word) || localeType < 0 || localeType > 1) { return; } if (frequency < 0) frequency = 0; if (frequency > 255) frequency = 255; String locale = null; // TODO: Verify if this is the best way to get the current locale if (localeType == LOCALE_TYPE_CURRENT) { locale = Locale.getDefault().toString(); } ContentValues values = new ContentValues(4); values.put(WORD, word); values.put(FREQUENCY, frequency); values.put(LOCALE, locale); values.put(APP_ID, 0); // TODO: Get App UID Uri result = resolver.insert(CONTENT_URI, values); // It's ok if the insert doesn't succeed because the word // already exists. } } }

Other Android examples (source code examples)

Here is a short list of links related to this Android UserDictionary.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.