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 implements IWeaver {
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     @Override
asBinder()60     public IHwBinder asBinder() {
61         throw new UnsupportedOperationException();
62     }
63 
64     @Override
interfaceChain()65     public ArrayList<String> interfaceChain() throws RemoteException {
66         throw new UnsupportedOperationException();
67     }
68 
69     @Override
interfaceDescriptor()70     public String interfaceDescriptor() throws RemoteException {
71         throw new UnsupportedOperationException();
72     }
73 
74     @Override
setHALInstrumentation()75     public void setHALInstrumentation() throws RemoteException {
76         throw new UnsupportedOperationException();
77     }
78 
79     @Override
linkToDeath(DeathRecipient recipient, long cookie)80     public boolean linkToDeath(DeathRecipient recipient, long cookie) throws RemoteException {
81         throw new UnsupportedOperationException();
82     }
83 
84     @Override
ping()85     public void ping() throws RemoteException {
86         throw new UnsupportedOperationException();
87     }
88 
89     @Override
getDebugInfo()90     public DebugInfo getDebugInfo() throws RemoteException {
91         throw new UnsupportedOperationException();
92     }
93 
94     @Override
notifySyspropsChanged()95     public void notifySyspropsChanged() throws RemoteException {
96         throw new UnsupportedOperationException();
97     }
98 
99     @Override
unlinkToDeath(DeathRecipient recipient)100     public boolean unlinkToDeath(DeathRecipient recipient) throws RemoteException {
101         throw new UnsupportedOperationException();
102     }
103 
104     @Override
getHashChain()105     public ArrayList<byte[]> getHashChain() throws RemoteException {
106         throw new UnsupportedOperationException();
107     }
108 }
109