From b5d6989e8b7a739881f54ce9db0d560fb69e7eb9 Mon Sep 17 00:00:00 2001 From: "Andrzej J.R. Hunt" Date: Fri, 3 Aug 2012 14:08:40 +0200 Subject: [PATCH] Implemented Server selector. Change-Id: Ib6d0712c498e831a559df00d301ea2d4b851deed --- android/sdremote/AndroidManifest.xml | 5 +- .../sdremote/res/layout/activity_selector.xml | 72 ++++++++ .../activity_selector_sublayout_server.xml | 20 +++ android/sdremote/res/values/strings.xml | 3 + android/sdremote/res/values/styles.xml | 2 + .../libreoffice/impressremote/Launcher.java | 23 --- .../impressremote/SelectorActivity.java | 161 ++++++++++++++++++ .../communication/ServerFinder.java | 3 +- 8 files changed, 260 insertions(+), 29 deletions(-) create mode 100644 android/sdremote/res/layout/activity_selector.xml create mode 100644 android/sdremote/res/layout/activity_selector_sublayout_server.xml delete mode 100644 android/sdremote/src/org/libreoffice/impressremote/Launcher.java create mode 100644 android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java diff --git a/android/sdremote/AndroidManifest.xml b/android/sdremote/AndroidManifest.xml index 14f457ba52b2..cea2e1482e1f 100644 --- a/android/sdremote/AndroidManifest.xml +++ b/android/sdremote/AndroidManifest.xml @@ -13,12 +13,9 @@ - + - diff --git a/android/sdremote/res/layout/activity_selector.xml b/android/sdremote/res/layout/activity_selector.xml new file mode 100644 index 000000000000..6f4cfa158cd8 --- /dev/null +++ b/android/sdremote/res/layout/activity_selector.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 new file mode 100644 index 000000000000..14bec2349d8e --- /dev/null +++ b/android/sdremote/res/layout/activity_selector_sublayout_server.xml @@ -0,0 +1,20 @@ + + + + + + + + \ No newline at end of file diff --git a/android/sdremote/res/values/strings.xml b/android/sdremote/res/values/strings.xml index ac51b864f488..0e14caa5a519 100644 --- a/android/sdremote/res/values/strings.xml +++ b/android/sdremote/res/values/strings.xml @@ -20,5 +20,8 @@ Change slides using volume buttons Switch computer Return to Slide + Bluetooth + WI-FI + Searching for computers… \ No newline at end of file diff --git a/android/sdremote/res/values/styles.xml b/android/sdremote/res/values/styles.xml index c95fb0cdbce7..e5a465100222 100644 --- a/android/sdremote/res/values/styles.xml +++ b/android/sdremote/res/values/styles.xml @@ -7,6 +7,7 @@ #000000 #E8E9E8 #C1C2C1 + #5E5F5F 3 @@ -48,6 +49,7 @@ @style/Theme.ImpressRemote.ActionBarMenu @color/light_grey @drawable/up_indicator_white + @color/text_grey \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/Launcher.java b/android/sdremote/src/org/libreoffice/impressremote/Launcher.java deleted file mode 100644 index 8b27ebf01eeb..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/Launcher.java +++ /dev/null @@ -1,23 +0,0 @@ -/* -*- 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 android.app.Activity; -import android.os.Bundle; - -public class Launcher extends Activity { - /** Called when the activity is first created. */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - } - -} -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java new file mode 100644 index 000000000000..4bed7105fca2 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java @@ -0,0 +1,161 @@ +/* -*- 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 java.util.Arrays; +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 android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.support.v4.content.LocalBroadcastManager; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +public class SelectorActivity extends Activity { + + private CommunicationService mCommunicationService; + + private LinearLayout mBluetoothList; + 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); + + mBluetoothList = (LinearLayout) findViewById(R.id.selector_list_bluetooth); + 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(); + 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(); + } + + @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 mBluetoothServers = new HashMap(); + private HashMap mNetworkServers = new HashMap(); + + private void refreshLists() { + if (mCommunicationService == null) + return; + + Server[] aServers = mCommunicationService.getServers(); + + // Bluetooth -- Remove old + for (Entry aEntry : mBluetoothServers.entrySet()) { + if (!Arrays.asList(aServers).contains(aEntry.getKey())) { + mBluetoothServers.remove(aEntry.getKey()); + mBluetoothList.removeView(aEntry.getValue()); + } + } + // Network -- Remove old + for (Entry 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 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 + + mBluetoothList.setVisibility((mBluetoothServers.size() != 0) ? View.VISIBLE + : View.INVISIBLE); + mNetworkList.setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE + : View.INVISIBLE); + + mNoServerLabel.setVisibility((mBluetoothServers.size() == 0) + && (mNetworkServers.size() == 0) ? View.VISIBLE + : View.INVISIBLE); + } +} +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java index 5ce7897e2d0c..c30f9c25085d 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java @@ -135,7 +135,6 @@ public class ServerFinder { } public Server[] getServerList() { - return (Server[]) mServerList.toArray(); + return mServerList.toArray(new Server[mServerList.size()]); } - }