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