MtpDatabaseConstants.java revision 4b54e036ef958947d6347a0971d32c08774e3495
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mtp;
18
19import android.provider.DocumentsContract.Document;
20import android.provider.DocumentsContract.Root;
21
22/**
23 * Class containing MtpDatabase constants.
24 */
25class MtpDatabaseConstants {
26    static final int DATABASE_VERSION = 1;
27    static final String DATABASE_NAME = "database";
28
29    static final int FLAG_DATABASE_IN_MEMORY = 1;
30    static final int FLAG_DATABASE_IN_FILE = 0;
31
32    /**
33     * Table representing documents including root documents.
34     */
35    static final String TABLE_DOCUMENTS = "Documents";
36
37    /**
38     * Table containing additional information only available for root documents.
39     * The table uses same primary keys with corresponding documents.
40     */
41    static final String TABLE_ROOT_EXTRA = "RootExtra";
42
43    /**
44     * View to join Documents and RootExtra tables to provide roots information.
45     */
46    static final String VIEW_ROOTS = "Roots";
47
48    static final String COLUMN_DEVICE_ID = "device_id";
49    static final String COLUMN_STORAGE_ID = "storage_id";
50    static final String COLUMN_OBJECT_HANDLE = "object_handle";
51    static final String COLUMN_PARENT_DOCUMENT_ID = "parent_document_id";
52    static final String COLUMN_DOCUMENT_TYPE = "document_type";
53    static final String COLUMN_ROW_STATE = "row_state";
54
55    /**
56     * The state represents that the row has a valid object handle.
57     */
58    static final int ROW_STATE_VALID = 0;
59
60    /**
61     * The state represents that the rows added at the previous cycle and need to be updated with
62     * fresh values.
63     * The row may not have valid object handle. External application can still fetch the documents.
64     * If the external application tries to fetch object handle, the provider resolves pending
65     * documents with invalidated documents ahead.
66     */
67    static final int ROW_STATE_INVALIDATED = 1;
68
69    /**
70     * The state represents the raw has a valid object handle but it may be going to be mapped with
71     * another rows invalidated. After fetching all documents under the parent, the database tries
72     * to map the pending documents and the invalidated documents in order to keep old document ID
73     * alive.
74     */
75    static final int ROW_STATE_PENDING = 2;
76
77    /**
78     * Mapping mode that uses MTP identifier to find corresponding rows.
79     */
80    static final int MAP_BY_MTP_IDENTIFIER = 0;
81
82    /**
83     * Mapping mode that uses name to find corresponding rows.
84     */
85    static final int MAP_BY_NAME = 1;
86
87    /**
88     * Document that represents a MTP device.
89     * Note we have "device" document only when the device has multiple storage volumes. Otherwise
90     * we regard the single "storage" document as root.
91     */
92    static final int DOCUMENT_TYPE_DEVICE = 0;
93
94    /**
95     * Document that represents a MTP storage.
96     */
97    static final int DOCUMENT_TYPE_STORAGE = 1;
98
99    /**
100     * Document that represents a MTP object.
101     */
102    static final int DOCUMENT_TYPE_OBJECT = 2;
103
104    static final String SELECTION_DOCUMENT_ID = Document.COLUMN_DOCUMENT_ID + " = ?";
105    static final String SELECTION_ROOT_ID = Root.COLUMN_ROOT_ID + " = ?";
106    static final String SELECTION_ROOT_DOCUMENTS =
107            COLUMN_DEVICE_ID + " = ? AND " + COLUMN_PARENT_DOCUMENT_ID + " IS NULL";
108    static final String SELECTION_CHILD_DOCUMENTS = COLUMN_PARENT_DOCUMENT_ID + " = ?";
109
110    static final String QUERY_CREATE_DOCUMENTS =
111            "CREATE TABLE " + TABLE_DOCUMENTS + " (" +
112            Document.COLUMN_DOCUMENT_ID +
113                " INTEGER PRIMARY KEY AUTOINCREMENT," +
114            COLUMN_DEVICE_ID + " INTEGER NOT NULL," +
115            COLUMN_STORAGE_ID + " INTEGER," +
116            COLUMN_OBJECT_HANDLE + " INTEGER," +
117            COLUMN_PARENT_DOCUMENT_ID + " INTEGER," +
118            COLUMN_ROW_STATE + " INTEGER NOT NULL," +
119            COLUMN_DOCUMENT_TYPE + " INTEGER NOT NULL," +
120            Document.COLUMN_MIME_TYPE + " TEXT," +
121            Document.COLUMN_DISPLAY_NAME + " TEXT NOT NULL," +
122            Document.COLUMN_SUMMARY + " TEXT," +
123            Document.COLUMN_LAST_MODIFIED + " INTEGER," +
124            Document.COLUMN_ICON + " INTEGER," +
125            Document.COLUMN_FLAGS + " INTEGER NOT NULL," +
126            Document.COLUMN_SIZE + " INTEGER NOT NULL);";
127
128    static final String QUERY_CREATE_ROOT_EXTRA =
129            "CREATE TABLE " + TABLE_ROOT_EXTRA + " (" +
130            Root.COLUMN_ROOT_ID + " INTEGER PRIMARY KEY," +
131            Root.COLUMN_FLAGS + " INTEGER NOT NULL," +
132            Root.COLUMN_AVAILABLE_BYTES + " INTEGER NOT NULL," +
133            Root.COLUMN_CAPACITY_BYTES + " INTEGER NOT NULL," +
134            Root.COLUMN_MIME_TYPES + " TEXT NOT NULL);";
135
136    /**
137     * Creates a view to join Documents table and RootExtra table on their primary keys to
138     * provide DocumentContract.Root equivalent information.
139     */
140    static final String QUERY_CREATE_VIEW_ROOTS =
141            "CREATE VIEW " + VIEW_ROOTS + " AS SELECT " +
142                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
143                            Root.COLUMN_ROOT_ID + "," +
144                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_FLAGS + "," +
145                    TABLE_DOCUMENTS + "." + Document.COLUMN_ICON + " AS " +
146                            Root.COLUMN_ICON + "," +
147                    TABLE_DOCUMENTS + "." + Document.COLUMN_DISPLAY_NAME + " AS " +
148                            Root.COLUMN_TITLE + "," +
149                    TABLE_DOCUMENTS + "." + Document.COLUMN_SUMMARY + " AS " +
150                            Root.COLUMN_SUMMARY + "," +
151                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
152                    Root.COLUMN_DOCUMENT_ID + "," +
153                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_AVAILABLE_BYTES + "," +
154                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_CAPACITY_BYTES + "," +
155                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_MIME_TYPES + "," +
156                    TABLE_DOCUMENTS + "." + COLUMN_ROW_STATE +
157            " FROM " + TABLE_DOCUMENTS + " INNER JOIN " + TABLE_ROOT_EXTRA +
158            " ON " +
159                    COLUMN_PARENT_DOCUMENT_ID + " IS NULL AND " +
160                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID +
161                    "=" +
162                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_ROOT_ID;
163}
164