|
24 | 24 | import com.microsoft.identity.client.User; |
25 | 25 |
|
26 | 26 | import java.util.List; |
| 27 | +import java.util.concurrent.ExecutionException; |
27 | 28 |
|
28 | 29 | /** |
29 | 30 | * Starting Activity of the app. Handles the connection to Office 365. |
@@ -51,29 +52,30 @@ protected void onCreate(Bundle savedInstanceState) { |
51 | 52 | super.onCreate(savedInstanceState); |
52 | 53 |
|
53 | 54 |
|
54 | | - Bundle extras = getIntent().getExtras(); |
55 | | - setContentView(R.layout.activity_connect); |
56 | | - setTitle(R.string.app_name); |
| 55 | + Bundle extras = getIntent().getExtras(); |
| 56 | + setContentView(R.layout.activity_connect); |
| 57 | + setTitle(R.string.app_name); |
57 | 58 |
|
58 | | - // set up our views |
59 | | - mConnectButton = (Button) findViewById(R.id.connectButton); |
60 | | - mConnectProgressBar = (ProgressBar) findViewById(R.id.connectProgressBar); |
61 | | - mTitleTextView = (TextView) findViewById(R.id.titleTextView); |
62 | | - mDescriptionTextView = (TextView) findViewById(R.id.descriptionTextView); |
| 59 | + // set up our views |
| 60 | + mConnectButton = (Button) findViewById(R.id.connectButton); |
| 61 | + mConnectProgressBar = (ProgressBar) findViewById(R.id.connectProgressBar); |
| 62 | + mTitleTextView = (TextView) findViewById(R.id.titleTextView); |
| 63 | + mDescriptionTextView = (TextView) findViewById(R.id.descriptionTextView); |
63 | 64 |
|
64 | | - Connect.getInstance().setConnectActivity(this); |
65 | | - // add click listener |
66 | | - mConnectButton.setOnClickListener(new View.OnClickListener() { |
67 | | - @Override |
68 | | - public void onClick(View v) { |
69 | | - showConnectingInProgressUI(); |
70 | | - connect(); |
71 | | - } |
72 | | - }); |
| 65 | + Connect.getInstance().setConnectActivity(this); |
| 66 | + // add click listener |
| 67 | + mConnectButton.setOnClickListener(new View.OnClickListener() { |
| 68 | + @Override |
| 69 | + public void onClick(View v) { |
| 70 | + showConnectingInProgressUI(); |
| 71 | + connect(); |
| 72 | + } |
| 73 | + }); |
73 | 74 |
|
74 | 75 | } |
75 | 76 |
|
76 | 77 | private void connect() { |
| 78 | + try { |
77 | 79 |
|
78 | 80 | // The sample app is having the PII enable setting on the MainActivity. Ideally, app should decide to enable Pii or not, |
79 | 81 | // if it's enabled, it should be the setting when the application is onCreate. |
@@ -110,11 +112,22 @@ private void connect() { |
110 | 112 | } |
111 | 113 | } catch (MsalClientException e) { |
112 | 114 | Log.d(TAG, "MSAL Exception Generated while getting users: " + e.toString()); |
| 115 | + showConnectErrorUI(e.getMessage()); |
| 116 | + |
113 | 117 |
|
114 | 118 | } catch (IndexOutOfBoundsException e) { |
115 | 119 | Log.d(TAG, "User at this position does not exist: " + e.toString()); |
| 120 | + showConnectErrorUI(e.getMessage()); |
| 121 | + |
116 | 122 | } |
117 | 123 |
|
| 124 | + } catch (IllegalStateException e) { |
| 125 | + Log.d(TAG, "MSAL Exception Generated: " + e.toString()); |
| 126 | + showConnectErrorUI(e.getMessage()); |
| 127 | + |
| 128 | + } catch (Exception e) { |
| 129 | + showConnectErrorUI(); |
| 130 | + } |
118 | 131 |
|
119 | 132 | } |
120 | 133 |
|
@@ -166,6 +179,18 @@ private void showConnectErrorUI() { |
166 | 179 | R.string.connect_toast_text_error, |
167 | 180 | Toast.LENGTH_LONG).show(); |
168 | 181 | } |
| 182 | + private void showConnectErrorUI(String errorMessage) { |
| 183 | + mConnectButton.setVisibility(View.VISIBLE); |
| 184 | + mConnectProgressBar.setVisibility(View.GONE); |
| 185 | + mTitleTextView.setText(R.string.title_text_error); |
| 186 | + mTitleTextView.setVisibility(View.VISIBLE); |
| 187 | + mDescriptionTextView.setText(errorMessage); |
| 188 | + mDescriptionTextView.setVisibility(View.VISIBLE); |
| 189 | + Toast.makeText( |
| 190 | + ConnectActivity.this, |
| 191 | + R.string.connect_toast_text_error, |
| 192 | + Toast.LENGTH_LONG).show(); |
| 193 | + } |
169 | 194 |
|
170 | 195 | private void showMessage(final String msg) { |
171 | 196 | getHandler().post(new Runnable() { |
@@ -235,10 +260,12 @@ public void onError(MsalException exception) { |
235 | 260 | // This means errors happened in the sdk itself, could be network, Json parse, etc. Check MsalError.java |
236 | 261 | // for detailed list of the errors. |
237 | 262 | showMessage(exception.getMessage()); |
| 263 | + showConnectErrorUI(exception.getMessage()); |
238 | 264 | } else if (exception instanceof MsalServiceException) { |
239 | 265 | // This means something is wrong when the sdk is communication to the service, mostly likely it's the client |
240 | 266 | // configuration. |
241 | 267 | showMessage(exception.getMessage()); |
| 268 | + showConnectErrorUI(exception.getMessage()); |
242 | 269 | } else if (exception instanceof MsalUiRequiredException) { |
243 | 270 | // This explicitly indicates that developer needs to prompt the user, it could be refresh token is expired, revoked |
244 | 271 | // or user changes the password; or it could be that no token was found in the token cache. |
|
0 commit comments