1190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank/*
2190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * Copyright (C) 2011 The Android Open Source Project
3190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank *
4190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * Licensed under the Apache License, Version 2.0 (the "License");
5190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * you may not use this file except in compliance with the License.
6190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * You may obtain a copy of the License at
7190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank *
8190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank *      http://www.apache.org/licenses/LICENSE-2.0
9190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank *
10190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * Unless required by applicable law or agreed to in writing, software
11190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * distributed under the License is distributed on an "AS IS" BASIS,
12190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * See the License for the specific language governing permissions and
14190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * limitations under the License.
15190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank */
16190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
17190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blankpackage com.android.emailcommon;
18190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
19190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blankimport android.content.Context;
20190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
21190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blankimport com.android.emailcommon.provider.Account;
22bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport com.android.emailcommon.provider.HostAuth;
23bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport com.android.emailcommon.utility.Utility;
24190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
25190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank/**
26190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * Constants for tagging threads for traffic stats, and associated utilities
27190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank *
28190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank * Example usage:
29bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook *  TrafficStats.setThreadStatsTag(accountId | PROTOCOL_IMAP | DATA_EMAIL | REASON_SYNC);
30190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank */
31190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blankpublic class TrafficFlags {
32190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    // Bits 0->15, account id
33190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final int ACCOUNT_MASK = 0x0000FFFF;
34190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
35bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    // Bits 16&17, protocol (0 = POP3)
36bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    private static final int PROTOCOL_SHIFT = 16;
37bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    private static final int PROTOCOL_MASK = 3 << PROTOCOL_SHIFT;
38bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    public static final int PROTOCOL_POP3 = 0 << PROTOCOL_SHIFT;
39bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    public static final int PROTOCOL_IMAP = 1 << PROTOCOL_SHIFT;
40bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    public static final int PROTOCOL_EAS = 2 << PROTOCOL_SHIFT;
41bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    public static final int PROTOCOL_SMTP = 3 << PROTOCOL_SHIFT;
42bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook    private static final String[] PROTOCOLS = new String[] {HostAuth.SCHEME_POP3,
43bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        HostAuth.SCHEME_IMAP, HostAuth.SCHEME_EAS, HostAuth.SCHEME_SMTP};
44bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook
45190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    // Bits 18&19, type (0 = EMAIL)
46190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final int DATA_SHIFT = 18;
47190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final int DATA_MASK = 3 << DATA_SHIFT;
48190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int DATA_EMAIL = 0 << DATA_SHIFT;
49190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int DATA_CONTACTS = 1 << DATA_SHIFT;
50190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int DATA_CALENDAR = 2 << DATA_SHIFT;
51190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
52190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    // Bits 20&21, reason (if protocol != SMTP)
53190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final int REASON_SHIFT = 20;
54190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final int REASON_MASK = 3 << REASON_SHIFT;
55190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int REASON_SYNC = 0 << REASON_SHIFT;
56190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int REASON_ATTACHMENT_USER = 1 << REASON_SHIFT;
57190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    // Note: We don't yet use the PRECACHE reason (it's complicated...)
58190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static final int REASON_ATTACHMENT_PRECACHE = 2 << REASON_SHIFT;
59190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    private static final String[] REASONS = new String[] {"sync", "attachment", "precache"};
60190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
61190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    /**
62190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * Get flags indicating sync of the passed-in account; note that, by default, these flags
63190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * indicate an email sync; to change the type of sync, simply "or" in DATA_CONTACTS or
64190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * DATA_CALENDAR (since DATA_EMAIL = 0)
65190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     *
66190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param context the caller's context
67190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param account the account being used
68190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @return flags for syncing this account
69190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     */
70190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static int getSyncFlags(Context context, Account account) {
71bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        int protocolIndex = Utility.arrayIndex(PROTOCOLS, account.getProtocol(context));
72bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        return (int)account.mId | REASON_SYNC | (protocolIndex << PROTOCOL_SHIFT);
73190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    }
74190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
75190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    /**
76190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * Get flags indicating attachment loading from the passed-in account
77190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     *
78190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param context the caller's context
79190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param account the account being used
80190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @return flags for loading an attachment in this account
81190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     */
82190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static int getAttachmentFlags(Context context, Account account) {
83bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        int protocolIndex = Utility.arrayIndex(PROTOCOLS, account.getProtocol(context));
84bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        return (int)account.mId | REASON_ATTACHMENT_USER | (protocolIndex << PROTOCOL_SHIFT);
85190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    }
86190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
87190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    /**
88190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * Get flags indicating sending SMTP email from the passed-in account
89190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     *
90190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param context the caller's context
91190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @param account the account being used
92190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     * @return flags for sending SMTP email from this account
93190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank     */
94190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static int getSmtpFlags(Context context, Account account) {
95bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        return (int)account.mId | REASON_SYNC | PROTOCOL_SMTP;
96190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    }
97190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank
98190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    public static String toString(int flags) {
99190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        StringBuilder sb = new StringBuilder();
100190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        sb.append("account ");
101190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        sb.append(flags & ACCOUNT_MASK);
102190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        sb.append(',');
103190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        sb.append(REASONS[(flags & REASON_MASK) >> REASON_SHIFT]);
104bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        sb.append(',');
105bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook        sb.append(PROTOCOLS[(flags & PROTOCOL_MASK) >> PROTOCOL_SHIFT]);
106190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        int maskedData = flags & DATA_MASK;
107190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        if (maskedData != 0) {
108190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank            sb.append(',');
109190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank            sb.append(maskedData == DATA_CALENDAR ? "calendar" : "contacts");
110190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        }
111190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank        return sb.toString();
112190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank    }
113190b2fb8a1be1d72475e2a60d0f00422712ee094Marc Blank}
114