summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej J.R. Hunt <andrzej@ahunt.org>2012-08-13 14:31:06 +0200
committerAndrzej J.R. Hunt <andrzej@ahunt.org>2012-08-13 21:41:34 +0200
commit61f92ee500c7757e65a3f1944af973c4914c717e (patch)
treedb11fc14bd4013e10c6a4ad25cb0537969a1c0f6
parentc8b7aa55138848fe07108779099267424bc03e3d (diff)
CommunicationService completely threaded. Preparations for pairing.
Change-Id: If5a984230e776b380b8e536655cef061d2298e15
-rw-r--r--android/sdremote/AndroidManifest.xml3
-rw-r--r--android/sdremote/res/layout/activity_pairing.xml36
-rw-r--r--android/sdremote/res/layout/activity_selector_sublayout_server.xml5
-rw-r--r--android/sdremote/res/values/strings.xml3
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java66
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java317
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/TestClient.java316
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java304
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/communication/Server.java43
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java6
10 files changed, 647 insertions, 452 deletions
diff --git a/android/sdremote/AndroidManifest.xml b/android/sdremote/AndroidManifest.xml
index cea2e1482e1f..4364b24aad5a 100644
--- a/android/sdremote/AndroidManifest.xml
+++ b/android/sdremote/AndroidManifest.xml
@@ -16,6 +16,7 @@
<activity android:name=".SelectorActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
+
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
@@ -23,6 +24,8 @@
<service android:name=".communication.CommunicationService" >
</service>
+ <activity android:name=".PairingActivity" >
+ </activity>
<activity
android:name=".PresentationActivity"
android:label="@string/title_activity_presentation" >
diff --git a/android/sdremote/res/layout/activity_pairing.xml b/android/sdremote/res/layout/activity_pairing.xml
new file mode 100644
index 000000000000..122dec613081
--- /dev/null
+++ b/android/sdremote/res/layout/activity_pairing.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/textView1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:text="@string/pairing_instructions_1" />
+
+ <TextView
+ android:id="@+id/textView2"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:text="@string/pairing_instructions_2" />
+
+ <TextView
+ android:id="@+id/textView3"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:text="@string/pairing_instructions_3" />
+
+ <TextView
+ android:id="@+id/pairing_pin"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:text=""
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+</LinearLayout> \ No newline at end of file
diff --git a/android/sdremote/res/layout/activity_selector_sublayout_server.xml b/android/sdremote/res/layout/activity_selector_sublayout_server.xml
index 14bec2349d8e..58a037423d84 100644
--- a/android/sdremote/res/layout/activity_selector_sublayout_server.xml
+++ b/android/sdremote/res/layout/activity_selector_sublayout_server.xml
@@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:orientation="vertical" >
+ android:orientation="vertical" android:clickable="true" >
<ImageView
android:id="@+id/sub_divider"
@@ -15,6 +15,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
- android:textAppearance="?android:attr/textAppearanceMedium" />
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ />
</LinearLayout> \ No newline at end of file
diff --git a/android/sdremote/res/values/strings.xml b/android/sdremote/res/values/strings.xml
index 0e14caa5a519..523f53865faa 100644
--- a/android/sdremote/res/values/strings.xml
+++ b/android/sdremote/res/values/strings.xml
@@ -23,5 +23,8 @@
<string name="bluetooth">Bluetooth</string>
<string name="wifi">WI-FI</string>
<string name="selector_noservers">Searching for computers…</string>
+ <string name="pairing_instructions_1">In Impress, click on the "Slideshow" menu and select "Impress Remote".</string>
+ <string name="pairing_instructions_2">Choose "{1}" as your device.</string>
+ <string name="pairing_instructions_3">Then input this PIN:</string>
</resources> \ No newline at end of file
diff --git a/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java b/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java
new file mode 100644
index 000000000000..386098ca8af4
--- /dev/null
+++ b/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java
@@ -0,0 +1,66 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+package org.libreoffice.impressremote;
+
+import org.libreoffice.impressremote.communication.CommunicationService;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+import android.widget.TextView;
+
+public class PairingActivity extends Activity {
+
+ private TextView mPinText;
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_pairing);
+
+ mPinText = (TextView) findViewById(R.id.pairing_pin);
+
+ IntentFilter aFilter = new IntentFilter(
+ CommunicationService.MSG_PAIRING_STARTED);
+ aFilter.addAction(CommunicationService.MSG_PAIRING_SUCCESSFUL);
+ LocalBroadcastManager.getInstance(this).registerReceiver(mListener,
+ aFilter);
+
+ // mBluetoothContainer = findViewById(R.id.selector_container_bluetooth);
+ // mBluetoothList = (LinearLayout) findViewById(R.id.selector_list_bluetooth);
+ // mNetworkContainer = findViewById(R.id.selector_container_network);
+ // mNetworkList = (LinearLayout) findViewById(R.id.selector_list_network);
+ // mNoServerLabel = (TextView) findViewById(R.id.selector_label_none);
+ //
+ // refreshLists();
+ }
+
+ private BroadcastReceiver mListener = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(Context aContext, Intent aIntent) {
+ if (aIntent.getAction().equals(
+ CommunicationService.MSG_PAIRING_STARTED)) {
+ String aPin = aIntent.getStringExtra("PIN");
+ mPinText.setText(aPin);
+ // refreshLists();
+ } else if (aIntent.getAction().equals(
+ CommunicationService.MSG_PAIRING_SUCCESSFUL)) {
+ mPinText.setText("Paired!");
+ }
+
+ }
+ };
+
+}
diff --git a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
index 201f41c4e846..a51df87b88be 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
@@ -13,8 +13,8 @@ import java.util.HashMap;
import java.util.Map.Entry;
import org.libreoffice.impressremote.communication.CommunicationService;
-import org.libreoffice.impressremote.communication.CommunicationService.Protocol;
-import org.libreoffice.impressremote.communication.CommunicationService.Server;
+import org.libreoffice.impressremote.communication.Server;
+import org.libreoffice.impressremote.communication.Server.Protocol;
import android.app.Activity;
import android.content.BroadcastReceiver;
@@ -27,145 +27,188 @@ import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
+import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SelectorActivity extends Activity {
- private CommunicationService mCommunicationService;
-
- private View mBluetoothContainer;
- private LinearLayout mBluetoothList;
- private View mNetworkContainer;
- private LinearLayout mNetworkList;
- private TextView mNoServerLabel;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_selector);
-
- IntentFilter aFilter = new IntentFilter(
- CommunicationService.MSG_SERVERLIST_CHANGED);
- LocalBroadcastManager.getInstance(this).registerReceiver(mListener,
- aFilter);
-
- mBluetoothContainer = findViewById(R.id.selector_container_bluetooth);
- mBluetoothList = (LinearLayout) findViewById(R.id.selector_list_bluetooth);
- mNetworkContainer = findViewById(R.id.selector_container_network);
- mNetworkList = (LinearLayout) findViewById(R.id.selector_list_network);
- mNoServerLabel = (TextView) findViewById(R.id.selector_label_none);
-
- refreshLists();
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- doBindService();
- }
-
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- if (mCommunicationService != null) {
- mCommunicationService.stopFindingServers();
- }
- doUnbindService();
- }
-
- void doBindService() {
- Intent aIntent = new Intent(this, CommunicationService.class);
- startService(aIntent);
- bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
- }
-
- void doUnbindService() {
- unbindService(mConnection);
- }
-
- private ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName aClassName,
- IBinder aService) {
- mCommunicationService = ((CommunicationService.CBinder) aService)
- .getService();
- mCommunicationService.startFindingServers();
- }
-
- @Override
- public void onServiceDisconnected(ComponentName aClassName) {
- mCommunicationService = null;
- }
- };
-
- private BroadcastReceiver mListener = new BroadcastReceiver() {
-
- @Override
- public void onReceive(Context aContext, Intent aIntent) {
- if (aIntent.getAction().equals(
- CommunicationService.MSG_SERVERLIST_CHANGED)) {
- refreshLists();
- }
-
- }
- };
-
- private HashMap<Server, View> mBluetoothServers = new HashMap<Server, View>();
- private HashMap<Server, View> mNetworkServers = new HashMap<Server, View>();
-
- private void refreshLists() {
- if (mCommunicationService != null) {
-
- Server[] aServers = mCommunicationService.getServers();
-
- // Bluetooth -- Remove old
- for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) {
- if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
- mBluetoothServers.remove(aEntry.getKey());
- mBluetoothList.removeView(aEntry.getValue());
- }
- }
- // Network -- Remove old
- for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
- if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
- mNetworkServers.remove(aEntry.getKey());
- mNetworkList.removeView(aEntry.getValue());
- }
- }
- // Add all new
- for (Server aServer : aServers) {
- boolean aIsBluetooth = (aServer.getProtocol() == Protocol.BLUETOOTH);
- HashMap<Server, View> aMap = aIsBluetooth ? mBluetoothServers
- : mNetworkServers;
- LinearLayout aLayout = aIsBluetooth ? mBluetoothList
- : mNetworkList;
-
- if (!aMap.containsValue(aServer)) {
- View aView = getLayoutInflater()
- .inflate(R.layout.activity_selector_sublayout_server,
- aLayout);
- TextView aText = (TextView) aView
- .findViewById(R.id.selector_sub_label);
- aText.setText(aServer.getName());
- aMap.put(aServer, aView);
- }
-
- }
- }
- // Hide as necessary
-
- mBluetoothContainer
- .setVisibility((mBluetoothServers.size() != 0) ? View.VISIBLE
- : View.GONE);
- mNetworkContainer
- .setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE
- : View.GONE);
-
- mNoServerLabel.setVisibility(((mBluetoothServers.size() == 0) && (mNetworkServers
- .size() == 0)) ? View.VISIBLE : View.GONE);
- }
+ private CommunicationService mCommunicationService;
+
+ private View mBluetoothContainer;
+ private LinearLayout mBluetoothList;
+ private View mNetworkContainer;
+ private LinearLayout mNetworkList;
+ private TextView mNoServerLabel;
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_selector);
+
+ IntentFilter aFilter = new IntentFilter(
+ CommunicationService.MSG_SERVERLIST_CHANGED);
+ LocalBroadcastManager.getInstance(this).registerReceiver(mListener,
+ aFilter);
+
+ mBluetoothContainer = findViewById(R.id.selector_container_bluetooth);
+ mBluetoothList = (LinearLayout) findViewById(R.id.selector_list_bluetooth);
+ mNetworkContainer = findViewById(R.id.selector_container_network);
+ mNetworkList = (LinearLayout) findViewById(R.id.selector_list_network);
+ mNoServerLabel = (TextView) findViewById(R.id.selector_label_none);
+
+ refreshLists();
+ }
+
+ @Override
+ public void onBackPressed() {
+ mCommunicationService.stopFindingServers();
+ Intent aIntent = new Intent(this, CommunicationService.class);
+ stopService(aIntent);
+ super.onBackPressed();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ doBindService();
+ }
+
+ @Override
+ protected void onPause() {
+ // TODO Auto-generated method stub
+ super.onPause();
+ if (mCommunicationService != null) {
+ mCommunicationService.stopFindingServers();
+ }
+ doUnbindService();
+ }
+
+ void doBindService() {
+ Intent aIntent = new Intent(this, CommunicationService.class);
+ startService(aIntent);
+ bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
+ }
+
+ void doUnbindService() {
+ unbindService(mConnection);
+ }
+
+ private ServiceConnection mConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName aClassName,
+ IBinder aService) {
+ mCommunicationService = ((CommunicationService.CBinder) aService)
+ .getService();
+ mCommunicationService.startFindingServers();
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName aClassName) {
+ mCommunicationService = null;
+ }
+ };
+
+ private BroadcastReceiver mListener = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(Context aContext, Intent aIntent) {
+ if (aIntent.getAction().equals(
+ CommunicationService.MSG_SERVERLIST_CHANGED)) {
+ refreshLists();
+ }
+
+ }
+ };
+
+ private HashMap<Server, View> mBluetoothServers = new HashMap<Server, View>();
+ private HashMap<Server, View> mNetworkServers = new HashMap<Server, View>();
+
+ private void refreshLists() {
+ if (mCommunicationService != null) {
+
+ Server[] aServers = mCommunicationService.getServers();
+
+ // Bluetooth -- Remove old
+ for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) {
+ if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
+ mBluetoothServers.remove(aEntry.getKey());
+ mBluetoothList.removeView(aEntry.getValue());
+ }
+ }
+ // Network -- Remove old
+ for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
+ if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
+ mNetworkServers.remove(aEntry.getKey());
+ mNetworkList.removeView(aEntry.getValue());
+ }
+ }
+ // Add all new
+ for (Server aServer : aServers) {
+ boolean aIsBluetooth = (aServer.getProtocol() == Protocol.BLUETOOTH);
+ HashMap<Server, View> aMap = aIsBluetooth ? mBluetoothServers
+ : mNetworkServers;
+ LinearLayout aLayout = aIsBluetooth ? mBluetoothList
+ : mNetworkList;
+
+ if (!aMap.containsValue(aServer)) {
+ View aView = getLayoutInflater()
+ .inflate(R.layout.activity_selector_sublayout_server,
+ aLayout);
+ TextView aText = (TextView) aView
+ .findViewById(R.id.selector_sub_label);
+ aView.setOnClickListener(mClickListener);
+ aText.setText(aServer.getName());
+ aMap.put(aServer, aView);
+ }
+
+ }
+ }
+ // Hide as necessary
+
+ mBluetoothContainer
+ .setVisibility((mBluetoothServers.size() != 0) ? View.VISIBLE
+ : View.GONE);
+ mNetworkContainer
+ .setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE
+ : View.GONE);
+
+ mNoServerLabel.setVisibility(((mBluetoothServers.size() == 0) && (mNetworkServers
+ .size() == 0)) ? View.VISIBLE : View.GONE);
+ }
+
+ private OnClickListener mClickListener = new OnClickListener() {
+
+ @Override
+ public void onClick(View aView) {
+ // TODO Auto-generated method stub
+ mCommunicationService.stopFindingServers();
+
+ Server aDesiredServer = null;
+
+ if (mBluetoothServers.containsValue(aView)) {
+ for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) {
+ if (aEntry.getValue() == aView) {
+ aDesiredServer = aEntry.getKey();
+ }
+ }
+ } else if (mNetworkServers.containsValue(aView)) {
+ for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
+ if (aEntry.getValue() == aView) {
+ aDesiredServer = aEntry.getKey();
+ }
+ }
+ }
+ if (aDesiredServer != null) {
+ mCommunicationService.connectTo(aDesiredServer);
+ Intent aIntent = new Intent(SelectorActivity.this,
+ PairingActivity.class);
+ startActivity(aIntent);
+ }
+
+ }
+ };
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ \ No newline at end of file
diff --git a/android/sdremote/src/org/libreoffice/impressremote/TestClient.java b/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
index dc74cd527f65..2bc6e907077f 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
@@ -29,168 +29,166 @@ import android.widget.TextView;
public class TestClient extends Activity {
- private boolean mCurrentPreviewImageMissing = false;
-
- private boolean mIsBound = false;
-
- private CommunicationService mCommunicationService;
-
- final Messenger mMessenger = new Messenger(new MessageHandler());
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.testlayout);
- setupUI();
-
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- doBindService();
+ private boolean mCurrentPreviewImageMissing = false;
+
+ private boolean mIsBound = false;
+
+ private CommunicationService mCommunicationService;
+
+ final Messenger mMessenger = new Messenger(new MessageHandler());
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.testlayout);
+ setupUI();
- }
+ }
- // FIXME: move all necessary code to CommunicationService.onUnbind
+ @Override
+ protected void onResume() {
+ super.onResume();
+ doBindService();
- @Override
- protected void onPause() {
- super.onPause();
- }
+ }
- @Override
- public void onBackPressed() {
- // TODO Auto-generated method stub
- mCommunicationService.disconnect();
- stopService(new Intent(this, CommunicationService.class));
- doUnbindService();
- finish();
- super.onBackPressed();
- }
-
- @Override
- protected void onStop() {
- // TODO Auto-generated method stub
- super.onStop();
- // mCommunicationService.disconnect();
- // stopService(new Intent(this, CommunicationService.class));
- }
-
- private ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName aClassName,
- IBinder aService) {
- mCommunicationService = ((CommunicationService.CBinder) aService)
- .getService();
- mCommunicationService.connectTo(
- CommunicationService.Protocol.NETWORK,
- "192.168.0.18");
- mCommunicationService.setActivityMessenger(mMessenger);
- enableButtons(true);
- }
-
- @Override
- public void onServiceDisconnected(ComponentName aClassName) {
- mCommunicationService = null;
- enableButtons(false);
- }
- };
-
- void doBindService() {
- Intent aIntent = new Intent(this, CommunicationService.class);
- startService(aIntent);
- bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
- mIsBound = true;
- }
-
- void doUnbindService() {
- mCommunicationService.setActivityMessenger(null);
- if (mIsBound) {
- unbindService(mConnection);
- mIsBound = false;
- }
- }
-
- private Button mButtonNext;
-
- private Button mButtonPrevious;
-
- private ImageView mImageView;
-
- private TextView mSlideLabel;
-
- private void setupUI() {
- mButtonNext = (Button) findViewById(R.id.button_next);
- mButtonPrevious = (Button) findViewById(R.id.button_previous);
- mImageView = (ImageView) findViewById(R.id.image_preview);
- mSlideLabel = (TextView) findViewById(R.id.label_curSlide);
-
- enableButtons(false);
-
- mButtonNext.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- mCommunicationService.getTransmitter().nextTransition();
-
- }
-
- });
-
- mButtonPrevious.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- mCommunicationService.getTransmitter().previousTransition();
-
- }
-
- });
-
- Button mThumbnailButton = (Button) findViewById(R.id.button_thumbnail);
-
- mThumbnailButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent aIntent = new Intent(TestClient.this,
- PresentationActivity.class);
- startActivity(aIntent);
- }
- });
-
- }
-
- private void enableButtons(boolean aEnabled) {
- mButtonNext.setEnabled(aEnabled);
- mButtonPrevious.setEnabled(aEnabled);
- }
-
- class MessageHandler extends Handler {
- @Override
- public void handleMessage(Message aMessage) {
- Bundle aData = aMessage.getData();
- switch (aMessage.what) {
- case CommunicationService.MSG_SLIDE_CHANGED:
- int newSlide = aData.getInt("slide_number");
- mSlideLabel.setText("Slide " + newSlide);
- mCurrentPreviewImageMissing = true;
- // We continue on to try and update the image.
- case CommunicationService.MSG_SLIDE_PREVIEW:
- int aSlideNumber = aData.getInt("slide_number");
- if (mCurrentPreviewImageMissing) {
- Bitmap aImage = mCommunicationService.getSlideShow()
- .getImage(aSlideNumber);
- if (aImage != null) {
- mImageView.setImageBitmap(aImage);
- mCurrentPreviewImageMissing = false;
- }
- }
- break;
-
- }
- }
- }
+ // FIXME: move all necessary code to CommunicationService.onUnbind
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ }
+
+ @Override
+ public void onBackPressed() {
+ // TODO Auto-generated method stub
+ mCommunicationService.disconnect();
+ stopService(new Intent(this, CommunicationService.class));
+ doUnbindService();
+ finish();
+ super.onBackPressed();
+ }
+
+ @Override
+ protected void onStop() {
+ // TODO Auto-generated method stub
+ super.onStop();
+ // mCommunicationService.disconnect();
+ // stopService(new Intent(this, CommunicationService.class));
+ }
+
+ private ServiceConnection mConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName aClassName,
+ IBinder aService) {
+ mCommunicationService = ((CommunicationService.CBinder) aService)
+ .getService();
+ // mCommunicationService.connectTo(Protocol.NETWORK, "192.168.0.18");
+ mCommunicationService.setActivityMessenger(mMessenger);
+ enableButtons(true);
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName aClassName) {
+ mCommunicationService = null;
+ enableButtons(false);
+ }
+ };
+
+ void doBindService() {
+ Intent aIntent = new Intent(this, CommunicationService.class);
+ startService(aIntent);
+ bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
+ mIsBound = true;
+ }
+
+ void doUnbindService() {
+ mCommunicationService.setActivityMessenger(null);
+ if (mIsBound) {
+ unbindService(mConnection);
+ mIsBound = false;
+ }
+ }
+
+ private Button mButtonNext;
+
+ private Button mButtonPrevious;
+
+ private ImageView mImageView;
+
+ private TextView mSlideLabel;
+
+ private void setupUI() {
+ mButtonNext = (Button) findViewById(R.id.button_next);
+ mButtonPrevious = (Button) findViewById(R.id.button_previous);
+ mImageView = (ImageView) findViewById(R.id.image_preview);
+ mSlideLabel = (TextView) findViewById(R.id.label_curSlide);
+
+ enableButtons(false);
+
+ mButtonNext.setOnClickListener(new OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ mCommunicationService.getTransmitter().nextTransition();
+
+ }
+
+ });
+
+ mButtonPrevious.setOnClickListener(new OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ mCommunicationService.getTransmitter().previousTransition();
+
+ }
+
+ });
+
+ Button mThumbnailButton = (Button) findViewById(R.id.button_thumbnail);
+
+ mThumbnailButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent aIntent = new Intent(TestClient.this,
+ PresentationActivity.class);
+ startActivity(aIntent);
+ }
+ });
+
+ }
+
+ private void enableButtons(boolean aEnabled) {
+ mButtonNext.setEnabled(aEnabled);
+ mButtonPrevious.setEnabled(aEnabled);
+ }
+
+ class MessageHandler extends Handler {
+ @Override
+ public void handleMessage(Message aMessage) {
+ Bundle aData = aMessage.getData();
+ switch (aMessage.what) {
+ case CommunicationService.MSG_SLIDE_CHANGED:
+ int newSlide = aData.getInt("slide_number");
+ mSlideLabel.setText("Slide " + newSlide);
+ mCurrentPreviewImageMissing = true;
+ // We continue on to try and update the image.
+ case CommunicationService.MSG_SLIDE_PREVIEW:
+ int aSlideNumber = aData.getInt("slide_number");
+ if (mCurrentPreviewImageMissing) {
+ Bitmap aImage = mCommunicationService.getSlideShow()
+ .getImage(aSlideNumber);
+ if (aImage != null) {
+ mImageView.setImageBitmap(aImage);
+ mCurrentPreviewImageMissing = false;
+ }
+ }
+ break;
+
+ }
+ }
+ }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ \ No newline at end of file
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
index 879c962bb3b2..c8ecace40b03 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
@@ -14,156 +14,160 @@ import android.os.Binder;
import android.os.IBinder;
import android.os.Messenger;
-public class CommunicationService extends Service {
-
- /**
- * Return the service to clients.
- */
- public class CBinder extends Binder {
- public CommunicationService getService() {
- return CommunicationService.this;
- }
- }
-
- private final IBinder mBinder = new CBinder();
-
- public enum Protocol {
- NETWORK, BLUETOOTH
- };
-
- public static final int MSG_SLIDESHOW_STARTED = 1;
- public static final int MSG_SLIDE_CHANGED = 2;
- public static final int MSG_SLIDE_PREVIEW = 3;
- public static final int MSG_SLIDE_NOTES = 4;
-
- public static final String MSG_SERVERLIST_CHANGED = "SERVERLIST_CHANGED";
-
- private Transmitter mTransmitter;
-
- private Client mClient;
-
- private Receiver mReceiver = new Receiver();
-
- private ServerFinder mFinder = new ServerFinder(this);
-
- public void setActivityMessenger(Messenger aActivityMessenger) {
- mReceiver.setActivityMessenger(aActivityMessenger);
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return mBinder;
- }
-
- @Override
- public void onCreate() {
- // TODO Create a notification (if configured).
- }
-
- @Override
- public void onDestroy() {
- // TODO Destroy the notification (as necessary).
- }
-
- public Transmitter getTransmitter() {
- return mTransmitter;
- }
-
- public Server[] getServers() {
- return mFinder.getServerList();
- }
-
- public void startFindingServers() {
- mFinder.startFinding();
- }
-
- public void stopFindingServers() {
- mFinder.stopFinding();
- }
-
- /**
- * Connect to a specific server. This method cannot be called on the main
- * activity thread.
- *
- * @param aServer
- * The Server to connect to.
- */
- public void connectTo(Server aServer) {
- connectTo(aServer.getProtocol(), aServer.getAddress());
- }
-
- /**
- * Connect to a specific server. This method cannot be called on the main
- * activity thread.
- *
- * @param aProtocol
- * @param address
- */
- public void connectTo(Protocol aProtocol, String address) {
- switch (aProtocol) {
- case NETWORK:
- mClient = new NetworkClient(address);
- mTransmitter = new Transmitter(mClient);
- mClient.setReceiver(mReceiver);
- break;
- case BLUETOOTH:
- break;
- default:
- break;
-
- }
-
- }
-
- public void disconnect() {
- mClient.closeConnection();
- }
-
- public SlideShow getSlideShow() {
- return mReceiver.getSlideShow();
- }
-
- // ---------------------------------------------------- SERVER -------------
- /**
- * Class describing a remote server.
- */
- public static class Server {
- private Protocol mProtocol;
- private String mAddress;
- private String mName;
- private long mTimeDiscovered;
-
- protected Server(Protocol aProtocol, String aAddress, String aName,
- long aTimeDiscovered) {
- mProtocol = aProtocol;
- mAddress = aAddress;
- mName = aName;
- mTimeDiscovered = aTimeDiscovered;
- }
-
- public Protocol getProtocol() {
- return mProtocol;
- }
-
- public String getAddress() {
- return mAddress;
- }
-
- /**
- * Get a human friendly name for the server.
- *
- * @return The name.
- */
- public String getName() {
- return mName;
- }
-
- public long getTimeDiscovered() {
- return mTimeDiscovered;
- }
-
- }
+public class CommunicationService extends Service implements Runnable {
+
+ public enum State {
+ DISCONNECTED, SEARCHING, CONNECTING, CONNECTED
+ };
+
+ /**
+ * Used to protect all writes to mState, mStateDesired, and mServerDesired.
+ */
+ private Object mConnectionVariableMutex = new Object();
+
+ private State mState = State.DISCONNECTED;
+
+ private State mStateDesired = State.DISCONNECTED;
+
+ private Server mServerDesired = null;
+
+ @Override
+ public void run() {
+ while (true) {
+ // Condition
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ // We don't care.
+ }
+ // Work
+ synchronized (mConnectionVariableMutex) {
+ if ((mStateDesired == State.CONNECTED && mState == State.CONNECTED)
+ || (mStateDesired == State.DISCONNECTED && mState == State.CONNECTED)) {
+ mClient.closeConnection();
+ mState = State.DISCONNECTED;
+ }
+ if (mStateDesired == State.CONNECTED) {
+ switch (mServerDesired.getProtocol()) {
+ case NETWORK:
+ mClient = new NetworkClient(mServerDesired.getAddress());
+ mTransmitter = new Transmitter(mClient);
+ mClient.setReceiver(mReceiver);
+ break;
+ case BLUETOOTH:
+ break;
+ }
+ mState = State.CONNECTED;
+ }
+ }
+ }
+
+ }
+
+ public void startSearching() {
+ synchronized (mConnectionVariableMutex) {
+ if (mState == State.CONNECTING || mState == State.CONNECTED) {
+ disconnect();
+ }
+ mFinder.startFinding();
+ mState = State.SEARCHING;
+ }
+ }
+
+ public void stopSearching() {
+ synchronized (mConnectionVariableMutex) {
+ mFinder.stopFinding();
+ mState = State.DISCONNECTED;
+ }
+ }
+
+ public void connectTo(Server aServer) {
+ synchronized (mConnectionVariableMutex) {
+ if (mState == State.SEARCHING) {
+ mFinder.stopFinding();
+ mState = State.DISCONNECTED;
+ }
+ mServerDesired = aServer;
+ mStateDesired = State.CONNECTED;
+ notify();
+ }
+ // TODO: connect
+ }
+
+ public void disconnect() {
+ synchronized (mConnectionVariableMutex) {
+ mStateDesired = State.DISCONNECTED;
+ notify();
+ }
+ }
+
+ /**
+ * Return the service to clients.
+ */
+ public class CBinder extends Binder {
+ public CommunicationService getService() {
+ return CommunicationService.this;
+ }
+ }
+
+ private final IBinder mBinder = new CBinder();
+
+ public static final int MSG_SLIDESHOW_STARTED = 1;
+ public static final int MSG_SLIDE_CHANGED = 2;
+ public static final int MSG_SLIDE_PREVIEW = 3;
+ public static final int MSG_SLIDE_NOTES = 4;
+
+ public static final String MSG_SERVERLIST_CHANGED = "SERVERLIST_CHANGED";
+ public static final String MSG_PAIRING_STARTED = "PAIRING_STARTED";
+ public static final String MSG_PAIRING_SUCCESSFUL = "PAIRING_SUCCESSFUL";
+
+ private Transmitter mTransmitter;
+
+ private Client mClient;
+
+ private Receiver mReceiver = new Receiver();
+
+ private ServerFinder mFinder = new ServerFinder(this);
+
+ public void setActivityMessenger(Messenger aActivityMessenger) {
+ mReceiver.setActivityMessenger(aActivityMessenger);
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ // TODO Auto-generated method stub
+ return mBinder;
+ }
+
+ @Override
+ public void onCreate() {
+ // TODO Create a notification (if configured).
+ }
+
+ @Override
+ public void onDestroy() {
+ // TODO Destroy the notification (as necessary).
+ }
+
+ public Transmitter getTransmitter() {
+ return mTransmitter;
+ }
+
+ public Server[] getServers() {
+ return mFinder.getServerList();
+ }
+
+ public void startFindingServers() {
+ mFinder.startFinding();
+ }
+
+ public void stopFindingServers() {
+ mFinder.stopFinding();
+ }
+
+ public SlideShow getSlideShow() {
+ return mReceiver.getSlideShow();
+ }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ \ No newline at end of file
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java
new file mode 100644
index 000000000000..dfa75fe7577a
--- /dev/null
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java
@@ -0,0 +1,43 @@
+package org.libreoffice.impressremote.communication;
+
+public class Server {
+
+ public enum Protocol {
+ NETWORK, BLUETOOTH
+ };
+
+ private Protocol mProtocol;
+ private String mAddress;
+ private String mName;
+ private long mTimeDiscovered;
+
+ protected Server(Protocol aProtocol, String aAddress, String aName,
+ long aTimeDiscovered) {
+ mProtocol = aProtocol;
+ mAddress = aAddress;
+ mName = aName;
+ mTimeDiscovered = aTimeDiscovered;
+ }
+
+ public Protocol getProtocol() {
+ return mProtocol;
+ }
+
+ public String getAddress() {
+ return mAddress;
+ }
+
+ /**
+ * Get a human friendly name for the server.
+ *
+ * @return The name.
+ */
+ public String getName() {
+ return mName;
+ }
+
+ public long getTimeDiscovered() {
+ return mTimeDiscovered;
+ }
+
+}
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
index 29ae7de98286..55b6f5472b49 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java
@@ -8,8 +8,6 @@ import java.net.InetAddress;
import java.net.SocketException;
import java.util.HashMap;
-import org.libreoffice.impressremote.communication.CommunicationService.Server;
-
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
@@ -66,8 +64,8 @@ public class ServerFinder {
if (aName == null) {
return;
}
- Server aServer = new Server(CommunicationService.Protocol.NETWORK,
- aPacket.getAddress().toString(), aName,
+ Server aServer = new Server(Server.Protocol.NETWORK, aPacket
+ .getAddress().toString(), aName,
System.currentTimeMillis());
mServerList.put(aServer.getAddress(), aServer);