MockProvider.java revision b711d57ca4e2c6a1befbfa1a41f4b8094755a93f
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
17package com.android.server.location;
18
19import android.location.Criteria;
20import android.location.ILocationManager;
21import android.location.Location;
22import android.location.LocationProvider;
23import android.os.Bundle;
24import android.os.RemoteException;
25import android.os.WorkSource;
26import android.util.Log;
27import android.util.PrintWriterPrinter;
28
29
30import java.io.FileDescriptor;
31import java.io.PrintWriter;
32
33import com.android.internal.location.ProviderProperties;
34import com.android.internal.location.ProviderRequest;
35
36/**
37 * A mock location provider used by LocationManagerService to implement test providers.
38 *
39 * {@hide}
40 */
41public 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
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
68    public String getName() {
69        return mName;
70    }
71
72    @Override
73    public ProviderProperties getProperties() {
74        return mProperties;
75    }
76
77    @Override
78    public void disable() {
79        mEnabled = false;
80    }
81
82    @Override
83    public void enable() {
84        mEnabled = true;
85    }
86
87    @Override
88    public boolean isEnabled() {
89        return mEnabled;
90    }
91
92    @Override
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
104    public long getStatusUpdateTime() {
105        return mStatusUpdateTime;
106    }
107
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
120    public void clearLocation() {
121        mHasLocation = false;
122    }
123
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
134    public void clearStatus() {
135        mHasStatus = false;
136        mStatusUpdateTime = 0;
137    }
138
139    @Override
140    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
141        dump(pw, "");
142    }
143
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
156    public void setRequest(ProviderRequest request, WorkSource source) { }
157
158    @Override
159    public void switchUser(int userId) {
160        // nothing to do here
161    }
162
163    @Override
164    public boolean sendExtraCommand(String command, Bundle extras) {
165        return false;
166    }
167}
168