Skip to content

Commit 7b21d35

Browse files
committed
Upgrade existing users if the app adds a unique account type.
1 parent b2a169b commit 7b21d35

4 files changed

Lines changed: 80 additions & 1 deletion

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/SalesforceSDKUpgradeManager.java

Lines changed: 38 additions & 1 deletion
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
*
@@ -130,6 +132,9 @@ else if (installedVersion.isGreaterThanOrEqualTo(new SdkVersion(9, 2, 0, false))
130132
if (installedVersion.isLessThan(new SdkVersion(12, 0, 0, false))) {
131133
updateFromBefore12_0_0();
132134
}
135+
if (installedVersion.isLessThan(new SdkVersion(15, 0, 0, false))) {
136+
migrateAccountType();
137+
}
133138
} catch (Exception e) {
134139
SalesforceSDKLogger.e(
135140
TAG,
@@ -306,4 +311,36 @@ private void updateFromBefore12_0_0() {
306311
PushMessaging.setReRegistrationRequested(true);
307312
}
308313

314+
315+
/*
316+
* Migrate any accounts with account_type "com.salesforce.androidsdk" to a unique value.
317+
* TODO: Remove this in Mobile SDK 15.0
318+
*/
319+
private void migrateAccountType() {
320+
final String LEGACY_ACCOUNT_TYPE = "com.salesforce.androidsdk";
321+
if (SalesforceSDKManager.getInstance().getAccountType().equals(LEGACY_ACCOUNT_TYPE)) {
322+
SalesforceSDKLogger.e(TAG, "No app specific account type found. To ensure users " +
323+
"can login override the account_type value in strings.xml.");
324+
return;
325+
}
326+
327+
final AccountManager accountManager = SalesforceSDKManager.getInstance().getClientManager().getAccountManager();
328+
final UserAccountManager userAccountManager = SalesforceSDKManager.getInstance().getUserAccountManager();
329+
330+
for (Account account : accountManager.getAccountsByType(LEGACY_ACCOUNT_TYPE)) {
331+
try {
332+
final UserAccount userAccount = userAccountManager.buildUserAccount(account);
333+
if (userAccount == null) {
334+
SalesforceSDKLogger.e(TAG, "Unable to build UserAccount from account: " + account.name);
335+
continue;
336+
}
337+
338+
// Android OS accounts immutable so we have to remove the account and add a new one.
339+
accountManager.removeAccountExplicitly(account);
340+
userAccountManager.createAccount(userAccount);
341+
} catch (Exception e) {
342+
SalesforceSDKLogger.e(TAG, "Failed to migrate account: " + account.name, e);
343+
}
344+
}
345+
}
309346
}
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)