tobiasKaminsky 8 роки тому
батько
коміт
99a8278de8

+ 1 - 0
AndroidManifest.xml

@@ -119,6 +119,7 @@
                 android:name="android.accounts.AccountAuthenticator"
                 android:resource="@xml/authenticator" />
         </service>
+        <service android:name=".services.observer.SyncedFolderObserverService"/>
         <service
             android:name=".syncadapter.FileSyncService"
             android:exported="true" >

+ 6 - 0
src/com/owncloud/android/MainApp.java

@@ -23,6 +23,7 @@ package com.owncloud.android;
 import android.app.Activity;
 import android.app.Application;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
@@ -33,6 +34,7 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.services.observer.SyncedFolderObserverService;
 
 
 /**
@@ -85,6 +87,10 @@ public class MainApp extends Application {
             Log_OC.d("Debug", "start logging");
         }
 
+        Log_OC.d("SyncedFolderObserverService", "Start service SyncedFolderObserverService");
+        Intent i = new Intent(this, SyncedFolderObserverService.class);
+        startService(i);
+
         // register global protection with pass code
         registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
 

+ 12 - 0
src/com/owncloud/android/providers/FileContentProvider.java

@@ -919,6 +919,18 @@ public class FileContentProvider extends ContentProvider {
                 + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " INTEGER, "            // account
                 + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_OPTION + " INTEGER );"     // upload action
         );
+
+        // TODO Tobi remove after testing
+        db.execSQL("INSERT INTO " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + "("
+                + ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH  + ", "            // local path
+                + ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH + ", "            // remote path
+                + ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY + ", "              // wifi_only
+                + ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY + ", "          // charging only
+                + ProviderTableMeta.SYNCED_FOLDER_ENABLED + ", "                // enabled
+                + ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE + ", "      // subfolder by date
+                + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + ", "                // account
+                + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_OPTION + ") "          // upload action
+                + "VALUES ('/sdcard/DCIM/', 'syncTest', 0, 0, 1, 1, 'tobi', 1)");
     }
 
     /**

+ 49 - 0
src/com/owncloud/android/services/observer/SyncedFolderObserver.java

@@ -0,0 +1,49 @@
+package com.owncloud.android.services.observer;
+
+import android.app.job.JobInfo;
+import android.app.job.JobScheduler;
+import android.content.ComponentName;
+import android.content.Context;
+import android.os.FileObserver;
+import android.util.Log;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.utils.RecursiveFileObserver;
+
+import java.io.File;
+
+public class SyncedFolderObserver extends RecursiveFileObserver {
+
+    File dirToWatch;
+    String remoteFolder;
+    Context context;
+
+    public static final int MY_BACKGROUND_JOB = 0;
+
+
+    public SyncedFolderObserver(String path, String remoteFolder) {
+        super(path, FileObserver.CREATE + FileObserver.MOVED_TO);
+
+        context = MainApp.getAppContext();
+        dirToWatch = new File(path);
+        this.remoteFolder = remoteFolder;
+        Log_OC.d("SyncedFolderObserver", "Started watching: "+ path);
+    }
+
+
+
+    @Override
+    public void onEvent(int event, String path) {
+        JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
+//        JobInfo job = new JobInfo.Builder(
+//                MY_BACKGROUND_JOB,
+//                new ComponentName(context, MyJobService.class))
+//                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
+//                .setRequiresCharging(true)
+//                .build();
+//        js.schedule(job);
+
+        Log.d("SyncedFolder", "Event: " + event + " Path: " + path);
+    }
+}

+ 59 - 0
src/com/owncloud/android/services/observer/SyncedFolderObserverService.java

@@ -0,0 +1,59 @@
+package com.owncloud.android.services.observer;
+
+import java.io.File;
+import java.util.HashSet;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.lib.common.utils.Log_OC;
+
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.IBinder;
+import android.widget.Toast;
+
+public class SyncedFolderObserverService extends Service {
+    private SyncedFolderObserver fileOb;
+    private static final String TAG = "InstantUploadFolderObserverService";
+
+    @Override
+    public void onCreate() {
+        File sdcard = new File("/mnt/sdcard/DCIM/");
+        Log_OC.d("SyncedFolderObserverService", "watching file: " + sdcard.getAbsolutePath());
+        fileOb = new SyncedFolderObserver(sdcard.getAbsolutePath(), "WhatsApp");
+        fileOb.startWatching();
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        // TODO Auto-generated method stub
+        Log_OC.d("SyncedFolderObserverService", "start");
+        return Service.START_NOT_STICKY;
+        //return super.onStartCommand(intent, flags, startId);
+
+    }
+
+    @Override
+    public void onStart(Intent intent, int startid) {
+        Log_OC.d("SyncedFolderObserverService", "start");
+        fileOb.startWatching();
+        /*for (int i = 0; i < fileOb_list.size(); ++i) {
+            fileOb_list.get(i).startWatching();
+        }*/
+        Toast.makeText(this.getApplicationContext(), "start monitoring file modification", Toast.LENGTH_SHORT).show();
+    }
+    @Override
+    public void onDestroy() {
+        fileOb.stopWatching();
+        /*for (int i = 0; i < fileOb_list.size(); ++i) {
+            fileOb_list.get(i).stopWatching();
+        }*/
+        Toast.makeText(this.getApplicationContext(), "stop monitoring file modification", Toast.LENGTH_SHORT).show();
+    }
+
+    @Override
+    public IBinder onBind(Intent arg0) {
+        return null;
+    }
+}