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