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