Skip to content

Commit 128018e

Browse files
committed
Fixed login into Gmail accounts from the main screen. | #76, #114.
1 parent 8b0fb74 commit 128018e

File tree

1 file changed

+23
-58
lines changed

1 file changed

+23
-58
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/SplashActivity.java

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.content.Intent;
1111
import android.net.Uri;
1212
import android.os.Bundle;
13-
import android.support.annotation.NonNull;
1413
import android.support.v4.app.LoaderManager;
1514
import android.support.v4.content.Loader;
1615
import android.text.TextUtils;
@@ -34,8 +33,6 @@
3433
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
3534
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
3635
import com.google.android.gms.common.api.GoogleApiClient;
37-
import com.google.android.gms.common.api.OptionalPendingResult;
38-
import com.google.android.gms.common.api.ResultCallback;
3936

4037
import org.acra.ACRA;
4138

@@ -60,6 +57,7 @@ public class SplashActivity extends BaseSignInActivity implements
6057
private View splashView;
6158

6259
private AccountDao accountDao;
60+
private GoogleSignInAccount currentGoogleSignInAccount;
6361

6462
@Override
6563
public void onCreate(Bundle savedInstanceState) {
@@ -102,14 +100,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
102100
case REQUEST_CODE_CHECK_PRIVATE_KEYS_FROM_GMAIL:
103101
switch (resultCode) {
104102
case Activity.RESULT_OK:
103+
AccountDao accountDao = addGmailAccount(currentGoogleSignInAccount);
105104
EmailSyncService.startEmailSyncService(this);
106105
EmailManagerActivity.runEmailManagerActivity(this, accountDao);
107106
finish();
108107
break;
109108

110109
case Activity.RESULT_CANCELED:
111110
case CheckKeysActivity.RESULT_NEGATIVE:
112-
clearInfoAboutOldAccount(accountDao);
111+
this.currentGoogleSignInAccount = null;
113112
UIUtil.exchangeViewVisibility(this, false, splashView, signInView);
114113
break;
115114
}
@@ -118,21 +117,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
118117
case REQUEST_CODE_CREATE_OR_IMPORT_KEY:
119118
switch (resultCode) {
120119
case Activity.RESULT_OK:
120+
AccountDao accountDao = addGmailAccount(currentGoogleSignInAccount);
121121
EmailSyncService.startEmailSyncService(this);
122122
EmailManagerActivity.runEmailManagerActivity(this, accountDao);
123123
finish();
124124
break;
125125

126126
case Activity.RESULT_CANCELED:
127-
finish();
128-
break;
129-
130127
case CreateOrImportKeyActivity.RESULT_CODE_USE_ANOTHER_ACCOUNT:
131-
this.accountDao = null;
132-
if (data != null) {
133-
clearInfoAboutOldAccount((AccountDao) data.getParcelableExtra(CreateOrImportKeyActivity
134-
.EXTRA_KEY_ACCOUNT_DAO));
135-
}
128+
this.currentGoogleSignInAccount = null;
129+
UIUtil.exchangeViewVisibility(this, false, splashView, signInView);
136130
break;
137131
}
138132
break;
@@ -200,8 +194,13 @@ public void onClick(View v) {
200194
public Loader<LoaderResult> onCreateLoader(int id, Bundle args) {
201195
switch (id) {
202196
case R.id.loader_id_load_private_key_backups_from_email:
197+
AccountDao accountDao = null;
203198
UIUtil.exchangeViewVisibility(this, true, splashView, signInView);
204-
return new LoadPrivateKeysFromMailAsyncTaskLoader(this, accountDao);
199+
if (currentGoogleSignInAccount != null) {
200+
accountDao = new AccountDao(currentGoogleSignInAccount.getEmail(), AccountDao.ACCOUNT_TYPE_GOOGLE,
201+
null, null, null, null, null);
202+
}
203+
return accountDao != null ? new LoadPrivateKeysFromMailAsyncTaskLoader(this, accountDao) : null;
205204

206205
default:
207206
return null;
@@ -252,26 +251,10 @@ private void clearInfoAboutOldAccount(AccountDao accountDao) {
252251

253252
private void handleSignInResult(GoogleSignInResult googleSignInResult) {
254253
if (googleSignInResult.isSuccess()) {
255-
GoogleSignInAccount googleSignInAccount = googleSignInResult.getSignInAccount();
256-
if (googleSignInAccount != null) {
257-
accountDao = updateInformationAboutAccountInLocalDatabase(googleSignInAccount);
258-
} else {
259-
//todo-denbond7 handle this situation
260-
}
254+
currentGoogleSignInAccount = googleSignInResult.getSignInAccount();
261255

262-
if (SecurityUtils.isBackupKeysExist(this)) {
263-
EmailSyncService.startEmailSyncService(this);
264-
EmailManagerActivity.runEmailManagerActivity(this, accountDao);
265-
finish();
266-
} else {
267-
startService(new Intent(this, CheckClipboardToFindPrivateKeyService.class));
268-
if (accountDao != null) {
269-
getSupportLoaderManager().restartLoader(R.id.loader_id_load_private_key_backups_from_email, null,
270-
this);
271-
} else {
272-
//todo-denbond7 handle this situation
273-
}
274-
}
256+
startService(new Intent(this, CheckClipboardToFindPrivateKeyService.class));
257+
getSupportLoaderManager().restartLoader(R.id.loader_id_load_private_key_backups_from_email, null, this);
275258
} else {
276259
if (!TextUtils.isEmpty(googleSignInResult.getStatus().getStatusMessage())) {
277260
UIUtil.showInfoSnackbar(signInView, googleSignInResult.getStatus().getStatusMessage());
@@ -280,7 +263,14 @@ private void handleSignInResult(GoogleSignInResult googleSignInResult) {
280263
}
281264
}
282265

283-
private AccountDao updateInformationAboutAccountInLocalDatabase(GoogleSignInAccount googleSignInAccount) {
266+
/**
267+
* Created a GMAIL {@link AccountDao} and add it to the database.
268+
*
269+
* @param googleSignInAccount The {@link GoogleSignInAccount} object which contains information about a Google
270+
* account.
271+
* @return Generated {@link AccountDao}.
272+
*/
273+
private AccountDao addGmailAccount(GoogleSignInAccount googleSignInAccount) {
284274
AccountDaoSource accountDaoSource = new AccountDaoSource();
285275

286276
boolean isAccountUpdated = accountDaoSource.updateAccountInformation(this, googleSignInAccount) > 0;
@@ -318,29 +308,4 @@ private void initViews() {
318308
findViewById(R.id.buttonSecurity).setOnClickListener(this);
319309
}
320310
}
321-
322-
/**
323-
* In this method we check GoogleSignResult. If the user's cached credentials are valid the
324-
* OptionalPendingResult will be "done" and the GoogleSignInResult will be available
325-
* instantly. If the user has not previously signed in on this device or the sign-in has
326-
* expired, this asynchronous branch will attempt to sign in the user silently. Cross-device
327-
* single sign-on will occur in this branch.
328-
*/
329-
private void checkGoogleSignResult() {
330-
OptionalPendingResult<GoogleSignInResult> optionalPendingResult
331-
= Auth.GoogleSignInApi.silentSignIn(googleApiClient);
332-
if (optionalPendingResult.isDone()) {
333-
GoogleSignInResult googleSignInResult = optionalPendingResult.get();
334-
handleSignInResult(googleSignInResult);
335-
} else {
336-
UIUtil.exchangeViewVisibility(this, true, splashView, signInView);
337-
optionalPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
338-
@Override
339-
public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
340-
UIUtil.exchangeViewVisibility(SplashActivity.this, false, splashView, signInView);
341-
handleSignInResult(googleSignInResult);
342-
}
343-
});
344-
}
345-
}
346311
}

0 commit comments

Comments
 (0)