Refactoring

pull/147/head
M66B 6 years ago
parent eac09f074e
commit da75d3623b

@ -44,7 +44,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class Provider {
public class EmailProvider {
public String name;
public int order;
public String link;
@ -63,17 +63,17 @@ public class Provider {
private static final int TIMEOUT = 20 * 1000; // milliseconds
private Provider() {
private EmailProvider() {
}
Provider(String name) {
EmailProvider(String name) {
this.name = name;
}
static List<Provider> loadProfiles(Context context) {
List<Provider> result = null;
static List<EmailProvider> loadProfiles(Context context) {
List<EmailProvider> result = null;
try {
Provider provider = null;
EmailProvider provider = null;
XmlResourceParser xml = context.getResources().getXml(R.xml.providers);
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
@ -82,7 +82,7 @@ public class Provider {
if ("providers".equals(name))
result = new ArrayList<>();
else if ("provider".equals(name)) {
provider = new Provider();
provider = new EmailProvider();
provider.name = xml.getAttributeValue(null, "name");
provider.order = xml.getAttributeIntValue(null, "order", Integer.MAX_VALUE);
provider.link = xml.getAttributeValue(null, "link");
@ -113,9 +113,9 @@ public class Provider {
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(result, new Comparator<Provider>() {
Collections.sort(result, new Comparator<EmailProvider>() {
@Override
public int compare(Provider p1, Provider p2) {
public int compare(EmailProvider p1, EmailProvider p2) {
int o = Integer.compare(p1.order, p2.order);
if (o == 0)
return collator.compare(p1.name, p2.name);
@ -127,13 +127,13 @@ public class Provider {
return result;
}
static Provider fromDomain(Context context, String domain) throws IOException {
static EmailProvider fromDomain(Context context, String domain) throws IOException {
try {
return Provider.fromISPDB(context, domain);
return EmailProvider.fromISPDB(context, domain);
} catch (Throwable ex) {
Log.w(ex);
try {
return Provider.fromDNS(context, domain);
return EmailProvider.fromDNS(context, domain);
} catch (UnknownHostException ex1) {
Log.w(ex1);
throw new UnknownHostException(context.getString(R.string.title_setup_no_settings, domain));
@ -141,8 +141,8 @@ public class Provider {
}
}
private static Provider fromISPDB(Context context, String domain) throws IOException, XmlPullParserException {
Provider provider = new Provider(domain);
private static EmailProvider fromISPDB(Context context, String domain) throws IOException, XmlPullParserException {
EmailProvider provider = new EmailProvider(domain);
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
URL url = new URL("https://autoconfig.thunderbird.net/v1.1/" + domain);
@ -319,12 +319,12 @@ public class Provider {
return addSpecials(context, provider);
}
private static Provider fromDNS(Context context, String domain) throws TextParseException, UnknownHostException {
private static EmailProvider fromDNS(Context context, String domain) throws TextParseException, UnknownHostException {
// https://tools.ietf.org/html/rfc6186
SRVRecord imap = lookup("_imaps._tcp." + domain);
SRVRecord smtp = lookup("_submission._tcp." + domain);
Provider provider = new Provider(domain);
EmailProvider provider = new EmailProvider(domain);
provider.imap_host = imap.getTarget().toString(true);
provider.imap_port = imap.getPort();
provider.imap_starttls = (provider.imap_port == 143);
@ -336,7 +336,7 @@ public class Provider {
return addSpecials(context, provider);
}
private static void addDocumentation(Provider provider, String href, String title) {
private static void addDocumentation(EmailProvider provider, String href, String title) {
if (provider.documentation == null)
provider.documentation = new StringBuilder();
else
@ -345,8 +345,8 @@ public class Provider {
provider.documentation.append("<a href=\"").append(href).append("\">").append(title).append("</a>");
}
private static Provider addSpecials(Context context, Provider provider) {
for (Provider predefined : loadProfiles(context))
private static EmailProvider addSpecials(Context context, EmailProvider provider) {
for (EmailProvider predefined : loadProfiles(context))
if (provider.imap_host.equals(predefined.imap_host)) {
provider.prefix = predefined.prefix;
break;

@ -211,7 +211,7 @@ public class FragmentAccount extends FragmentEx {
spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemid) {
Provider provider = (Provider) adapterView.getSelectedItem();
EmailProvider provider = (EmailProvider) adapterView.getSelectedItem();
grpServer.setVisibility(position == 1 ? View.VISIBLE : View.GONE);
grpAuthorize.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
@ -270,7 +270,7 @@ public class FragmentAccount extends FragmentEx {
Bundle args = new Bundle();
args.putString("domain", etDomain.getText().toString());
new SimpleTask<Provider>() {
new SimpleTask<EmailProvider>() {
@Override
protected void onPreExecute(Bundle args) {
etDomain.setEnabled(false);
@ -284,13 +284,13 @@ public class FragmentAccount extends FragmentEx {
}
@Override
protected Provider onExecute(Context context, Bundle args) throws Throwable {
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
String domain = args.getString("domain");
return Provider.fromDomain(context, domain);
return EmailProvider.fromDomain(context, domain);
}
@Override
protected void onExecuted(Bundle args, Provider provider) {
protected void onExecuted(Bundle args, EmailProvider provider) {
etHost.setText(provider.imap_host);
etPort.setText(Integer.toString(provider.imap_port));
cbStartTls.setChecked(provider.imap_starttls);
@ -364,7 +364,7 @@ public class FragmentAccount extends FragmentEx {
btnAuthorize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Provider provider = (Provider) spProvider.getSelectedItem();
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
Log.i("Authorize " + provider);
if ("com.google".equals(provider.type)) {
@ -418,7 +418,7 @@ public class FragmentAccount extends FragmentEx {
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Provider provider = (Provider) spProvider.getSelectedItem();
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
Bundle args = new Bundle();
args.putLong("id", id);
@ -630,7 +630,7 @@ public class FragmentAccount extends FragmentEx {
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Provider provider = (Provider) spProvider.getSelectedItem();
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
@ -1006,11 +1006,11 @@ public class FragmentAccount extends FragmentEx {
@Override
protected void onExecuted(Bundle args, EntityAccount account) {
// Get providers
List<Provider> providers = Provider.loadProfiles(getContext());
providers.add(0, new Provider(getString(R.string.title_select)));
providers.add(1, new Provider(getString(R.string.title_custom)));
List<EmailProvider> providers = EmailProvider.loadProfiles(getContext());
providers.add(0, new EmailProvider(getString(R.string.title_select)));
providers.add(1, new EmailProvider(getString(R.string.title_custom)));
ArrayAdapter<Provider> aaProvider =
ArrayAdapter<EmailProvider> aaProvider =
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, providers);
aaProvider.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spProvider.setAdapter(aaProvider);
@ -1019,7 +1019,7 @@ public class FragmentAccount extends FragmentEx {
if (account != null) {
boolean found = false;
for (int pos = 2; pos < providers.size(); pos++) {
Provider provider = providers.get(pos);
EmailProvider provider = providers.get(pos);
if (provider.imap_host.equals(account.host) &&
provider.imap_port == account.port) {
found = true;
@ -1265,7 +1265,7 @@ public class FragmentAccount extends FragmentEx {
private void selectAccount() {
Log.i("Select account");
Provider provider = (Provider) spProvider.getSelectedItem();
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
if (provider.type != null)
startActivityForResult(
Helper.getChooser(getContext(), newChooseAccountIntent(

@ -209,7 +209,7 @@ public class FragmentIdentity extends FragmentEx {
else {
boolean found = false;
for (int pos = 1; pos < spProvider.getAdapter().getCount(); pos++) {
Provider provider = (Provider) spProvider.getItemAtPosition(pos);
EmailProvider provider = (EmailProvider) spProvider.getItemAtPosition(pos);
if (provider.imap_host.equals(account.host) &&
provider.imap_port == account.port) {
found = true;
@ -351,7 +351,7 @@ public class FragmentIdentity extends FragmentEx {
return;
adapterView.setTag(position);
Provider provider = (Provider) adapterView.getSelectedItem();
EmailProvider provider = (EmailProvider) adapterView.getSelectedItem();
// Set associated host/port/starttls
etHost.setText(provider.smtp_host);
@ -388,7 +388,7 @@ public class FragmentIdentity extends FragmentEx {
Bundle args = new Bundle();
args.putString("domain", etDomain.getText().toString());
new SimpleTask<Provider>() {
new SimpleTask<EmailProvider>() {
@Override
protected void onPreExecute(Bundle args) {
etDomain.setEnabled(false);
@ -402,13 +402,13 @@ public class FragmentIdentity extends FragmentEx {
}
@Override
protected Provider onExecute(Context context, Bundle args) throws Throwable {
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
String domain = args.getString("domain");
return Provider.fromDomain(context, domain);
return EmailProvider.fromDomain(context, domain);
}
@Override
protected void onExecuted(Bundle args, Provider provider) {
protected void onExecuted(Bundle args, EmailProvider provider) {
etHost.setText(provider.smtp_host);
etPort.setText(Integer.toString(provider.smtp_port));
cbStartTls.setChecked(provider.smtp_starttls);
@ -801,10 +801,10 @@ public class FragmentIdentity extends FragmentEx {
spAccount.setAdapter(aaAccount);
// Get providers
List<Provider> providers = Provider.loadProfiles(getContext());
providers.add(0, new Provider(getString(R.string.title_custom)));
List<EmailProvider> providers = EmailProvider.loadProfiles(getContext());
providers.add(0, new EmailProvider(getString(R.string.title_custom)));
ArrayAdapter<Provider> aaProfile =
ArrayAdapter<EmailProvider> aaProfile =
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, providers);
aaProfile.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spProvider.setAdapter(aaProfile);

@ -285,12 +285,12 @@ public class FragmentSetup extends FragmentEx {
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid));
String[] dparts = email.split("@");
Provider provider = Provider.fromDomain(context, dparts[1]);
EmailProvider provider = EmailProvider.fromDomain(context, dparts[1]);
if (provider.documentation != null)
args.putString("documentation", provider.documentation.toString());
String user = (provider.user == Provider.UserType.EMAIL ? email : dparts[0]);
String user = (provider.user == EmailProvider.UserType.EMAIL ? email : dparts[0]);
Character separator;
long now = new Date().getTime();

Loading…
Cancel
Save