Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 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 | |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 17 | package com.android.server.location; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 18 | |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 19 | import android.location.ILocationManager; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 20 | import android.location.Location; |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 21 | import android.location.LocationProvider; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 23 | import android.os.RemoteException; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 24 | import android.os.WorkSource; |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 25 | import android.util.Log; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 26 | import android.util.PrintWriterPrinter; |
| 27 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.FileDescriptor; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 30 | import java.io.PrintWriter; |
| 31 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 32 | import com.android.internal.location.ProviderProperties; |
| 33 | import com.android.internal.location.ProviderRequest; |
| 34 | |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 35 | /** |
| 36 | * A mock location provider used by LocationManagerService to implement test providers. |
| 37 | * |
| 38 | * {@hide} |
| 39 | */ |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 40 | public class MockProvider implements LocationProviderInterface { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 41 | private final String mName; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 42 | private final ProviderProperties mProperties; |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 43 | private final ILocationManager mLocationManager; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 44 | |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 45 | private final Location mLocation; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 46 | private final Bundle mExtras = new Bundle(); |
| 47 | |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 48 | private int mStatus; |
| 49 | private long mStatusUpdateTime; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 50 | private boolean mHasLocation; |
| 51 | private boolean mHasStatus; |
| 52 | private boolean mEnabled; |
| 53 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 54 | private static final String TAG = "MockProvider"; |
| 55 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 56 | public MockProvider(String name, ILocationManager locationManager, |
| 57 | ProviderProperties properties) { |
| 58 | if (properties == null) throw new NullPointerException("properties is null"); |
| 59 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 60 | mName = name; |
| 61 | mLocationManager = locationManager; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 62 | mProperties = properties; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 63 | mLocation = new Location(name); |
| 64 | } |
| 65 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 66 | @Override |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 67 | public String getName() { |
| 68 | return mName; |
| 69 | } |
| 70 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public ProviderProperties getProperties() { |
| 73 | return mProperties; |
| 74 | } |
| 75 | |
| 76 | @Override |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 77 | public void disable() { |
| 78 | mEnabled = false; |
| 79 | } |
| 80 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 81 | @Override |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 82 | public void enable() { |
| 83 | mEnabled = true; |
| 84 | } |
| 85 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 86 | @Override |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 87 | public boolean isEnabled() { |
| 88 | return mEnabled; |
| 89 | } |
| 90 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 91 | @Override |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 92 | public int getStatus(Bundle extras) { |
| 93 | if (mHasStatus) { |
| 94 | extras.clear(); |
| 95 | extras.putAll(mExtras); |
| 96 | return mStatus; |
| 97 | } else { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 98 | return LocationProvider.AVAILABLE; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 102 | @Override |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 103 | public long getStatusUpdateTime() { |
| 104 | return mStatusUpdateTime; |
| 105 | } |
| 106 | |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 107 | public void setLocation(Location l) { |
| 108 | mLocation.set(l); |
| 109 | mHasLocation = true; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 110 | if (mEnabled) { |
| 111 | try { |
| 112 | mLocationManager.reportLocation(mLocation, false); |
| 113 | } catch (RemoteException e) { |
| 114 | Log.e(TAG, "RemoteException calling reportLocation"); |
| 115 | } |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 116 | } |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 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; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 135 | mStatusUpdateTime = 0; |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 140 | dump(pw, ""); |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 143 | public void dump(PrintWriter pw, String prefix) { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 144 | pw.println(prefix + mName); |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 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 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 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 | } |
Mike Lockwood | 7ec434e | 2009-03-27 07:46:48 -0700 | [diff] [blame] | 161 | } |