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      */
ImapStore(Context context, ImapHelper helper, String username, String password, int port, String serverName, int flags, Network network)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 
getContext()64     public Context getContext() {
65         return mContext;
66     }
67 
getImapHelper()68     public ImapHelper getImapHelper() {
69         return mHelper;
70     }
71 
getUsername()72     public String getUsername() {
73         return mUsername;
74     }
75 
getPassword()76     public String getPassword() {
77         return mPassword;
78     }
79 
80     /** Returns a clone of the transport associated with this store. */
cloneTransport()81     MailTransport cloneTransport() {
82         return mTransport.clone();
83     }
84 
85     /**
86      * Returns UIDs of Messages joined with "," as the separator.
87      */
joinMessageUids(Message[] messages)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 
ImapMessage(String uid, ImapFolder folder)104         ImapMessage(String uid, ImapFolder folder) {
105             mUid = uid;
106             mFolder = folder;
107         }
108 
setSize(int size)109         public void setSize(int size) {
110             mSize = size;
111         }
112 
113         @Override
parse(InputStream in)114         public void parse(InputStream in) throws IOException, MessagingException {
115             super.parse(in);
116         }
117 
setFlagInternal(String flag, boolean set)118         public void setFlagInternal(String flag, boolean set) throws MessagingException {
119             super.setFlag(flag, set);
120         }
121 
122         @Override
setFlag(String flag, boolean set)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 mAlertText;
134         private final String mResponseCode;
135 
ImapException(String message, String status, String alertText, String responseCode)136         public ImapException(String message, String status, String alertText,
137                 String responseCode) {
138             super(message);
139             mStatus = status;
140             mAlertText = alertText;
141             mResponseCode = responseCode;
142         }
143 
getStatus()144         public String getStatus() {
145             return mStatus;
146         }
147 
getAlertText()148         public String getAlertText() {
149             return mAlertText;
150         }
151 
getResponseCode()152         public String getResponseCode() {
153             return mResponseCode;
154         }
155     }
156 
closeConnection()157     public void closeConnection() {
158         if (mConnection != null) {
159             mConnection.close();
160             mConnection = null;
161         }
162     }
163 
getConnection()164     public ImapConnection getConnection() {
165         if (mConnection == null) {
166             mConnection = new ImapConnection(this);
167         }
168         return mConnection;
169     }
170 }