-
Notifications
You must be signed in to change notification settings - Fork 385
/
ServerPickerActivity.java
326 lines (292 loc) · 11.9 KB
/
ServerPickerActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2014-present, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.ui;
import static com.salesforce.androidsdk.security.BiometricAuthenticationManager.SHOW_BIOMETRIC;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.window.OnBackInvokedDispatcher;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import com.salesforce.androidsdk.R;
import com.salesforce.androidsdk.app.SalesforceSDKManager;
import com.salesforce.androidsdk.config.LoginServerManager;
import com.salesforce.androidsdk.config.LoginServerManager.LoginServer;
import com.salesforce.androidsdk.config.RuntimeConfig;
import com.salesforce.androidsdk.security.interfaces.BiometricAuthenticationManager;
import java.util.List;
import kotlin.Unit;
/**
* This class provides UI to change the login server URL to use
* during an OAuth flow. The user can add a number of custom servers,
* or switch between the list of existing servers in the list.
*
* @author bhariharan
*/
public class ServerPickerActivity extends AppCompatActivity implements
android.widget.RadioGroup.OnCheckedChangeListener {
private static final String SERVER_DIALOG_NAME = "custom_server_dialog";
private CustomServerUrlEditor urlEditDialog;
private LoginServerManager loginServerManager;
/**
* Clears any custom URLs that may have been set.
*/
private void clearCustomUrlSetting() {
loginServerManager.reset();
rebuildDisplay();
urlEditDialog = new CustomServerUrlEditor();
}
/**
* Sets the return value of the activity. Selection is stored in the
* shared prefs file, AuthActivity pulls from the file or a default value.
*/
@SuppressLint("MissingSuperCall")
@Override
public void onBackPressed() {
reconfigureAuthorization();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group != null) {
final SalesforceServerRadioButton rb = group.findViewById(checkedId);
if (rb != null) {
final String name = rb.getName();
final String url = rb.getUrl();
boolean isCustom = rb.isCustom();
loginServerManager.setSelectedLoginServer(new LoginServer(name,
url, isCustom));
}
}
}
@Override
public boolean onNavigateUp() {
onBackPressed();
return true;
}
/**
* Called when the 'Reset' button is clicked. Clears custom URLs.
*
* @param v View that was clicked.
*/
public void onResetClick(View v) {
clearCustomUrlSetting();
}
/**
* Returns the server list group ID.
*
* @return Server list group ID.
*/
protected int getServerListGroupId() {
return R.id.sf__server_list_group;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isDarkTheme = SalesforceSDKManager.getInstance().isDarkTheme();
setTheme(isDarkTheme ? R.style.SalesforceSDK_Dark : R.style.SalesforceSDK);
// This makes the navigation bar visible on light themes.
SalesforceSDKManager.getInstance().setViewNavigationVisibility(this);
loginServerManager = SalesforceSDKManager.getInstance().getLoginServerManager();
setContentView(R.layout.sf__server_picker);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(R.string.sf__server_picker_title);
actionBar.setDisplayHomeAsUpEnabled(true);
}
/*
* Hides the 'Add Connection' button if the MDM variable to disable
* adding of custom hosts is set.
*/
final Button addConnectionButton = findViewById(R.id.sf__show_custom_url_edit);
if (addConnectionButton != null) {
if (RuntimeConfig.getRuntimeConfig(this).getBoolean(RuntimeConfig.ConfigKey.OnlyShowAuthorizedHosts)) {
addConnectionButton.setVisibility(View.GONE);
}
}
final RadioGroup radioGroup = findViewById(getServerListGroupId());
radioGroup.setOnCheckedChangeListener(this);
urlEditDialog = new CustomServerUrlEditor();
if (SalesforceSDKManager.getInstance().compactScreen(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
}
// TODO: Remove this when min API > 33
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
this::onBackPressed
);
}
}
@Override
public void onResume() {
super.onResume();
rebuildDisplay();
}
@Override
public void onDestroy() {
final RadioGroup radioGroup = findViewById(getServerListGroupId());
radioGroup.setOnCheckedChangeListener(null);
urlEditDialog = null;
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sf__clear_custom_url, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
reconfigureAuthorization();
finish();
return true;
} else if (item.getItemId() == R.id.sf__menu_clear_custom_url) {
clearCustomUrlSetting();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
/**
* Shows the custom URL dialog.
*
* @param v View.
*/
public void showCustomUrlDialog(View v) {
final FragmentManager fragMgr = getSupportFragmentManager();
// Adds fragment only if it has not been added already.
if (!urlEditDialog.isAdded()) {
urlEditDialog.show(fragMgr, SERVER_DIALOG_NAME);
}
}
/**
* Returns the custom URL editor dialog.
*
* @return Custom URL editor dialog.
*/
public CustomServerUrlEditor getCustomServerUrlEditor() {
return urlEditDialog;
}
/**
* Refreshes the authorization configuration.
*/
private void reconfigureAuthorization() {
SalesforceSDKManager.getInstance().fetchAuthenticationConfiguration(() -> {
onAuthConfigFetched();
return Unit.INSTANCE;
});
}
/**
* Sets the radio state.
*
* @param radioGroup RadioGroup instance.
* @param server Login server.
*/
private void setRadioState(RadioGroup radioGroup, LoginServer server) {
final SalesforceServerRadioButton rb = new SalesforceServerRadioButton(this,
server.name, server.url, server.isCustom);
boolean isDarkTheme = SalesforceSDKManager.getInstance().isDarkTheme();
int textColor = getColor(isDarkTheme ? R.color.sf__text_color_dark : R.color.sf__text_color);
rb.setTextColor(textColor);
final Drawable buttonDrawable = rb.getButtonDrawable();
if (buttonDrawable != null) {
buttonDrawable.setTint(getColor(R.color.sf__primary_color));
}
radioGroup.addView(rb);
((ScrollView) radioGroup.getParent()).scrollTo(0, radioGroup.getBottom());
}
/**
* Controls the elements in the layout based on past user choices.
*/
protected void setupRadioButtons() {
final RadioGroup radioGroup = findViewById(getServerListGroupId());
final List<LoginServer> servers = loginServerManager.getLoginServers();
if (servers != null) {
for (final LoginServer currentServer : servers) {
setRadioState(radioGroup, currentServer);
}
}
}
/**
* Rebuilds the display.
*/
public void rebuildDisplay() {
final RadioGroup radioGroup = findViewById(getServerListGroupId());
radioGroup.removeAllViews();
setupRadioButtons();
// Sets selected server.
final LoginServer selectedServer = loginServerManager.getSelectedLoginServer();
int numServers = radioGroup.getChildCount();
for (int i = 0; i < numServers; i++) {
final SalesforceServerRadioButton rb = (SalesforceServerRadioButton) radioGroup.getChildAt(i);
if (rb != null) {
final LoginServer loginServer = new LoginServer(rb.getName(), rb.getUrl(), rb.isCustom());
if (loginServer.equals(selectedServer)) {
rb.setChecked(true);
}
}
}
}
public void onAuthConfigFetched() {
setResult(Activity.RESULT_OK, null);
Context ctx = SalesforceSDKManager.getInstance().getAppContext();
Bundle options = SalesforceSDKManager.getInstance().getLoginOptions().asBundle();
Intent intent = new Intent(ctx, SalesforceSDKManager.getInstance().getWebviewLoginActivityClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
BiometricAuthenticationManager bioAuthManager =
SalesforceSDKManager.getInstance().getBiometricAuthenticationManager();
if (bioAuthManager != null && bioAuthManager.isLocked()) {
options.putBoolean(SHOW_BIOMETRIC, true);
}
intent.putExtras(options);
/*
* The activity needs to be recreated (instead of just reloading the webview)
* to support the transition from custom tab to standard webview login. If we do this
* with the CLEAR_TOP flag we ensure unnecessary Custom Tabs are destroyed.
*
* The only other way to do this is with the NO_HISTORY flag on the custom tab, however
* this will cause it to always reload on background -- breaking MFA.
*/
ctx.startActivity(intent);
finish();
}
}