2019-09-21 19:53:54 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2019-09-21 19:53:54 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-20 13:44:53 +00:00
|
|
|
import android.Manifest;
|
2019-09-21 19:53:54 +00:00
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.accounts.AccountManagerCallback;
|
|
|
|
import android.accounts.AccountManagerFuture;
|
2020-07-22 14:23:54 +00:00
|
|
|
import android.accounts.AuthenticatorException;
|
2019-09-21 19:53:54 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.database.Cursor;
|
2020-12-02 18:07:34 +00:00
|
|
|
import android.graphics.Paint;
|
2020-07-29 12:06:40 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
2019-12-25 10:49:34 +00:00
|
|
|
import android.net.Uri;
|
2019-09-21 19:53:54 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Button;
|
2020-11-26 08:45:04 +00:00
|
|
|
import android.widget.CheckBox;
|
2019-09-21 19:53:54 +00:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ScrollView;
|
|
|
|
import android.widget.TextView;
|
2020-04-06 19:00:14 +00:00
|
|
|
import android.widget.Toast;
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2019-11-15 08:33:43 +00:00
|
|
|
import androidx.constraintlayout.widget.Group;
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
import java.util.Date;
|
2020-05-20 15:34:57 +00:00
|
|
|
import java.util.HashMap;
|
2019-09-21 19:53:54 +00:00
|
|
|
import java.util.List;
|
2020-05-20 15:34:57 +00:00
|
|
|
import java.util.Map;
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
import static android.accounts.AccountManager.newChooseAccountIntent;
|
2019-09-24 08:32:36 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
2020-10-26 08:31:10 +00:00
|
|
|
import static eu.faircode.email.GmailState.TYPE_GOOGLE;
|
2020-10-25 21:20:48 +00:00
|
|
|
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_GMAIL;
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
public class FragmentGmail extends FragmentBase {
|
|
|
|
private ViewGroup view;
|
|
|
|
private ScrollView scroll;
|
|
|
|
|
|
|
|
private Button btnGrant;
|
|
|
|
private TextView tvGranted;
|
|
|
|
private EditText etName;
|
2020-11-26 08:45:04 +00:00
|
|
|
private CheckBox cbUpdate;
|
2019-09-21 19:53:54 +00:00
|
|
|
private Button btnSelect;
|
|
|
|
private ContentLoadingProgressBar pbSelect;
|
|
|
|
|
2020-12-02 18:07:34 +00:00
|
|
|
private TextView tvAppPassword;
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
private TextView tvError;
|
2019-12-25 10:49:34 +00:00
|
|
|
private Button btnSupport;
|
|
|
|
|
2019-11-15 08:33:43 +00:00
|
|
|
private Group grpError;
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
setSubtitle(R.string.title_setup_quick);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
|
|
|
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_gmail, container, false);
|
|
|
|
scroll = view.findViewById(R.id.scroll);
|
|
|
|
|
|
|
|
// Get controls
|
|
|
|
btnGrant = view.findViewById(R.id.btnGrant);
|
|
|
|
tvGranted = view.findViewById(R.id.tvGranted);
|
|
|
|
etName = view.findViewById(R.id.etName);
|
2020-11-26 08:45:04 +00:00
|
|
|
cbUpdate = view.findViewById(R.id.cbUpdate);
|
2019-09-21 19:53:54 +00:00
|
|
|
btnSelect = view.findViewById(R.id.btnSelect);
|
|
|
|
pbSelect = view.findViewById(R.id.pbSelect);
|
|
|
|
|
2020-12-02 18:07:34 +00:00
|
|
|
tvAppPassword = view.findViewById(R.id.tvAppPassword);
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
tvError = view.findViewById(R.id.tvError);
|
2019-12-25 10:49:34 +00:00
|
|
|
btnSupport = view.findViewById(R.id.btnSupport);
|
|
|
|
|
2019-11-15 08:33:43 +00:00
|
|
|
grpError = view.findViewById(R.id.grpError);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
// Wire controls
|
|
|
|
|
|
|
|
btnGrant.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-12-12 12:34:04 +00:00
|
|
|
requestPermissions(Helper.getOAuthPermissions(), ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
btnSelect.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2020-01-04 07:43:39 +00:00
|
|
|
try {
|
|
|
|
grpError.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
String name = etName.getText().toString().trim();
|
|
|
|
if (TextUtils.isEmpty(name))
|
|
|
|
throw new IllegalArgumentException(getString(R.string.title_no_name));
|
|
|
|
|
2020-02-05 09:59:48 +00:00
|
|
|
|
|
|
|
Intent intent = newChooseAccountIntent(
|
|
|
|
null,
|
|
|
|
null,
|
2020-04-06 19:00:14 +00:00
|
|
|
new String[]{TYPE_GOOGLE},
|
2020-02-05 09:59:48 +00:00
|
|
|
false,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null);
|
|
|
|
PackageManager pm = getContext().getPackageManager();
|
2020-06-14 16:34:13 +00:00
|
|
|
if (intent.resolveActivity(pm) == null) // system whitelisted
|
2020-02-05 09:59:48 +00:00
|
|
|
throw new IllegalArgumentException(getString(R.string.title_no_viewer, intent));
|
|
|
|
startActivityForResult(intent, ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
2020-01-04 07:43:39 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
tvError.setText(ex.getMessage());
|
|
|
|
else
|
|
|
|
tvError.setText(Log.formatThrowable(ex, false));
|
2019-11-15 08:33:43 +00:00
|
|
|
grpError.setVisibility(View.VISIBLE);
|
2019-10-02 12:54:10 +00:00
|
|
|
}
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-02 18:07:34 +00:00
|
|
|
tvAppPassword.setPaintFlags(tvAppPassword.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
|
|
|
|
tvAppPassword.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Helper.viewFAQ(v.getContext(), 6);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-25 10:49:34 +00:00
|
|
|
btnSupport.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2021-03-19 14:18:49 +00:00
|
|
|
Helper.view(view.getContext(), Uri.parse(Helper.SUPPORT_URI), false);
|
2019-12-25 10:49:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
// Initialize
|
|
|
|
Helper.setViewsEnabled(view, false);
|
|
|
|
pbSelect.setVisibility(View.GONE);
|
2019-11-15 08:33:43 +00:00
|
|
|
grpError.setVisibility(View.GONE);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
2019-12-12 12:34:04 +00:00
|
|
|
boolean granted = Helper.hasPermissions(getContext(), Helper.getOAuthPermissions());
|
2019-09-21 19:53:54 +00:00
|
|
|
setGranted(granted);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.menu_quick_setup, menu);
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2021-02-05 10:15:02 +00:00
|
|
|
if (item.getItemId() == R.id.menu_help) {
|
|
|
|
onMenuHelp();
|
|
|
|
return true;
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
2021-02-05 10:15:02 +00:00
|
|
|
return super.onOptionsItemSelected(item);
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onMenuHelp() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString("name", "SETUP.md");
|
|
|
|
|
|
|
|
FragmentDialogMarkdown fragment = new FragmentDialogMarkdown();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.show(getChildFragmentManager(), "help");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
boolean granted = true;
|
|
|
|
for (int i = 0; i < permissions.length; i++)
|
|
|
|
granted = (granted && grantResults[i] == PackageManager.PERMISSION_GRANTED);
|
|
|
|
|
|
|
|
setGranted(granted);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
2020-04-06 19:00:14 +00:00
|
|
|
try {
|
|
|
|
switch (requestCode) {
|
|
|
|
case ActivitySetup.REQUEST_CHOOSE_ACCOUNT:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onAccountSelected(data);
|
|
|
|
else
|
|
|
|
onNoAccountSelected();
|
|
|
|
break;
|
|
|
|
case ActivitySetup.REQUEST_DONE:
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setGranted(boolean granted) {
|
|
|
|
btnGrant.setEnabled(!granted);
|
|
|
|
tvGranted.setVisibility(granted ? View.VISIBLE : View.GONE);
|
|
|
|
|
|
|
|
if (granted) {
|
|
|
|
try (Cursor cursor = getContext().getContentResolver().query(
|
|
|
|
ContactsContract.Profile.CONTENT_URI,
|
|
|
|
new String[]{ContactsContract.Profile.DISPLAY_NAME}, null, null, null)) {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
int colDisplay = cursor.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME);
|
|
|
|
etName.setText(cursor.getString(colDisplay));
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
etName.setEnabled(granted);
|
2020-11-26 08:45:04 +00:00
|
|
|
cbUpdate.setEnabled(granted);
|
2019-09-21 19:53:54 +00:00
|
|
|
btnSelect.setEnabled(granted);
|
2020-02-05 09:46:05 +00:00
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
getMainHandler().post(new Runnable() {
|
2020-02-05 09:46:05 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
etName.requestFocus();
|
|
|
|
}
|
|
|
|
});
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:00:14 +00:00
|
|
|
private void onNoAccountSelected() {
|
|
|
|
AccountManager am = AccountManager.get(getContext());
|
|
|
|
Account[] accounts = am.getAccountsByType(TYPE_GOOGLE);
|
|
|
|
if (accounts.length == 0)
|
|
|
|
ToastEx.makeText(getContext(), R.string.title_no_account, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
private void onAccountSelected(Intent data) {
|
|
|
|
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
|
|
|
|
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
|
|
|
|
|
2020-07-22 14:23:54 +00:00
|
|
|
final String disabled = getString(R.string.title_setup_advanced_protection);
|
|
|
|
|
2020-05-20 13:44:53 +00:00
|
|
|
boolean found = false;
|
2019-09-21 19:53:54 +00:00
|
|
|
AccountManager am = AccountManager.get(getContext());
|
|
|
|
Account[] accounts = am.getAccountsByType(type);
|
|
|
|
for (final Account account : accounts)
|
2020-05-20 15:35:19 +00:00
|
|
|
if (name.equalsIgnoreCase(account.name)) {
|
2020-05-20 13:44:53 +00:00
|
|
|
found = true;
|
|
|
|
Log.i("Requesting token name=" + account.name);
|
2019-09-21 19:53:54 +00:00
|
|
|
am.getAuthToken(
|
|
|
|
account,
|
2020-10-25 21:20:48 +00:00
|
|
|
ServiceAuthenticator.getAuthTokenType(type),
|
2019-09-21 19:53:54 +00:00
|
|
|
new Bundle(),
|
|
|
|
getActivity(),
|
|
|
|
new AccountManagerCallback<Bundle>() {
|
|
|
|
@Override
|
|
|
|
public void run(AccountManagerFuture<Bundle> future) {
|
|
|
|
try {
|
|
|
|
Bundle bundle = future.getResult();
|
|
|
|
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
if (token == null)
|
|
|
|
throw new IllegalArgumentException("no token");
|
2020-05-20 13:44:53 +00:00
|
|
|
Log.i("Got token name=" + account.name);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
onAuthorized(name, token);
|
|
|
|
} catch (Throwable ex) {
|
2020-07-22 14:23:54 +00:00
|
|
|
if (ex instanceof AuthenticatorException &&
|
|
|
|
"ServiceDisabled".equals(ex.getMessage()))
|
|
|
|
ex = new IllegalArgumentException(disabled, ex);
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
Log.e(ex);
|
2020-07-22 14:23:54 +00:00
|
|
|
|
2019-12-06 07:50:46 +00:00
|
|
|
tvError.setText(Log.formatThrowable(ex));
|
2019-11-15 08:33:43 +00:00
|
|
|
grpError.setVisibility(View.VISIBLE);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
getMainHandler().post(new Runnable() {
|
2019-09-21 19:53:54 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
scroll.smoothScrollTo(0, tvError.getBottom());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-20 13:44:53 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2020-05-20 15:34:57 +00:00
|
|
|
boolean permission = Helper.hasPermission(getContext(), Manifest.permission.GET_ACCOUNTS);
|
|
|
|
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("type", type);
|
|
|
|
crumb.put("count", Integer.toString(accounts.length));
|
|
|
|
crumb.put("permission", Boolean.toString(permission));
|
|
|
|
Log.breadcrumb("Gmail", crumb);
|
|
|
|
|
2020-05-20 13:44:53 +00:00
|
|
|
Log.e("Account missing");
|
|
|
|
|
|
|
|
tvError.setText(getString(R.string.title_no_account));
|
|
|
|
grpError.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 08:31:10 +00:00
|
|
|
private void onAuthorized(String user, String token) {
|
|
|
|
GmailState state = GmailState.jsonDeserialize(token);
|
|
|
|
|
2019-09-21 19:53:54 +00:00
|
|
|
Bundle args = new Bundle();
|
2019-10-02 12:54:10 +00:00
|
|
|
args.putString("name", etName.getText().toString().trim());
|
2020-11-26 08:45:04 +00:00
|
|
|
args.putBoolean("update", cbUpdate.isChecked());
|
2019-09-21 19:53:54 +00:00
|
|
|
args.putString("user", user);
|
2020-10-26 08:31:10 +00:00
|
|
|
args.putString("password", state.jsonSerializeString());
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
etName.setEnabled(false);
|
2020-11-26 08:45:04 +00:00
|
|
|
cbUpdate.setEnabled(false);
|
2019-09-21 19:53:54 +00:00
|
|
|
btnSelect.setEnabled(false);
|
|
|
|
pbSelect.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
etName.setEnabled(true);
|
2020-11-26 08:45:04 +00:00
|
|
|
cbUpdate.setEnabled(true);
|
2019-09-21 19:53:54 +00:00
|
|
|
btnSelect.setEnabled(true);
|
|
|
|
pbSelect.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
String name = args.getString("name");
|
|
|
|
String user = args.getString("user");
|
|
|
|
String password = args.getString("password");
|
|
|
|
|
2019-10-02 12:54:10 +00:00
|
|
|
// Safety checks
|
2020-03-05 13:45:29 +00:00
|
|
|
if (!Helper.EMAIL_ADDRESS.matcher(user).matches())
|
2019-10-02 12:54:10 +00:00
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, user));
|
|
|
|
if (TextUtils.isEmpty(password))
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
|
2019-09-21 19:53:54 +00:00
|
|
|
|
2020-07-29 12:06:40 +00:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
|
|
|
|
if (ani == null || !ani.isConnected())
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
|
|
|
|
|
2020-07-14 06:29:52 +00:00
|
|
|
int at = user.indexOf('@');
|
|
|
|
String username = user.substring(0, at);
|
|
|
|
|
2019-10-08 08:25:41 +00:00
|
|
|
EmailProvider provider = EmailProvider.fromDomain(context, "gmail.com", EmailProvider.Discover.ALL);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
List<EntityFolder> folders;
|
|
|
|
|
2020-08-23 07:28:10 +00:00
|
|
|
String aprotocol = (provider.imap.starttls ? "imap" : "imaps");
|
|
|
|
int aencryption = (provider.imap.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
|
2020-02-06 12:06:10 +00:00
|
|
|
try (EmailService iservice = new EmailService(
|
2020-08-23 07:28:10 +00:00
|
|
|
context, aprotocol, null, aencryption, false,
|
|
|
|
EmailService.PURPOSE_CHECK, true)) {
|
2020-02-06 12:06:10 +00:00
|
|
|
iservice.connect(
|
|
|
|
provider.imap.host, provider.imap.port,
|
2020-10-25 21:20:48 +00:00
|
|
|
AUTH_TYPE_GMAIL, null,
|
2020-02-09 12:58:16 +00:00
|
|
|
user, password,
|
2020-02-10 15:35:14 +00:00
|
|
|
null, null);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
folders = iservice.getFolders();
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:52:42 +00:00
|
|
|
Long max_size;
|
2020-08-23 07:28:10 +00:00
|
|
|
String iprotocol = (provider.smtp.starttls ? "smtp" : "smtps");
|
|
|
|
int iencryption = (provider.smtp.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
|
2020-02-06 12:06:10 +00:00
|
|
|
try (EmailService iservice = new EmailService(
|
2020-08-23 07:28:10 +00:00
|
|
|
context, iprotocol, null, iencryption, false,
|
|
|
|
EmailService.PURPOSE_CHECK, true)) {
|
2020-02-06 12:06:10 +00:00
|
|
|
iservice.connect(
|
|
|
|
provider.smtp.host, provider.smtp.port,
|
2020-10-25 21:20:48 +00:00
|
|
|
AUTH_TYPE_GMAIL, null,
|
2020-02-09 12:58:16 +00:00
|
|
|
user, password,
|
2020-02-10 15:35:14 +00:00
|
|
|
null, null);
|
2020-07-02 11:52:42 +00:00
|
|
|
max_size = iservice.getMaxSize();
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2020-11-26 08:45:04 +00:00
|
|
|
EntityAccount update = null;
|
|
|
|
if (args.getBoolean("update"))
|
|
|
|
update = db.account().getAccount(user, AUTH_TYPE_GMAIL);
|
2020-11-25 18:05:32 +00:00
|
|
|
if (update == null) {
|
|
|
|
EntityAccount primary = db.account().getPrimaryAccount();
|
|
|
|
|
|
|
|
// Create account
|
|
|
|
EntityAccount account = new EntityAccount();
|
|
|
|
|
|
|
|
account.host = provider.imap.host;
|
|
|
|
account.encryption = aencryption;
|
|
|
|
account.port = provider.imap.port;
|
|
|
|
account.auth_type = AUTH_TYPE_GMAIL;
|
|
|
|
account.user = user;
|
|
|
|
account.password = password;
|
|
|
|
|
|
|
|
account.name = provider.name + "/" + username;
|
|
|
|
|
|
|
|
account.synchronize = true;
|
|
|
|
account.primary = (primary == null);
|
|
|
|
|
|
|
|
account.created = new Date().getTime();
|
|
|
|
account.last_connected = account.created;
|
|
|
|
|
|
|
|
account.id = db.account().insertAccount(account);
|
|
|
|
args.putLong("account", account.id);
|
|
|
|
EntityLog.log(context, "Gmail account=" + account.name);
|
|
|
|
|
|
|
|
// Create folders
|
|
|
|
for (EntityFolder folder : folders) {
|
|
|
|
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
|
|
|
|
if (existing == null) {
|
|
|
|
folder.account = account.id;
|
2020-12-01 20:07:13 +00:00
|
|
|
folder.setSpecials(account);
|
2020-11-25 18:05:32 +00:00
|
|
|
folder.id = db.folder().insertFolder(folder);
|
|
|
|
EntityLog.log(context, "Gmail folder=" + folder.name + " type=" + folder.type);
|
|
|
|
if (folder.synchronize)
|
2021-01-24 13:02:35 +00:00
|
|
|
EntityOperation.sync(context, folder.id, true);
|
2020-11-25 18:05:32 +00:00
|
|
|
}
|
2020-03-09 14:57:58 +00:00
|
|
|
}
|
2019-09-21 19:53:54 +00:00
|
|
|
|
2020-11-25 18:05:32 +00:00
|
|
|
// Set swipe left/right folder
|
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
if (EntityFolder.TRASH.equals(folder.type))
|
|
|
|
account.swipe_left = folder.id;
|
|
|
|
else if (EntityFolder.ARCHIVE.equals(folder.type))
|
|
|
|
account.swipe_right = folder.id;
|
|
|
|
|
|
|
|
db.account().updateAccount(account);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(name))
|
|
|
|
name = user.split("@")[0];
|
|
|
|
|
|
|
|
// Create identity
|
|
|
|
EntityIdentity identity = new EntityIdentity();
|
|
|
|
identity.name = name;
|
|
|
|
identity.email = user;
|
|
|
|
identity.account = account.id;
|
|
|
|
|
|
|
|
identity.host = provider.smtp.host;
|
|
|
|
identity.encryption = iencryption;
|
|
|
|
identity.port = provider.smtp.port;
|
|
|
|
identity.auth_type = AUTH_TYPE_GMAIL;
|
|
|
|
identity.user = user;
|
|
|
|
identity.password = password;
|
|
|
|
identity.synchronize = true;
|
|
|
|
identity.primary = true;
|
|
|
|
identity.max_size = max_size;
|
|
|
|
|
|
|
|
identity.id = db.identity().insertIdentity(identity);
|
|
|
|
EntityLog.log(context, "Gmail identity=" + identity.name + " email=" + identity.email);
|
|
|
|
} else {
|
2020-11-26 08:45:04 +00:00
|
|
|
args.putLong("account", -1);
|
2020-11-25 18:05:32 +00:00
|
|
|
EntityLog.log(context, "Gmail update account=" + update.name);
|
|
|
|
db.account().setAccountPassword(update.id, password);
|
|
|
|
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type);
|
|
|
|
}
|
2019-09-21 19:53:54 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
2021-03-25 08:36:45 +00:00
|
|
|
Context context = getContext();
|
|
|
|
if (context != null)
|
|
|
|
ServiceSynchronize.eval(context, "Gmail");
|
|
|
|
|
2020-11-26 08:45:04 +00:00
|
|
|
if (args.getLong("account") < 0) {
|
|
|
|
finish();
|
2021-03-25 08:36:45 +00:00
|
|
|
if (context != null)
|
|
|
|
ToastEx.makeText(context, R.string.title_setup_oauth_updated, Toast.LENGTH_LONG).show();
|
2020-11-26 08:45:04 +00:00
|
|
|
} else {
|
|
|
|
FragmentReview fragment = new FragmentReview();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.setTargetFragment(FragmentGmail.this, ActivitySetup.REQUEST_DONE);
|
|
|
|
fragment.show(getParentFragmentManager(), "quick:review");
|
|
|
|
}
|
2019-09-21 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-10-02 12:54:10 +00:00
|
|
|
|
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
tvError.setText(ex.getMessage());
|
|
|
|
else
|
2019-12-06 07:50:46 +00:00
|
|
|
tvError.setText(Log.formatThrowable(ex));
|
2019-11-15 08:33:43 +00:00
|
|
|
grpError.setVisibility(View.VISIBLE);
|
2019-09-21 19:53:54 +00:00
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
getMainHandler().post(new Runnable() {
|
2019-09-21 19:53:54 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
scroll.smoothScrollTo(0, tvError.getBottom());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.execute(this, args, "setup:gmail");
|
|
|
|
}
|
|
|
|
}
|