Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentSetup.java

702 lines
28 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.Manifest;
2019-09-18 16:34:07 +02:00
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AccountsException;
import android.app.Activity;
import android.app.Dialog;
2018-12-27 16:49:37 +00:00
import android.content.ComponentName;
import android.content.Context;
2018-09-04 18:06:22 +00:00
import android.content.DialogInterface;
import android.content.Intent;
2019-06-09 11:22:50 +02:00
import android.content.SharedPreferences;
2018-08-02 13:33:06 +00:00
import android.content.pm.PackageManager;
2019-09-18 16:34:07 +02:00
import android.database.Cursor;
2018-09-07 12:34:54 +00:00
import android.graphics.drawable.Drawable;
2019-09-02 11:29:48 +02:00
import android.net.ConnectivityManager;
import android.net.Uri;
2018-09-04 18:06:22 +00:00
import android.os.Build;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-09-04 18:06:22 +00:00
import android.os.PowerManager;
2019-09-18 16:34:07 +02:00
import android.provider.ContactsContract;
2018-09-04 18:06:22 +00:00
import android.provider.Settings;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
2019-07-02 10:04:50 +02:00
import android.widget.ImageButton;
2018-08-02 13:33:06 +00:00
import android.widget.TextView;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
2019-07-02 10:04:50 +02:00
import androidx.constraintlayout.widget.Group;
2019-09-18 16:34:07 +02:00
import androidx.lifecycle.Lifecycle;
2018-08-08 06:55:47 +00:00
import androidx.lifecycle.Observer;
2019-06-12 16:15:46 +02:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2019-06-09 11:22:50 +02:00
import androidx.preference.PreferenceManager;
2018-08-08 06:55:47 +00:00
2019-09-18 16:34:07 +02:00
import com.google.android.material.snackbar.Snackbar;
import java.io.IOException;
2019-09-21 12:04:36 +02:00
import java.net.UnknownHostException;
2019-09-18 16:34:07 +02:00
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
2019-09-18 16:34:07 +02:00
import static android.accounts.AccountManager.newChooseAccountIntent;
2019-01-15 17:41:55 +00:00
public class FragmentSetup extends FragmentBase {
2018-09-14 11:33:22 +00:00
private ViewGroup view;
2019-09-07 18:07:58 +02:00
private TextView tvWelcome;
2019-07-02 10:04:50 +02:00
private ImageButton ibWelcome;
2019-06-12 16:15:46 +02:00
private Button btnHelp;
2018-12-29 17:00:10 +00:00
private Button btnQuick;
2018-08-02 13:33:06 +00:00
private TextView tvAccountDone;
2019-01-15 09:46:25 +00:00
private Button btnAccount;
private TextView tvNoPrimaryDrafts;
2018-08-04 15:13:19 +00:00
2018-08-02 13:33:06 +00:00
private TextView tvIdentityDone;
2019-01-15 09:46:25 +00:00
private Button btnIdentity;
2019-04-11 12:09:20 +02:00
private TextView tvNoIdentities;
2018-08-04 15:13:19 +00:00
2018-08-02 13:33:06 +00:00
private TextView tvPermissionsDone;
2019-01-15 09:46:25 +00:00
private Button btnPermissions;
2018-08-02 13:33:06 +00:00
2018-09-04 18:06:22 +00:00
private TextView tvDozeDone;
2019-01-15 09:46:25 +00:00
private Button btnDoze;
2019-07-08 19:58:02 +02:00
private Button btnBattery;
2018-09-04 18:06:22 +00:00
2019-09-02 11:29:48 +02:00
private Button btnDataSaver;
2019-01-15 09:46:25 +00:00
private Button btnInbox;
2019-07-02 10:04:50 +02:00
private Group grpWelcome;
2019-09-02 11:29:48 +02:00
private Group grpDataSaver;
2019-07-02 10:04:50 +02:00
2019-01-14 12:31:23 +00:00
private int textColorPrimary;
private int colorWarning;
2018-09-07 12:34:54 +00:00
private Drawable check;
2018-08-02 13:33:06 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_setup);
2019-01-14 12:31:23 +00:00
textColorPrimary = Helper.resolveColor(getContext(), android.R.attr.textColorPrimary);
colorWarning = Helper.resolveColor(getContext(), R.attr.colorWarning);
2018-09-07 12:34:54 +00:00
check = getResources().getDrawable(R.drawable.baseline_check_24, getContext().getTheme());
2018-09-14 11:33:22 +00:00
view = (ViewGroup) inflater.inflate(R.layout.fragment_setup, container, false);
2018-08-02 13:33:06 +00:00
// Get controls
2019-09-07 18:07:58 +02:00
tvWelcome = view.findViewById(R.id.tvWelcome);
2019-07-02 10:04:50 +02:00
ibWelcome = view.findViewById(R.id.ibWelcome);
2019-06-12 16:15:46 +02:00
btnHelp = view.findViewById(R.id.btnHelp);
2018-12-29 17:00:10 +00:00
btnQuick = view.findViewById(R.id.btnQuick);
2018-08-02 13:33:06 +00:00
tvAccountDone = view.findViewById(R.id.tvAccountDone);
2019-01-15 09:46:25 +00:00
btnAccount = view.findViewById(R.id.btnAccount);
tvNoPrimaryDrafts = view.findViewById(R.id.tvNoPrimaryDrafts);
2018-08-04 15:13:19 +00:00
2018-08-02 13:33:06 +00:00
tvIdentityDone = view.findViewById(R.id.tvIdentityDone);
2019-01-15 09:46:25 +00:00
btnIdentity = view.findViewById(R.id.btnIdentity);
2019-04-11 12:09:20 +02:00
tvNoIdentities = view.findViewById(R.id.tvNoIdentities);
2018-08-04 15:13:19 +00:00
2018-08-02 13:33:06 +00:00
tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone);
2019-01-15 09:46:25 +00:00
btnPermissions = view.findViewById(R.id.btnPermissions);
2018-08-02 13:33:06 +00:00
2018-09-04 18:06:22 +00:00
tvDozeDone = view.findViewById(R.id.tvDozeDone);
2019-01-15 09:46:25 +00:00
btnDoze = view.findViewById(R.id.btnDoze);
2019-07-08 19:58:02 +02:00
btnBattery = view.findViewById(R.id.btnBattery);
2018-09-04 18:06:22 +00:00
2019-09-02 11:29:48 +02:00
btnDataSaver = view.findViewById(R.id.btnDataSaver);
2019-01-15 09:46:25 +00:00
btnInbox = view.findViewById(R.id.btnInbox);
2019-07-02 10:04:50 +02:00
grpWelcome = view.findViewById(R.id.grpWelcome);
2019-09-02 11:29:48 +02:00
grpDataSaver = view.findViewById(R.id.grpDataSaver);
2019-07-02 10:04:50 +02:00
2019-07-08 19:58:02 +02:00
PackageManager pm = getContext().getPackageManager();
2019-07-02 10:04:50 +02:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2018-08-02 13:33:06 +00:00
// Wire controls
2019-09-07 18:07:58 +02:00
tvWelcome.setText(getString(R.string.title_setup_welcome)
.replaceAll("^\\s+", "").replaceAll("\\s+", " "));
2019-07-02 10:04:50 +02:00
ibWelcome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
prefs.edit().putBoolean("welcome", false).apply();
grpWelcome.setVisibility(View.GONE);
}
});
2019-06-12 16:15:46 +02:00
btnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.putString("name", "SETUP.md");
FragmentDialogMarkdown fragment = new FragmentDialogMarkdown();
fragment.setArguments(args);
fragment.show(getChildFragmentManager(), "help");
2019-06-12 16:15:46 +02:00
}
});
2018-12-29 17:00:10 +00:00
btnQuick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), btnQuick);
popupMenu.getMenu().add(Menu.NONE, R.string.title_setup_gmail, 1, R.string.title_setup_gmail)
.setEnabled(Helper.hasValidFingerprint(getContext()));
popupMenu.getMenu().add(Menu.NONE, R.string.title_setup_other, 2, R.string.title_setup_other);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.string.title_setup_gmail:
onGmail();
return true;
case R.string.title_setup_other:
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivitySetup.ACTION_QUICK_SETUP));
return true;
default:
return false;
}
2019-09-18 16:34:07 +02:00
}
});
2019-09-18 16:34:07 +02:00
popupMenu.show();
2019-09-18 16:34:07 +02:00
}
});
2018-08-02 13:33:06 +00:00
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2019-06-12 16:15:46 +02:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivitySetup.ACTION_VIEW_ACCOUNTS));
2018-08-02 13:33:06 +00:00
}
});
btnIdentity.setOnClickListener(new View.OnClickListener() {
2018-08-05 18:46:36 +00:00
@Override
public void onClick(View view) {
2019-06-12 16:15:46 +02:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivitySetup.ACTION_VIEW_IDENTITIES));
2018-08-04 15:13:19 +00:00
}
});
2018-08-06 12:29:14 +00:00
btnPermissions.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View view) {
2018-08-10 16:06:39 +00:00
btnPermissions.setEnabled(false);
2019-09-18 16:34:07 +02:00
String permission = Manifest.permission.READ_CONTACTS;
requestPermissions(new String[]{permission}, ActivitySetup.REQUEST_PERMISSION);
2018-08-02 13:33:06 +00:00
}
});
2018-09-04 18:06:22 +00:00
btnDoze.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new FragmentDialogDoze().show(getFragmentManager(), "setup:doze");
2018-09-04 18:06:22 +00:00
}
});
2019-07-08 19:58:02 +02:00
btnBattery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2019-08-15 07:58:20 +02:00
Helper.viewFAQ(getContext(), 39);
2019-07-08 19:58:02 +02:00
}
});
2019-09-02 11:29:48 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final Intent settings = new Intent(
Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS,
Uri.parse("package:" + BuildConfig.APPLICATION_ID));
btnDataSaver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(settings);
}
});
btnDataSaver.setEnabled(settings.resolveActivity(pm) != null);
}
2019-01-15 09:46:25 +00:00
btnInbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-06-12 16:15:46 +02:00
((FragmentBase) getParentFragment()).finish();
2019-01-15 09:46:25 +00:00
}
});
2018-08-02 13:33:06 +00:00
// Initialize
2018-09-04 18:06:22 +00:00
tvAccountDone.setText(null);
2018-09-07 12:34:54 +00:00
tvAccountDone.setCompoundDrawables(null, null, null, null);
tvNoPrimaryDrafts.setVisibility(View.GONE);
2018-09-07 12:34:54 +00:00
2018-09-04 18:06:22 +00:00
tvIdentityDone.setText(null);
2018-09-07 12:34:54 +00:00
tvIdentityDone.setCompoundDrawables(null, null, null, null);
2019-01-15 09:46:25 +00:00
btnIdentity.setEnabled(false);
2019-04-11 12:09:20 +02:00
tvNoIdentities.setVisibility(View.GONE);
2018-09-07 12:34:54 +00:00
2018-09-04 18:06:22 +00:00
tvPermissionsDone.setText(null);
2018-09-07 12:34:54 +00:00
tvPermissionsDone.setCompoundDrawables(null, null, null, null);
2018-09-04 18:06:22 +00:00
tvDozeDone.setText(null);
2018-09-07 12:34:54 +00:00
tvDozeDone.setCompoundDrawables(null, null, null, null);
2019-01-15 09:46:25 +00:00
btnDoze.setEnabled(false);
2018-09-07 12:34:54 +00:00
2019-01-15 09:46:25 +00:00
btnInbox.setEnabled(false);
2019-07-02 10:04:50 +02:00
boolean welcome = prefs.getBoolean("welcome", true);
2019-07-13 13:28:33 +02:00
grpWelcome.setVisibility(welcome ? View.VISIBLE : View.GONE);
2019-09-02 11:29:48 +02:00
grpDataSaver.setVisibility(View.GONE);
2019-07-02 10:04:50 +02:00
2019-09-18 16:34:07 +02:00
setContactsPermission(hasPermission(Manifest.permission.READ_CONTACTS));
2018-08-02 13:33:06 +00:00
2018-08-06 12:29:14 +00:00
// Create outbox
new SimpleTask<Void>() {
@Override
2018-12-31 07:03:48 +00:00
protected Void onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
outbox.name = "OUTBOX";
outbox.type = EntityFolder.OUTBOX;
outbox.synchronize = false;
2018-11-14 10:49:59 +01:00
outbox.sync_days = 0;
outbox.keep_days = 0;
outbox.id = db.folder().insertFolder(outbox);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
2019-01-12 11:48:00 +00:00
}.execute(this, new Bundle(), "outbox:create");
2018-08-02 13:33:06 +00:00
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final DB db = DB.getInstance(getContext());
2019-06-09 11:22:50 +02:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-02-17 18:29:00 +00:00
db.account().liveSynchronizingAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
private boolean done = false;
@Override
public void onChanged(@Nullable List<EntityAccount> accounts) {
done = (accounts != null && accounts.size() > 0);
2019-01-14 17:58:50 +00:00
getActivity().invalidateOptionsMenu();
2018-09-07 12:34:54 +00:00
tvAccountDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
2019-01-14 12:31:23 +00:00
tvAccountDone.setTextColor(done ? textColorPrimary : colorWarning);
2018-09-07 12:34:54 +00:00
tvAccountDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
2019-01-15 09:46:25 +00:00
btnIdentity.setEnabled(done);
btnInbox.setEnabled(done);
2019-06-09 11:22:50 +02:00
prefs.edit().putBoolean("has_accounts", done).apply();
2019-01-15 09:46:25 +00:00
2019-08-30 08:10:28 +02:00
if (done)
new SimpleTask<EntityFolder>() {
@Override
protected EntityFolder onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
return db.folder().getPrimaryDrafts();
}
@Override
protected void onExecuted(Bundle args, EntityFolder drafts) {
tvNoPrimaryDrafts.setVisibility(drafts == null ? View.VISIBLE : View.GONE);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(FragmentSetup.this, new Bundle(), "setup:drafts");
}
});
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<TupleIdentityEx>>() {
@Override
public void onChanged(@Nullable List<TupleIdentityEx> identities) {
2018-09-07 12:34:54 +00:00
boolean done = (identities != null && identities.size() > 0);
tvIdentityDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
2019-01-14 12:31:23 +00:00
tvIdentityDone.setTextColor(done ? textColorPrimary : colorWarning);
2018-09-07 12:34:54 +00:00
tvIdentityDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
2019-04-11 12:09:20 +02:00
tvNoIdentities.setVisibility(done ? View.GONE : View.VISIBLE);
}
});
2019-02-18 09:49:59 +00:00
// Backward compatibility
PackageManager pm = getContext().getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(getContext(), ActivitySearch.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
2018-09-04 18:06:22 +00:00
@Override
public void onResume() {
super.onResume();
2019-07-08 19:58:02 +02:00
// Doze
2018-12-09 18:49:52 +01:00
boolean ignoring = true;
2018-12-28 08:41:05 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
2019-07-08 19:58:02 +02:00
ignoring = (pm != null && pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID));
2018-12-28 08:41:05 +00:00
}
}
2019-07-08 19:58:02 +02:00
2018-09-04 18:06:22 +00:00
btnDoze.setEnabled(!ignoring);
2019-07-08 19:58:02 +02:00
// https://issuetracker.google.com/issues/37070074
//ignoring = (ignoring || Build.VERSION.SDK_INT != Build.VERSION_CODES.M);
2018-09-04 18:06:22 +00:00
tvDozeDone.setText(ignoring ? R.string.title_setup_done : R.string.title_setup_to_do);
2019-01-14 12:31:23 +00:00
tvDozeDone.setTextColor(ignoring ? textColorPrimary : colorWarning);
2018-09-07 12:34:54 +00:00
tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(ignoring ? check : null, null, null, null);
2019-09-02 11:29:48 +02:00
// https://developer.android.com/training/basics/network-ops/data-saver.html
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
int status = cm.getRestrictBackgroundStatus();
grpDataSaver.setVisibility(
status == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED
? View.VISIBLE : View.GONE);
}
}
2018-09-04 18:06:22 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
2019-09-18 16:34:07 +02:00
boolean granted = true;
for (int i = 0; i < permissions.length; i++)
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
if (Manifest.permission.READ_CONTACTS.equals(permissions[i]))
setContactsPermission(true);
} else
granted = false;
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)
if (granted)
selectAccount();
}
private void setContactsPermission(boolean granted) {
if (granted)
ContactInfo.init(getContext());
tvPermissionsDone.setText(granted ? R.string.title_setup_done : R.string.title_setup_to_do);
tvPermissionsDone.setTextColor(granted ? textColorPrimary : colorWarning);
tvPermissionsDone.setCompoundDrawablesWithIntrinsicBounds(granted ? check : null, null, null, null);
btnPermissions.setEnabled(!granted);
}
private void onGmail() {
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.READ_CONTACTS); // profile
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
permissions.add(Manifest.permission.GET_ACCOUNTS);
boolean granted = true;
for (String permission : permissions)
if (!hasPermission(permission)) {
granted = false;
break;
}
if (granted)
selectAccount();
else
2019-09-19 11:18:34 +02:00
new AlertDialog.Builder(getContext())
.setMessage(R.string.title_setup_gmail_rationale)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(permissions.toArray(new String[0]), ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
2019-09-18 16:34:07 +02:00
private void selectAccount() {
Log.i("Select account");
startActivityForResult(
Helper.getChooser(getContext(), newChooseAccountIntent(
null,
null,
new String[]{"com.google"},
false,
null,
null,
null,
null)),
ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ActivitySetup.REQUEST_CHOOSE_ACCOUNT:
if (resultCode == Activity.RESULT_OK && data != null)
onAccountSelected(data);
break;
}
}
2019-09-18 16:34:07 +02:00
private void onAccountSelected(Intent data) {
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
AccountManager am = AccountManager.get(getContext());
Account[] accounts = am.getAccountsByType(type);
for (final Account account : accounts)
if (name.equals(account.name)) {
Snackbar.make(view, R.string.title_authorizing, Snackbar.LENGTH_LONG).show();
am.getAuthToken(
account,
MailService.getAuthTokenType(type),
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);
2019-09-18 22:11:48 +02:00
Log.i("Got token");
2019-09-18 16:34:07 +02:00
onAuthorized(name, token);
} catch (Throwable ex) {
if (ex instanceof AccountsException || ex instanceof IOException) {
2019-09-18 22:11:48 +02:00
Log.w(ex);
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
2019-09-18 16:34:07 +02:00
Snackbar.make(view, Helper.formatThrowable(ex), Snackbar.LENGTH_LONG).show();
2019-09-20 14:35:26 +02:00
} else
2019-09-18 16:34:07 +02:00
Helper.unexpectedError(getFragmentManager(), ex);
}
}
},
null);
2018-08-02 13:33:06 +00:00
break;
}
2019-09-18 16:34:07 +02:00
}
2018-08-02 13:33:06 +00:00
2019-09-18 16:34:07 +02:00
private void onAuthorized(String user, String password) {
Bundle args = new Bundle();
args.putString("user", user);
args.putString("password", password);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
String user = args.getString("user");
String password = args.getString("password");
if (!user.contains("@"))
2019-09-21 12:04:36 +02:00
throw new IllegalArgumentException(
context.getString(R.string.title_email_invalid, user));
2019-09-18 16:34:07 +02:00
String domain = user.split("@")[1];
EmailProvider provider = EmailProvider.fromDomain(context, domain, EmailProvider.Discover.ALL);
List<EntityFolder> folders;
String aprotocol = provider.imap.starttls ? "imap" : "imaps";
try (MailService iservice = new MailService(context, aprotocol, null, false, true)) {
iservice.connect(provider.imap.host, provider.imap.port, MailService.AUTH_TYPE_GMAIL, user, password);
folders = iservice.getFolders();
if (folders == null)
2019-09-21 12:04:36 +02:00
throw new IllegalArgumentException(
context.getString(R.string.title_setup_no_settings, domain));
2019-09-18 16:34:07 +02:00
}
2019-07-24 08:52:38 +02:00
2019-09-18 16:34:07 +02:00
String iprotocol = provider.smtp.starttls ? "smtp" : "smtps";
try (MailService iservice = new MailService(context, iprotocol, null, false, true)) {
iservice.connect(provider.smtp.host, provider.smtp.port, MailService.AUTH_TYPE_GMAIL, user, password);
}
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityAccount primary = db.account().getPrimaryAccount();
// Create account
EntityAccount account = new EntityAccount();
account.host = provider.imap.host;
account.starttls = provider.imap.starttls;
account.port = provider.imap.port;
account.auth_type = MailService.AUTH_TYPE_GMAIL;
account.user = user;
account.password = password;
account.name = provider.name;
account.synchronize = true;
account.primary = (primary == null);
account.created = new Date().getTime();
account.last_connected = account.created;
account.id = db.account().insertAccount(account);
2019-09-21 12:04:36 +02:00
args.putLong("account", account.id);
2019-09-18 16:34:07 +02:00
EntityLog.log(context, "Gmail account=" + account.name);
// Create folders
for (EntityFolder folder : folders) {
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
EntityLog.log(context, "Gmail folder=" + folder.name + " type=" + folder.type);
}
// 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);
String name = user.split("@")[0];
try (Cursor cursor = context.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);
name = cursor.getString(colDisplay);
}
} catch (Throwable ex) {
Log.e(ex);
}
// Create identity
EntityIdentity identity = new EntityIdentity();
identity.name = name;
identity.email = user;
identity.account = account.id;
identity.host = provider.smtp.host;
identity.starttls = provider.smtp.starttls;
identity.port = provider.smtp.port;
identity.auth_type = MailService.AUTH_TYPE_GMAIL;
identity.user = user;
identity.password = password;
identity.synchronize = true;
identity.primary = true;
identity.id = db.identity().insertIdentity(identity);
2019-09-21 12:04:36 +02:00
args.putLong("identity", identity.id);
2019-09-18 16:34:07 +02:00
EntityLog.log(context, "Gmail identity=" + identity.name + " email=" + identity.email);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.reload(getContext(), "Gmail");
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
FragmentQuickSetup.FragmentDialogDone fragment = new FragmentQuickSetup.FragmentDialogDone();
2019-09-21 12:04:36 +02:00
fragment.setArguments(args);
2019-09-18 16:34:07 +02:00
fragment.show(getFragmentManager(), "gmail:done");
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-09-21 12:04:36 +02:00
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Snackbar.make(view, Helper.formatThrowable(ex, false), Snackbar.LENGTH_LONG).show();
2019-09-18 16:34:07 +02:00
}
}.execute(this, args, "setup:gmail");
2018-08-02 13:33:06 +00:00
}
2019-09-11 21:43:27 +02:00
public static class FragmentDialogDoze extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext())
.setMessage(R.string.title_setup_doze_instructions)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
} catch (Throwable ex) {
Log.e(ex);
}
}
})
2019-09-12 13:23:32 +02:00
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
2018-08-02 13:33:06 +00:00
}