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