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
17package com.android.sdklib.devices;
18
19import com.android.resources.Keyboard;
20import com.android.resources.Navigation;
21import com.android.resources.UiMode;
22
23import java.util.ArrayList;
24import java.util.Collection;
25import java.util.HashSet;
26import java.util.List;
27import java.util.Set;
28
29public class Hardware {
30    private Screen mScreen;
31    private Set<Network> mNetworking = new HashSet<Network>();
32    private Set<Sensor> mSensors = new HashSet<Sensor>();
33    private boolean mMic;
34    private List<Camera> mCameras = new ArrayList<Camera>();
35    private Keyboard mKeyboard;
36    private Navigation mNav;
37    private Storage mRam;
38    private ButtonType mButtons;
39    private List<Storage> mInternalStorage = new ArrayList<Storage>();
40    private List<Storage> mRemovableStorage = new ArrayList<Storage>();
41    private String mCpu;
42    private String mGpu;
43    private Set<Abi> mAbis = new HashSet<Abi>();
44    private Set<UiMode> mUiModes = new HashSet<UiMode>();
45    private PowerType mPluggedIn;
46
47    public Set<Network> getNetworking() {
48        return mNetworking;
49    }
50
51    public void addNetwork(Network n) {
52        mNetworking.add(n);
53    }
54
55    public void addAllNetworks(Collection<Network> ns) {
56        mNetworking.addAll(ns);
57    }
58
59    public Set<Sensor> getSensors() {
60        return mSensors;
61    }
62
63    public void addSensor(Sensor sensor) {
64        mSensors.add(sensor);
65    }
66
67    public void addAllSensors(Collection<Sensor> sensors) {
68        mSensors.addAll(sensors);
69    }
70
71    public boolean hasMic() {
72        return mMic;
73    }
74
75    public void setHasMic(boolean hasMic) {
76        mMic = hasMic;
77    }
78
79    public List<Camera> getCameras() {
80        return mCameras;
81    }
82
83    public void addCamera(Camera c) {
84        mCameras.add(c);
85    }
86
87    public void addAllCameras(Collection<Camera> cs) {
88        mCameras.addAll(cs);
89    }
90
91    public Camera getCamera(int i) {
92        return mCameras.get(i);
93    }
94
95    public Camera getCamera(CameraLocation location) {
96        for (Camera c : mCameras) {
97            if (location.equals(c.getLocation())) {
98                return c;
99            }
100        }
101        return null;
102    }
103
104    public Keyboard getKeyboard() {
105        return mKeyboard;
106    }
107
108    public void setKeyboard(Keyboard k) {
109        mKeyboard = k;
110    }
111
112    public Navigation getNav() {
113        return mNav;
114    }
115
116    public void setNav(Navigation n) {
117        mNav = n;
118    }
119
120    public Storage getRam() {
121        return mRam;
122    }
123
124    public void setRam(Storage ram) {
125        mRam = ram;
126    }
127
128    public ButtonType getButtonType() {
129        return mButtons;
130    }
131
132    public void setButtonType(ButtonType bt) {
133        mButtons = bt;
134    }
135
136    public List<Storage> getInternalStorage() {
137        return mInternalStorage;
138    }
139
140    public void addInternalStorage(Storage is) {
141        mInternalStorage.add(is);
142    }
143
144    public void addAllInternalStorage(Collection<Storage> is) {
145        mInternalStorage.addAll(is);
146    }
147
148    public List<Storage> getRemovableStorage() {
149        return mRemovableStorage;
150    }
151
152    public void addRemovableStorage(Storage rs) {
153        mRemovableStorage.add(rs);
154    }
155
156    public void addAllRemovableStorage(Collection<Storage> rs) {
157        mRemovableStorage.addAll(rs);
158    }
159
160    public String getCpu() {
161        return mCpu;
162    }
163
164    public void setCpu(String cpuName) {
165        mCpu = cpuName;
166    }
167
168    public String getGpu() {
169        return mGpu;
170    }
171
172    public void setGpu(String gpuName) {
173        mGpu = gpuName;
174    }
175
176    public Set<Abi> getSupportedAbis() {
177        return mAbis;
178    }
179
180    public void addSupportedAbi(Abi abi) {
181        mAbis.add(abi);
182    }
183
184    public void addAllSupportedAbis(Collection<Abi> abis) {
185        mAbis.addAll(abis);
186    }
187
188    public Set<UiMode> getSupportedUiModes() {
189        return mUiModes;
190    }
191
192    public void addSupportedUiMode(UiMode uiMode) {
193        mUiModes.add(uiMode);
194    }
195
196    public void addAllSupportedUiModes(Collection<UiMode> uiModes) {
197        mUiModes.addAll(uiModes);
198    }
199
200    public PowerType getChargeType() {
201        return mPluggedIn;
202    }
203
204    public void setChargeType(PowerType chargeType) {
205        mPluggedIn = chargeType;
206    }
207
208    public Screen getScreen() {
209        return mScreen;
210    }
211
212    public void setScreen(Screen s) {
213        mScreen = s;
214    }
215
216    /**
217     * Returns a copy of the object that shares no state with it,
218     * but is initialized to equivalent values.
219     *
220     * @return A copy of the object.
221     */
222    public Hardware deepCopy() {
223        Hardware hw = new Hardware();
224        hw.mScreen = mScreen.deepCopy();
225        hw.mNetworking = new HashSet<Network>(mNetworking);
226        hw.mSensors = new HashSet<Sensor>(mSensors);
227        // Get the constant boolean value
228        hw.mMic = mMic;
229        hw.mCameras = new ArrayList<Camera>();
230        for (Camera c : mCameras) {
231            hw.mCameras.add(c.deepCopy());
232        }
233        hw.mKeyboard = mKeyboard;
234        hw.mNav = mNav;
235        hw.mRam = mRam.deepCopy();
236        hw.mButtons = mButtons;
237        hw.mInternalStorage = new ArrayList<Storage>();
238        for (Storage s : mInternalStorage) {
239            hw.mInternalStorage.add(s.deepCopy());
240        }
241        hw.mRemovableStorage = new ArrayList<Storage>();
242        for (Storage s : mRemovableStorage) {
243            hw.mRemovableStorage.add(s.deepCopy());
244        }
245        hw.mCpu = mCpu;
246        hw.mGpu = mGpu;
247        hw.mAbis = new HashSet<Abi>(mAbis);
248        hw.mUiModes = new HashSet<UiMode>(mUiModes);
249        hw.mPluggedIn = mPluggedIn;
250        return hw;
251    }
252
253    @Override
254    public boolean equals(Object o) {
255        if (o == this) {
256            return true;
257        }
258        if (!(o instanceof Hardware)) {
259            return false;
260        }
261        Hardware hw = (Hardware) o;
262        return mScreen.equals(hw.getScreen())
263                && mNetworking.equals(hw.getNetworking())
264                && mSensors.equals(hw.getSensors())
265                && mMic == hw.hasMic()
266                && mCameras.equals(hw.getCameras())
267                && mKeyboard == hw.getKeyboard()
268                && mNav == hw.getNav()
269                && mRam.equals(hw.getRam())
270                && mButtons == hw.getButtonType()
271                && mInternalStorage.equals(hw.getInternalStorage())
272                && mRemovableStorage.equals(hw.getRemovableStorage())
273                && mCpu.equals(hw.getCpu())
274                && mGpu.equals(hw.getGpu())
275                && mAbis.equals(hw.getSupportedAbis())
276                && mUiModes.equals(hw.getSupportedUiModes())
277                && mPluggedIn == hw.getChargeType();
278
279    }
280
281    @Override
282    public int hashCode() {
283        int hash = 17;
284        hash = 31 * hash + mScreen.hashCode();
285
286        // Since sets have no defined order, we need to hash them in such a way that order doesn't
287        // matter.
288        int temp = 0;
289        for (Network n : mNetworking) {
290            temp |= 1 << n.ordinal();
291        }
292        hash = 31 * hash + temp;
293
294        temp = 0;
295        for (Sensor s : mSensors) {
296            temp |= 1 << s.ordinal();
297        }
298
299        hash = 31 * hash + temp;
300        hash = 31 * hash + (mMic ? 1 : 0);
301        hash = mCameras.hashCode();
302        hash = 31 * hash + mKeyboard.ordinal();
303        hash = 31 * hash + mNav.ordinal();
304        hash = 31 * hash + mRam.hashCode();
305        hash = 31 * hash + mButtons.ordinal();
306        hash = 31 * hash + mInternalStorage.hashCode();
307        hash = 31 * hash + mRemovableStorage.hashCode();
308
309        for (Character c : mCpu.toCharArray()) {
310            hash = 31 * hash + c;
311        }
312
313        for (Character c : mGpu.toCharArray()) {
314            hash = 31 * hash + c;
315        }
316
317        temp = 0;
318        for (Abi a : mAbis) {
319            temp |= 1 << a.ordinal();
320        }
321        hash = 31 * hash + temp;
322
323        temp = 0;
324        for (UiMode ui : mUiModes) {
325            temp |= 1 << ui.ordinal();
326        }
327        hash = 31 * hash + temp;
328
329        return hash;
330    }
331}
332