Skip to content

Commit a99d2c9

Browse files
authored
Merge pull request #2747 from brandonpage/upgrade-account-type-login
Upgrade existing users if the app adds a unique account type.
2 parents f3fada9 + a3939e0 commit a99d2c9

6 files changed

Lines changed: 350 additions & 16 deletions

File tree

libs/SalesforceSDK/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
android:resource="@xml/authenticator" />
2828
</service>
2929

30+
<service android:exported="true"
31+
android:name="com.salesforce.androidsdk.auth.LegacyAuthenticatorService"
32+
tools:ignore="ExportedService">
33+
<intent-filter>
34+
<action android:name="android.accounts.AccountAuthenticator" />
35+
</intent-filter>
36+
<meta-data android:name="android.accounts.AccountAuthenticator"
37+
android:resource="@xml/legacy_authenticator" />
38+
</service>
39+
3040
<!-- Login activity -->
3141
<activity android:name="com.salesforce.androidsdk.ui.LoginActivity"
3242
android:theme="@style/SalesforceSDK"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:accountType="com.salesforce.androidsdk" android:icon="@drawable/sf__icon"
3+
android:smallIcon="@drawable/sf__icon" android:label="@string/app_name" />

libs/SalesforceSDK/src/com/salesforce/androidsdk/app/SalesforceSDKManager.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,16 @@ open class SalesforceSDKManager protected constructor(
11921192
open val isHybrid = false
11931193

11941194
/** The authentication account type, which should match authenticator.xml */
1195-
val accountType = appContext.getString(account_type)
1195+
val accountType: String by lazy {
1196+
val type = appContext.getString(account_type)
1197+
if (type == "com.salesforce.androidsdk") {
1198+
// TODO: Turn this logline into an assert in 14.0
1199+
e(TAG, "No app specific account type found. To ensure users " +
1200+
"can login override the \"account_type\" value in your strings.xml.")
1201+
}
1202+
1203+
return@lazy type
1204+
}
11961205

11971206
override fun toString() =
11981207
"""

libs/SalesforceSDK/src/com/salesforce/androidsdk/app/SalesforceSDKUpgradeManager.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
import static com.salesforce.androidsdk.security.ScreenLockManager.SCREEN_LOCK;
3131
import static com.salesforce.androidsdk.security.ScreenLockManager.SCREEN_LOCK_TIMEOUT;
3232

33+
import android.accounts.Account;
34+
import android.accounts.AccountManager;
3335
import android.content.Context;
3436
import android.content.SharedPreferences;
3537

3638
import com.salesforce.androidsdk.accounts.UserAccount;
39+
import com.salesforce.androidsdk.accounts.UserAccountManager;
3740
import com.salesforce.androidsdk.auth.HttpAccess;
3841
import com.salesforce.androidsdk.auth.OAuth2;
3942
import com.salesforce.androidsdk.config.AdminSettingsManager;
@@ -46,7 +49,6 @@
4649
import java.util.Map;
4750
import java.util.concurrent.Executors;
4851

49-
5052
/**
5153
* This class handles upgrades from one version to another.
5254
*
@@ -60,7 +62,7 @@ public class SalesforceSDKUpgradeManager {
6062

6163
private static SalesforceSDKUpgradeManager INSTANCE = null;
6264

63-
private UserManager userManager;
65+
private final UserManager userManager;
6466

6567
/**
6668
* Returns an instance of this class.
@@ -82,12 +84,7 @@ interface UserManager {
8284
}
8385

8486
public SalesforceSDKUpgradeManager() {
85-
this(new UserManager() {
86-
@Override
87-
public List<UserAccount> getAuthenticatedUsers() {
88-
return SalesforceSDKManager.getInstance().getUserAccountManager().getAuthenticatedUsers();
89-
}
90-
});
87+
this(() -> SalesforceSDKManager.getInstance().getUserAccountManager().getAuthenticatedUsers());
9188
}
9289
public SalesforceSDKUpgradeManager(UserManager userManager) {
9390
this.userManager = userManager;
@@ -130,6 +127,9 @@ else if (installedVersion.isGreaterThanOrEqualTo(new SdkVersion(9, 2, 0, false))
130127
if (installedVersion.isLessThan(new SdkVersion(12, 0, 0, false))) {
131128
updateFromBefore12_0_0();
132129
}
130+
if (installedVersion.isLessThan(new SdkVersion(15, 0, 0, false))) {
131+
migrateAccountType();
132+
}
133133
} catch (Exception e) {
134134
SalesforceSDKLogger.e(
135135
TAG,
@@ -306,4 +306,36 @@ private void updateFromBefore12_0_0() {
306306
PushMessaging.setReRegistrationRequested(true);
307307
}
308308

309+
310+
/*
311+
* Migrate any accounts with account_type "com.salesforce.androidsdk" to a unique value.
312+
* TODO: Remove this in Mobile SDK 15.0
313+
*/
314+
private void migrateAccountType() {
315+
final String LEGACY_ACCOUNT_TYPE = "com.salesforce.androidsdk";
316+
if (SalesforceSDKManager.getInstance().getAccountType().equals(LEGACY_ACCOUNT_TYPE)) {
317+
SalesforceSDKLogger.e(TAG, "No app specific account type found. To ensure users " +
318+
"can login override the \"account_type\" value in your strings.xml.");
319+
return;
320+
}
321+
322+
final AccountManager accountManager = SalesforceSDKManager.getInstance().getClientManager().getAccountManager();
323+
final UserAccountManager userAccountManager = SalesforceSDKManager.getInstance().getUserAccountManager();
324+
325+
for (Account account : accountManager.getAccountsByType(LEGACY_ACCOUNT_TYPE)) {
326+
try {
327+
final UserAccount userAccount = userAccountManager.buildUserAccount(account);
328+
if (userAccount == null) {
329+
SalesforceSDKLogger.e(TAG, "Unable to build UserAccount from account: " + account.name);
330+
continue;
331+
}
332+
333+
// Android OS accounts are immutable so we have to remove the account and add a new one.
334+
accountManager.removeAccountExplicitly(account);
335+
userAccountManager.createAccount(userAccount);
336+
} catch (Exception e) {
337+
SalesforceSDKLogger.e(TAG, "Failed to migrate account: " + account.name, e);
338+
}
339+
}
340+
}
309341
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2011-present, salesforce.com, inc.
3+
* All rights reserved.
4+
* Redistribution and use of this software in source and binary forms, with or
5+
* without modification, are permitted provided that the following conditions
6+
* are met:
7+
* - Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
* - Redistributions in binary form must reproduce the above copyright notice,
10+
* this list of conditions and the following disclaimer in the documentation
11+
* and/or other materials provided with the distribution.
12+
* - Neither the name of salesforce.com, inc. nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software without
14+
* specific prior written permission of salesforce.com, inc.
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
* POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
package com.salesforce.androidsdk.auth;
28+
29+
class LegacyAuthenticatorService extends AuthenticatorService { }

0 commit comments

Comments
 (0)