2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ingest;
19 import android.app.NotificationManager;
20 import android.app.PendingIntent;
21 import android.app.Service;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.media.MediaScannerConnection;
25 import android.media.MediaScannerConnection.MediaScannerConnectionClient;
26 import android.mtp.MtpDevice;
27 import android.mtp.MtpDeviceInfo;
28 import android.mtp.MtpObjectInfo;
29 import android.net.Uri;
30 import android.os.Binder;
31 import android.os.IBinder;
32 import android.os.SystemClock;
33 import android.support.v4.app.NotificationCompat;
34 import android.util.SparseBooleanArray;
35 import android.widget.Adapter;
37 import com.android.gallery3d.R;
38 import com.android.gallery3d.app.NotificationIds;
39 import com.android.gallery3d.data.MtpClient;
40 import com.android.gallery3d.ingest.data.MtpBitmapFetch;
41 import com.android.gallery3d.util.BucketNames;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
47 public class IngestService extends Service implements ImportTask.Listener,
48 MtpDeviceIndex.ProgressListener, MtpClient.Listener {
50 public class LocalBinder extends Binder {
51 IngestService getService() {
52 return IngestService.this;
56 private static final int PROGRESS_UPDATE_INTERVAL_MS = 180;
58 private static MtpClient sClient;
60 private final IBinder mBinder = new LocalBinder();
61 private ScannerClient mScannerClient;
62 private MtpDevice mDevice;
63 private String mDevicePrettyName;
64 private MtpDeviceIndex mIndex;
65 private IngestActivity mClientActivity;
66 private boolean mRedeliverImportFinish = false;
67 private Collection<MtpObjectInfo> mRedeliverObjectsNotImported;
68 private boolean mRedeliverNotifyIndexChanged = false;
69 private boolean mRedeliverIndexFinish = false;
70 private NotificationManager mNotificationManager;
71 private NotificationCompat.Builder mNotificationBuilder;
72 private long mLastProgressIndexTime = 0;
75 public void onCreate() {
77 mScannerClient = new ScannerClient(this);
78 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
79 mNotificationBuilder = new NotificationCompat.Builder(this);
80 mNotificationBuilder.setSmallIcon(android.R.drawable.stat_notify_sync) // TODO drawable
81 .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, IngestActivity.class), 0));
82 mIndex = MtpDeviceIndex.getInstance();
83 mIndex.setProgressListener(this);
85 if (sClient == null) {
86 sClient = new MtpClient(getApplicationContext());
88 List<MtpDevice> devices = sClient.getDeviceList();
89 if (devices.size() > 0) {
90 setDevice(devices.get(0));
92 sClient.addListener(this);
96 public void onDestroy() {
97 sClient.removeListener(this);
98 mIndex.unsetProgressListener(this);
103 public IBinder onBind(Intent intent) {
107 private void setDevice(MtpDevice device) {
108 if (mDevice == device) return;
109 mRedeliverImportFinish = false;
110 mRedeliverObjectsNotImported = null;
111 mRedeliverNotifyIndexChanged = false;
112 mRedeliverIndexFinish = false;
114 mIndex.setDevice(mDevice);
115 if (mDevice != null) {
116 MtpDeviceInfo deviceInfo = mDevice.getDeviceInfo();
117 if (deviceInfo == null) {
121 mDevicePrettyName = deviceInfo.getModel();
122 mNotificationBuilder.setContentTitle(mDevicePrettyName);
123 new Thread(mIndex.getIndexRunnable()).start();
126 mDevicePrettyName = null;
128 if (mClientActivity != null) {
129 mClientActivity.notifyIndexChanged();
131 mRedeliverNotifyIndexChanged = true;
135 protected MtpDeviceIndex getIndex() {
139 protected void setClientActivity(IngestActivity activity) {
140 if (mClientActivity == activity) return;
141 mClientActivity = activity;
142 if (mClientActivity == null) return;
143 mNotificationManager.cancel(NotificationIds.INGEST_NOTIFICATION_IMPORTING);
144 mNotificationManager.cancel(NotificationIds.INGEST_NOTIFICATION_SCANNING);
145 if (mRedeliverImportFinish) {
146 mClientActivity.onImportFinish(mRedeliverObjectsNotImported);
147 mRedeliverImportFinish = false;
148 mRedeliverObjectsNotImported = null;
150 if (mRedeliverNotifyIndexChanged) {
151 mClientActivity.notifyIndexChanged();
152 mRedeliverNotifyIndexChanged = false;
154 if (mRedeliverIndexFinish) {
155 mClientActivity.onIndexFinish();
156 mRedeliverIndexFinish = false;
160 protected void importSelectedItems(SparseBooleanArray selected, Adapter adapter) {
161 List<MtpObjectInfo> importHandles = new ArrayList<MtpObjectInfo>();
162 for (int i = 0; i < selected.size(); i++) {
163 if (selected.valueAt(i)) {
164 Object item = adapter.getItem(selected.keyAt(i));
165 if (item instanceof MtpObjectInfo) {
166 importHandles.add(((MtpObjectInfo) item));
170 ImportTask task = new ImportTask(mDevice, importHandles, BucketNames.IMPORTED, this);
171 task.setListener(this);
172 mNotificationBuilder.setProgress(0, 0, true)
173 .setContentText(getResources().getText(R.string.ingest_importing));
174 startForeground(NotificationIds.INGEST_NOTIFICATION_IMPORTING,
175 mNotificationBuilder.build());
176 new Thread(task).start();
180 public void deviceAdded(MtpDevice device) {
181 if (mDevice == null) {
187 public void deviceRemoved(MtpDevice device) {
188 if (device == mDevice) {
190 MtpBitmapFetch.onDeviceDisconnected(device);
195 public void onImportProgress(int visitedCount, int totalCount,
196 String pathIfSuccessful) {
197 if (pathIfSuccessful != null) {
198 mScannerClient.scanPath(pathIfSuccessful);
200 if (mClientActivity != null) {
201 mClientActivity.onImportProgress(visitedCount, totalCount, pathIfSuccessful);
203 mNotificationBuilder.setProgress(totalCount, visitedCount, false)
204 .setContentText(getResources().getText(R.string.ingest_importing));
205 mNotificationManager.notify(NotificationIds.INGEST_NOTIFICATION_IMPORTING,
206 mNotificationBuilder.build());
210 public void onImportFinish(Collection<MtpObjectInfo> objectsNotImported) {
211 stopForeground(true);
212 if (mClientActivity != null) {
213 mClientActivity.onImportFinish(objectsNotImported);
215 mRedeliverImportFinish = true;
216 mRedeliverObjectsNotImported = objectsNotImported;
217 mNotificationBuilder.setProgress(0, 0, false)
218 .setContentText(getResources().getText(R.string.import_complete));
219 mNotificationManager.notify(NotificationIds.INGEST_NOTIFICATION_IMPORTING,
220 mNotificationBuilder.build());
225 public void onObjectIndexed(MtpObjectInfo object, int numVisited) {
226 if (mClientActivity != null) {
227 mClientActivity.onObjectIndexed(object, numVisited);
229 // Throttle the updates to one every PROGRESS_UPDATE_INTERVAL_MS milliseconds
230 long currentTime = SystemClock.uptimeMillis();
231 if (currentTime > mLastProgressIndexTime + PROGRESS_UPDATE_INTERVAL_MS) {
232 mLastProgressIndexTime = currentTime;
233 mNotificationBuilder.setProgress(0, numVisited, true)
234 .setContentText(getResources().getText(R.string.ingest_scanning));
235 mNotificationManager.notify(NotificationIds.INGEST_NOTIFICATION_SCANNING,
236 mNotificationBuilder.build());
242 public void onSorting() {
243 if (mClientActivity != null) mClientActivity.onSorting();
247 public void onIndexFinish() {
248 if (mClientActivity != null) {
249 mClientActivity.onIndexFinish();
251 mNotificationBuilder.setProgress(0, 0, false)
252 .setContentText(getResources().getText(R.string.ingest_scanning_done));
253 mNotificationManager.notify(NotificationIds.INGEST_NOTIFICATION_SCANNING,
254 mNotificationBuilder.build());
255 mRedeliverIndexFinish = true;
259 // Copied from old Gallery3d code
260 private static final class ScannerClient implements MediaScannerConnectionClient {
261 ArrayList<String> mPaths = new ArrayList<String>();
262 MediaScannerConnection mScannerConnection;
264 Object mLock = new Object();
266 public ScannerClient(Context context) {
267 mScannerConnection = new MediaScannerConnection(context, this);
270 public void scanPath(String path) {
271 synchronized (mLock) {
273 mScannerConnection.scanFile(path, null);
276 mScannerConnection.connect();
282 public void onMediaScannerConnected() {
283 synchronized (mLock) {
285 if (!mPaths.isEmpty()) {
286 for (String path : mPaths) {
287 mScannerConnection.scanFile(path, null);
295 public void onScanCompleted(String path, Uri uri) {