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