1 package com.android.server.locksettings;
2 
3 import android.hardware.weaver.V1_0.IWeaver;
4 import android.hardware.weaver.V1_0.WeaverConfig;
5 import android.hardware.weaver.V1_0.WeaverReadResponse;
6 import android.hardware.weaver.V1_0.WeaverStatus;
7 import android.hidl.base.V1_0.DebugInfo;
8 import android.os.IHwBinder;
9 import android.os.IHwBinder.DeathRecipient;
10 import android.os.RemoteException;
11 import android.util.Pair;
12 
13 import android.util.Log;
14 
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 
18 public class MockWeaverService extends IWeaver.Stub {
19 
20     private static final int MAX_SLOTS = 8;
21     private static final int KEY_LENGTH = 256 / 8;
22     private static final int VALUE_LENGTH = 256 / 8;
23 
24     private Pair<ArrayList<Byte>, ArrayList<Byte>>[] slots = new Pair[MAX_SLOTS];
25     @Override
getConfig(getConfigCallback cb)26     public void getConfig(getConfigCallback cb) throws RemoteException {
27         WeaverConfig config = new WeaverConfig();
28         config.keySize = KEY_LENGTH;
29         config.valueSize = VALUE_LENGTH;
30         config.slots = MAX_SLOTS;
31         cb.onValues(WeaverStatus.OK, config);
32     }
33 
34     @Override
write(int slotId, ArrayList<Byte> key, ArrayList<Byte> value)35     public int write(int slotId, ArrayList<Byte> key, ArrayList<Byte> value)
36             throws RemoteException {
37         if (slotId < 0 || slotId >= MAX_SLOTS) {
38             throw new RuntimeException("Invalid slot id");
39         }
40         slots[slotId] = Pair.create((ArrayList<Byte>) key.clone(), (ArrayList<Byte>) value.clone());
41         return WeaverStatus.OK;
42     }
43 
44     @Override
read(int slotId, ArrayList<Byte> key, readCallback cb)45     public void read(int slotId, ArrayList<Byte> key, readCallback cb) throws RemoteException {
46         if (slotId < 0 || slotId >= MAX_SLOTS) {
47             throw new RuntimeException("Invalid slot id");
48         }
49 
50         WeaverReadResponse response = new WeaverReadResponse();
51         if (key.equals(slots[slotId].first)) {
52             response.value.addAll(slots[slotId].second);
53             cb.onValues(WeaverStatus.OK, response);
54         } else {
55             cb.onValues(WeaverStatus.FAILED, response);
56         }
57     }
58 }
59