Skip to content

Commit

Permalink
add join and leave apis
Browse files Browse the repository at this point in the history
  • Loading branch information
race604 committed Feb 4, 2015
1 parent f5c810e commit 2107339
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.race604.client"
minSdkVersion 10
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
Expand Down
1 change: 1 addition & 0 deletions client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
80 changes: 75 additions & 5 deletions client/src/main/java/com/race604/client/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.race604.servicelib.IRemoteService;

import java.util.List;
import java.util.Random;


public class MainActivity extends ActionBarActivity implements View.OnClickListener {

private static final String TAG = MainActivity.class.getSimpleName();

private IRemoteService mService;
private boolean mIsBound = false;
private boolean mIsJoin = false;

private IBinder mToken = new Binder();
private Random mRand = new Random();

private Button mJoinBtn;

private ListView mList;

private ArrayAdapter<String> mAdapter;

private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
Expand All @@ -47,20 +61,35 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.bind).setOnClickListener(this);
findViewById(R.id.unbind).setOnClickListener(this);
findViewById(R.id.call).setOnClickListener(this);
findViewById(R.id.get_participators).setOnClickListener(this);

mList = (ListView) findViewById(R.id.list);
mJoinBtn = (Button) findViewById(R.id.join);
mJoinBtn.setOnClickListener(this);

mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
mList.setAdapter(mAdapter);
}

private void callRemote() {

if (mService != null) {
if (isServiceReady()) {
try {
int result = mService.someOperate(1, 2);
Toast.makeText(this, "Remote call return: " + result, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(this, "Remote call error!", Toast.LENGTH_SHORT).show();
}
}
}

private boolean isServiceReady() {
if (mService != null) {
return true;
} else {
Toast.makeText(this, "Service is not available yet!", Toast.LENGTH_SHORT).show();
return false;
}
}

Expand Down Expand Up @@ -91,6 +120,47 @@ public void onClick(View v) {
case R.id.call:
callRemote();
break;
case R.id.join:
toggleJoin();
break;
case R.id.get_participators:
updateParticipators();
break;
}
}

private void updateParticipators() {
if (!isServiceReady()) {
return;
}

try {
List<String> participators = mService.getParticipators();
mAdapter.clear();
mAdapter.addAll(participators);
} catch (RemoteException e) {
e.printStackTrace();
}
}

private void toggleJoin() {
if (!isServiceReady()) {
return;
}

try {
if (!mIsJoin) {
String name = "Client:" + mRand.nextInt(10);
mService.join(mToken, name);
mJoinBtn.setText(R.string.leave);
mIsJoin = true;
} else {
mService.leave(mToken);
mJoinBtn.setText(R.string.join);
mIsJoin = false;
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
21 changes: 17 additions & 4 deletions client/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@
android:layout_height="wrap_content"
android:text="@string/call_remote"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/join"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/join"/>

<Button
android:id="@+id/get_participators"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/get_participators"/>

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
3 changes: 3 additions & 0 deletions client/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<string name="bind">Bind</string>
<string name="unbind">Unbind</string>
<string name="call_remote">Call Remote</string>
<string name="get_participators">Get Participators</string>
<string name="join">Join</string>
<string name="leave">Leave</string>
</resources>
73 changes: 73 additions & 0 deletions service/src/main/java/com/race604/remoteservice/RemoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,98 @@

import com.race604.servicelib.IRemoteService;

import java.util.ArrayList;
import java.util.List;

public class RemoteService extends Service {

private static final String TAG = RemoteService.class.getSimpleName();

private List<Client> mClients = new ArrayList<>();

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
@Override
public int someOperate(int a, int b) throws RemoteException {
Log.d(TAG, "called RemoteService someOperate()");
return a + b;
}

@Override
public void join(IBinder token, String name) throws RemoteException {
int idx = findClient(token);
if (idx >= 0) {
Log.d(TAG, "already joined");
return;
}

Client client = new Client(token, name);

// 注册客户端死掉的通知
token.linkToDeath(client, 0);
mClients.add(client);
}

@Override
public void leave(IBinder token) throws RemoteException {
int idx = findClient(token);
if (idx < 0) {
Log.d(TAG, "already left");
return;
}

Client client = mClients.get(idx);
mClients.remove(client);

// 取消注册
client.mToken.unlinkToDeath(client, 0);
}

@Override
public List<String> getParticipators() throws RemoteException {
ArrayList<String> names = new ArrayList<>();
for (Client client : mClients) {
names.add(client.mName);
}
return names;
}
};

public RemoteService() {
}

private int findClient(IBinder token) {
for (int i = 0; i < mClients.size(); i++) {
if (mClients.get(i).mToken == token) {
return i;
}
}
return -1;
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

private final class Client implements IBinder.DeathRecipient {
public final IBinder mToken;
public final String mName;

public Client(IBinder token, String name) {
mToken = token;
mName = name;
}

@Override
public void binderDied() {
// 客户端死掉,执行此回调
int index = mClients.indexOf(this);
if (index < 0) {
return;
}

Log.d(TAG, "client died: " + mName);
mClients.remove(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ package com.race604.servicelib;

interface IRemoteService {
int someOperate(int a, int b);

void join(IBinder token, String name);
void leave(IBinder token);
List<String> getParticipators();
}

0 comments on commit 2107339

Please sign in to comment.