github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/android/src/org/camlistore/CamliFileObserver.java (about) 1 /* 2 Copyright 2011 Google Inc. 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 17 package org.camlistore; 18 19 import java.io.File; 20 21 import android.net.Uri; 22 import android.os.FileObserver; 23 import android.os.RemoteException; 24 import android.util.Log; 25 26 import org.camlistore.IUploadService.Stub; 27 28 public class CamliFileObserver extends FileObserver { 29 private static final String TAG = "CamliFileObserver"; 30 31 private final File mDirectory; 32 private final Stub mServiceStub; 33 34 public CamliFileObserver(IUploadService.Stub service, File directory) { 35 super(directory.getAbsolutePath(), FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO); 36 // TODO: Docs say: "The monitored file or directory must exist at this 37 // time, or else no events will be reported (even if it appears 38 // later).". This means that a user without, say, a "gpx/" directory 39 // that then goes to "Export all Tracks.." won't start them uploading. 40 mDirectory = directory; 41 mServiceStub = service; 42 Log.d(TAG, "Starting to watch: " + mDirectory.getAbsolutePath()); 43 startWatching(); 44 } 45 46 @Override 47 public void onEvent(int event, String path) { 48 if (path == null) { 49 // It's null for certain directory-level events. 50 return; 51 } 52 53 // Note from docs: 54 // "This method is invoked on a special FileObserver thread." 55 56 // Order in which we get events for a new camera picture: 57 // CREATE, OPEN, MODIFY, [OPEN, CLOSE_NOWRITE], CLOSE_WRITE 58 File fullFile = new File(mDirectory, path); 59 Log.d(TAG, "event " + event + " for " + fullFile.getAbsolutePath()); 60 try { 61 mServiceStub.enqueueUpload(Uri.fromFile(fullFile)); 62 } catch (RemoteException e) { 63 } 64 } 65 }