/** * Tests for requestLocation. * * @throws Exception */ @Test public void requestLocation() throws Exception { GoogleApiClient mockGoogleApiClient = mock(GoogleApiClient.class); doReturn(true).when(mockGoogleApiClient).isConnected(); LocationManager mockLocationManager = spy(mLocationManager); Whitebox.setInternalState(mockLocationManager, "googleApiClient", mockGoogleApiClient); FusedLocationProviderApi mockLocationProviderApi = mock(FusedLocationProviderApi.class); Whitebox.setInternalState(LocationServices.class, "FusedLocationApi", mockLocationProviderApi); // Testing when a customer did not disableLocationCollection. Whitebox.invokeMethod(mockLocationManager, "requestLocation"); verify(mockLocationProviderApi).requestLocationUpdates(any(GoogleApiClient.class), any(LocationRequest.class), any(LocationListener.class)); // Testing when a customer disableLocationCollection. Leanplum.disableLocationCollection(); Whitebox.invokeMethod(mockLocationManager, "requestLocation"); verifyNoMoreInteractions(mockLocationProviderApi); }
@Override public void onReceive(Context context, Intent intent) { this.context=context; notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); nBuilder = new NotificationCompat.Builder(context); nBuilder.setContentTitle("DroidAssistant"); nBuilder.setTicker("DroidAssistant"); nBuilder.setSmallIcon(R.drawable.robert_icono); nBuilder.setLargeIcon(BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.robert_icono)); nBuilder.setAutoCancel(true); mLastLocation = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); if(mLastLocation==null) { } else { endLatitude=mLastLocation.getLatitude(); endLongitude=mLastLocation.getLongitude(); fetchLocationDB(); } }
/** * Called when the location has been updated */ private void locationUpdated(Intent intent) { Log.v(TAG, ACTION_LOCATION_UPDATED); // Extra new location Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); if (location != null) { LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude()); // Store in a local preference as well Utils.storeLocation(this, latLngLocation); // Send a local broadcast so if an Activity is open it can respond // to the updated location LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } }
@Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) { final Location location = intent.getParcelableExtra( FusedLocationProviderApi.KEY_LOCATION_CHANGED); handleActionCheckLocation(location); } else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) { handleActionChangeToNormalUpdates(); } else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) { handleActionStartFastUpdates(); } } }
@Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) { final Location location = intent.getParcelableExtra( FusedLocationProviderApi.KEY_LOCATION_CHANGED); handleActionCheckLocation(location); } else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) { handleActionStartNormalUpdates(); } else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) { handleActionChangeToFastUpdates(); } } }
@Before public void setup() { lp = spy(LocationPublisher.class); lp.mGoogClient = mock(GoogleApiClient.class); lp.mLocProvider = mock(FusedLocationProviderApi.class); shadowOf(RuntimeEnvironment.application) .setComponentNameAndServiceForBindService( new ComponentName("org.tlc.whereat.modules.pubsub", "LocationPublisher"), mock(IBinder.class)); lp.mPrefs = ShadowPreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application); lp.mPrefListener = lp.buildPrefListener(); lp.mPrefs.registerOnSharedPreferenceChangeListener(lp.mPrefListener); }
@Test public void poll_turnsOnPolling() { lp.mLocProvider = mock(FusedLocationProviderApi.class); lp.poll(); verify(lp.mLocProvider).requestLocationUpdates(lp.mGoogClient, lp.mLocReq, lp); assertThat(lp.mPolling).isTrue(); }
@Test public void stopPolling_stopsPolling() { lp.mLocProvider = mock(FusedLocationProviderApi.class); lp.stopPolling(); verify(lp.mLocProvider).removeLocationUpdates(lp.mGoogClient, lp); assertThat(lp.mPolling).isFalse(); }
static LocationPublisher setupMockLocationService(){ LocationPublisher lp = spy(LocationPublisher.class); lp.mGoogClient = mock(GoogleApiClient.class); lp.mLocProvider = mock(FusedLocationProviderApi.class); bindService(); return lp; }
/** * Called when a location update is requested */ private void requestLocationInternal() { Log.v(TAG, ACTION_REQUEST_LOCATION); GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .build(); // It's OK to use blockingConnect() here as we are running in an // IntentService that executes work on a separate (background) thread. ConnectionResult connectionResult = googleApiClient.blockingConnect( Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS); if (connectionResult.isSuccess() && googleApiClient.isConnected()) { Intent locationUpdatedIntent = new Intent(this, UtilityService.class); locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED); // Send last known location out first if available Location location = FusedLocationApi.getLastLocation(googleApiClient); if (location != null) { Intent lastLocationIntent = new Intent(locationUpdatedIntent); lastLocationIntent.putExtra( FusedLocationProviderApi.KEY_LOCATION_CHANGED, location); startService(lastLocationIntent); } // Request new location LocationRequest mLocationRequest = new LocationRequest() .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); FusedLocationApi.requestLocationUpdates( googleApiClient, mLocationRequest, PendingIntent.getService(this, 0, locationUpdatedIntent, 0)); googleApiClient.disconnect(); } else { Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG, connectionResult.getErrorCode())); } }
@Override public void onReceive(Context context, Intent intent) { Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); if (location != null) { mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude()); mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation); mAdapter.notifyDataSetChanged(); } }
@Override public FusedLocationProviderApi create() { return LocationServices.FusedLocationApi; }
@Override public FusedLocationProviderApi create() { return new MockFusedLocationProvider(); }
LocationProviderGmsCore(GoogleApiClient client, FusedLocationProviderApi locationApi) { mGoogleApiClient = client; mLocationProviderApi = locationApi; }
@Override protected void onHandleIntent(Intent intent) { //String className = intent.getStringExtra("backgroundClass"); final android.location.Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); if (AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener != null) { // This is basically just a short-term location request and we are using the in-memory listeners. AndroidLocationPlayServiceManager mgr = AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener; mgr.onLocationChanged(location); return; } if (intent.getDataString() == null) { System.out.println("BackgroundLocationHandler received update without data string."); return; } String[] params = intent.getDataString().split("[?]"); //might happen on some occasions, no need to do anything. if (location == null) { return; } //if the Display is not initialized we need to launch the CodenameOneBackgroundLocationActivity //activity to handle this boolean shouldStopWhenDone = false; if (!Display.isInitialized()) { shouldStopWhenDone = true; AndroidImplementation.startContext(this); //Display.init(this); /* Intent bgIntent = new Intent(getBaseContext(), CodenameOneBackgroundLocationActivity.class); Bundle b = new Bundle(); b.putString("backgroundLocation", params[1]); b.putParcelable("Location", location); bgIntent.putExtras(b); //Put your id to your next Intent bgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(bgIntent); */ } //else { try { //the 2nd parameter is the class name we need to create LocationListener l = (LocationListener) Class.forName(params[1]).newInstance(); l.locationUpdated(AndroidLocationManager.convert(location)); } catch (Exception e) { Log.e("Codename One", "background location error", e); } if (shouldStopWhenDone) { AndroidImplementation.stopContext(this); } //} }
public ObservableLocation(GoogleApiClient googleAPIClient, FusedLocationProviderApi locationProviderAPI, LocationRequest request){ this.mLocationProviderAPI = locationProviderAPI; this.mGoogleAPIClient = googleAPIClient; this.mLocationRequest = request; }
private void setupMocks() { mockGoogleAPIClient = mock(GoogleApiClient.class); mockLocationProvider = mock(FusedLocationProviderApi.class); }
FusedLocationProviderApi create();