IntentBindRecord peekBinding(Intent service) { synchronized (bindings) { for (IntentBindRecord bindRecord : bindings) { if (bindRecord.intent.filterEquals(service)) { return bindRecord; } } } return null; }
@Override @Implementation public boolean equals(Object o) { if (this == o) return true; if (o == null || realPendingIntent.getClass() != o.getClass()) return false; ShadowPendingIntent that = Shadow.extract((PendingIntent) o); String packageName = savedContext == null ? null : savedContext.getPackageName(); String thatPackageName = that.savedContext == null ? null : that.savedContext.getPackageName(); if (!Objects.equals(packageName, thatPackageName)) { return false; } if (this.savedIntents.length != that.savedIntents.length) { return false; } for (int i = 0; i < this.savedIntents.length; i++) { if (!this.savedIntents[i].filterEquals(that.savedIntents[i])) { return false; } } if (this.requestCode != that.requestCode) { return false; } return true; }
@Implementation protected void cancel(PendingIntent operation) { ShadowPendingIntent shadowPendingIntent = Shadow.extract(operation); final Intent toRemove = shadowPendingIntent.getSavedIntent(); final int requestCode = shadowPendingIntent.getRequestCode(); for (ScheduledAlarm scheduledAlarm : scheduledAlarms) { if (scheduledAlarm.operation != null) { ShadowPendingIntent scheduledShadowPendingIntent = Shadow.extract(scheduledAlarm.operation); final Intent scheduledIntent = scheduledShadowPendingIntent.getSavedIntent(); final int scheduledRequestCode = scheduledShadowPendingIntent.getRequestCode(); if (scheduledIntent.filterEquals(toRemove) && scheduledRequestCode == requestCode) { scheduledAlarms.remove(scheduledAlarm); break; } } } }
@Test public void send_shouldFillInIntentData() throws Exception { Intent intent = new Intent("action"); Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent fillIntent = new Intent().putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); Intent i = shadowOf(otherContext).getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i.filterEquals(intent)).isTrue(); // Ignore extras. assertThat(i.getIntExtra("TEST", -1)).isEqualTo(23); }
@Test public void send_shouldNotReusePreviouslyFilledInIntentData() throws Exception { Intent intent = new Intent("action"); Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent firstFillIntent = new Intent().putExtra("KEY1", 23).putExtra("KEY2", 24); pendingIntent.send(otherContext, 0, firstFillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); shadowActivity.clearNextStartedActivities(); Intent secondFillIntent = new Intent().putExtra("KEY1", 50); pendingIntent.send(otherContext, 0, secondFillIntent); Intent i = shadowActivity.getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i.filterEquals(intent)).isTrue(); // Ignore extras. assertThat(i.getIntExtra("KEY1", -1)).isEqualTo(50); assertThat(i.hasExtra("KEY2")).isFalse(); }
@Test public void shouldHaveStoppedServiceByStartedComponent() { ShadowApplication shadowApplication = shadowOf(context); Activity activity = Robolectric.setupActivity(Activity.class); ComponentName componentName = new ComponentName("package.test", "package.test.TestClass"); Intent startServiceIntent = new Intent().setComponent(componentName); ComponentName startedComponent = activity.startService(startServiceIntent); assertThat(startedComponent.getPackageName()).isEqualTo("package.test"); assertThat(startedComponent.getClassName()).isEqualTo("package.test.TestClass"); Intent stopServiceIntent = new Intent().setComponent(startedComponent); stopServiceIntent.putExtra("someExtra", "someValue"); boolean wasRunning = activity.stopService(stopServiceIntent); assertTrue(wasRunning); final Intent nextStoppedService = shadowApplication.getNextStoppedService(); assertThat(nextStoppedService.filterEquals(startServiceIntent)).isTrue(); assertThat(nextStoppedService.getStringExtra("someExtra")).isEqualTo("someValue"); }
@Test public void send_shouldNotUsePreviouslyFilledInLastIntentData() throws Exception { Intent[] intents = {new Intent("first"), new Intent("second")}; Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent firstFillIntent = new Intent(); firstFillIntent.putExtra("KEY1", 23); firstFillIntent.putExtra("KEY2", 24); pendingIntent.send(otherContext, 0, firstFillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); shadowActivity.clearNextStartedActivities(); Intent secondFillIntent = new Intent(); secondFillIntent.putExtra("KEY1", 50); pendingIntent.send(otherContext, 0, secondFillIntent); Intent second = shadowActivity.getNextStartedActivity(); assertThat(second).isNotNull(); assertThat(second.filterEquals(intents[1])).isTrue(); // Ignore extras. assertThat(second.getIntExtra("KEY1", -1)).isEqualTo(50); assertThat(second.hasExtra("KEY2")).isFalse(); }
@Test public void send_shouldFillInLastIntentData() throws Exception { Intent[] intents = {new Intent("first"), new Intent("second")}; Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent fillIntent = new Intent(); fillIntent.putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); Intent second = shadowActivity.getNextStartedActivity(); assertThat(second).isNotNull(); assertThat(second.filterEquals(intents[1])).isTrue(); // Ignore extras. assertThat(second.getIntExtra("TEST", -1)).isEqualTo(23); Intent first = shadowActivity.getNextStartedActivity(); assertThat(first).isNotNull(); assertThat(first).isSameAs(intents[0]); assertThat(first.hasExtra("TEST")).isFalse(); }
private static PendingIntent getCreatedIntentFor(Type type, Intent[] intents, int requestCode, int flags) { synchronized (lock) { for (PendingIntent createdIntent : createdIntents) { ShadowPendingIntent shadowPendingIntent = Shadow.extract(createdIntent); if (isOneShot(shadowPendingIntent.flags) != isOneShot(flags)) { continue; } if (isMutable(shadowPendingIntent.flags) != isMutable(flags)) { continue; } if (shadowPendingIntent.type != type) { continue; } if (shadowPendingIntent.requestCode != requestCode) { continue; } // The last Intent in the array acts as the "significant element" for matching as per // {@link #getActivities(Context, int, Intent[], int)}. Intent savedIntent = shadowPendingIntent.getSavedIntent(); Intent targetIntent = intents[intents.length - 1]; if (savedIntent == null ? targetIntent == null : savedIntent.filterEquals(targetIntent)) { return createdIntent; } } return null; } }
@Test public void testBroadcast() { sCondomProcessPackageManager.mCondom.mOutboundJudge = mBlockAllJudge; final Intent intent = new Intent(); mIntent = null; context().sendBroadcast(intent); assertOutboundJudgeCalled(0); assertNull(mIntent); mIntent = null; context().sendBroadcast(new Intent(intent.setPackage("a.b.c"))); assertOutboundJudgeCalled(1); assertNotNull(mIntent); assertTrue(mIntent.filterEquals(intent)); }
IntentBindRecord peekBinding(Intent service) { synchronized (bindings) { for (IntentBindRecord bindRecord : bindings) { if (bindRecord.intent.filterEquals(service)) { return bindRecord; } } } return null; }
IntentBindRecord peekBinding(Intent service) { synchronized (bindings) { for (IntentBindRecord bindRecord : bindings) { if (bindRecord.intent.filterEquals(service)) { return bindRecord; } } } return null; }
private void internalSet(int type, long triggerAtTime, long interval, PendingIntent operation) { Intent intent = Shadows.shadowOf(operation).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : scheduledAlarms) { Intent scheduledIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (scheduledIntent.filterEquals(intent)) { scheduledAlarms.remove(scheduledAlarm); break; } } scheduledAlarms.add(new ScheduledAlarm(type, triggerAtTime, interval, operation)); }
@Implementation public void cancel(PendingIntent pendingIntent) { final Intent intentTypeToRemove = Shadows.shadowOf(pendingIntent).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : new ArrayList<ScheduledAlarm>(scheduledAlarms)) { final Intent alarmIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (intentTypeToRemove.filterEquals(alarmIntent)) { scheduledAlarms.remove(scheduledAlarm); } } }
private void internalSet(int type, long triggerAtTime, long interval, PendingIntent operation) { Intent intent = Shadows.shadowOf(operation).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : scheduledAlarms) { Intent scheduledIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (scheduledIntent.filterEquals(intent)) { scheduledAlarms.remove(scheduledAlarm); break; } } scheduledAlarms.add(new ScheduledAlarm(type, triggerAtTime, interval, operation)); }
private void internalSet(int type, long triggerAtTime, long interval, PendingIntent operation) { Intent intent = shadowOf(operation).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : scheduledAlarms) { Intent scheduledIntent = shadowOf(scheduledAlarm.operation).getSavedIntent(); if (scheduledIntent.filterEquals(intent)) { scheduledAlarms.remove(scheduledAlarm); break; } } scheduledAlarms.add(new ScheduledAlarm(type, triggerAtTime, interval, operation)); }
private void internalSet(int type, long triggerAtTime, long interval, PendingIntent operation) { Intent intent = Shadows.shadowOf(operation).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : scheduledAlarms) { Intent scheduledIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (scheduledIntent.filterEquals(intent)) { scheduledAlarms.remove(scheduledAlarm); break; } } scheduledAlarms.add(new ScheduledAlarm(type, triggerAtTime, interval, operation)); }
@Implementation public void cancel(PendingIntent pendingIntent) { final Intent intentTypeToRemove = Shadows.shadowOf(pendingIntent).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : new ArrayList<ScheduledAlarm>(scheduledAlarms)) { final Intent alarmIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (intentTypeToRemove.filterEquals(alarmIntent)) { scheduledAlarms.remove(scheduledAlarm); } } }
@Implementation public void cancel(PendingIntent pendingIntent) { final Intent intentTypeToRemove = shadowOf(pendingIntent).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : new ArrayList<ScheduledAlarm>(scheduledAlarms)) { final Intent alarmIntent = shadowOf(scheduledAlarm.operation).getSavedIntent(); if (intentTypeToRemove.filterEquals(alarmIntent)) { scheduledAlarms.remove(scheduledAlarm); } } }
@Implementation public void cancel(PendingIntent pendingIntent) { final Intent intentTypeToRemove = Shadows.shadowOf(pendingIntent).getSavedIntent(); for (ScheduledAlarm scheduledAlarm : new ArrayList<ScheduledAlarm>(scheduledAlarms)) { final Intent alarmIntent = Shadows.shadowOf(scheduledAlarm.operation).getSavedIntent(); if (intentTypeToRemove.filterEquals(alarmIntent)) { scheduledAlarms.remove(scheduledAlarm); } } }