• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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;
18 
19 import android.location.ILocationManager;
20 import android.location.Location;
21 import android.location.LocationProvider;
22 import android.os.Bundle;
23 import android.os.RemoteException;
24 import android.os.WorkSource;
25 import android.util.Log;
26 import android.util.PrintWriterPrinter;
27 
28 
29 import java.io.FileDescriptor;
30 import java.io.PrintWriter;
31 
32 import com.android.internal.location.ProviderProperties;
33 import com.android.internal.location.ProviderRequest;
34 
35 /**
36  * A mock location provider used by LocationManagerService to implement test providers.
37  *
38  * {@hide}
39  */
40 public class MockProvider implements LocationProviderInterface {
41     private final String mName;
42     private final ProviderProperties mProperties;
43     private final ILocationManager mLocationManager;
44 
45     private final Location mLocation;
46     private final Bundle mExtras = new Bundle();
47 
48     private int mStatus;
49     private long mStatusUpdateTime;
50     private boolean mHasLocation;
51     private boolean mHasStatus;
52     private boolean mEnabled;
53 
54     private static final String TAG = "MockProvider";
55 
MockProvider(String name, ILocationManager locationManager, ProviderProperties properties)56     public MockProvider(String name, ILocationManager locationManager,
57             ProviderProperties properties) {
58         if (properties == null) throw new NullPointerException("properties is null");
59 
60         mName = name;
61         mLocationManager = locationManager;
62         mProperties = properties;
63         mLocation = new Location(name);
64     }
65 
66     @Override
getName()67     public String getName() {
68         return mName;
69     }
70 
71     @Override
getProperties()72     public ProviderProperties getProperties() {
73         return mProperties;
74     }
75 
76     @Override
disable()77     public void disable() {
78         mEnabled = false;
79     }
80 
81     @Override
enable()82     public void enable() {
83         mEnabled = true;
84     }
85 
86     @Override
isEnabled()87     public boolean isEnabled() {
88         return mEnabled;
89     }
90 
91     @Override
getStatus(Bundle extras)92     public int getStatus(Bundle extras) {
93         if (mHasStatus) {
94             extras.clear();
95             extras.putAll(mExtras);
96             return mStatus;
97         } else {
98             return LocationProvider.AVAILABLE;
99         }
100     }
101 
102     @Override
getStatusUpdateTime()103     public long getStatusUpdateTime() {
104         return mStatusUpdateTime;
105     }
106 
setLocation(Location l)107     public void setLocation(Location l) {
108         mLocation.set(l);
109         mHasLocation = true;
110         if (mEnabled) {
111             try {
112                 mLocationManager.reportLocation(mLocation, false);
113             } catch (RemoteException e) {
114                 Log.e(TAG, "RemoteException calling reportLocation");
115             }
116         }
117     }
118 
clearLocation()119     public void clearLocation() {
120         mHasLocation = false;
121     }
122 
setStatus(int status, Bundle extras, long updateTime)123     public void setStatus(int status, Bundle extras, long updateTime) {
124         mStatus = status;
125         mStatusUpdateTime = updateTime;
126         mExtras.clear();
127         if (extras != null) {
128             mExtras.putAll(extras);
129         }
130         mHasStatus = true;
131     }
132 
clearStatus()133     public void clearStatus() {
134         mHasStatus = false;
135         mStatusUpdateTime = 0;
136     }
137 
138     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)139     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
140         dump(pw, "");
141     }
142 
dump(PrintWriter pw, String prefix)143     public void dump(PrintWriter pw, String prefix) {
144         pw.println(prefix + mName);
145         pw.println(prefix + "mHasLocation=" + mHasLocation);
146         pw.println(prefix + "mLocation:");
147         mLocation.dump(new PrintWriterPrinter(pw), prefix + "  ");
148         pw.println(prefix + "mHasStatus=" + mHasStatus);
149         pw.println(prefix + "mStatus=" + mStatus);
150         pw.println(prefix + "mStatusUpdateTime=" + mStatusUpdateTime);
151         pw.println(prefix + "mExtras=" + mExtras);
152     }
153 
154     @Override
setRequest(ProviderRequest request, WorkSource source)155     public void setRequest(ProviderRequest request, WorkSource source) { }
156 
157     @Override
sendExtraCommand(String command, Bundle extras)158     public boolean sendExtraCommand(String command, Bundle extras) {
159         return false;
160     }
161 }
162