Skip to content

Commit

Permalink
Use new version of k9-mail
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkel committed Dec 15, 2014
1 parent 507c761 commit 4bed396
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 67 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
</dependency>

<dependency>
<groupId>com.zegoggles</groupId>
<artifactId>k9-imapstore</artifactId>
<version>0.4</version>
<groupId>com.fsck.k9</groupId>
<artifactId>mailz</artifactId>
<type>aar</type>
<version>0.0.1.bc8b2ea</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zegoggles/smssync/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.zegoggles.smssync;

import android.app.Application;
import com.fsck.k9.K9;
import com.fsck.k9.mail.K9;
import com.squareup.otto.Bus;

public class App extends Application {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/zegoggles/smssync/mail/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public InputStream getInputStream() throws MessagingException {
return new ByteArrayInputStream(new byte[0]);
}
}

@Override
public void setEncoding(String s) throws MessagingException {
}
}

private static class FileBody extends Base64Body {
Expand All @@ -104,6 +108,10 @@ public InputStream getInputStream() throws MessagingException {
return new ByteArrayInputStream(new byte[0]);
}
}

@Override
public void setEncoding(String s) throws MessagingException {
}
}

// RFC2231 encoding from geronimo-javamail
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/com/zegoggles/smssync/mail/BackupImapStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.fsck.k9.Account;
import com.fsck.k9.mail.FetchProfile;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.Folder.FolderType;
import com.fsck.k9.mail.Folder.OpenMode;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.store.ImapResponseParser.ImapResponse;
import com.fsck.k9.mail.store.ImapResponseParser;
import com.fsck.k9.mail.store.ImapStore;
import com.zegoggles.smssync.MmsConsts;
import com.zegoggles.smssync.SmsConsts;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -45,26 +43,20 @@

import static com.zegoggles.smssync.App.LOCAL_LOGV;
import static com.zegoggles.smssync.App.TAG;
import static java.util.Collections.sort;

public class BackupImapStore extends ImapStore {
private final Context context;
private final String uri;
private final Map<DataType, BackupFolder> openFolders = new HashMap<DataType, BackupFolder>();

static {
// increase read timeout a bit
com.fsck.k9.mail.Store.SOCKET_READ_TIMEOUT = 60000 * 5;
// com.fsck.k9.mail.Store.SOCKET_READ_TIMEOUT = 60000 * 5;
}

public BackupImapStore(final Context context, final String uri) throws MessagingException {
super(new Account(context) {
@Override
public String getStoreUri() {
return uri;
}
});
super(new BackupStoreConfig(uri));
this.context = context;
this.uri = uri;
}

public BackupFolder getFolder(DataType type) throws MessagingException {
Expand Down Expand Up @@ -102,7 +94,7 @@ public void closeFolders() {
* @return a uri which can be used for logging (i.e. with credentials masked)
*/
public String getStoreUriForLogging() {
Uri uri = Uri.parse(this.uri);
Uri uri = Uri.parse(this.mStoreConfig.getStoreUri());
String userInfo = uri.getUserInfo();

if (!TextUtils.isEmpty(userInfo) && userInfo.contains(":")) {
Expand All @@ -125,7 +117,7 @@ public String getStoreUriForLogging() {
Log.i(TAG, "Label '" + label + "' does not exist yet. Creating.");
folder.create(FolderType.HOLDS_MESSAGES);
}
folder.open(OpenMode.READ_WRITE);
folder.open(Folder.OPEN_MODE_RW);
return folder;
} catch (IllegalArgumentException e) {
// thrown inside K9
Expand All @@ -134,6 +126,10 @@ public String getStoreUriForLogging() {
}
}

public String getStoreUri() {
return mStoreConfig.getStoreUri();
}

public class BackupFolder extends ImapFolder {
private final DataType type;

Expand All @@ -150,7 +146,7 @@ public List<Message> getMessages(final int max, final boolean flagged, final Dat
final List<Message> messages;
final ImapSearcher searcher = new ImapSearcher() {
@Override
public List<ImapResponse> search() throws IOException, MessagingException {
public List<ImapResponseParser.ImapResponse> search() throws IOException, MessagingException {
final StringBuilder sb = new StringBuilder("UID SEARCH 1:*")
.append(' ')
.append(getQuery())
Expand All @@ -162,10 +158,10 @@ public List<ImapResponse> search() throws IOException, MessagingException {
}
};

final Message[] msgs = search(searcher, null);
final List<Message> msgs = search(searcher, null);

Log.i(TAG, "Found " + msgs.length + " msgs" + (since == null ? "" : " (since " + since + ")"));
if (max > 0 && msgs.length > max) {
Log.i(TAG, "Found " + msgs.size() + " msgs" + (since == null ? "" : " (since " + since + ")"));
if (max > 0 && msgs.size() > max) {
if (LOCAL_LOGV) Log.v(TAG, "Fetching envelopes");

FetchProfile fp = new FetchProfile();
Expand All @@ -174,15 +170,14 @@ public List<ImapResponse> search() throws IOException, MessagingException {

if (LOCAL_LOGV) Log.v(TAG, "Sorting");
//Debug.startMethodTracing("sorting");
Arrays.sort(msgs, MessageComparator.INSTANCE);
sort(msgs, MessageComparator.INSTANCE);
//Debug.stopMethodTracing();
if (LOCAL_LOGV) Log.v(TAG, "Sorting done");

messages = new ArrayList<Message>(max);
messages.addAll(Arrays.asList(msgs).subList(0, max));
messages.addAll(msgs.subList(0, max));
} else {
messages = new ArrayList<Message>(msgs.length);
Collections.addAll(messages, msgs);
messages = msgs;
}

Collections.reverse(messages);
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/zegoggles/smssync/mail/BackupStoreConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.zegoggles.smssync.mail;

import com.fsck.k9.mail.store.StoreConfig;

class BackupStoreConfig implements StoreConfig {
private static final String INBOX = "INBOX";
private static final String UUID = "SMSBACKUP";
private final String storeUri;

BackupStoreConfig(String storeUri) {
this.storeUri = storeUri;
}

@Override public String getUuid() {
return UUID;
}
@Override public String getStoreUri() {
return storeUri;
}
@Override public String getTransportUri() { return null; }
@Override public boolean subscribedFoldersOnly() {
return false;
}
@Override public boolean useCompression(int i) {
return false;
}
@Override public String getInboxFolderName() {
return INBOX;
}
@Override public String getOutboxFolderName() {
return null;
}
@Override public String getDraftsFolderName() {
return null;
}
@Override public void setDraftsFolderName(String s) { }
@Override public void setTrashFolderName(String s) { }
@Override public void setSpamFolderName(String s) { }
@Override public void setSentFolderName(String s) { }
@Override public void setAutoExpandFolderName(String s) { }
@Override public void setInboxFolderName(String s) { }
@Override public int getMaximumAutoDownloadMessageSize() { return 0; }
@Override public boolean allowRemoteSearch() { return false; }
@Override public boolean isRemoteSearchFullText() {
return false;
}
@Override public boolean isPushPollOnConnect() {
return false;
}
@Override public int getDisplayCount() {
return 0;
}
@Override public int getIdleRefreshMinutes() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import com.fsck.k9.mail.AuthType;
import com.zegoggles.smssync.auth.TokenRefresher;
import com.zegoggles.smssync.auth.XOAuthConsumer;
import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Locale;

import static com.zegoggles.smssync.App.TAG;

Expand Down Expand Up @@ -154,22 +156,13 @@ public String getStoreUri() {
if (useXOAuth()) {
if (hasOauthTokens()) {
XOAuthConsumer consumer = getOAuthConsumer();
return String.format(IMAP_URI,
ServerPreferences.Defaults.SERVER_PROTOCOL,
"xoauth:" + encode(consumer.getUsername()),
encode(consumer.generateXOAuthString()),
serverPreferences.getServerAddress());
return formatUri(AuthType.XOAUTH, consumer.getUsername(), consumer.generateXOAuthString());
} else if (hasOAuth2Tokens()) {
return String.format(IMAP_URI,
ServerPreferences.Defaults.SERVER_PROTOCOL,
"xoauth2:" + encode(getOauth2Username()),
encode(generateXOAuth2Token()),
serverPreferences.getServerAddress());
return formatUri(AuthType.XOAUTH2, getOauth2Username(), generateXOAuth2Token());
} else {
Log.w(TAG, "No valid xoauth1/2 tokens");
return null;
}

} else {
return String.format(IMAP_URI,
serverPreferences.getServerProtocol(),
Expand All @@ -179,6 +172,14 @@ public String getStoreUri() {
}
}

private String formatUri(AuthType authType, String username, String password) {
return String.format(IMAP_URI,
ServerPreferences.Defaults.SERVER_PROTOCOL,
authType.name().toUpperCase(Locale.US) + ":" + encode(username),
encode(password),
serverPreferences.getServerAddress());
}

private String getOauthTokenSecret() {
return getCredentials().getString(OAUTH_TOKEN_SECRET, null);
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/zegoggles/smssync/service/BackupTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.XOAuth2AuthenticationFailedException;
import com.fsck.k9.mail.store.XOAuth2AuthenticationFailedException;
import com.squareup.otto.Subscribe;
import com.zegoggles.smssync.App;
import com.zegoggles.smssync.R;
Expand All @@ -32,8 +32,16 @@

import static com.zegoggles.smssync.App.LOCAL_LOGV;
import static com.zegoggles.smssync.App.TAG;
import static com.zegoggles.smssync.mail.DataType.*;
import static com.zegoggles.smssync.service.state.SmsSyncState.*;
import static com.zegoggles.smssync.mail.DataType.CALLLOG;
import static com.zegoggles.smssync.mail.DataType.Defaults;
import static com.zegoggles.smssync.mail.DataType.MMS;
import static com.zegoggles.smssync.mail.DataType.SMS;
import static com.zegoggles.smssync.service.state.SmsSyncState.BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.CALC;
import static com.zegoggles.smssync.service.state.SmsSyncState.CANCELED_BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.ERROR;
import static com.zegoggles.smssync.service.state.SmsSyncState.FINISHED_BACKUP;
import static com.zegoggles.smssync.service.state.SmsSyncState.LOGIN;

class BackupTask extends AsyncTask<BackupConfig, BackupState, BackupState> {
private final SmsBackupService service;
Expand Down Expand Up @@ -254,7 +262,7 @@ private BackupState backupCursors(BackupCursors cursors, BackupImapStore store,
messages.size(), cursor.type));
}

store.getFolder(cursor.type).appendMessages(messages.toArray(new Message[messages.size()]));
store.getFolder(cursor.type).appendMessages(messages);

if (cursor.type == CALLLOG && calendarSyncer != null) {
calendarSyncer.syncCalendar(result);
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/zegoggles/smssync/service/RestoreTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fsck.k9.mail.FetchProfile;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.XOAuth2AuthenticationFailedException;
import com.fsck.k9.mail.store.XOAuth2AuthenticationFailedException;
import com.squareup.otto.Subscribe;
import com.zegoggles.smssync.App;
import com.zegoggles.smssync.Consts;
Expand All @@ -27,6 +27,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -35,7 +36,13 @@
import static com.zegoggles.smssync.App.TAG;
import static com.zegoggles.smssync.mail.DataType.CALLLOG;
import static com.zegoggles.smssync.mail.DataType.SMS;
import static com.zegoggles.smssync.service.state.SmsSyncState.*;
import static com.zegoggles.smssync.service.state.SmsSyncState.CALC;
import static com.zegoggles.smssync.service.state.SmsSyncState.CANCELED_RESTORE;
import static com.zegoggles.smssync.service.state.SmsSyncState.ERROR;
import static com.zegoggles.smssync.service.state.SmsSyncState.FINISHED_RESTORE;
import static com.zegoggles.smssync.service.state.SmsSyncState.LOGIN;
import static com.zegoggles.smssync.service.state.SmsSyncState.RESTORE;
import static com.zegoggles.smssync.service.state.SmsSyncState.UPDATING_THREADS;

class RestoreTask extends AsyncTask<RestoreConfig, RestoreState, RestoreState> {
private Set<String> smsIds = new HashSet<String>();
Expand Down Expand Up @@ -214,7 +221,7 @@ private DataType importMessage(Message message) {
DataType dataType = null;
try {
if (LOCAL_LOGV) Log.v(TAG, "fetching message uid " + message.getUid());
message.getFolder().fetch(new Message[]{message}, fp, null);
message.getFolder().fetch(Arrays.asList(message), fp, null);
dataType = converter.getDataType(message);
//only restore sms+call log for now
switch (dataType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.text.TextUtils;
import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.XOAuth2AuthenticationFailedException;
import com.fsck.k9.mail.store.XOAuth2AuthenticationFailedException;
import com.zegoggles.smssync.R;
import com.zegoggles.smssync.mail.DataType;
import com.zegoggles.smssync.service.exception.ConnectivityException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void shouldTestForValidFolder() throws Exception {
@Test public void testAccountHasStoreUri() throws Exception {
String uri = "imap://xoauth:[email protected]";
BackupImapStore store = new BackupImapStore(Robolectric.application, uri);
assertThat(store.getAccount().getStoreUri()).isEqualTo(uri);
assertThat(store.getStoreUri()).isEqualTo(uri);
}

@Test public void shouldHaveToStringWithObfuscatedStoreURI() throws Exception {
Expand Down
Loading

0 comments on commit 4bed396

Please sign in to comment.