1 /*
2  * Copyright (C) 2020 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.server.location.provider;
18 
19 import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR;
20 
21 import android.annotation.Nullable;
22 import android.location.Location;
23 import android.location.LocationResult;
24 import android.location.LocationResult.BadLocationException;
25 import android.location.provider.ProviderProperties;
26 import android.location.provider.ProviderRequest;
27 import android.location.util.identity.CallerIdentity;
28 import android.os.Bundle;
29 
30 import java.io.FileDescriptor;
31 import java.io.PrintWriter;
32 import java.util.Set;
33 
34 /**
35  * A mock location provider used by LocationManagerService to implement test providers.
36  *
37  * {@hide}
38  */
39 public class MockLocationProvider extends AbstractLocationProvider {
40 
41     @Nullable private Location mLocation;
42 
MockLocationProvider(ProviderProperties properties, CallerIdentity identity, Set<String> extraAttributionTags)43     public MockLocationProvider(ProviderProperties properties, CallerIdentity identity,
44             Set<String> extraAttributionTags) {
45         // using a direct executor is ok because this class has no locks that could deadlock
46         super(DIRECT_EXECUTOR, identity, properties, extraAttributionTags);
47     }
48 
49     /** Sets the allowed state of this mock provider. */
setProviderAllowed(boolean allowed)50     public void setProviderAllowed(boolean allowed) {
51         setAllowed(allowed);
52     }
53 
54     /** Sets the location to report for this mock provider. */
setProviderLocation(Location l)55     public void setProviderLocation(Location l) {
56         Location location = new Location(l);
57         location.setIsFromMockProvider(true);
58         mLocation = location;
59         try {
60             reportLocation(LocationResult.wrap(location).validate());
61         } catch (BadLocationException e) {
62             throw new IllegalArgumentException(e);
63         }
64     }
65 
66     @Override
onSetRequest(ProviderRequest request)67     public void onSetRequest(ProviderRequest request) {}
68 
69     @Override
onFlush(Runnable callback)70     protected void onFlush(Runnable callback) {
71         callback.run();
72     }
73 
74     @Override
onExtraCommand(int uid, int pid, String command, Bundle extras)75     protected void onExtraCommand(int uid, int pid, String command, Bundle extras) {}
76 
77     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)78     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
79         pw.println("last mock location=" + mLocation);
80     }
81 }
82