/
ServerPickerActivity.java
528 lines (443 loc) · 15.7 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright (c) 2011, 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 java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.TextAppearanceSpan;
import android.util.Pair;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView.BufferType;
import com.salesforce.androidsdk.app.ForceApp;
import com.salesforce.androidsdk.auth.OAuth2;
/**
*
* UI to change the login server url to use during an oAuth flow
*
* If the user selects one of the prefabs, save the selected index and the final
* url into SharedPrefs[SERVER_URL_PREFS_SETTINGS] If the user selects a custom
* url, that subsystem saves the final validated not null label and url AND
* saves the selectedindex.
*/
public class ServerPickerActivity extends Activity implements
OnDismissListener, OnCancelListener, View.OnClickListener,
android.widget.RadioGroup.OnCheckedChangeListener {
private static class SavedConfig {
public final int currentSelection;
public final int originalStartingIndex;
SavedConfig(int currentSelection, int originalStartingIndex) {
this.currentSelection = currentSelection;
this.originalStartingIndex = originalStartingIndex;
}
}
private static final int DEFAULT_URL_SELECTION = 0;
private static final int SERVER_DIALOG_ID = 0;
// custom url radio button has, sigh, custom event handling
private int customRadioButtonId = -1;
private ArrayList<Pair<String, String>> defaultServers;
private Logger logger;
private int restoredConfigIndex = -1;
// used to restore radio index on a cancel (case where it gets saved by the
// edit dialog)
private int startingIndex = -1;
/** who we are in the logs */
protected final String TAG = this.getClass().getSimpleName();
public CustomServerUrlEditor urlEditDialog;
private SalesforceR salesforceR;
/**
* hooks for the edit url dialog dismiss is positive (apply) cancel is back
* or cancel onDimiss is still called when the dialog cancel() method is
* invoked...
*/
boolean wasEditUrlDialogCanceled = false;
private void clearCustomUrlSetting() {
SharedPreferences settings = getSharedPreferences(
LoginActivity.SERVER_URL_PREFS_SETTINGS,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
rebuildDisplay();
}
public int getIndexForCustomUrl() {
return defaultServers.size();
}
@Override
public void onBackPressed() {
setCancelReturnValue(null);
}
@Override
public void onCancel(DialogInterface dialog) {
wasEditUrlDialogCanceled = true;
}
/**
* support rogue radio button, only gets hooked when there is a custom url
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// right? some things like this work, but most don't
if (checkedId < 0 || customRadioButtonId < 0
|| checkedId == customRadioButtonId) {
return;
}
if (((RadioButton) group.findViewById(checkedId)).isChecked()) {
// turn off rogue by hand (may or may not actually be checked)
RadioButton rb = (RadioButton) group
.findViewById(customRadioButtonId);
if (rb != null) {
rb.setChecked(false);
} else {
logger.logp(Level.WARNING, TAG, "onCheckedChanged",
"Failed to find custom URL radio");
}
}
}
/**
* custom handling for rogue radio button (custom link) actual click of the
* rogue
*/
@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
if (rb.isChecked()) {
RadioGroup radioGroup = (RadioGroup) findViewById(getServerListGroupId());
radioGroup.clearCheck();
}
}
protected int getServerListGroupId() {
return salesforceR.idServerListGroup();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Object which allows reference to resources living outside the SDK
salesforceR = ForceApp.APP.getSalesforceR();
setContentView(salesforceR.layoutServerPicker());
defaultServers = new ArrayList<Pair<String, String>>();
// start in one place for standard urls
defaultServers.add(new Pair<String, String>(
getString(salesforceR.stringAuthLoginProduction()),
OAuth2.DEFAULT_LOGIN_URL));
defaultServers.add(new Pair<String, String>(
getString(salesforceR.stringAuthLoginSandbox()),
OAuth2.SANDBOX_LOGIN_URL));
SavedConfig savedConfig = (SavedConfig) getLastNonConfigurationInstance();
if (null == savedConfig) {
// save starting state to restore on cancel.
saveStartingState();
} else {
startingIndex = savedConfig.originalStartingIndex;
restoredConfigIndex = savedConfig.currentSelection;
}
rebuildDisplay();
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == SERVER_DIALOG_ID) {
urlEditDialog = new CustomServerUrlEditor(this);
urlEditDialog.setOnDismissListener(this);
urlEditDialog.setOnCancelListener(this);
return urlEditDialog;
}
return super.onCreateDialog(id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(salesforceR.menuClearCustomUrl(), menu);
return true;
}
@Override
public void onDismiss(DialogInterface dialog) {
if (!wasEditUrlDialogCanceled) {
rebuildDisplay();
}
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if(item.getItemId() == salesforceR.idMenuClearCustomUrl()) {
clearCustomUrlSetting();
return true;
}
else {
return super.onMenuItemSelected(featureId, item);
}
}
/**
* this is called by the OS before it kills & restarts the activity due to
* device rotation, we save our state here, and recreate it in create, see
* http
* ://developer.android.com/resources/articles/faster-screen-orientation-
* change.html
*/
@Override
public Object onRetainNonConfigurationInstance() {
RadioGroup radioGroup = (RadioGroup) findViewById(getServerListGroupId());
return new SavedConfig(radioGroup.getCheckedRadioButtonId(),
startingIndex);
}
private void rebuildDisplay() {
RadioGroup radioGroup = (RadioGroup) findViewById(getServerListGroupId());
radioGroup.removeAllViews();
radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(null);
customRadioButtonId = -1;
setupRadioButtons();
}
private void restoreStartingState() {
SharedPreferences settings = getSharedPreferences(
LoginActivity.SERVER_URL_PREFS_SETTINGS,
Context.MODE_PRIVATE);
Editor edit = settings.edit();
edit.putInt(LoginActivity.SERVER_URL_PREFS_WHICH_SERVER,
startingIndex);
edit.commit();
}
/**
* restore the starting index if the user cancels after setting a custom url
*/
private void saveStartingState() {
SharedPreferences settings = getSharedPreferences(
LoginActivity.SERVER_URL_PREFS_SETTINGS,
Context.MODE_PRIVATE);
startingIndex = settings.getInt(
LoginActivity.SERVER_URL_PREFS_WHICH_SERVER,
DEFAULT_URL_SELECTION);
}
/**
* save the current selection state into shared prefs
*
* @return
*/
private void saveUrlEdits() {
// the id is also the index, but use findViewById to get the actual
// radio button,
// not childAtIndex, which can be anything
RadioGroup radioGroup = (RadioGroup) findViewById(getServerListGroupId());
// if the selected item is the custom radio this won't return it
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == -1) {
selectedId = customRadioButtonId;
}
View selectedView = radioGroup.findViewById(selectedId);
if (selectedView == null) {
// should never happen, but you know how that goes
logger.logp(Level.WARNING, TAG, "saveUrlEdits",
"Failed to save state, could not find a selected URL");
rebuildDisplay();
return;
}
String selectedUrl = selectedView.getTag().toString();
SharedPreferences settings = getSharedPreferences(
LoginActivity.SERVER_URL_PREFS_SETTINGS,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// used here
editor.putInt(LoginActivity.SERVER_URL_PREFS_WHICH_SERVER,
selectedId);
// used by auth flow etc.
editor.putString(LoginActivity.SERVER_URL_CURRENT_SELECTION,
selectedUrl);
editor.commit();
}
/**
* clicked cancel or back button
*
* @param v
*/
public void setCancelReturnValue(View v) {
setResult(Activity.RESULT_CANCELED, null);
restoreStartingState();
finish();
}
/**
* set the retval of the activity. selection is stored in the prefs,
* AuthActivity pull from the prefs or an OAuth2 default
*
* @param v
*/
public void setPositiveReturnValue(View v) {
saveUrlEdits();
setResult(Activity.RESULT_OK, null);
finish();
}
/**
* the index is set as the id of the actual radio buttons other view do not
* get an explicit id, but it means you can't get radio buttons by
* selectedId -> childAtIndex, use findViewById
*
* @param radioGroup
* @param index
* @param isCustom
* @param titleText
* @param urlText
*/
private void setRadioState(RadioGroup radioGroup, int index,
boolean isCustom, String titleText, String urlText) {
RadioButton rb = new RadioButton(this);
rb.setId(index);
SpannableStringBuilder result = new SpannableStringBuilder();
SpannableString titleSpan = new SpannableString(titleText);
titleSpan.setSpan(new TextAppearanceSpan(this,
android.R.style.TextAppearance_Medium), 0, titleText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString urlSpan = new SpannableString(urlText);
urlSpan.setSpan(new TextAppearanceSpan(this,
android.R.style.TextAppearance_Small), 0, urlText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
result.append(titleSpan);
result.append(System.getProperty("line.separator"));
result.append(urlSpan);
rb.setText(result, BufferType.SPANNABLE);
rb.setTag(urlText);
if (isCustom) {
// keep the edit link always to the right of the row
final LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
rowParams.weight = 1;
final LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.weight = (float) 0.75;
final LinearLayout.LayoutParams linkParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
// interesting side effect of this? if the url gets big, the runtime
// will shrink this guy, doesn't matter if the w/h are set
// linkParams.weight=(float)0.25;
linkParams.gravity = Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL;
ImageView iv = new ImageView(this);
iv.setImageResource(salesforceR.drawableEditIcon());
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomUrlDialog(null);
}
});
customRadioButtonId = index;
// problem here is that if the radio button is not the top level
// child view when adding,
// the group does not control it (check the radio group code) so do
// it by hand.
// **It sort of controls it, you can select it through through the
// group but it does not get deselected correctly and is not
// returned by a get selected call on the group
rb.setOnClickListener(this);
LinearLayout lay = new LinearLayout(this);
lay.addView(rb, 0, buttonParams);
lay.addView(iv, 1, linkParams);
radioGroup.addView(lay, rowParams);
radioGroup.setOnCheckedChangeListener(this);
} else {
radioGroup.addView(rb);
}
// spacer line
View spacerView = new View(this);
spacerView.setBackgroundColor(0xdcdcdc);
LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, 1);
spacerView.setLayoutParams(lp);
radioGroup.addView(spacerView);
}
/**
* control the elements in the layout based on past user choices said
* another way, select the correct radio button, show the custom url if set
*/
private void setupRadioButtons() {
SharedPreferences settings = getSharedPreferences(
LoginActivity.SERVER_URL_PREFS_SETTINGS,
Context.MODE_PRIVATE);
RadioGroup radioGroup = (RadioGroup) findViewById(getServerListGroupId());
int index = 0;
for (Pair<String, String> currServer : defaultServers) {
setRadioState(radioGroup, index, false, currServer.first,
currServer.second);
index++;
}
// custom url set, may or may not be selected. ui is a radio with an
// edit link either way.
View postView = findViewById(salesforceR.idShowCustomUrlEdit());
if (settings.getString(
LoginActivity.SERVER_URL_PREFS_CUSTOM_URL, null) != null) {
postView.setVisibility(View.GONE);
// add another radio button with an edit link set to the custom
// state, track the id correctly
setRadioState(
radioGroup,
getIndexForCustomUrl(),
true,
settings
.getString(
LoginActivity.SERVER_URL_PREFS_CUSTOM_LABEL,
"Error"), settings.getString(
LoginActivity.SERVER_URL_PREFS_CUSTOM_URL,
"Error"));
} else {
postView.setVisibility(View.VISIBLE);
}
// set selection
int which = -1;
if (restoredConfigIndex >= 0) {
which = restoredConfigIndex;
restoredConfigIndex = -1;
} else {
which = settings.getInt(
LoginActivity.SERVER_URL_PREFS_WHICH_SERVER,
DEFAULT_URL_SELECTION);
}
radioGroup.check(which);
}
public void showCustomUrlDialog(View v) {
// once a dialog is created once, onCreateDialog will most likely not
// get called again
// so setup any state here
wasEditUrlDialogCanceled = false;
showDialog(SERVER_DIALOG_ID);
}
}