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 mChecksum; 34 public final String mLocalFilename; 35 public final String mRemoteFilename; 36 public final int mVersion; // version of this word list 37 public final int mFlags; // Always 0 in this version, reserved for future use 38 39 // The locale is matched against the locale requested by the client. The matching algorithm 40 // is a standard locale matching with fallback; it is implemented in 41 // DictionaryProvider#getDictionaryFileForContentUri. 42 public final String mLocale; 43 44 45 // Version number of the format. 46 // This implementation of the DictionaryDataService knows how to handle format 1 only. 47 // This is only for forward compatibility, to be able to upgrade the format without 48 // breaking old implementations. 49 public final int mFormatVersion; 50 51 public WordListMetadata(final String id, final int type, 52 final String description, final long lastUpdate, final long fileSize, 53 final String checksum, final String localFilename, final String remoteFilename, 54 final int version, final int formatVersion, final int flags, final String locale) { 55 mId = id; 56 mType = type; 57 mDescription = description; 58 mLastUpdate = lastUpdate; // In milliseconds 59 mFileSize = fileSize; 60 mChecksum = checksum; 61 mLocalFilename = localFilename; 62 mRemoteFilename = remoteFilename; 63 mVersion = version; 64 mFormatVersion = formatVersion; 65 mFlags = flags; 66 mLocale = locale; 67 } 68 69 /** 70 * Create a WordListMetadata from the contents of a ContentValues. 71 * 72 * If this lacks any required field, IllegalArgumentException is thrown. 73 */ 74 public static WordListMetadata createFromContentValues(final ContentValues values) { 75 final String id = values.getAsString(MetadataDbHelper.WORDLISTID_COLUMN); 76 final Integer type = values.getAsInteger(MetadataDbHelper.TYPE_COLUMN); 77 final String description = values.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN); 78 final Long lastUpdate = values.getAsLong(MetadataDbHelper.DATE_COLUMN); 79 final Long fileSize = values.getAsLong(MetadataDbHelper.FILESIZE_COLUMN); 80 final String checksum = values.getAsString(MetadataDbHelper.CHECKSUM_COLUMN); 81 final String localFilename = values.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN); 82 final String remoteFilename = values.getAsString(MetadataDbHelper.REMOTE_FILENAME_COLUMN); 83 final Integer version = values.getAsInteger(MetadataDbHelper.VERSION_COLUMN); 84 final Integer formatVersion = values.getAsInteger(MetadataDbHelper.FORMATVERSION_COLUMN); 85 final Integer flags = values.getAsInteger(MetadataDbHelper.FLAGS_COLUMN); 86 final String locale = values.getAsString(MetadataDbHelper.LOCALE_COLUMN); 87 if (null == id 88 || null == type 89 || null == description 90 || null == lastUpdate 91 || null == fileSize 92 || null == checksum 93 || null == localFilename 94 || null == remoteFilename 95 || null == version 96 || null == formatVersion 97 || null == flags 98 || null == locale) { 99 throw new IllegalArgumentException(); 100 } 101 return new WordListMetadata(id, type, description, lastUpdate, fileSize, checksum, 102 localFilename, remoteFilename, version, formatVersion, flags, locale); 103 } 104 105 @Override 106 public String toString() { 107 final StringBuilder sb = new StringBuilder(WordListMetadata.class.getSimpleName()); 108 sb.append(" : ").append(mId); 109 sb.append("\nType : ").append(mType); 110 sb.append("\nDescription : ").append(mDescription); 111 sb.append("\nLastUpdate : ").append(mLastUpdate); 112 sb.append("\nFileSize : ").append(mFileSize); 113 sb.append("\nChecksum : ").append(mChecksum); 114 sb.append("\nLocalFilename : ").append(mLocalFilename); 115 sb.append("\nRemoteFilename : ").append(mRemoteFilename); 116 sb.append("\nVersion : ").append(mVersion); 117 sb.append("\nFormatVersion : ").append(mFormatVersion); 118 sb.append("\nFlags : ").append(mFlags); 119 sb.append("\nLocale : ").append(mLocale); 120 return sb.toString(); 121 } 122 } 123