1/*
2 * Copyright (C) 2011 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
17package com.android.emailcommon;
18
19import android.content.Context;
20
21import com.android.emailcommon.provider.Account;
22
23/**
24 * Constants for tagging threads for traffic stats, and associated utilities
25 *
26 * Example usage:
27 *  TrafficStats.setThreadStatsTag(accountId | DATA_EMAIL | REASON_SYNC);
28 */
29public class TrafficFlags {
30    // Bits 0->15, account id
31    private static final int ACCOUNT_MASK = 0x0000FFFF;
32
33    // Bits 18&19, type (0 = EMAIL)
34    private static final int DATA_SHIFT = 18;
35    private static final int DATA_MASK = 3 << DATA_SHIFT;
36    public static final int DATA_EMAIL = 0 << DATA_SHIFT;
37    public static final int DATA_CONTACTS = 1 << DATA_SHIFT;
38    public static final int DATA_CALENDAR = 2 << DATA_SHIFT;
39
40    // Bits 20&21, reason (if protocol != SMTP)
41    private static final int REASON_SHIFT = 20;
42    private static final int REASON_MASK = 3 << REASON_SHIFT;
43    public static final int REASON_SYNC = 0 << REASON_SHIFT;
44    public static final int REASON_ATTACHMENT_USER = 1 << REASON_SHIFT;
45    // Note: We don't yet use the PRECACHE reason (it's complicated...)
46    public static final int REASON_ATTACHMENT_PRECACHE = 2 << REASON_SHIFT;
47    private static final String[] REASONS = new String[] {"sync", "attachment", "precache"};
48
49    /**
50     * Get flags indicating sync of the passed-in account; note that, by default, these flags
51     * indicate an email sync; to change the type of sync, simply "or" in DATA_CONTACTS or
52     * DATA_CALENDAR (since DATA_EMAIL = 0)
53     *
54     * @param context the caller's context
55     * @param account the account being used
56     * @return flags for syncing this account
57     */
58    public static int getSyncFlags(Context context, Account account) {
59        return (int)account.mId | REASON_SYNC;
60    }
61
62    /**
63     * Get flags indicating attachment loading from the passed-in account
64     *
65     * @param context the caller's context
66     * @param account the account being used
67     * @return flags for loading an attachment in this account
68     */
69    public static int getAttachmentFlags(Context context, Account account) {
70        return (int)account.mId | REASON_ATTACHMENT_USER;
71    }
72
73    /**
74     * Get flags indicating sending SMTP email from the passed-in account
75     *
76     * @param context the caller's context
77     * @param account the account being used
78     * @return flags for sending SMTP email from this account
79     */
80    public static int getSmtpFlags(Context context, Account account) {
81        return (int)account.mId | REASON_SYNC;
82    }
83
84    public static String toString(int flags) {
85        StringBuilder sb = new StringBuilder();
86        sb.append("account ");
87        sb.append(flags & ACCOUNT_MASK);
88        sb.append(',');
89        sb.append(REASONS[(flags & REASON_MASK) >> REASON_SHIFT]);
90        int maskedData = flags & DATA_MASK;
91        if (maskedData != 0) {
92            sb.append(',');
93            sb.append(maskedData == DATA_CALENDAR ? "calendar" : "contacts");
94        }
95        return sb.toString();
96    }
97}
98