mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-11 20:15:03 -05:00
Made the whole AutoconfigInfo class parcelable so it can be passed through the use of intents. This required a very minor change in the parser class.
This commit is contained in:
parent
1f0f229b76
commit
52d9ba89df
@ -10,6 +10,8 @@ package com.fsck.k9.helper.configxmlparser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.Pair;
|
||||
|
||||
/*
|
||||
@ -23,24 +25,47 @@ import android.util.Pair;
|
||||
*/
|
||||
|
||||
|
||||
public class AutoconfigInfo {
|
||||
public class AutoconfigInfo implements Parcelable {
|
||||
|
||||
/*******************************************************************************
|
||||
Fullfil the parcelable interface ( creator part )
|
||||
*******************************************************************************/
|
||||
|
||||
public static final Parcelable.Creator<AutoconfigInfo> CREATOR
|
||||
= new Parcelable.Creator<AutoconfigInfo>() {
|
||||
|
||||
// using a custom constructor to implement this
|
||||
@Override
|
||||
public AutoconfigInfo createFromParcel(Parcel parcel) {
|
||||
return new AutoconfigInfo(parcel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AutoconfigInfo[] newArray(int i) {
|
||||
return new AutoconfigInfo[i];
|
||||
}
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
Define types for some of the data
|
||||
*******************************************************************************/
|
||||
public static enum AuthenticationType { plain, secure, NTLM, GSSAPI, clientIPaddress, TLSclientcert, none };
|
||||
public static enum SocketType { plain, SSL, STARTTLS};
|
||||
public static enum AuthenticationType { plain, secure, NTLM, GSSAPI, clientIPaddress, TLSclientcert, none, UNSET };
|
||||
public static enum SocketType { plain, SSL, STARTTLS, UNSET};
|
||||
public static enum RestrictionType { clientIPAddress };
|
||||
|
||||
public static enum ServerType{ IMAP(0), POP3(1), SMTP(2), UNSET(3), NO_VALUE(4), WRONG_TAG(5);
|
||||
|
||||
private int type;
|
||||
ServerType(int type){this.type = type;}
|
||||
public Server getServerObject(){
|
||||
public Server getServerObject(Parcel parcel){
|
||||
// ugly here but cleanest solution to mix the parcelable and inheritance in the server classes ( user of super() )
|
||||
switch(type){
|
||||
case 0: return new IncomingServerIMAP();
|
||||
case 1: return new IncomingServerPOP3();
|
||||
case 2: return new OutgoingServerSMTP();
|
||||
case 0: if( parcel != null ) return new IncomingServerIMAP(parcel);
|
||||
else return new IncomingServerIMAP();
|
||||
case 1: if( parcel != null )return new IncomingServerPOP3(parcel);
|
||||
else return new IncomingServerPOP3();
|
||||
case 2: if( parcel != null ) return new OutgoingServerSMTP(parcel);
|
||||
else return new OutgoingServerSMTP();
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
@ -60,7 +85,7 @@ public class AutoconfigInfo {
|
||||
/*******************************************************************************
|
||||
Server types hierarchy
|
||||
*******************************************************************************/
|
||||
public static abstract class Server{
|
||||
public static abstract class Server implements Parcelable{
|
||||
public ServerType type;
|
||||
public String hostname;
|
||||
public int port;
|
||||
@ -68,16 +93,58 @@ public class AutoconfigInfo {
|
||||
public String username;
|
||||
public AuthenticationType authentication;
|
||||
|
||||
public Server(ServerType type){ this.type = type; }
|
||||
public Server(ServerType type){
|
||||
if( type != null )
|
||||
this.type = type;
|
||||
else this.type = ServerType.UNSET;
|
||||
}
|
||||
|
||||
public Server(Parcel parcel, ServerType type){
|
||||
this(type);
|
||||
hostname = parcel.readString();
|
||||
port = parcel.readInt();
|
||||
socketType = SocketType.valueOf(parcel.readString());
|
||||
username = parcel.readString();
|
||||
authentication = AuthenticationType.valueOf(parcel.readString());
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<Server> CREATOR
|
||||
= new Parcelable.Creator<Server>() {
|
||||
@Override
|
||||
public Server createFromParcel(Parcel parcel) {
|
||||
return ServerType.toType(parcel.readString()).getServerObject(parcel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server[] newArray(int i) { return new Server[i]; }
|
||||
};
|
||||
|
||||
@Override
|
||||
public int describeContents() { return this.hashCode(); }
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
if( socketType == null ) socketType = SocketType.UNSET;
|
||||
if( type == null ) type = ServerType.UNSET;
|
||||
if( authentication == null ) authentication = AuthenticationType.UNSET;
|
||||
|
||||
parcel.writeString(hostname);
|
||||
parcel.writeInt(port);
|
||||
parcel.writeString(socketType.name());
|
||||
parcel.writeString(username);
|
||||
parcel.writeString(authentication.name());
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class IncomingServer extends Server{
|
||||
public IncomingServer(ServerType type) {super(type);}
|
||||
public IncomingServer(Parcel parcel, ServerType type) {super(parcel, type);}
|
||||
}
|
||||
|
||||
public static abstract class OutgoingServer extends Server{
|
||||
public RestrictionType restriction;
|
||||
public OutgoingServer(ServerType type) {super(type);}
|
||||
public OutgoingServer(Parcel parcel, ServerType type) {super(parcel, type);}
|
||||
}
|
||||
|
||||
public static class IncomingServerPOP3 extends IncomingServer {
|
||||
@ -90,8 +157,29 @@ public class AutoconfigInfo {
|
||||
public int daysToLeaveMessagesOnServer;
|
||||
public int checkInterval;
|
||||
|
||||
// constructor
|
||||
// constructors
|
||||
public IncomingServerPOP3(){ super(ServerType.POP3); }
|
||||
|
||||
public IncomingServerPOP3(Parcel parcel){
|
||||
super(parcel, ServerType.POP3);
|
||||
|
||||
// load in extras
|
||||
leaveMessagesOnServer = ( parcel.readInt() == 1 ) ? true : false;
|
||||
downloadOnBiff = ( parcel.readInt() == 1 ) ? true : false;
|
||||
daysToLeaveMessagesOnServer = parcel.readInt();
|
||||
checkInterval = parcel.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
super.writeToParcel(parcel, i);
|
||||
|
||||
// extra fields in this class
|
||||
parcel.writeInt(leaveMessagesOnServer ? 1 : 0);
|
||||
parcel.writeInt(downloadOnBiff ? 1 : 0);
|
||||
parcel.writeInt(daysToLeaveMessagesOnServer);
|
||||
parcel.writeInt(checkInterval);
|
||||
}
|
||||
}
|
||||
|
||||
public static class IncomingServerIMAP extends IncomingServer {
|
||||
@ -99,6 +187,9 @@ public class AutoconfigInfo {
|
||||
|
||||
// constructor
|
||||
public IncomingServerIMAP(){ super(ServerType.IMAP); }
|
||||
public IncomingServerIMAP(Parcel parcel){
|
||||
super(parcel, ServerType.IMAP);
|
||||
}
|
||||
}
|
||||
|
||||
public static class OutgoingServerSMTP extends OutgoingServer {
|
||||
@ -111,35 +202,135 @@ public class AutoconfigInfo {
|
||||
|
||||
// constructor
|
||||
public OutgoingServerSMTP(){ super(ServerType.SMTP); }
|
||||
public OutgoingServerSMTP(Parcel parcel){
|
||||
super(parcel, ServerType.SMTP);
|
||||
|
||||
// load in extras
|
||||
addThisServer = ( parcel.readInt() == 1 ) ? true : false;
|
||||
useGlobalPreferredServer = ( parcel.readInt() == 1 ) ? true : false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
super.writeToParcel(parcel, i);
|
||||
|
||||
// extra fields in this class
|
||||
parcel.writeInt(addThisServer ? 1 : 0);
|
||||
parcel.writeInt(useGlobalPreferredServer ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
Other sub classes, wrappers for sets of data in the xml
|
||||
*******************************************************************************/
|
||||
public static class InputField {
|
||||
public static class InputField implements Parcelable{
|
||||
|
||||
public static final Parcelable.Creator<InputField> CREATOR
|
||||
= new Parcelable.Creator<InputField>() {
|
||||
@Override
|
||||
public InputField createFromParcel(Parcel parcel) { return new InputField(parcel); }
|
||||
@Override
|
||||
public InputField[] newArray(int i) { return new InputField[i]; }
|
||||
};
|
||||
|
||||
public String key;
|
||||
public String label;
|
||||
public String text;
|
||||
|
||||
public InputField(Parcel parcel){
|
||||
key = parcel.readString();
|
||||
label = parcel.readString();
|
||||
text = parcel.readString();
|
||||
}
|
||||
|
||||
// keep default constructor
|
||||
public InputField() {}
|
||||
|
||||
@Override
|
||||
public int describeContents() { return hashCode(); }
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
parcel.writeString(key);
|
||||
parcel.writeString(label);
|
||||
parcel.writeString(text);
|
||||
}
|
||||
}
|
||||
|
||||
// Serves for the visit tag and the instruction tag
|
||||
public static class InformationBlock {
|
||||
public String url;
|
||||
public static class InformationBlock implements Parcelable{
|
||||
|
||||
public static final Parcelable.Creator<InformationBlock> CREATOR
|
||||
= new Parcelable.Creator<InformationBlock>() {
|
||||
@Override
|
||||
public InformationBlock createFromParcel(Parcel parcel) { return new InformationBlock(parcel); }
|
||||
@Override
|
||||
public InformationBlock[] newArray(int i) { return new InformationBlock[i]; }
|
||||
};
|
||||
|
||||
// fields
|
||||
public String url;
|
||||
// first one is the language, second the text
|
||||
public ArrayList<MutablePair<String, String>> descriptions =
|
||||
new ArrayList<MutablePair<String, String>>();
|
||||
public ArrayList<ParcelableMutablePair<String, String>> descriptions = new ArrayList<ParcelableMutablePair<String, String>>();
|
||||
|
||||
public InformationBlock(Parcel parcel){
|
||||
url = parcel.readString();
|
||||
descriptions = parcel.readArrayList(ParcelableMutablePair.class.getClassLoader());
|
||||
}
|
||||
|
||||
public static class MutablePair<K, V> extends Pair<K, V>{
|
||||
// keep the default constructor too
|
||||
public InformationBlock(){}
|
||||
|
||||
@Override
|
||||
public int describeContents() { return hashCode(); }
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
parcel.writeString(url);
|
||||
parcel.writeList(descriptions);
|
||||
}
|
||||
}
|
||||
|
||||
// This class is pretty 'unstable'. We could force the generic classes to be classes implementing the parcelable
|
||||
// interface but since primitive types and others who are parcelable do not extends this interface we would have to create
|
||||
// a lot of specific classes accepting for example Strings, int's,.... So we just do it this way and trust the programmer using
|
||||
// it to be sure it is only used with parcelable stuff.
|
||||
public static class ParcelableMutablePair<K, V> extends Pair<K, V> implements Parcelable{
|
||||
|
||||
public static final Parcelable.Creator<ParcelableMutablePair> CREATOR
|
||||
= new Parcelable.Creator<ParcelableMutablePair>() {
|
||||
@Override
|
||||
public ParcelableMutablePair createFromParcel(Parcel parcel) { return new ParcelableMutablePair(parcel); }
|
||||
@Override
|
||||
public ParcelableMutablePair[] newArray(int i) { return new ParcelableMutablePair[i]; }
|
||||
};
|
||||
|
||||
private K first;
|
||||
private V second;
|
||||
public MutablePair(K first, V second) {
|
||||
|
||||
public ParcelableMutablePair(K first, V second) {
|
||||
super(first, second);
|
||||
}
|
||||
|
||||
public ParcelableMutablePair(Parcel parcel){
|
||||
super(null, null);
|
||||
first = (K) parcel.readValue(first.getClass().getClassLoader());
|
||||
second = (V) parcel.readValue(second.getClass().getClassLoader());
|
||||
}
|
||||
|
||||
public void setFirst(K val){ this.first = val; }
|
||||
public void setSecond(V val){ this.second = val; }
|
||||
|
||||
@Override
|
||||
public int describeContents() {return hashCode();}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
// NOTE: read info above class declaration!!!!
|
||||
parcel.writeValue(first);
|
||||
parcel.writeValue(second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -170,7 +361,7 @@ public class AutoconfigInfo {
|
||||
|
||||
|
||||
/*
|
||||
Constructor
|
||||
Constructors
|
||||
*/
|
||||
public AutoconfigInfo(){
|
||||
// initialise the fields
|
||||
@ -180,10 +371,48 @@ public class AutoconfigInfo {
|
||||
inputFields = new ArrayList<InputField>();
|
||||
}
|
||||
|
||||
public AutoconfigInfo(Parcel parcel){
|
||||
version = parcel.readString();
|
||||
clientConfigUpdate = parcel.readString();
|
||||
|
||||
id = parcel.readString();
|
||||
domains = parcel.readArrayList(String.class.getClassLoader());
|
||||
displayName = parcel.readString();
|
||||
displayShortName = parcel.readString();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Logic
|
||||
*/
|
||||
|
||||
// will be added when called for
|
||||
|
||||
|
||||
/*
|
||||
What's left of the parcelable interface
|
||||
*/
|
||||
@Override
|
||||
public int describeContents(){ return hashCode(); }
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
parcel.writeString(version);
|
||||
parcel.writeString(clientConfigUpdate);
|
||||
|
||||
parcel.writeString(id);
|
||||
parcel.writeList(domains);
|
||||
parcel.writeString(displayName);
|
||||
parcel.writeString(displayShortName);
|
||||
|
||||
parcel.writeList(incomingServer);
|
||||
parcel.writeList(outgoingServer);
|
||||
|
||||
parcel.writeString(identity);
|
||||
parcel.writeList(inputFields);
|
||||
parcel.writeValue(enable);
|
||||
parcel.writeValue(instruction);
|
||||
parcel.writeValue(documentation);
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,6 @@ import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.IncomingServerPOP3;
|
||||
import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.OutgoingServerSMTP;
|
||||
import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.InputField;
|
||||
import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.InformationBlock;
|
||||
import com.fsck.k9.helper.configxmlparser.AutoconfigInfo.MutablePair;
|
||||
|
||||
// Other
|
||||
import java.util.HashMap;
|
||||
@ -197,7 +196,7 @@ public class ConfigurationXMLHandler extends DefaultHandler {
|
||||
private Server mServerInProgress;
|
||||
private InputField mInputFieldInProgress;
|
||||
private InformationBlock mInformationBlockInProgress;
|
||||
private MutablePair<String, String> mInformationStringInProgress;
|
||||
private AutoconfigInfo.ParcelableMutablePair<String, String> mInformationStringInProgress;
|
||||
|
||||
// Other stuff
|
||||
private Locator mLocator;
|
||||
@ -281,7 +280,7 @@ public class ConfigurationXMLHandler extends DefaultHandler {
|
||||
throw new SAXParseException("Nested server-tags. This is not allowed!", mLocator);
|
||||
String type = attributes.getValue(ATTRIBUTE.TYPE.getXMLStringVersion());
|
||||
if( type != null ){
|
||||
mServerInProgress = ServerType.toType(type).getServerObject();
|
||||
mServerInProgress = ServerType.toType(type).getServerObject(null);
|
||||
mAutoconfigInfo.incomingServer.add(mServerInProgress);
|
||||
}else{ // this should never happen, this file is not formed correctly ( check the relaxng scheme )
|
||||
throw new SAXParseException("Incoming|Outgoing-Server tag has no type attribute!", mLocator);
|
||||
@ -364,7 +363,7 @@ public class ConfigurationXMLHandler extends DefaultHandler {
|
||||
if( mIsEnable || TAG.toTag(localName) == TAG.DESCR ){ // nested version of instruction in enable tag
|
||||
if( mInformationStringInProgress != null )
|
||||
throw new SAXParseException("Illegal nesting of description or instruction-tags.", mLocator);
|
||||
mInformationStringInProgress = new MutablePair<String, String>
|
||||
mInformationStringInProgress = new AutoconfigInfo.ParcelableMutablePair<String, String>
|
||||
(attributes.getValue(ATTRIBUTE.LANG.getXMLStringVersion()),"");
|
||||
mInformationBlockInProgress.descriptions.add(mInformationStringInProgress);
|
||||
mIsInInformationString = true;
|
||||
|
Loading…
Reference in New Issue
Block a user