1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.engine; 19 20 import java.util.Collections; 21 import java.util.Map; 22 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 /** 27 * A <code>Presence</code> is an abstract presentation of the user's presence 28 * information. 29 * 30 * Note that changes made to the Presence data won't be reflected to the 31 * server until <code>ImConnection.updateUserPresence</code> is called. 32 * Only the logged in user can update its own presence data via 33 * <code>ImConnection.updateUserPresence</code>. Changes to any other 34 * contact's presence data won't be saved or sent to the server. 35 */ 36 public final class Presence implements Parcelable { 37 public static final int OFFLINE = 0; 38 public static final int DO_NOT_DISTURB = 1; 39 public static final int AWAY = 2; 40 public static final int IDLE = 3; 41 public static final int AVAILABLE = 4; 42 43 public static final int CLIENT_TYPE_DEFAULT = 0; 44 public static final int CLIENT_TYPE_MOBILE = 1; 45 46 private int mStatus; 47 private String mStatusText; 48 private byte[] mAvatarData; 49 private String mAvatarType; 50 private int mClientType; 51 52 private Map<String, String> mExtendedInfo; 53 Presence()54 public Presence() { 55 this(Presence.OFFLINE, null, null, null, CLIENT_TYPE_DEFAULT, null); 56 } 57 Presence(int status, String statusText, byte[] avatarData, String avatarType, int clientType)58 public Presence(int status, String statusText, byte[] avatarData, 59 String avatarType, int clientType) { 60 this(status, statusText, avatarData, avatarType, clientType, null); 61 } 62 Presence(int status, String statusText, byte[] avatarData, String avatarType, int clientType, Map<String, String> extendedInfo)63 public Presence(int status, String statusText, byte[] avatarData, 64 String avatarType, int clientType, Map<String, String> extendedInfo) { 65 setStatus(status); 66 mStatusText = statusText; 67 setAvatar(avatarData, avatarType); 68 mClientType = clientType; 69 mExtendedInfo = extendedInfo; 70 } 71 Presence(Presence p)72 public Presence(Presence p) { 73 this(p.mStatus, p.mStatusText, p.mAvatarData, p.mAvatarType, 74 p.mClientType, p.mExtendedInfo); 75 } 76 Presence(Parcel source)77 public Presence(Parcel source) { 78 mStatus = source.readInt(); 79 mStatusText = source.readString(); 80 mAvatarData = source.createByteArray(); 81 mAvatarType = source.readString(); 82 mClientType = source.readInt(); 83 // TODO - what ClassLoader should be passed to readMap? 84 // TODO - switch to Bundle 85 mExtendedInfo = source.readHashMap(null); 86 } 87 88 /** 89 * Get avatar bitmap. 90 * 91 * @return Avatar bitmap. Note any changes made to the bitmap itself 92 * won't be saved or sent back to the server. To change avatar 93 * call <code>setAvatar</code> with a <b>new</b> bitmap instance. 94 * FIXME: Avatar is stored as a byte array and a type string now, it will 95 * be encapsulated with an Object after we change to ContentProvider. 96 */ getAvatarData()97 public byte[] getAvatarData() { 98 if(mAvatarData == null){ 99 return null; 100 } else { 101 byte[] data = new byte[mAvatarData.length]; 102 System.arraycopy(mAvatarData, 0, data, 0, mAvatarData.length); 103 return data; 104 } 105 } 106 107 /** 108 * Get the MIME type of avatar. 109 * 110 * @return the MIME type of avatar. 111 */ getAvatarType()112 public String getAvatarType() { 113 return mAvatarType; 114 } 115 getClientType()116 public int getClientType() { 117 return mClientType; 118 } 119 getExtendedInfo()120 public Map<String, String> getExtendedInfo() { 121 return mExtendedInfo == null ? null : Collections.unmodifiableMap(mExtendedInfo); 122 } 123 isOnline()124 public boolean isOnline() { 125 return mStatus != OFFLINE; 126 } 127 getStatus()128 public int getStatus() { 129 return mStatus; 130 } 131 setStatus(int status)132 public void setStatus(int status) { 133 if (status < OFFLINE || status > AVAILABLE ) { 134 throw new IllegalArgumentException("invalid presence status value"); 135 } 136 mStatus = status; 137 } 138 getStatusText()139 public String getStatusText() { 140 return mStatusText; 141 } 142 setAvatar(byte[] data, String type)143 public void setAvatar(byte[] data, String type) { 144 if(data != null) { 145 mAvatarData = new byte[data.length]; 146 System.arraycopy(data, 0, mAvatarData, 0, data.length); 147 } else { 148 mAvatarData = null; 149 } 150 mAvatarType = type; 151 } 152 setStatusText(String statusText)153 public void setStatusText(String statusText) { 154 mStatusText = statusText; 155 } 156 setClientType(int clientType)157 public void setClientType(int clientType) { 158 mClientType = clientType; 159 } 160 writeToParcel(Parcel dest, int flags)161 public void writeToParcel(Parcel dest, int flags) { 162 dest.writeInt(mStatus); 163 dest.writeString(mStatusText); 164 dest.writeByteArray(mAvatarData); 165 dest.writeString(mAvatarType); 166 dest.writeInt(mClientType); 167 dest.writeMap(mExtendedInfo); 168 } 169 describeContents()170 public int describeContents() { 171 return 0; 172 } 173 174 public static final Parcelable.Creator<Presence> CREATOR = new Parcelable.Creator<Presence>() { 175 public Presence createFromParcel(Parcel source) { 176 return new Presence(source); 177 } 178 179 public Presence[] newArray(int size) { 180 return new Presence[size]; 181 } 182 }; 183 } 184