MockProvider.java revision 6fa9ad4afcd762aea519ff61811386c23d18ddb2
17ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood/*
27ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * Copyright (C) 2009 The Android Open Source Project
37ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood *
47ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
57ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * you may not use this file except in compliance with the License.
67ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * You may obtain a copy of the License at
77ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood *
87ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
97ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood *
107ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * Unless required by applicable law or agreed to in writing, software
117ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
127ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * See the License for the specific language governing permissions and
147ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * limitations under the License.
157ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood */
167ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwoodpackage com.android.server.location;
187ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1903ca216ac19ea4e7afcb183c20c7c780f0d97756Mike Lockwoodimport android.location.Criteria;
204e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwoodimport android.location.ILocationManager;
217ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwoodimport android.location.Location;
2215e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwoodimport android.location.LocationProvider;
237ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwoodimport android.os.Bundle;
2415e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwoodimport android.os.RemoteException;
257e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackbornimport android.os.WorkSource;
2615e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwoodimport android.util.Log;
277ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwoodimport android.util.PrintWriterPrinter;
287ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.FileDescriptor;
317ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwoodimport java.io.PrintWriter;
327ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ProviderProperties;
346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.internal.location.ProviderRequest;
356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
367ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood/**
377ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * A mock location provider used by LocationManagerService to implement test providers.
387ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood *
397ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood * {@hide}
407ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood */
41d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwoodpublic class MockProvider implements LocationProviderInterface {
4215e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood    private final String mName;
436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final ProviderProperties mProperties;
4415e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood    private final ILocationManager mLocationManager;
456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
467ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private final Location mLocation;
476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    private final Bundle mExtras = new Bundle();
486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
497ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private int mStatus;
507ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private long mStatusUpdateTime;
517ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private boolean mHasLocation;
527ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private boolean mHasStatus;
537ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    private boolean mEnabled;
547ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
5515e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood    private static final String TAG = "MockProvider";
5615e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood
576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public MockProvider(String name, ILocationManager locationManager,
586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            ProviderProperties properties) {
596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (properties == null) throw new NullPointerException("properties is null");
606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
6115e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood        mName = name;
6215e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood        mLocationManager = locationManager;
636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        mProperties = properties;
647ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mLocation = new Location(name);
657ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
667ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
68d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood    public String getName() {
69d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood        return mName;
70d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood    }
71d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood
726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public ProviderProperties getProperties() {
746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        return mProperties;
756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
787ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void disable() {
797ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mEnabled = false;
807ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
817ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
837ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void enable() {
847ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mEnabled = true;
857ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
867ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
88d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood    public boolean isEnabled() {
89d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood        return mEnabled;
90d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood    }
91d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood
926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
937ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public int getStatus(Bundle extras) {
947ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        if (mHasStatus) {
957ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood            extras.clear();
967ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood            extras.putAll(mExtras);
977ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood            return mStatus;
987ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        } else {
9915e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood            return LocationProvider.AVAILABLE;
1007ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        }
1017ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1027ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
1044e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwood    public long getStatusUpdateTime() {
1054e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwood        return mStatusUpdateTime;
1064e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwood    }
1074e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwood
1087ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void setLocation(Location l) {
1097ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mLocation.set(l);
1107ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mHasLocation = true;
1116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        if (mEnabled) {
1126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            try {
1136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                mLocationManager.reportLocation(mLocation, false);
1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            } catch (RemoteException e) {
1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly                Log.e(TAG, "RemoteException calling reportLocation");
1166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly            }
11715e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood        }
1187ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1197ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1207ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void clearLocation() {
1217ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mHasLocation = false;
1227ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1237ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1247ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void setStatus(int status, Bundle extras, long updateTime) {
1257ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mStatus = status;
1267ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mStatusUpdateTime = updateTime;
1277ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mExtras.clear();
1287ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        if (extras != null) {
1297ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood            mExtras.putAll(extras);
1307ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        }
1317ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mHasStatus = true;
1327ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1337ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1347ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void clearStatus() {
1357ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mHasStatus = false;
1364e50b78bda9cd58be61581d2886c88ff6348a1c1Mike Lockwood        mStatusUpdateTime = 0;
1377ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1387ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood
1396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
1406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        dump(pw, "");
14215e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood    }
14315e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood
1447ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    public void dump(PrintWriter pw, String prefix) {
14515e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood        pw.println(prefix + mName);
1467ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mHasLocation=" + mHasLocation);
1477ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mLocation:");
1487ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        mLocation.dump(new PrintWriterPrinter(pw), prefix + "  ");
1497ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mHasStatus=" + mHasStatus);
1507ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mStatus=" + mStatus);
1517ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mStatusUpdateTime=" + mStatusUpdateTime);
1527ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood        pw.println(prefix + "mExtras=" + mExtras);
1537ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood    }
1546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
1566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public void setRequest(ProviderRequest request, WorkSource source) { }
1576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly
1586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    @Override
1596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    public boolean sendExtraCommand(String command, Bundle extras) {
1606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly        return false;
1616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly    }
1627ec434e2e179776d75c8a4da66dcfd2ee630c5d8Mike Lockwood}
163