observers are restarted after changing a setting

This commit is contained in:
tobiasKaminsky 2016-10-26 20:32:31 +02:00
parent 235b01b4bf
commit 3c62f4128d
4 changed files with 77 additions and 25 deletions

View file

@ -22,12 +22,15 @@ package com.owncloud.android;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder;
import com.owncloud.android.authentication.PassCodeManager; import com.owncloud.android.authentication.PassCodeManager;
import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.datamodel.ThumbnailsCacheManager;
@ -58,6 +61,9 @@ public class MainApp extends Application {
private static boolean mOnlyOnDevice = false; private static boolean mOnlyOnDevice = false;
private static SyncedFolderObserverService mObserverService;
private boolean mBound;
public void onCreate(){ public void onCreate(){
super.onCreate(); super.onCreate();
@ -90,6 +96,7 @@ public class MainApp extends Application {
Log_OC.d("SyncedFolderObserverService", "Start service SyncedFolderObserverService"); Log_OC.d("SyncedFolderObserverService", "Start service SyncedFolderObserverService");
Intent i = new Intent(this, SyncedFolderObserverService.class); Intent i = new Intent(this, SyncedFolderObserverService.class);
startService(i); startService(i);
bindService(i, syncedFolderObserverServiceConnection, Context.BIND_AUTO_CREATE);
// register global protection with pass code // register global protection with pass code
registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() { registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
@ -189,6 +196,10 @@ public class MainApp extends Application {
return mOnlyOnDevice; return mOnlyOnDevice;
} }
public static SyncedFolderObserverService getSyncedFolderObserverService() {
return mObserverService;
}
// user agent // user agent
public static String getUserAgent() { public static String getUserAgent() {
String appString = getAppContext().getResources().getString(R.string.user_agent); String appString = getAppContext().getResources().getString(R.string.user_agent);
@ -210,4 +221,21 @@ public class MainApp extends Application {
return userAgent; return userAgent;
} }
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection syncedFolderObserverServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
SyncedFolderObserverService.SyncedFolderObserverBinder binder = (SyncedFolderObserverService.SyncedFolderObserverBinder) service;
mObserverService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
} }

View file

@ -25,6 +25,7 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.owncloud.android.MainApp;
import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.db.ProviderMeta;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
@ -66,7 +67,7 @@ public class SyncedFolderProvider extends Observable {
Uri result = mContentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, cv); Uri result = mContentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, cv);
if (result != null) { if (result != null) {
notifyFolderSyncObservers(); notifyFolderSyncObservers(syncedFolder);
return Long.parseLong(result.getPathSegments().get(1)); return Long.parseLong(result.getPathSegments().get(1));
} else { } else {
Log_OC.e(TAG, "Failed to insert item " + syncedFolder.getLocalPath() + " into folder sync db."); Log_OC.e(TAG, "Failed to insert item " + syncedFolder.getLocalPath() + " into folder sync db.");
@ -204,7 +205,7 @@ public class SyncedFolderProvider extends Observable {
); );
if (result > 0) { if (result > 0) {
notifyFolderSyncObservers(); notifyFolderSyncObservers(syncedFolder);
} }
return result; return result;
@ -258,9 +259,8 @@ public class SyncedFolderProvider extends Observable {
/** /**
* Inform all observers about data change. * Inform all observers about data change.
*/ */
private void notifyFolderSyncObservers() { private void notifyFolderSyncObservers(SyncedFolder syncedFolder) {
Log_OC.d(TAG, "notifying folder sync data observers"); MainApp.getSyncedFolderObserverService().restartObserver(syncedFolder);
setChanged(); Log_OC.d(TAG, "notifying folder sync data observers for changed/added: " + syncedFolder.getLocalPath());
notifyObservers();
} }
} }

View file

@ -28,11 +28,7 @@ import android.app.job.JobService;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Message;
import android.os.Messenger;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.os.RemoteException;
import android.util.Log;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.authentication.AccountUtils;
@ -43,11 +39,6 @@ import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.MimetypeIconUtil; import com.owncloud.android.utils.MimetypeIconUtil;
import java.io.File; import java.io.File;
import java.util.Date;
/**
* Created by tobi on 25.09.16.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class SyncedFolderJobService extends JobService { public class SyncedFolderJobService extends JobService {

View file

@ -2,6 +2,7 @@ package com.owncloud.android.services.observer;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
@ -9,13 +10,13 @@ import com.owncloud.android.datamodel.SyncedFolder;
import com.owncloud.android.datamodel.SyncedFolderProvider; import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List;
public class SyncedFolderObserverService extends Service { public class SyncedFolderObserverService extends Service {
private static final String TAG = "SyncedFolderObserverService"; private static final String TAG = "SyncedFolderObserverService";
private SyncedFolderProvider mProvider; private SyncedFolderProvider mProvider;
private List<SyncedFolderObserver> syncedFolderObservers = new ArrayList<>(); private HashMap<String, SyncedFolderObserver> syncedFolderMap = new HashMap<>();
private final IBinder mBinder = new SyncedFolderObserverBinder();
@Override @Override
public void onCreate() { public void onCreate() {
@ -26,10 +27,11 @@ public class SyncedFolderObserverService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log_OC.d(TAG, "start"); Log_OC.d(TAG, "start");
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) { for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
if (syncedFolder.isEnabled()) {
SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder); SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder);
observer.startWatching(); observer.startWatching();
syncedFolderObservers.add(observer); syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
}
} }
return Service.START_NOT_STICKY; return Service.START_NOT_STICKY;
@ -37,14 +39,45 @@ public class SyncedFolderObserverService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
for (SyncedFolderObserver observer : syncedFolderObservers) { for (SyncedFolderObserver observer : syncedFolderMap.values()) {
observer.stopWatching(); observer.stopWatching();
syncedFolderObservers.remove(observer); syncedFolderMap.remove(observer);
}
}
/**
* Restart oberver if it is enabled
* If syncedFolder exists already, use it, otherwise create new observer
* @param syncedFolder
*/
public void restartObserver(SyncedFolder syncedFolder){
if (syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
Log_OC.d(TAG, "stop observer: " + syncedFolder.getLocalPath());
syncedFolderMap.get(syncedFolder.getLocalPath()).stopWatching();
syncedFolderMap.remove(syncedFolder.getLocalPath());
}
if (syncedFolder.isEnabled()) {
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath());
if (syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
syncedFolderMap.get(syncedFolder.getLocalPath()).startWatching();
} else {
SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder);
observer.startWatching();
syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
}
} }
} }
@Override @Override
public IBinder onBind(Intent arg0) { public IBinder onBind(Intent arg0) {
return null; return mBinder;
}
public class SyncedFolderObserverBinder extends Binder {
public SyncedFolderObserverService getService() {
return SyncedFolderObserverService.this;
} }
} }
}