@Test @SmallTest @SdkSuppress(minSdkVersion = 23) public void testOnBroadcastReceive_notifiesListeners_afterApi23() { mockContextReturns(null); mTracker.addListener(mListener); verify(mListener, never()).onConstraintChanged(anyBoolean()); mTracker.onBroadcastReceive(mMockContext, createChargingIntent_afterApi23(true)); verify(mListener).onConstraintChanged(true); mTracker.onBroadcastReceive(mMockContext, createChargingIntent_afterApi23(false)); verify(mListener).onConstraintChanged(false); }
@Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } }
BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } } }
BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } } BatteryChargingTracker(Context context); }
BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } } BatteryChargingTracker(Context context); @Override Boolean getInitialState(); @Override IntentFilter getIntentFilter(); @Override void onBroadcastReceive(Context context, @NonNull Intent intent); }
BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } } BatteryChargingTracker(Context context); @Override Boolean getInitialState(); @Override IntentFilter getIntentFilter(); @Override void onBroadcastReceive(Context context, @NonNull Intent intent);  }