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.phone.common.mail.store; 18 19 import android.content.Context; 20 import android.net.Network; 21 22 import com.android.phone.common.mail.MailTransport; 23 import com.android.phone.common.mail.Message; 24 import com.android.phone.common.mail.MessagingException; 25 import com.android.phone.common.mail.internet.MimeMessage; 26 import com.android.phone.vvm.omtp.imap.ImapHelper; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 public class ImapStore { 32 /** 33 * A global suggestion to Store implementors on how much of the body 34 * should be returned on FetchProfile.Item.BODY_SANE requests. We'll use 125k now. 35 */ 36 public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (125 * 1024); 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 /** 52 * Contains all the information necessary to log into an imap server 53 */ 54 public ImapStore(Context context, ImapHelper helper, String username, String password, int port, 55 String serverName, int flags, Network network) { 56 mContext = context; 57 mHelper = helper; 58 mUsername = username; 59 mPassword = password; 60 mTransport = new MailTransport(context, this.getImapHelper(), 61 network, serverName, port, flags); 62 } 63 64 public Context getContext() { 65 return mContext; 66 } 67 68 public ImapHelper getImapHelper() { 69 return mHelper; 70 } 71 72 public String getUsername() { 73 return mUsername; 74 } 75 76 public String getPassword() { 77 return mPassword; 78 } 79 80 /** Returns a clone of the transport associated with this store. */ 81 MailTransport cloneTransport() { 82 return mTransport.clone(); 83 } 84 85 /** 86 * Returns UIDs of Messages joined with "," as the separator. 87 */ 88 static String joinMessageUids(Message[] messages) { 89 StringBuilder sb = new StringBuilder(); 90 boolean notFirst = false; 91 for (Message m : messages) { 92 if (notFirst) { 93 sb.append(','); 94 } 95 sb.append(m.getUid()); 96 notFirst = true; 97 } 98 return sb.toString(); 99 } 100 101 static class ImapMessage extends MimeMessage { 102 private ImapFolder mFolder; 103 104 ImapMessage(String uid, ImapFolder folder) { 105 mUid = uid; 106 mFolder = folder; 107 } 108 109 public void setSize(int size) { 110 mSize = size; 111 } 112 113 @Override 114 public void parse(InputStream in) throws IOException, MessagingException { 115 super.parse(in); 116 } 117 118 public void setFlagInternal(String flag, boolean set) throws MessagingException { 119 super.setFlag(flag, set); 120 } 121 122 @Override 123 public void setFlag(String flag, boolean set) throws MessagingException { 124 super.setFlag(flag, set); 125 mFolder.setFlags(new Message[] { this }, new String[] { flag }, set); 126 } 127 } 128 129 static class ImapException extends MessagingException { 130 private static final long serialVersionUID = 1L; 131 132 private final String mStatus; 133 private final String mStatusMessage; 134 private final String mAlertText; 135 private final String mResponseCode; 136 137 public ImapException(String message, String status, String statusMessage, String alertText, 138 String responseCode) { 139 super(message); 140 mStatus = status; 141 mStatusMessage = statusMessage; 142 mAlertText = alertText; 143 mResponseCode = responseCode; 144 } 145 146 public String getStatus() { 147 return mStatus; 148 } 149 150 public String getStatusMessage() { 151 return mStatusMessage; 152 } 153 154 public String getAlertText() { 155 return mAlertText; 156 } 157 158 public String getResponseCode() { 159 return mResponseCode; 160 } 161 } 162 163 public void closeConnection() { 164 if (mConnection != null) { 165 mConnection.close(); 166 mConnection = null; 167 } 168 } 169 170 public ImapConnection getConnection() { 171 if (mConnection == null) { 172 mConnection = new ImapConnection(this); 173 } 174 return mConnection; 175 } 176 }