diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b26a8452abca51ea785757230b5deedeece26da9..ceff9bbadb089fbccea45d29ce21519c0040595d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,3 +25,4 @@ publish: stage: publish script: - ./gradlew :lib:publish + when: manual diff --git a/lib/build.gradle b/lib/build.gradle index 9aa9ed7e1b300ddb1007649c1192436c4520f968..67a353c12921efea7ae7f98edf66af2dbf4afe4c 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -18,8 +18,8 @@ group = 'foundation.e.lib' def versionMajor = 1 def versionMinor = 0 -def versionPatch = 5 -def releasePatch = "alpha" +def versionPatch = 8 +def releasePatch = "release" def libName = "Android-SingleSignOn" diff --git a/lib/src/main/java/com/nextcloud/android/sso/AccountImporter.java b/lib/src/main/java/com/nextcloud/android/sso/AccountImporter.java index d00c96f38ac39848e52259651d12e8d9147cc4e7..76f646d6242e940d84d15ac4a6fe09c05ebf5ec1 100644 --- a/lib/src/main/java/com/nextcloud/android/sso/AccountImporter.java +++ b/lib/src/main/java/com/nextcloud/android/sso/AccountImporter.java @@ -422,7 +422,7 @@ public class AccountImporter { } } - protected static String getPrefKeyForAccount(String accountName) { + public static String getPrefKeyForAccount(String accountName) { return PREF_ACCOUNT_STRING + accountName; } @@ -434,4 +434,20 @@ public class AccountImporter { public static void setSharedPreferences(SharedPreferences sharedPreferences) { AccountImporter.SHARED_PREFERENCES = sharedPreferences; } + + @NonNull + public static SingleSignOnAccount pickLocalAccount(@NonNull Context context) { + final String accountName = Constants.LOCAL_ACCOUNT_NAME; + final SingleSignOnAccount ssoAccount = new SingleSignOnAccount(accountName, accountName, "", "", Constants.LOCAL_ACCOUNT_TYPE); + + final SharedPreferences preferences = getSharedPreferences(context); + final String preferenceKey = getPrefKeyForAccount(accountName); + try { + preferences.edit().putString(preferenceKey, SingleSignOnAccount.toString(ssoAccount)).apply(); + } catch (IOException e) { + Log.e(TAG, "pickLocalAccount failed", e); + } + + return ssoAccount; + } } diff --git a/lib/src/main/java/com/nextcloud/android/sso/Constants.java b/lib/src/main/java/com/nextcloud/android/sso/Constants.java index ff3aa3a73f448cb762ee64cc3c1cb3f48ef34361..349d0a4d6eca1f8cc1434aa40d4e85e083ea6562 100644 --- a/lib/src/main/java/com/nextcloud/android/sso/Constants.java +++ b/lib/src/main/java/com/nextcloud/android/sso/Constants.java @@ -38,4 +38,6 @@ public class Constants { public static final String EXCEPTION_HTTP_REQUEST_FAILED = "CE_5"; public static final String EXCEPTION_ACCOUNT_ACCESS_DECLINED = "CE_6"; + public static final String LOCAL_ACCOUNT_NAME = "Local"; + public static final String LOCAL_ACCOUNT_TYPE = "eos.android-sso.account.local"; } diff --git a/lib/src/main/java/com/nextcloud/android/sso/helper/SingleAccountHelper.java b/lib/src/main/java/com/nextcloud/android/sso/helper/SingleAccountHelper.java index 8d669786f1a012c2c718e750480aad795700635c..721ebcf151c65cf483d7845126606bf632525058 100644 --- a/lib/src/main/java/com/nextcloud/android/sso/helper/SingleAccountHelper.java +++ b/lib/src/main/java/com/nextcloud/android/sso/helper/SingleAccountHelper.java @@ -24,17 +24,25 @@ import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import com.nextcloud.android.sso.AccountImporter; +import com.nextcloud.android.sso.Constants; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountPermissionNotGrantedException; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotSupportedException; import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; import com.nextcloud.android.sso.model.SingleSignOnAccount; +import java.io.IOException; + +import trikita.log.Log; + public final class SingleAccountHelper { + private static final String TAG = SingleAccountHelper.class.getSimpleName(); + private static final String PREF_CURRENT_ACCOUNT_STRING = "PREF_CURRENT_ACCOUNT_STRING"; private SingleAccountHelper() { @@ -93,4 +101,29 @@ public final class SingleAccountHelper { .unregisterOnSharedPreferenceChangeListener(listener); } + + public static boolean isLocalAccount(@NonNull SingleSignOnAccount account) { + return account.type.equals(Constants.LOCAL_ACCOUNT_TYPE); + } + + public static boolean isLocalAccount(@NonNull Context context, @NonNull String accountName) + throws NextcloudFilesAppAccountNotFoundException { + final SharedPreferences preferences = AccountImporter.getSharedPreferences(context); + final String preferenceKey = AccountImporter.getPrefKeyForAccount(accountName); + + if (preferences.contains(preferenceKey)) { + try { + final SingleSignOnAccount account = SingleSignOnAccount.fromString(preferences.getString(preferenceKey, null)); + + if (account == null) { + throw new NextcloudFilesAppAccountNotFoundException(); + } + + return account.type.equals(Constants.LOCAL_ACCOUNT_TYPE); + } catch (ClassNotFoundException | IOException e) { + Log.e(TAG, "[isLocalAccount(context, accountName)]", e); + } + } + throw new NextcloudFilesAppAccountNotFoundException(); + } }