Skip to content

Commit

Permalink
Switch to EventBus 3.0.0.
Browse files Browse the repository at this point in the history
Use new @subscribe annotations. Have to add explicit priorities on
subscriptions, instead of doing it when regstering on the bus.

In addition, it's not enough to post a sticky event, subscribers to
sticky events also have to annotate that they want to receive the
sticky version.
  • Loading branch information
nikclayton committed May 25, 2016
1 parent 484ea34 commit 15e08b0
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 43 deletions.
11 changes: 9 additions & 2 deletions Squeezer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.github.triplet.gradle:play-publisher:1.1.0'
classpath 'io.fabric.tools:gradle:1.+'
}
Expand Down Expand Up @@ -39,7 +39,8 @@ dependencies {
compile 'com.google.code.findbugs:jsr305:2.0.2'

// EventBus, https://github.com/greenrobot/EventBus.
compile 'de.greenrobot:eventbus:2.4.1'
compile 'org.greenrobot:eventbus:3.0.0'
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'

// Changelogs, see https://github.com/cketti/ckChangeLog.
compile 'de.cketti.library.changelog:ckchangelog:1.2.0'
Expand All @@ -61,6 +62,12 @@ dependencies {
testCompile 'junit:junit:4.12'
}

apt {
arguments {
eventBusIndex "uk.org.ngo.squeezer.SqueezerEventBusIndex"
}
}

android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
Expand Down
9 changes: 8 additions & 1 deletion Squeezer/proguard-eventbus.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-keepattributes *Annotation*
-keepclassmembers class ** {
public void onEvent*(**);
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Intent;
import android.test.ServiceTestCase;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -201,6 +203,7 @@ public void testAuthenticationFailure() throws InterruptedException {
ConnectionState.LOGIN_FAILED), mActualConnectionStates);
}

@Subscribe(sticky = true)
public void onEvent(ConnectionChanged event) {
mActualConnectionStates.add(event.connectionState);

Expand All @@ -217,6 +220,7 @@ public void onEvent(ConnectionChanged event) {
}
}

@Subscribe(sticky = true)
public void onEvent(HandshakeComplete event) {
mLastHandshakeCompleteEvent = event;
synchronized (mLockHandshakeComplete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

Expand Down Expand Up @@ -161,6 +164,7 @@ public void onUserInitiatesConnect(View view) {
fragment.startVisibleConnection();
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
// The user requested a connection to the server, which succeeded. There's
// no prior activity to go to, so launch HomeActivity, with flags to
Expand Down
5 changes: 5 additions & 0 deletions Squeezer/src/main/java/uk/org/ngo/squeezer/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.MainThread;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -34,6 +35,8 @@
import com.crashlytics.android.Crashlytics;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -132,6 +135,8 @@ public void onCreate(Bundle savedInstanceState) {
}
}

@MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
int[] icons = new int[]{
R.drawable.ic_artists,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -568,7 +571,7 @@ public void onResume() {
*/
private void maybeRegisterCallbacks(@NonNull ISqueezeService service) {
if (!mRegisteredCallbacks) {
service.getEventBus().registerSticky(this);
service.getEventBus().register(this);

mRegisteredCallbacks = true;
}
Expand Down Expand Up @@ -973,6 +976,7 @@ public void startVisibleConnection() {
}

@MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(ConnectionChanged event) {
Log.d(TAG, "ConnectionChanged: " + event);

Expand Down Expand Up @@ -1035,6 +1039,7 @@ public void onEventMainThread(ConnectionChanged event) {
}

@MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
// Event might arrive before this fragment has connected to the service (e.g.,
// the activity connected before this fragment did).
Expand Down Expand Up @@ -1072,31 +1077,36 @@ public void onEventMainThread(HandshakeComplete event) {
}

@MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(MusicChanged event) {
if (event.player.equals(mService.getActivePlayer())) {
updateSongInfo(event.playerState);
}
}

@MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlayersChanged event) {
updatePlayerDropDown(event.players.values(), mService.getActivePlayer());
updateUiFromPlayerState(mService.getActivePlayerState());
}

@MainThread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlayStatusChanged event) {
updatePlayPauseIcon(event.playStatus);
}

@MainThread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PowerStatusChanged event) {
if (event.player.equals(mService.getActivePlayer())) {
updatePowerMenuItems(event.canPowerOn, event.canPowerOff);
}
}

@MainThread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(RepeatStatusChanged event) {
if (event.player.equals(mService.getActivePlayer())) {
updateRepeatStatus(event.repeatStatus);
Expand All @@ -1108,6 +1118,7 @@ public void onEventMainThread(RepeatStatusChanged event) {
}

@MainThread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ShuffleStatusChanged event) {
if (event.player.equals(mService.getActivePlayer())) {
updateShuffleStatus(event.shuffleStatus);
Expand All @@ -1120,6 +1131,7 @@ public void onEventMainThread(ShuffleStatusChanged event) {
}

@MainThread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(SongTimeChanged event) {
if (event.player.equals(mService.getActivePlayer())) {
updateTimeDisplayTo(event.currentPosition, event.duration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import com.google.common.collect.ImmutableList;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
Expand Down Expand Up @@ -60,6 +63,7 @@ public void onCreate(Bundle savedInstanceState) {
listView = (ListView) findViewById(R.id.item_list);
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
setRandomPlayList(getService());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import android.widget.AbsListView;
import android.widget.ExpandableListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -92,6 +95,7 @@ protected void onServiceConnected(@NonNull ISqueezeService service) {
* the server. Only do this after the handshake has completed. When done, perform the
* search.
*/
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
resultsExpandableListView.setAdapter(searchResultsAdapter);
doSearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import android.view.MenuItem;
import android.widget.Toast;

import org.greenrobot.eventbus.Subscribe;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

Expand Down Expand Up @@ -269,7 +271,7 @@ protected void onServiceConnected(@NonNull ISqueezeService service) {
*/
private void maybeRegisterOnEventBus(@NonNull ISqueezeService service) {
if (!mRegisteredOnEventBus) {
service.getEventBus().registerSticky(this);
service.getEventBus().register(this);
mRegisteredOnEventBus = true;
}
}
Expand Down Expand Up @@ -386,6 +388,7 @@ private boolean changeVolumeBy(int delta) {
return true;
}

@Subscribe
public void onEvent(PlayerVolume event) {
if (!mIgnoreVolumeChange && mVolumePanel != null && event.player == mService.getActivePlayer()) {
mVolumePanel.postVolumeChanged(event.volume, event.player.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import android.widget.ListView;
import android.widget.ProgressBar;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -123,6 +126,7 @@ public void onMovedToScrapHeap(View view) {
mListView.setOnCreateContextMenuListener(getItemAdapter());
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(HandshakeComplete event) {
maybeOrderVisiblePages(mListView);
setAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
Expand Down Expand Up @@ -157,6 +159,7 @@ public boolean maybeOrderPage(int pagePosition) {
/**
* Orders any pages requested before the handshake completed.
*/
@Subscribe(sticky = true)
public void onEvent(HandshakeComplete event) {
// Order any pages that were requested before the handshake complete.
while (!mOrderedPagesBeforeHandshake.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import com.android.datetimepicker.time.TimePickerDialog;
import com.google.common.collect.ImmutableList;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
Expand Down Expand Up @@ -204,6 +207,7 @@ public Object getClient() {
}
};

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlayerPrefReceived event) {
if (!event.player.equals(getService().getActivePlayer())) {
return;
Expand All @@ -230,6 +234,7 @@ public void onEventMainThread(PlayerPrefReceived event) {
}
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlayersChanged event) {
// Only include players that are connected to the server.
ArrayList<Player> connectedPlayers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import android.view.ViewGroup;
import android.widget.ListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -258,11 +261,13 @@ private String getCurrentPlaylist() {
}

@Override
@Subscribe(sticky = true)
public void onEvent(HandshakeComplete event) {
super.onEvent(event);
player = getService().getActivePlayer();
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(MusicChanged event) {
if (event.player.equals(getService().getActivePlayer())) {
Log.d(getTag(), "onMusicChanged " + event.playerState.getCurrentSong());
Expand All @@ -271,6 +276,7 @@ public void onEventMainThread(MusicChanged event) {
}
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlayersChanged event) {
supportInvalidateOptionsMenu();

Expand All @@ -288,11 +294,13 @@ public void onEventMainThread(PlayersChanged event) {
}
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlaylistTracksAdded event) {
clearAndReOrderItems();
getItemAdapter().notifyDataSetChanged();
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlaylistTracksDeleted event) {
// TODO: Investigate feasibility of deleting single items from the adapter.
clearAndReOrderItems();
Expand Down
Loading

0 comments on commit 15e08b0

Please sign in to comment.