Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 17 | package com.android.mtp; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Process; |
| 22 | import android.provider.DocumentsContract; |
| 23 | import android.util.Log; |
| 24 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 25 | import java.io.FileNotFoundException; |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 26 | import java.util.concurrent.CountDownLatch; |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 29 | import java.util.concurrent.TimeUnit; |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 30 | import java.util.concurrent.TimeoutException; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 31 | |
| 32 | final class RootScanner { |
| 33 | /** |
| 34 | * Polling interval in milliseconds used for first SHORT_POLLING_TIMES because it is more |
| 35 | * likely to add new root just after the device is added. |
| 36 | */ |
| 37 | private final static long SHORT_POLLING_INTERVAL = 2000; |
| 38 | |
| 39 | /** |
| 40 | * Polling interval in milliseconds for low priority polling, when changes are not expected. |
| 41 | */ |
| 42 | private final static long LONG_POLLING_INTERVAL = 30 * 1000; |
| 43 | |
| 44 | /** |
| 45 | * @see #SHORT_POLLING_INTERVAL |
| 46 | */ |
| 47 | private final static long SHORT_POLLING_TIMES = 10; |
| 48 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 49 | /** |
| 50 | * Milliseconds we wait for background thread when pausing. |
| 51 | */ |
| 52 | private final static long AWAIT_TERMINATION_TIMEOUT = 2000; |
| 53 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 54 | final ContentResolver mResolver; |
| 55 | final MtpManager mManager; |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 56 | final MtpDatabase mDatabase; |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 57 | |
| 58 | ExecutorService mExecutor; |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 59 | private UpdateRootsRunnable mCurrentTask; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 60 | |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 61 | RootScanner( |
| 62 | ContentResolver resolver, |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 63 | MtpManager manager, |
| 64 | MtpDatabase database) { |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 65 | mResolver = resolver; |
| 66 | mManager = manager; |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 67 | mDatabase = database; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 70 | /** |
| 71 | * Notifies a change of the roots list via ContentResolver. |
| 72 | */ |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 73 | void notifyChange() { |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 74 | final Uri uri = DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 75 | mResolver.notifyChange(uri, null, false); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Starts to check new changes right away. |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 80 | */ |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 81 | synchronized CountDownLatch resume() { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 82 | if (mExecutor == null) { |
| 83 | // Only single thread updates the database. |
| 84 | mExecutor = Executors.newSingleThreadExecutor(); |
| 85 | } |
| 86 | if (mCurrentTask != null) { |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 87 | // Stop previous task. |
| 88 | mCurrentTask.stop(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 89 | } |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 90 | mCurrentTask = new UpdateRootsRunnable(); |
| 91 | mExecutor.execute(mCurrentTask); |
| 92 | return mCurrentTask.mFirstScanCompleted; |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Stops background thread and wait for its termination. |
| 97 | * @throws InterruptedException |
| 98 | */ |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 99 | synchronized void pause() throws InterruptedException, TimeoutException { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 100 | if (mExecutor == null) { |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 101 | return; |
| 102 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 103 | mExecutor.shutdownNow(); |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 104 | try { |
| 105 | if (!mExecutor.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS)) { |
| 106 | throw new TimeoutException( |
| 107 | "Timeout for terminating RootScanner's background thread."); |
| 108 | } |
| 109 | } finally { |
| 110 | mExecutor = null; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 114 | /** |
| 115 | * Runnable to scan roots and update the database information. |
| 116 | */ |
| 117 | private final class UpdateRootsRunnable implements Runnable { |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 118 | /** |
| 119 | * Count down latch that specifies the runnable is stopped. |
| 120 | */ |
| 121 | final CountDownLatch mStopped = new CountDownLatch(1); |
| 122 | |
| 123 | /** |
| 124 | * Count down latch that specifies the first scan is completed. |
| 125 | */ |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 126 | final CountDownLatch mFirstScanCompleted = new CountDownLatch(1); |
| 127 | |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 128 | @Override |
| 129 | public void run() { |
| 130 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 131 | int pollingCount = 0; |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 132 | while (mStopped.getCount() > 0) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 133 | boolean changed = false; |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 134 | |
| 135 | // Update devices. |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 136 | final MtpDeviceRecord[] devices = mManager.getDevices(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 137 | try { |
| 138 | mDatabase.getMapper().startAddingDocuments(null /* parentDocumentId */); |
| 139 | for (final MtpDeviceRecord device : devices) { |
| 140 | if (mDatabase.getMapper().putDeviceDocument(device)) { |
| 141 | changed = true; |
| 142 | } |
| 143 | } |
| 144 | if (mDatabase.getMapper().stopAddingDocuments( |
| 145 | null /* parentDocumentId */)) { |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 146 | changed = true; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 147 | } |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 148 | } catch (FileNotFoundException exception) { |
| 149 | // The top root (ID is null) must exist always. |
| 150 | // FileNotFoundException is unexpected. |
| 151 | Log.e(MtpDocumentsProvider.TAG, "Unexpected FileNotFoundException", exception); |
| 152 | throw new AssertionError("Unexpected exception for the top parent", exception); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 153 | } |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 154 | |
| 155 | // Update roots. |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 156 | for (final MtpDeviceRecord device : devices) { |
| 157 | final String documentId = mDatabase.getDocumentIdForDevice(device.deviceId); |
| 158 | if (documentId == null) { |
| 159 | continue; |
| 160 | } |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 161 | try { |
| 162 | mDatabase.getMapper().startAddingDocuments(documentId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 163 | if (mDatabase.getMapper().putStorageDocuments( |
Daichi Hirono | 72de7a9 | 2017-03-21 09:55:07 +0900 | [diff] [blame] | 164 | documentId, device.operationsSupported, device.roots)) { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 165 | changed = true; |
| 166 | } |
| 167 | if (mDatabase.getMapper().stopAddingDocuments(documentId)) { |
| 168 | changed = true; |
| 169 | } |
| 170 | } catch (FileNotFoundException exception) { |
| 171 | Log.e(MtpDocumentsProvider.TAG, "Parent document is gone.", exception); |
| 172 | continue; |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 173 | } |
Daichi Hirono | 7a375c4 | 2015-12-14 17:14:29 +0900 | [diff] [blame] | 174 | } |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 175 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 176 | if (changed) { |
| 177 | notifyChange(); |
| 178 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 179 | mFirstScanCompleted.countDown(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 180 | pollingCount++; |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 181 | if (devices.length == 0) { |
| 182 | break; |
| 183 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 184 | try { |
| 185 | // Use SHORT_POLLING_PERIOD for the first SHORT_POLLING_TIMES because it is |
| 186 | // more likely to add new root just after the device is added. |
| 187 | // TODO: Use short interval only for a device that is just added. |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 188 | mStopped.await(pollingCount > SHORT_POLLING_TIMES ? |
| 189 | LONG_POLLING_INTERVAL : SHORT_POLLING_INTERVAL, TimeUnit.MILLISECONDS); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 190 | } catch (InterruptedException exp) { |
Daichi Hirono | 2bdb388 | 2015-12-22 12:57:47 +0900 | [diff] [blame] | 191 | break; |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 192 | } |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 193 | } |
| 194 | } |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 195 | |
| 196 | void stop() { |
| 197 | mStopped.countDown(); |
| 198 | } |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 199 | } |
| 200 | } |