MtpDatabaseConstants.java revision 42a5e0e3a4ba13a3c15d74ea0b410ff8667fdfa4
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
107    static final String QUERY_CREATE_DOCUMENTS =
108            "CREATE TABLE " + TABLE_DOCUMENTS + " (" +
109            Document.COLUMN_DOCUMENT_ID +
110                " INTEGER PRIMARY KEY AUTOINCREMENT," +
111            COLUMN_DEVICE_ID + " INTEGER NOT NULL," +
112            COLUMN_STORAGE_ID + " INTEGER," +
113            COLUMN_OBJECT_HANDLE + " INTEGER," +
114            COLUMN_PARENT_DOCUMENT_ID + " INTEGER," +
115            COLUMN_ROW_STATE + " INTEGER NOT NULL," +
116            COLUMN_DOCUMENT_TYPE + " INTEGER NOT NULL," +
117            Document.COLUMN_MIME_TYPE + " TEXT," +
118            Document.COLUMN_DISPLAY_NAME + " TEXT NOT NULL," +
119            Document.COLUMN_SUMMARY + " TEXT," +
120            Document.COLUMN_LAST_MODIFIED + " INTEGER," +
121            Document.COLUMN_ICON + " INTEGER," +
122            Document.COLUMN_FLAGS + " INTEGER NOT NULL," +
123            Document.COLUMN_SIZE + " INTEGER NOT NULL);";
124
125    static final String QUERY_CREATE_ROOT_EXTRA =
126            "CREATE TABLE " + TABLE_ROOT_EXTRA + " (" +
127            Root.COLUMN_ROOT_ID + " INTEGER PRIMARY KEY," +
128            Root.COLUMN_FLAGS + " INTEGER NOT NULL," +
129            Root.COLUMN_AVAILABLE_BYTES + " INTEGER NOT NULL," +
130            Root.COLUMN_CAPACITY_BYTES + " INTEGER NOT NULL," +
131            Root.COLUMN_MIME_TYPES + " TEXT NOT NULL);";
132
133    /**
134     * Creates a view to join Documents table and RootExtra table on their primary keys to
135     * provide DocumentContract.Root equivalent information.
136     */
137    static final String QUERY_CREATE_VIEW_ROOTS =
138            "CREATE VIEW " + VIEW_ROOTS + " AS SELECT " +
139                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
140                            Root.COLUMN_ROOT_ID + "," +
141                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_FLAGS + "," +
142                    TABLE_DOCUMENTS + "." + Document.COLUMN_ICON + " AS " +
143                            Root.COLUMN_ICON + "," +
144                    TABLE_DOCUMENTS + "." + Document.COLUMN_DISPLAY_NAME + " AS " +
145                            Root.COLUMN_TITLE + "," +
146                    TABLE_DOCUMENTS + "." + Document.COLUMN_SUMMARY + " AS " +
147                            Root.COLUMN_SUMMARY + "," +
148                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
149                    Root.COLUMN_DOCUMENT_ID + "," +
150                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_AVAILABLE_BYTES + "," +
151                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_CAPACITY_BYTES + "," +
152                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_MIME_TYPES + "," +
153                    TABLE_DOCUMENTS + "." + COLUMN_ROW_STATE +
154            " FROM " + TABLE_DOCUMENTS + " INNER JOIN " + TABLE_ROOT_EXTRA +
155            " ON " +
156                    COLUMN_PARENT_DOCUMENT_ID + " IS NULL AND " +
157                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID +
158                    "=" +
159                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_ROOT_ID;
160}
161