blob: 39bea49ea6759cc1948302623053a7e18cac9581 [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;
Daichi Hironodc473442015-11-13 15:42:28 +090020import android.content.res.Resources;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090021import android.net.Uri;
22import android.os.Process;
23import android.provider.DocumentsContract;
24import android.util.Log;
25
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;
Daichi Hironodc473442015-11-13 15:42:28 +090055 final Resources mResources;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090056 final MtpManager mManager;
Daichi Hironodc473442015-11-13 15:42:28 +090057 final MtpDatabase mDatabase;
Daichi Hironoe1d57712015-11-17 10:55:45 +090058
59 ExecutorService mExecutor;
60 FutureTask<Void> mCurrentTask;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090061
Daichi Hironodc473442015-11-13 15:42:28 +090062 RootScanner(
63 ContentResolver resolver,
64 Resources resources,
65 MtpManager manager,
66 MtpDatabase database) {
Daichi Hirono8b9024f2015-08-12 12:59:09 +090067 mResolver = resolver;
Daichi Hironodc473442015-11-13 15:42:28 +090068 mResources = resources;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090069 mManager = manager;
Daichi Hironodc473442015-11-13 15:42:28 +090070 mDatabase = database;
Daichi Hirono8b9024f2015-08-12 12:59:09 +090071 }
72
Daichi Hironoe1d57712015-11-17 10:55:45 +090073 /**
74 * Notifies a change of the roots list via ContentResolver.
75 */
Daichi Hironodc473442015-11-13 15:42:28 +090076 void notifyChange() {
77 final Uri uri =
78 DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY);
79 mResolver.notifyChange(uri, null, false);
Daichi Hirono8b9024f2015-08-12 12:59:09 +090080 }
81
82 /**
83 * Starts to check new changes right away.
Daichi Hirono8b9024f2015-08-12 12:59:09 +090084 */
Daichi Hironofda74742016-02-01 13:00:31 +090085 synchronized CountDownLatch resume() {
Daichi Hironoe1d57712015-11-17 10:55:45 +090086 if (mExecutor == null) {
87 // Only single thread updates the database.
88 mExecutor = Executors.newSingleThreadExecutor();
89 }
90 if (mCurrentTask != null) {
91 // Cancel previous task.
92 mCurrentTask.cancel(true);
93 }
Daichi Hironofda74742016-02-01 13:00:31 +090094 final UpdateRootsRunnable runnable = new UpdateRootsRunnable();
95 mCurrentTask = new FutureTask<Void>(runnable, null);
Daichi Hironoe1d57712015-11-17 10:55:45 +090096 mExecutor.submit(mCurrentTask);
Daichi Hironofda74742016-02-01 13:00:31 +090097 return runnable.mFirstScanCompleted;
Daichi Hironoe1d57712015-11-17 10:55:45 +090098 }
99
100 /**
101 * Stops background thread and wait for its termination.
102 * @throws InterruptedException
103 */
104 synchronized void pause() throws InterruptedException {
105 if (mExecutor == null) {
Daichi Hironodc473442015-11-13 15:42:28 +0900106 return;
107 }
Daichi Hironoe1d57712015-11-17 10:55:45 +0900108 mExecutor.shutdownNow();
109 if (!mExecutor.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS)) {
110 Log.e(MtpDocumentsProvider.TAG, "Failed to terminate RootScanner's background thread.");
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900111 }
Daichi Hironoe1d57712015-11-17 10:55:45 +0900112 mExecutor = null;
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900113 }
114
Daichi Hironoe1d57712015-11-17 10:55:45 +0900115 /**
116 * Runnable to scan roots and update the database information.
117 */
118 private final class UpdateRootsRunnable implements Runnable {
Daichi Hironofda74742016-02-01 13:00:31 +0900119 final CountDownLatch mFirstScanCompleted = new CountDownLatch(1);
120
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900121 @Override
122 public void run() {
123 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Daichi Hironoe1d57712015-11-17 10:55:45 +0900124 int pollingCount = 0;
Daichi Hirono2bdb3882015-12-22 12:57:47 +0900125 while (true) {
Daichi Hironoe1d57712015-11-17 10:55:45 +0900126 boolean changed = false;
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000127
128 // Update devices.
Daichi Hirono20754c52015-12-15 18:52:26 +0900129 final MtpDeviceRecord[] devices = mManager.getDevices();
Daichi Hirono7a375c42015-12-14 17:14:29 +0900130 mDatabase.getMapper().startAddingDocuments(null /* parentDocumentId */);
Daichi Hirono20754c52015-12-15 18:52:26 +0900131 for (final MtpDeviceRecord device : devices) {
132 if (mDatabase.getMapper().putDeviceDocument(device)) {
133 changed = true;
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900134 }
135 }
Daichi Hirono20754c52015-12-15 18:52:26 +0900136 if (mDatabase.getMapper().stopAddingDocuments(null /* parentDocumentId */)) {
137 changed = true;
138 }
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000139
140 // Update roots.
Daichi Hirono20754c52015-12-15 18:52:26 +0900141 for (final MtpDeviceRecord device : devices) {
142 final String documentId = mDatabase.getDocumentIdForDevice(device.deviceId);
143 if (documentId == null) {
144 continue;
145 }
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000146 mDatabase.getMapper().startAddingDocuments(documentId);
Daichi Hirono81d48532015-12-16 15:03:19 +0900147 if (mDatabase.getMapper().putStorageDocuments(
Daichi Hirono20754c52015-12-15 18:52:26 +0900148 documentId, mResources, device.roots)) {
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000149 changed = true;
150 }
151 if (mDatabase.getMapper().stopAddingDocuments(documentId)) {
152 changed = true;
153 }
Daichi Hirono7a375c42015-12-14 17:14:29 +0900154 }
Daichi Hironob3fe72b2015-12-15 07:45:06 +0000155
Daichi Hironoe1d57712015-11-17 10:55:45 +0900156 if (changed) {
157 notifyChange();
158 }
Daichi Hironofda74742016-02-01 13:00:31 +0900159 mFirstScanCompleted.countDown();
Daichi Hironoe1d57712015-11-17 10:55:45 +0900160 pollingCount++;
161 try {
162 // Use SHORT_POLLING_PERIOD for the first SHORT_POLLING_TIMES because it is
163 // more likely to add new root just after the device is added.
164 // TODO: Use short interval only for a device that is just added.
165 Thread.sleep(pollingCount > SHORT_POLLING_TIMES ?
166 LONG_POLLING_INTERVAL : SHORT_POLLING_INTERVAL);
167 } catch (InterruptedException exp) {
Daichi Hirono2bdb3882015-12-22 12:57:47 +0900168 break;
Daichi Hironoe1d57712015-11-17 10:55:45 +0900169 }
Daichi Hirono8b9024f2015-08-12 12:59:09 +0900170 }
171 }
172 }
173}