2016-01-19 12:51:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package at.bitfire.davdroid;
|
|
|
|
|
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import android.accounts.Account;
|
2016-01-19 23:39:10 +00:00
|
|
|
|
import android.accounts.AccountManager;
|
2016-09-02 09:57:16 +00:00
|
|
|
|
import android.annotation.SuppressLint;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import android.app.Service;
|
|
|
|
|
import android.content.Intent;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import android.database.DatabaseUtils;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import android.os.Binder;
|
|
|
|
|
import android.os.IBinder;
|
2016-03-31 10:21:58 +00:00
|
|
|
|
import android.support.annotation.NonNull;
|
2016-01-19 23:39:10 +00:00
|
|
|
|
import android.text.TextUtils;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
|
2016-05-20 19:38:04 +00:00
|
|
|
|
import java.lang.ref.WeakReference;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import java.util.HashSet;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
import java.util.Iterator;
|
2016-01-19 12:51:52 +00:00
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2016-02-24 22:08:19 +00:00
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB.OpenHelper;
|
|
|
|
|
import at.bitfire.davdroid.model.ServiceDB.Services;
|
2016-01-19 19:04:25 +00:00
|
|
|
|
|
2016-01-19 12:51:52 +00:00
|
|
|
|
public class DavService extends Service {
|
|
|
|
|
|
|
|
|
|
public static final String
|
2016-01-19 23:39:10 +00:00
|
|
|
|
ACTION_ACCOUNTS_UPDATED = "accountsUpdated",
|
2016-01-19 12:51:52 +00:00
|
|
|
|
EXTRA_DAV_SERVICE_ID = "davServiceID";
|
|
|
|
|
|
|
|
|
|
private final IBinder binder = new InfoBinder();
|
|
|
|
|
|
|
|
|
|
private final Set<Long> runningRefresh = new HashSet<>();
|
2016-05-20 19:38:04 +00:00
|
|
|
|
private final List<WeakReference<RefreshingStatusListener>> refreshingStatusListeners = new LinkedList<>();
|
2016-01-19 12:51:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2016-04-10 13:41:10 +00:00
|
|
|
|
if (intent != null) {
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
long id = intent.getLongExtra(EXTRA_DAV_SERVICE_ID, -1);
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case ACTION_ACCOUNTS_UPDATED:
|
|
|
|
|
cleanupAccounts();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-01-19 12:51:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* BOUND SERVICE PART
|
|
|
|
|
for communicating with the activities
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return binder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface RefreshingStatusListener {
|
|
|
|
|
void onDavRefreshStatusChanged(long id, boolean refreshing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InfoBinder extends Binder {
|
|
|
|
|
public boolean isRefreshing(long id) {
|
|
|
|
|
return runningRefresh.contains(id);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 19:38:04 +00:00
|
|
|
|
public void addRefreshingStatusListener(@NonNull RefreshingStatusListener listener, boolean callImmediate) {
|
|
|
|
|
refreshingStatusListeners.add(new WeakReference<>(listener));
|
2016-01-19 12:51:52 +00:00
|
|
|
|
if (callImmediate)
|
|
|
|
|
for (long id : runningRefresh)
|
|
|
|
|
listener.onDavRefreshStatusChanged(id, true);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 19:38:04 +00:00
|
|
|
|
public void removeRefreshingStatusListener(@NonNull RefreshingStatusListener listener) {
|
|
|
|
|
for (Iterator<WeakReference<RefreshingStatusListener>> iterator = refreshingStatusListeners.iterator(); iterator.hasNext(); ) {
|
|
|
|
|
RefreshingStatusListener item = iterator.next().get();
|
|
|
|
|
if (listener.equals(item))
|
|
|
|
|
iterator.remove();
|
|
|
|
|
}
|
2016-01-19 12:51:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ACTION RUNNABLES
|
|
|
|
|
which actually do the work
|
|
|
|
|
*/
|
|
|
|
|
|
2016-09-02 09:57:16 +00:00
|
|
|
|
@SuppressLint("MissingPermission")
|
2016-01-19 23:39:10 +00:00
|
|
|
|
void cleanupAccounts() {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
App.log.info("Cleaning up orphaned accounts");
|
2016-01-19 23:39:10 +00:00
|
|
|
|
|
|
|
|
|
final OpenHelper dbHelper = new OpenHelper(this);
|
|
|
|
|
try {
|
|
|
|
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
|
|
List<String> sqlAccountNames = new LinkedList<>();
|
|
|
|
|
AccountManager am = AccountManager.get(this);
|
|
|
|
|
for (Account account : am.getAccountsByType(Constants.ACCOUNT_TYPE))
|
|
|
|
|
sqlAccountNames.add(DatabaseUtils.sqlEscapeString(account.name));
|
|
|
|
|
|
|
|
|
|
if (sqlAccountNames.isEmpty())
|
|
|
|
|
db.delete(Services._TABLE, null, null);
|
|
|
|
|
else
|
|
|
|
|
db.delete(Services._TABLE, Services.ACCOUNT_NAME + " NOT IN (" + TextUtils.join(",", sqlAccountNames) + ")", null);
|
|
|
|
|
} finally {
|
|
|
|
|
dbHelper.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 12:51:52 +00:00
|
|
|
|
}
|