FusedLocationProvider.java revision 6fa9ad4afcd762aea519ff61811386c23d18ddb2
16fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/* 26fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Copyright (C) 2012 The Android Open Source Project 36fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * 46fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 56fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * you may not use this file except in compliance with the License. 66fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * You may obtain a copy of the License at 76fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * 86fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * http://www.apache.org/licenses/LICENSE-2.0 96fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * 106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * Unless required by applicable law or agreed to in writing, software 116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * See the License for the specific language governing permissions and 146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * limitations under the License. 156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */ 166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellypackage com.android.location.fused; 186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.FileDescriptor; 216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport java.io.PrintWriter; 226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.location.provider.LocationProviderBase; 246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.location.provider.ProviderPropertiesUnbundled; 256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport com.android.location.provider.ProviderRequestUnbundled; 266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.content.Context; 286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.Criteria; 296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.location.LocationProvider; 306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.Bundle; 316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.Handler; 326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.Looper; 336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.Message; 346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellyimport android.os.WorkSource; 356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellypublic class FusedLocationProvider extends LocationProviderBase implements FusionEngine.Callback { 376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static final String TAG = "FusedLocationProvider"; 386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static ProviderPropertiesUnbundled PROPERTIES = ProviderPropertiesUnbundled.create( 406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly false, false, false, false, true, true, true, Criteria.POWER_LOW, 416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly Criteria.ACCURACY_FINE); 426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static final int MSG_ENABLE = 1; 446fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static final int MSG_DISABLE = 2; 456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static final int MSG_SET_REQUEST = 3; 466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private final Context mContext; 486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private final FusionEngine mEngine; 496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private static class RequestWrapper { 516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public ProviderRequestUnbundled request; 526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public WorkSource source; 536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public RequestWrapper(ProviderRequestUnbundled request, WorkSource source) { 546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly this.request = request; 556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly this.source = source; 566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public FusedLocationProvider(Context context) { 606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly super(TAG, PROPERTIES); 616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mContext = context; 626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mEngine = new FusionEngine(context, Looper.myLooper()); 636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly /** 666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * For serializing requests to mEngine. 676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */ 686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly private Handler mHandler = new Handler() { 696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public void handleMessage(Message msg) { 716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly switch (msg.what) { 726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly case MSG_ENABLE: 736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mEngine.init(FusedLocationProvider.this); 746fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly break; 756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly case MSG_DISABLE: 766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mEngine.deinit(); 776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly break; 786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly case MSG_SET_REQUEST: 796fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly { 806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly RequestWrapper wrapper = (RequestWrapper) msg.obj; 816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mEngine.setRequirements(wrapper.request, wrapper.source); 826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly break; 836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly }; 876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public void onEnable() { 906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mHandler.sendEmptyMessage(MSG_ENABLE); 916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public void onDisable() { 956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mHandler.sendEmptyMessage(MSG_DISABLE); 966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public void onSetRequest(ProviderRequestUnbundled request, WorkSource source) { 1006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mHandler.obtainMessage(MSG_SET_REQUEST, new RequestWrapper(request, source)); 1016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 1026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 1046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public void onDump(FileDescriptor fd, PrintWriter pw, String[] args) { 1056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly // perform synchronously 1066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mEngine.dump(fd, pw, args); 1076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 1086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 1106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public int onGetStatus(Bundle extras) { 1116fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly return LocationProvider.AVAILABLE; 1126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 1136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly @Override 1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly public long onGetStatusUpdateTime() { 1166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly return 0; 1176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 1186fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} 119