• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.os.Parcel;
21 import android.util.Log;
22 
23 import com.android.im.imps.ImpsContactListAddress;
24 import com.android.im.imps.ImpsGroupAddress;
25 import com.android.im.imps.ImpsUserAddress;
26 
27 /**
28  * A helper for marshalling and unmarshaling an Address Object to a Parcel.
29  * The Address is only an abstract representation, the underlying protocol
30  * implementation MUST provide a public default constructor and register
31  * their implementing class here.
32  */
33 public class AddressParcelHelper {
34     private static Class[] sAddressClasses = new Class[] {
35         ImpsUserAddress.class,
36         ImpsGroupAddress.class,
37         ImpsContactListAddress.class,
38     };
39 
AddressParcelHelper()40     private AddressParcelHelper() {
41     }
42 
readFromParcel(Parcel source)43     public static Address readFromParcel(Parcel source) {
44         int classIndex = source.readInt();
45         if(classIndex == -1) {
46             return null;
47         }
48         if(classIndex < 0 || classIndex >= sAddressClasses.length) {
49             throw new RuntimeException("Unknown Address type index: " + classIndex);
50         }
51         try {
52             Address address = (Address)sAddressClasses[classIndex].newInstance();
53             address.readFromParcel(source);
54             return address;
55         } catch (InstantiationException e) {
56             Log.e("AddressParcel", "Default constructor are required on Class"
57                     + sAddressClasses[classIndex].getName());
58             throw new RuntimeException(e);
59         } catch (IllegalAccessException e) {
60             Log.e("AddressParcel", "Default constructor are required on Class"
61                     + sAddressClasses[classIndex].getName());
62             throw new RuntimeException(e);
63         }
64     }
65 
writeToParcel(Parcel dest, Address address)66     public static void writeToParcel(Parcel dest, Address address) {
67         if(address == null) {
68             dest.writeInt(-1);
69         } else {
70             dest.writeInt(getClassIndex(address));
71             address.writeToParcel(dest);
72         }
73     }
74 
getClassIndex(Address address)75     private static int getClassIndex(Address address) {
76         for(int i = 0; i < sAddressClasses.length; i++) {
77             if(address.getClass() == sAddressClasses[i]) {
78                 return i;
79             }
80         }
81         throw new RuntimeException("Unregistered Address type: " + address);
82     }
83 }
84