MtpDatabaseConstants.java revision b3fe72bfb288a509a953e5586264ca1c4460d2df
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.database.sqlite.SQLiteQueryBuilder;
20import android.provider.DocumentsContract.Document;
21import android.provider.DocumentsContract.Root;
22
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * Class containing MtpDatabase constants.
28 */
29class MtpDatabaseConstants {
30    static final int DATABASE_VERSION = 1;
31    static final String DATABASE_NAME = "database";
32
33    static final int FLAG_DATABASE_IN_MEMORY = 1;
34    static final int FLAG_DATABASE_IN_FILE = 0;
35
36    /**
37     * Table representing documents including root documents.
38     */
39    static final String TABLE_DOCUMENTS = "Documents";
40
41    /**
42     * Table containing additional information only available for root documents.
43     * The table uses same primary keys with corresponding documents.
44     */
45    static final String TABLE_ROOT_EXTRA = "RootExtra";
46
47    /**
48     * 'FROM' closure of joining TABLE_DOCUMENTS and TABLE_ROOT_EXTRA.
49     */
50    static final String JOIN_ROOTS = createJoinFromClosure(
51            TABLE_DOCUMENTS,
52            TABLE_ROOT_EXTRA,
53            Document.COLUMN_DOCUMENT_ID,
54            Root.COLUMN_ROOT_ID);
55
56    static final String COLUMN_DEVICE_ID = "device_id";
57    static final String COLUMN_STORAGE_ID = "storage_id";
58    static final String COLUMN_OBJECT_HANDLE = "object_handle";
59    static final String COLUMN_PARENT_DOCUMENT_ID = "parent_document_id";
60    static final String COLUMN_DOCUMENT_TYPE = "document_type";
61    static final String COLUMN_ROW_STATE = "row_state";
62
63    /**
64     * The state represents that the row has a valid object handle.
65     */
66    static final int ROW_STATE_VALID = 0;
67
68    /**
69     * The state represents that the rows added at the previous cycle and need to be updated with
70     * fresh values.
71     * The row may not have valid object handle. External application can still fetch the documents.
72     * If the external application tries to fetch object handle, the provider resolves pending
73     * documents with invalidated documents ahead.
74     */
75    static final int ROW_STATE_INVALIDATED = 1;
76
77    /**
78     * The state represents the raw has a valid object handle but it may be going to be mapped with
79     * another rows invalidated. After fetching all documents under the parent, the database tries
80     * to map the pending documents and the invalidated documents in order to keep old document ID
81     * alive.
82     */
83    static final int ROW_STATE_PENDING = 2;
84
85    /**
86     * Mapping mode that uses MTP identifier to find corresponding rows.
87     */
88    static final int MAP_BY_MTP_IDENTIFIER = 0;
89
90    /**
91     * Mapping mode that uses name to find corresponding rows.
92     */
93    static final int MAP_BY_NAME = 1;
94
95    /**
96     * Document that represents a MTP device.
97     */
98    static final int DOCUMENT_TYPE_DEVICE = 0;
99
100    /**
101     * Document that represents a MTP storage.
102     */
103    static final int DOCUMENT_TYPE_STORAGE = 1;
104
105    /**
106     * Document that represents a MTP object.
107     */
108    static final int DOCUMENT_TYPE_OBJECT = 2;
109
110    static final String SELECTION_DOCUMENT_ID = Document.COLUMN_DOCUMENT_ID + " = ?";
111    static final String SELECTION_ROOT_ID = Root.COLUMN_ROOT_ID + " = ?";
112
113    static final String QUERY_CREATE_DOCUMENTS =
114            "CREATE TABLE " + TABLE_DOCUMENTS + " (" +
115            Document.COLUMN_DOCUMENT_ID +
116                " INTEGER PRIMARY KEY AUTOINCREMENT," +
117            COLUMN_DEVICE_ID + " INTEGER NOT NULL," +
118            COLUMN_STORAGE_ID + " INTEGER," +
119            COLUMN_OBJECT_HANDLE + " INTEGER," +
120            COLUMN_PARENT_DOCUMENT_ID + " INTEGER," +
121            COLUMN_ROW_STATE + " INTEGER NOT NULL," +
122            COLUMN_DOCUMENT_TYPE + " INTEGER NOT NULL," +
123            Document.COLUMN_MIME_TYPE + " TEXT," +
124            Document.COLUMN_DISPLAY_NAME + " TEXT NOT NULL," +
125            Document.COLUMN_SUMMARY + " TEXT," +
126            Document.COLUMN_LAST_MODIFIED + " INTEGER," +
127            Document.COLUMN_ICON + " INTEGER," +
128            Document.COLUMN_FLAGS + " INTEGER NOT NULL," +
129            Document.COLUMN_SIZE + " INTEGER NOT NULL);";
130
131    static final String QUERY_CREATE_ROOT_EXTRA =
132            "CREATE TABLE " + TABLE_ROOT_EXTRA + " (" +
133            Root.COLUMN_ROOT_ID + " INTEGER PRIMARY KEY," +
134            Root.COLUMN_FLAGS + " INTEGER NOT NULL," +
135            Root.COLUMN_AVAILABLE_BYTES + " INTEGER NOT NULL," +
136            Root.COLUMN_CAPACITY_BYTES + " INTEGER NOT NULL," +
137            Root.COLUMN_MIME_TYPES + " TEXT NOT NULL);";
138
139    /**
140     * Map for columns names to provide DocumentContract.Root compatible columns.
141     * @see SQLiteQueryBuilder#setProjectionMap(Map)
142     */
143    static final Map<String, String> COLUMN_MAP_ROOTS;
144    static {
145        COLUMN_MAP_ROOTS = new HashMap<>();
146        COLUMN_MAP_ROOTS.put(Root.COLUMN_ROOT_ID, TABLE_ROOT_EXTRA + "." + Root.COLUMN_ROOT_ID);
147        COLUMN_MAP_ROOTS.put(Root.COLUMN_FLAGS, TABLE_ROOT_EXTRA + "." + Root.COLUMN_FLAGS);
148        COLUMN_MAP_ROOTS.put(Root.COLUMN_ICON, TABLE_DOCUMENTS + "." + Document.COLUMN_ICON);
149        COLUMN_MAP_ROOTS.put(
150                Root.COLUMN_TITLE, TABLE_DOCUMENTS + "." + Document.COLUMN_DISPLAY_NAME);
151        COLUMN_MAP_ROOTS.put(Root.COLUMN_SUMMARY, TABLE_DOCUMENTS + "." + Document.COLUMN_SUMMARY);
152        COLUMN_MAP_ROOTS.put(
153                Root.COLUMN_DOCUMENT_ID, TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID);
154        COLUMN_MAP_ROOTS.put(
155                Root.COLUMN_AVAILABLE_BYTES, TABLE_ROOT_EXTRA + "." + Root.COLUMN_AVAILABLE_BYTES);
156        COLUMN_MAP_ROOTS.put(
157                Root.COLUMN_CAPACITY_BYTES, TABLE_ROOT_EXTRA + "." + Root.COLUMN_CAPACITY_BYTES);
158        COLUMN_MAP_ROOTS.put(
159                Root.COLUMN_MIME_TYPES, TABLE_ROOT_EXTRA + "." + Root.COLUMN_MIME_TYPES);
160    }
161
162    private static String createJoinFromClosure(
163            String table1, String table2, String column1, String column2) {
164        return table1 + " INNER JOIN " + table2 +
165                " ON " + table1 + "." + column1 + " = " + table2 + "." + column2;
166    }
167}
168