1 /*
2  * Copyright (C) 2012 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.nfc.beam;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.net.Uri;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 public class BeamTransferRecord implements Parcelable {
25 
26     public static int DATA_LINK_TYPE_BLUETOOTH = 0;
27 
28     public int id;
29     public boolean remoteActivating;
30     public Uri[] uris;
31     public int dataLinkType;
32 
33     // Data link specific fields
34     public BluetoothDevice remoteDevice;
35 
36 
BeamTransferRecord(BluetoothDevice remoteDevice, boolean remoteActivating, Uri[] uris)37     private BeamTransferRecord(BluetoothDevice remoteDevice,
38                                boolean remoteActivating, Uri[] uris) {
39         this.id = 0;
40         this.remoteDevice = remoteDevice;
41         this.remoteActivating = remoteActivating;
42         this.uris = uris;
43 
44         this.dataLinkType = DATA_LINK_TYPE_BLUETOOTH;
45     }
46 
forBluetoothDevice( BluetoothDevice remoteDevice, boolean remoteActivating, Uri[] uris)47     public static final BeamTransferRecord forBluetoothDevice(
48             BluetoothDevice remoteDevice, boolean remoteActivating,
49             Uri[] uris) {
50        return new BeamTransferRecord(remoteDevice, remoteActivating, uris);
51     }
52 
53     public static final Parcelable.Creator<BeamTransferRecord> CREATOR
54             = new Parcelable.Creator<BeamTransferRecord>() {
55         public BeamTransferRecord createFromParcel(Parcel in) {
56             int deviceType = in.readInt();
57 
58             if (deviceType != DATA_LINK_TYPE_BLUETOOTH) {
59                 // only support BLUETOOTH
60                 return null;
61             }
62 
63             BluetoothDevice remoteDevice = in.readParcelable(getClass().getClassLoader());
64             boolean remoteActivating = (in.readInt() == 1);
65             int numUris = in.readInt();
66             Uri[] uris = null;
67             if (numUris > 0) {
68                 uris = new Uri[numUris];
69                 in.readTypedArray(uris, Uri.CREATOR);
70             }
71 
72             return new BeamTransferRecord(remoteDevice,
73                     remoteActivating, uris);
74 
75         }
76 
77         @Override
78         public BeamTransferRecord[] newArray(int size) {
79             return new BeamTransferRecord[size];
80         }
81     };
82 
83     @Override
describeContents()84     public int describeContents() {
85         return 0;
86     }
87 
88     @Override
writeToParcel(Parcel dest, int flags)89     public void writeToParcel(Parcel dest, int flags) {
90         dest.writeInt(dataLinkType);
91         dest.writeParcelable(remoteDevice, 0);
92         dest.writeInt(remoteActivating ? 1 : 0);
93         dest.writeInt(uris != null ? uris.length : 0);
94         if (uris != null && uris.length > 0) {
95             dest.writeTypedArray(uris, 0);
96         }
97     }
98 }
99