-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCBDeviceInfo.java
More file actions
163 lines (151 loc) · 5.38 KB
/
Copy pathCBDeviceInfo.java
File metadata and controls
163 lines (151 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright (c) 2013 Cloudbase.io Limited
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
*/
package com.cloudbase;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Locale;
/**
* This object describes the device the application is running on. The information contained in the
* CBDeviceInfo class are used to register the device with cloudbase.io and to generate the analytics
* @author Stefano Buliani
*
*/
public class CBDeviceInfo {
// Device information used for the analytics
private String deviceUniqueIdentifier;
private String deviceName;
private String deviceModel;
private String language;
private String country;
private String temporaryFilesPath;
/**
* Creates a new CBDeviceInfo object and sets the language and country
* variables to their default values.
*/
public CBDeviceInfo() {
this.setTemporaryFilesPath(".");
Locale currentLocale = Locale.getDefault();
this.setCountry(currentLocale.getCountry());
this.setLanguage(currentLocale.getLanguage());
this.setDeviceUniqueIdentifier(CBDeviceInfo.getMacAddress());
}
/**
* Gets the unique identifier set for the device. By default this is set to the MAC Address of the device
* @return An alphanumeric unique identifier for the device.
*/
public String getDeviceUniqueIdentifier() {
return deviceUniqueIdentifier;
}
/**
* Sets the unique identifier for the device. By default this is set to the MAC Address of the device
* @param deviceUniqueIdentifier An alphanumeric unique identifier
*/
public void setDeviceUniqueIdentifier(String deviceUniqueIdentifier) {
this.deviceUniqueIdentifier = deviceUniqueIdentifier;
}
/**
* Returns he name of the device type (iPhone, iPad, Raspberry Pi etc)
* @return A string identifying the device type
*/
public String getDeviceName() {
return deviceName;
}
/**
* Sets the device type (iPhone, iPad, Raspberry Pi etc)
* @param deviceName A string identifying the device type
*/
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
/**
* Returns the device model to go along with the device name. For example if the device name
* was iPhone the device model could be 4S
* @return A string identifying the device model
*/
public String getDeviceModel() {
return deviceModel;
}
/**
* Sets the device model identifying. This will be used alongside the device name in the
* analytics. For example if the device name was iPhone the device model could be 4S
* @param deviceModel A string identifying the device model
*/
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
/**
* Returns the two letter code of the language of the device. By default this is set to the current locale
* @return The 2 letter code of the language
*/
public String getLanguage() {
return language;
}
/**
* Sets the language of the device. By default this is set to the current locale
* @param language The 2 letter code of the language used on the device
*/
public void setLanguage(String language) {
this.language = language;
}
/**
* Returns the 2 letter country code for the device. By default this is set using the current locale
* @return The 2 letter country code for the device.
*/
public String getCountry() {
return country;
}
/**
* Sets the 2 letter country code for the device. By default this is set using the current locale
* @param country The 2 letter country code for the device.
*/
public void setCountry(String country) {
this.country = country;
}
/**
* Returns the temporary path used by the client library to store queued requests. By default this is set to "."
* @return The full path to the temporary file storate location
*/
public String getTemporaryFilesPath() {
return temporaryFilesPath;
}
/**
* Sets the temporary path used by the client library to store queued requests. By default this is set to "."
* @param temporaryFilesPath The full path to the temporary file storate location
*/
public void setTemporaryFilesPath(String temporaryFilesPath) {
this.temporaryFilesPath = temporaryFilesPath;
}
/**
* Returns the MAC address for the LocalHost network interface. This is used to generate the default unique
* identifier for the device.
* @return A string representation of the network interface MAC address.
*/
public static String getMacAddress() {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
return sb.toString();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
return null;
}
}