blob: a48bf12b308f17d5f7afa4bc664693e50d5d340e [file] [log] [blame]
Daichi Hironofda74742016-02-01 13:00:31 +09001/*
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 Hirono8b9024f2015-08-12 12:59:09 +090017package com.android.mtp;
18
19import android.content.ContentResolver;
20import android.net.Uri;
21import android.os.Process;
22import android.provider.DocumentsContract;
23import android.util.Log;
24
Daichi Hirono619afda2016-02-07 14:23:43 +090025import java.io.FileNotFoundException;
Daichi Hironofda74742016-02-01 13:00:31 +090026import java.util.concurrent.CountDownLatch;
Daichi Hironoe1d57712015-11-17 10:55:45 +090027import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Executors;
29import java.util.concurrent.FutureTask;
30import java.util.concurrent.TimeUnit;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090031
32final 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 Hironoe1d57712015-11-17 10:55:45 +090049 /**
50 * Milliseconds we wait for background thread when pausing.
51 */
52 private final static long AWAIT_TERMINATION_TIMEOUT = 2000;
53
Daichi Hirono8b9024f2015-08-12 12:59:09 +090054 final ContentResolver mResolver;
55 final MtpManager mManager;
Daichi Hironodc473442015-11-13 15:42:28 +090056 final MtpDatabase mDatabase;
Daichi Hironoe1d57712015-11-17 10:55:45 +090057
58 ExecutorService mExecutor;
59 FutureTask<Void> mCurrentTask;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090060
Daichi Hironodc473442015-11-13 15:42:28 +090061 RootScanner(
62 ContentResolver resolver,
Daichi Hironodc473442015-11-13 15:42:28 +090063 MtpManager manager,
64 MtpDatabase database) {
Daichi Hirono8b9024f2015-08-12 12:59:09 +090065 mResolver = resolver;
66 mManager = manager;
Daichi Hironodc473442015-11-13 15:42:28 +090067 mDatabase = database;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090068 }
69
Daichi Hironoe1d57712015-11-17 10:55:45 +090070 /**
71 * Notifies a change of the roots list via ContentResolver.
72 */
Daichi Hironodc473442015-11-13 15:42:28 +090073 void notifyChange() {
Daichi Hironoebd24052016-02-06 21:05:57 +090074 final Uri uri = DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY);
Daichi Hironodc473442015-11-13 15:42:28 +090075 mResolver.notifyChange(uri, null, false);
Daichi Hirono8b9024f2015-08-12 12:59:09 +090076 }
77
78 /**
79 * Starts to check new changes right away.
Daichi Hirono8b9024f2015-08-12 12:59:09 +090080 */
Daichi Hironofda74742016-02-01 13:00:31 +090081 synchronized CountDownLatch resume() {
Daichi Hironoe1d57712015-11-17 10:55:45 +090082 if (mExecutor == null) {
83 // Only single thread updates the database.
84 mExecutor = Executors.newSingleThreadExecutor();
85 }
86 if (mCurrentTask != null) {
87 // Cancel previous task.
88 mCurrentTask.cancel(true);
89 }
Daichi Hironofda74742016-02-01 13:00:31 +090090 final UpdateRootsRunnable runnable = new UpdateRootsRunnable();
91 mCurrentTask = new FutureTask<Void>(runnable, null);
Daichi Hironoe1d57712015-11-17 10:55:45 +090092 mExecutor.submit(mCurrentTask);
Daichi Hironofda74742016-02-01 13:00:31 +090093 return runnable.mFirstScanCompleted;
Daichi Hironoe1d57712015-11-17 10:55:45 +090094 }
95
96 /**
97 * Stops background thread and wait for its termination.
98 * @throws InterruptedException
99 */
100 synchronized void pause() throws InterruptedException {
101 if (mExecutor == null) {
Daichi Hironodc473442015-11-13 15:42:28 +0900102 return;
103 }
Daichi Hironoe1d57712015-11-17 10:55:45 +0900104 mExecutor.shutdownNow();
105 if (!mExecutor.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS)) {
106 Log.e(MtpDocumentsProvider.TAG, "Failed to terminate RootScanner's background thread.");
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900107 }
Daichi Hironoe1d57712015-11-17 10:55:45 +0900108 mExecutor = null;
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900109 }
110
Daichi Hironoe1d57712015-11-17 10:55:45 +0900111 /**
112 * Runnable to scan roots and update the database information.
113 */
114 private final class UpdateRootsRunnable implements Runnable {
Daichi Hironofda74742016-02-01 13:00:31 +0900115 final CountDownLatch mFirstScanCompleted = new CountDownLatch(1);
116
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900117 @Override
118 public void run() {
119 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Daichi Hironoe1d57712015-11-17 10:55:45 +0900120 int pollingCount = 0;
Daichi Hirono2bdb3882015-12-22 12:57:47 +0900121 while (true) {
Daichi Hironoe1d57712015-11-17 10:55:45 +0900122 boolean changed = false;
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000123
124 // Update devices.
Daichi Hirono20754c52015-12-15 18:52:26 +0900125 final MtpDeviceRecord[] devices = mManager.getDevices();
Daichi Hirono619afda2016-02-07 14:23:43 +0900126 try {
127 mDatabase.getMapper().startAddingDocuments(null /* parentDocumentId */);
128 for (final MtpDeviceRecord device : devices) {
129 if (mDatabase.getMapper().putDeviceDocument(device)) {
130 changed = true;
131 }
132 }
133 if (mDatabase.getMapper().stopAddingDocuments(
134 null /* parentDocumentId */)) {
Daichi Hirono20754c52015-12-15 18:52:26 +0900135 changed = true;
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900136 }
Daichi Hirono619afda2016-02-07 14:23:43 +0900137 } catch (FileNotFoundException exception) {
138 // The top root (ID is null) must exist always.
139 // FileNotFoundException is unexpected.
140 Log.e(MtpDocumentsProvider.TAG, "Unexpected FileNotFoundException", exception);
141 throw new AssertionError("Unexpected exception for the top parent", exception);
Daichi Hirono20754c52015-12-15 18:52:26 +0900142 }
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000143
144 // Update roots.
Daichi Hirono20754c52015-12-15 18:52:26 +0900145 for (final MtpDeviceRecord device : devices) {
146 final String documentId = mDatabase.getDocumentIdForDevice(device.deviceId);
147 if (documentId == null) {
148 continue;
149 }
Daichi Hirono619afda2016-02-07 14:23:43 +0900150 try {
151 mDatabase.getMapper().startAddingDocuments(documentId);
152 if (mDatabase.getMapper().putStorageDocuments(documentId, device.roots)) {
153 changed = true;
154 }
155 if (mDatabase.getMapper().stopAddingDocuments(documentId)) {
156 changed = true;
157 }
158 } catch (FileNotFoundException exception) {
159 Log.e(MtpDocumentsProvider.TAG, "Parent document is gone.", exception);
160 continue;
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000161 }
Daichi Hirono7a375c42015-12-14 17:14:29 +0900162 }
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000163
Daichi Hironoe1d57712015-11-17 10:55:45 +0900164 if (changed) {
165 notifyChange();
166 }
Daichi Hironofda74742016-02-01 13:00:31 +0900167 mFirstScanCompleted.countDown();
Daichi Hironoe1d57712015-11-17 10:55:45 +0900168 pollingCount++;
169 try {
170 // Use SHORT_POLLING_PERIOD for the first SHORT_POLLING_TIMES because it is
171 // more likely to add new root just after the device is added.
172 // TODO: Use short interval only for a device that is just added.
173 Thread.sleep(pollingCount > SHORT_POLLING_TIMES ?
174 LONG_POLLING_INTERVAL : SHORT_POLLING_INTERVAL);
175 } catch (InterruptedException exp) {
Daichi Hirono2bdb3882015-12-22 12:57:47 +0900176 break;
Daichi Hironoe1d57712015-11-17 10:55:45 +0900177 }
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900178 }
179 }
180 }
181}