Skip to content

Commit

Permalink
sync: Add base check provider add for a single account per provider
Browse files Browse the repository at this point in the history
Update the Box, Dropbox, and OneDrive providers to use the base class
method.  The GDrive and ownCloud have their own implementations.
  • Loading branch information
jefftharris committed Jan 1, 2025
1 parent 6cf7674 commit 18b2e49
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (©) 2017 Jeff Harris <[email protected]>
* Copyright (©) 2017-2025 Jeff Harris <[email protected]>
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
Expand All @@ -13,7 +13,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -40,8 +39,6 @@
import com.jefftharris.passwdsafe.sync.lib.SyncDb;
import com.jefftharris.passwdsafe.sync.lib.SyncLogRecord;

import java.util.List;

/**
* Implements a provider for the Box.com service
*/
Expand Down Expand Up @@ -160,17 +157,6 @@ public Account getAccount(String acctName)
return new Account(acctName, SyncDb.BOX_ACCOUNT_TYPE);
}

@Override
public void checkProviderAdd(SQLiteDatabase db) throws Exception
{
List<DbProvider> providers = SyncDb.getProviders(db);
for (DbProvider provider: providers) {
if (provider.itsType == ProviderType.BOX) {
throw new Exception("Only one Box account allowed");
}
}
}

@Override
public void cleanupOnDelete()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
Expand Down Expand Up @@ -184,18 +183,6 @@ public Account getAccount(String acctName)
}


@Override
public void checkProviderAdd(SQLiteDatabase db) throws Exception
{
List<DbProvider> providers = SyncDb.getProviders(db);
for (DbProvider provider: providers) {
if (provider.itsType == ProviderType.DROPBOX) {
throw new Exception("Only one Dropbox account allowed");
}
}
}


@Override
public void cleanupOnDelete()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

import android.accounts.Account;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import androidx.annotation.CallSuper;
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;

import com.jefftharris.passwdsafe.lib.PasswdSafeUtil;
import com.jefftharris.passwdsafe.lib.ProviderType;

import java.util.List;

/**
* Abstract provider that uses a system timer to perform syncing
*/
Expand Down Expand Up @@ -71,6 +75,25 @@ public synchronized void setPendingAdd(boolean pending)
itsIsPendingAdd = pending;
}

/**
* Check whether a provider can be added. By default, only a single
* account can be added for a provider type.
*/
@Override
@MainThread
public void checkProviderAdd(SQLiteDatabase db)
throws Exception
{
List<DbProvider> providers = SyncDb.getProviders(db);
for (DbProvider provider: providers) {
if (provider.itsType == itsProviderType) {
throw new Exception(
String.format("Only one %s account allowed",
itsProviderType.getName(itsContext)));
}
}
}

@Override
public void updateSyncFreq(Account acct, final int freq)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
Expand Down Expand Up @@ -54,7 +53,6 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -230,27 +228,9 @@ public boolean isAccountAuthorized()
@MainThread
public Account getAccount(String acctName)
{
// TODO: Add base-class impl to use account type
return new Account(acctName, SyncDb.ONEDRIVE_ACCOUNT_TYPE);
}

/**
* Check whether a provider can be added
*/
@Override
@MainThread
public void checkProviderAdd(SQLiteDatabase db)
throws Exception
{
// TODO: Add base-class impl to check against type
List<DbProvider> providers = SyncDb.getProviders(db);
for (DbProvider provider: providers) {
if (provider.itsType == ProviderType.ONEDRIVE) {
throw new Exception("Only one OneDrive account allowed");
}
}
}

/**
* Cleanup a provider when deleted
*/
Expand Down

0 comments on commit 18b2e49

Please sign in to comment.