This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathConnectActivity.java
156 lines (135 loc) · 6.08 KB
/
ConnectActivity.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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
package com.microsoft.office365.connect;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.microsoft.aad.adal.AuthenticationCallback;
import com.microsoft.aad.adal.AuthenticationResult;
import java.net.URI;
import java.util.UUID;
/**
* Starting activity of the app. Handles the connection to Office 365.
* When it first starts it only displays a button to Connect to Office 365.
* If there are no cached tokens, the user is required to sign in to Office 365.
* If there are cached tokens, the app tries to reuse them.
* The activity redirects the user to the SendMailActivity upon successful connection.
*/
public class ConnectActivity extends AppCompatActivity {
private static final String TAG = "ConnectActivity";
private Button mConnectButton;
private TextView mTitleTextView;
private ProgressBar mConnectProgressBar;
private TextView mDescriptionTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
initializeViews();
}
/**
* Event handler for the onclick event of the button.
* @param v The view that sent the event.
*/
public void onConnectButtonClick(View v) {
showConnectingInProgressUI();
//check that client id and redirect have been set correctly
try {
UUID.fromString(Constants.CLIENT_ID);
URI.create(Constants.REDIRECT_URI);
}
catch (IllegalArgumentException e) {
Toast.makeText(
this
, getString(R.string.warning_client_id_redirect_uri_incorrect)
, Toast.LENGTH_LONG).show();
resetUIForConnect();
return;
}
final Intent sendMailIntent = new Intent(this, SendMailActivity.class);
AuthenticationManager.getInstance().setContextActivity(this);
AuthenticationManager.getInstance().connect(
new AuthenticationCallback<AuthenticationResult>() {
/**
* If the connection is successful, the activity extracts the username and
* displayableId values from the authentication result object and sends them
* to the SendMail activity.
* @param result The authentication result object that contains information about
* the user and the tokens.
*/
@Override
public void onSuccess(AuthenticationResult result) {
Log.i(TAG, "onConnectButtonClick - Successfully connected to Office 365");
sendMailIntent.putExtra("givenName", result
.getUserInfo()
.getGivenName());
sendMailIntent.putExtra("displayableId", result
.getUserInfo()
.getDisplayableId());
startActivity(sendMailIntent);
resetUIForConnect();
}
@Override
public void onError(final Exception e) {
Log.e(TAG, "onCreate - " + e.getMessage());
showConnectErrorUI();
}
});
}
/**
* This activity gets notified about the completion of the ADAL activity through this method.
* @param requestCode The integer request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its
* setResult().
* @param data An Intent, which can return result data to the caller (various data
* can be attached to Intent "extras").
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "onActivityResult - AuthenticationActivity has come back with results");
super.onActivityResult(requestCode, resultCode, data);
AuthenticationManager
.getInstance()
.getAuthenticationContext()
.onActivityResult(requestCode, resultCode, data);
}
private void initializeViews(){
mConnectButton = (Button)findViewById(R.id.connectButton);
mConnectProgressBar = (ProgressBar)findViewById(R.id.connectProgressBar);
mTitleTextView = (TextView)findViewById(R.id.titleTextView);
mDescriptionTextView = (TextView)findViewById(R.id.descriptionTextView);
}
private void resetUIForConnect(){
mConnectButton.setVisibility(View.VISIBLE);
mTitleTextView.setVisibility(View.GONE);
mDescriptionTextView.setVisibility(View.GONE);
mConnectProgressBar.setVisibility(View.GONE);
}
private void showConnectingInProgressUI(){
mConnectButton.setVisibility(View.GONE);
mTitleTextView.setVisibility(View.GONE);
mDescriptionTextView.setVisibility(View.GONE);
mConnectProgressBar.setVisibility(View.VISIBLE);
}
private void showConnectErrorUI(){
mConnectButton.setVisibility(View.VISIBLE);
mConnectProgressBar.setVisibility(View.GONE);
mTitleTextView.setText(R.string.title_text_error);
mTitleTextView.setVisibility(View.VISIBLE);
mDescriptionTextView.setText(R.string.connect_text_error);
mDescriptionTextView.setVisibility(View.VISIBLE);
Toast.makeText(
ConnectActivity.this,
R.string.connect_toast_text_error,
Toast.LENGTH_LONG).show();
}
}