Home | History | Annotate | Download | only in dictionarypack
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.dictionarypack;
     18 
     19 import android.content.ContentValues;
     20 
     21 /**
     22  * The metadata for a single word list.
     23  *
     24  * Instances of this class are always immutable.
     25  */
     26 public class WordListMetadata {
     27 
     28     public final String mId;
     29     public final int mType; // Type, as of MetadataDbHelper#TYPE_*
     30     public final String mDescription;
     31     public final long mLastUpdate;
     32     public final long mFileSize;
     33     public final String mRawChecksum;
     34     public final String mChecksum;
     35     public final String mLocalFilename;
     36     public final String mRemoteFilename;
     37     public final int mVersion; // version of this word list
     38     public final int mFlags; // Always 0 in this version, reserved for future use
     39 
     40     // The locale is matched against the locale requested by the client. The matching algorithm
     41     // is a standard locale matching with fallback; it is implemented in
     42     // DictionaryProvider#getDictionaryFileForContentUri.
     43     public final String mLocale;
     44 
     45 
     46     // Version number of the format.
     47     // This implementation of the DictionaryDataService knows how to handle format 1 only.
     48     // This is only for forward compatibility, to be able to upgrade the format without
     49     // breaking old implementations.
     50     public final int mFormatVersion;
     51 
     52     public WordListMetadata(final String id, final int type,
     53             final String description, final long lastUpdate, final long fileSize,
     54             final String rawChecksum, final String checksum, final String localFilename,
     55             final String remoteFilename, final int version, final int formatVersion,
     56             final int flags, final String locale) {
     57         mId = id;
     58         mType = type;
     59         mDescription = description;
     60         mLastUpdate = lastUpdate; // In milliseconds
     61         mFileSize = fileSize;
     62         mRawChecksum = rawChecksum;
     63         mChecksum = checksum;
     64         mLocalFilename = localFilename;
     65         mRemoteFilename = remoteFilename;
     66         mVersion = version;
     67         mFormatVersion = formatVersion;
     68         mFlags = flags;
     69         mLocale = locale;
     70     }
     71 
     72     /**
     73      * Create a WordListMetadata from the contents of a ContentValues.
     74      *
     75      * If this lacks any required field, IllegalArgumentException is thrown.
     76      */
     77     public static WordListMetadata createFromContentValues(final ContentValues values) {
     78         final String id = values.getAsString(MetadataDbHelper.WORDLISTID_COLUMN);
     79         final Integer type = values.getAsInteger(MetadataDbHelper.TYPE_COLUMN);
     80         final String description = values.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN);
     81         final Long lastUpdate = values.getAsLong(MetadataDbHelper.DATE_COLUMN);
     82         final Long fileSize = values.getAsLong(MetadataDbHelper.FILESIZE_COLUMN);
     83         final String rawChecksum = values.getAsString(MetadataDbHelper.RAW_CHECKSUM_COLUMN);
     84         final String checksum = values.getAsString(MetadataDbHelper.CHECKSUM_COLUMN);
     85         final String localFilename = values.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN);
     86         final String remoteFilename = values.getAsString(MetadataDbHelper.REMOTE_FILENAME_COLUMN);
     87         final Integer version = values.getAsInteger(MetadataDbHelper.VERSION_COLUMN);
     88         final Integer formatVersion = values.getAsInteger(MetadataDbHelper.FORMATVERSION_COLUMN);
     89         final Integer flags = values.getAsInteger(MetadataDbHelper.FLAGS_COLUMN);
     90         final String locale = values.getAsString(MetadataDbHelper.LOCALE_COLUMN);
     91         if (null == id
     92                 || null == type
     93                 || null == description
     94                 || null == lastUpdate
     95                 || null == fileSize
     96                 || null == checksum
     97                 || null == localFilename
     98                 || null == remoteFilename
     99                 || null == version
    100                 || null == formatVersion
    101                 || null == flags
    102                 || null == locale) {
    103             throw new IllegalArgumentException();
    104         }
    105         return new WordListMetadata(id, type, description, lastUpdate, fileSize, rawChecksum,
    106                 checksum, localFilename, remoteFilename, version, formatVersion, flags, locale);
    107     }
    108 
    109     @Override
    110     public String toString() {
    111         final StringBuilder sb = new StringBuilder(WordListMetadata.class.getSimpleName());
    112         sb.append(" : ").append(mId);
    113         sb.append("\nType : ").append(mType);
    114         sb.append("\nDescription : ").append(mDescription);
    115         sb.append("\nLastUpdate : ").append(mLastUpdate);
    116         sb.append("\nFileSize : ").append(mFileSize);
    117         sb.append("\nRawChecksum : ").append(mRawChecksum);
    118         sb.append("\nChecksum : ").append(mChecksum);
    119         sb.append("\nLocalFilename : ").append(mLocalFilename);
    120         sb.append("\nRemoteFilename : ").append(mRemoteFilename);
    121         sb.append("\nVersion : ").append(mVersion);
    122         sb.append("\nFormatVersion : ").append(mFormatVersion);
    123         sb.append("\nFlags : ").append(mFlags);
    124         sb.append("\nLocale : ").append(mLocale);
    125         return sb.toString();
    126     }
    127 }
    128