1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.voicemail.impl.mail.store; 18 19 import android.content.Context; 20 import android.net.Network; 21 import com.android.voicemail.impl.imap.ImapHelper; 22 import com.android.voicemail.impl.mail.MailTransport; 23 import com.android.voicemail.impl.mail.Message; 24 import com.android.voicemail.impl.mail.MessagingException; 25 import com.android.voicemail.impl.mail.internet.MimeMessage; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import org.apache.james.mime4j.MimeException; 29 30 public class ImapStore { 31 /** 32 * A global suggestion to Store implementors on how much of the body should be returned on 33 * FetchProfile.Item.BODY_SANE requests. We'll use 125k now. 34 */ 35 public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (125 * 1024); 36 37 private final Context mContext; 38 private final ImapHelper mHelper; 39 private final String mUsername; 40 private final String mPassword; 41 private final MailTransport mTransport; 42 private ImapConnection mConnection; 43 44 public static final int FLAG_NONE = 0x00; // No flags 45 public static final int FLAG_SSL = 0x01; // Use SSL 46 public static final int FLAG_TLS = 0x02; // Use TLS 47 public static final int FLAG_AUTHENTICATE = 0x04; // Use name/password for authentication 48 public static final int FLAG_TRUST_ALL = 0x08; // Trust all certificates 49 public static final int FLAG_OAUTH = 0x10; // Use OAuth for authentication 50 51 /** Contains all the information necessary to log into an imap server */ ImapStore( Context context, ImapHelper helper, String username, String password, int port, String serverName, int flags, Network network)52 public ImapStore( 53 Context context, 54 ImapHelper helper, 55 String username, 56 String password, 57 int port, 58 String serverName, 59 int flags, 60 Network network) { 61 mContext = context; 62 mHelper = helper; 63 mUsername = username; 64 mPassword = password; 65 mTransport = new MailTransport(context, this.getImapHelper(), network, serverName, port, flags); 66 } 67 getContext()68 public Context getContext() { 69 return mContext; 70 } 71 getImapHelper()72 public ImapHelper getImapHelper() { 73 return mHelper; 74 } 75 getUsername()76 public String getUsername() { 77 return mUsername; 78 } 79 getPassword()80 public String getPassword() { 81 return mPassword; 82 } 83 84 /** Returns a clone of the transport associated with this store. */ cloneTransport()85 MailTransport cloneTransport() { 86 return mTransport.clone(); 87 } 88 89 /** Returns UIDs of Messages joined with "," as the separator. */ joinMessageUids(Message[] messages)90 static String joinMessageUids(Message[] messages) { 91 StringBuilder sb = new StringBuilder(); 92 boolean notFirst = false; 93 for (Message m : messages) { 94 if (notFirst) { 95 sb.append(','); 96 } 97 sb.append(m.getUid()); 98 notFirst = true; 99 } 100 return sb.toString(); 101 } 102 103 static class ImapMessage extends MimeMessage { 104 private ImapFolder mFolder; 105 ImapMessage(String uid, ImapFolder folder)106 ImapMessage(String uid, ImapFolder folder) { 107 mUid = uid; 108 mFolder = folder; 109 } 110 setSize(int size)111 public void setSize(int size) { 112 mSize = size; 113 } 114 115 @Override parse(InputStream in)116 public void parse(InputStream in) throws IOException, MessagingException, MimeException { 117 super.parse(in); 118 } 119 setFlagInternal(String flag, boolean set)120 public void setFlagInternal(String flag, boolean set) throws MessagingException { 121 super.setFlag(flag, set); 122 } 123 124 @Override setFlag(String flag, boolean set)125 public void setFlag(String flag, boolean set) throws MessagingException { 126 super.setFlag(flag, set); 127 mFolder.setFlags(new Message[] {this}, new String[] {flag}, set); 128 } 129 } 130 131 static class ImapException extends MessagingException { 132 private static final long serialVersionUID = 1L; 133 134 private final String mStatus; 135 private final String mStatusMessage; 136 private final String mAlertText; 137 private final String mResponseCode; 138 ImapException( String message, String status, String statusMessage, String alertText, String responseCode)139 public ImapException( 140 String message, 141 String status, 142 String statusMessage, 143 String alertText, 144 String responseCode) { 145 super(message); 146 mStatus = status; 147 mStatusMessage = statusMessage; 148 mAlertText = alertText; 149 mResponseCode = responseCode; 150 } 151 getStatus()152 public String getStatus() { 153 return mStatus; 154 } 155 getStatusMessage()156 public String getStatusMessage() { 157 return mStatusMessage; 158 } 159 getAlertText()160 public String getAlertText() { 161 return mAlertText; 162 } 163 getResponseCode()164 public String getResponseCode() { 165 return mResponseCode; 166 } 167 } 168 closeConnection()169 public void closeConnection() { 170 if (mConnection != null) { 171 mConnection.close(); 172 mConnection = null; 173 } 174 } 175 getConnection()176 public ImapConnection getConnection() { 177 if (mConnection == null) { 178 mConnection = new ImapConnection(this); 179 } 180 return mConnection; 181 } 182 } 183