/services/java/com/android/server/location/FusedProxy.java

https://github.com/aizuzi/platform_frameworks_base · Java · 128 lines · 72 code · 12 blank · 44 comment · 3 complexity · 25bc9ba82fc3122ce2229620c2501894 MD5 · raw file

  1. /*
  2. * Copyright (C) 2013 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 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. package com.android.server.location;
  17. import com.android.server.ServiceWatcher;
  18. import android.Manifest;
  19. import android.content.Context;
  20. import android.hardware.location.IFusedLocationHardware;
  21. import android.location.IFusedProvider;
  22. import android.os.Handler;
  23. import android.os.RemoteException;
  24. import android.util.Log;
  25. /**
  26. * Proxy that helps bind GCore FusedProvider implementations to the Fused Hardware instances.
  27. *
  28. * @hide
  29. */
  30. public final class FusedProxy {
  31. private final String TAG = "FusedProxy";
  32. private final ServiceWatcher mServiceWatcher;
  33. private final FusedLocationHardwareSecure mLocationHardware;
  34. /**
  35. * Constructor of the class.
  36. * This is private as the class follows a factory pattern for construction.
  37. *
  38. * @param context The context needed for construction.
  39. * @param handler The handler needed for construction.
  40. * @param locationHardware The instance of the Fused location hardware assigned to the proxy.
  41. */
  42. private FusedProxy(
  43. Context context,
  44. Handler handler,
  45. IFusedLocationHardware locationHardware,
  46. int overlaySwitchResId,
  47. int defaultServicePackageNameResId,
  48. int initialPackageNameResId) {
  49. mLocationHardware = new FusedLocationHardwareSecure(
  50. locationHardware,
  51. context,
  52. Manifest.permission.LOCATION_HARDWARE);
  53. Runnable newServiceWork = new Runnable() {
  54. @Override
  55. public void run() {
  56. bindProvider(mLocationHardware);
  57. }
  58. };
  59. // prepare the connection to the provider
  60. mServiceWatcher = new ServiceWatcher(
  61. context,
  62. TAG,
  63. "com.android.location.service.FusedProvider",
  64. overlaySwitchResId,
  65. defaultServicePackageNameResId,
  66. initialPackageNameResId,
  67. newServiceWork,
  68. handler);
  69. }
  70. /**
  71. * Creates an instance of the proxy and binds it to the appropriate FusedProvider.
  72. *
  73. * @param context The context needed for construction.
  74. * @param handler The handler needed for construction.
  75. * @param locationHardware The instance of the Fused location hardware assigned to the proxy.
  76. *
  77. * @return An instance of the proxy if it could be bound, null otherwise.
  78. */
  79. public static FusedProxy createAndBind(
  80. Context context,
  81. Handler handler,
  82. IFusedLocationHardware locationHardware,
  83. int overlaySwitchResId,
  84. int defaultServicePackageNameResId,
  85. int initialPackageNameResId) {
  86. FusedProxy fusedProxy = new FusedProxy(
  87. context,
  88. handler,
  89. locationHardware,
  90. overlaySwitchResId,
  91. defaultServicePackageNameResId,
  92. initialPackageNameResId);
  93. // try to bind the Fused provider
  94. if (!fusedProxy.mServiceWatcher.start()) {
  95. return null;
  96. }
  97. return fusedProxy;
  98. }
  99. /**
  100. * Helper function to bind the FusedLocationHardware to the appropriate FusedProvider instance.
  101. *
  102. * @param locationHardware The FusedLocationHardware instance to use for the binding operation.
  103. */
  104. private void bindProvider(IFusedLocationHardware locationHardware) {
  105. IFusedProvider provider = IFusedProvider.Stub.asInterface(mServiceWatcher.getBinder());
  106. if (provider == null) {
  107. Log.e(TAG, "No instance of FusedProvider found on FusedLocationHardware connected.");
  108. return;
  109. }
  110. try {
  111. provider.onFusedLocationHardwareChange(locationHardware);
  112. } catch (RemoteException e) {
  113. Log.e(TAG, e.toString());
  114. }
  115. }
  116. }