diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-07-02 22:10:13 +0300 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2013-07-25 18:01:50 +0100 |
commit | ba412940872cfb309980bdd97584ba803ed1c1d4 (patch) | |
tree | a82fc64733b98ee52cd4ad518591ccc06d42e560 /android/sdremote | |
parent | fd5ec7142fcc15d3644a6e2afeb1d2c5a21ec09d (diff) |
Add basic functionality of displaying servers list to the new UI.
Change-Id: I1df9d85d7c03884c9572a33957232edc576c9965
Diffstat (limited to 'android/sdremote')
6 files changed, 284 insertions, 6 deletions
diff --git a/android/sdremote/res/layout/list_item.xml b/android/sdremote/res/layout/list_item.xml new file mode 100644 index 000000000000..275b9bafe774 --- /dev/null +++ b/android/sdremote/res/layout/list_item.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:singleLine="true" + android:textSize="@dimen/text_size_list_item" + android:gravity="center_vertical" + android:padding="@dimen/padding_horizontal_list_item" + android:layout_width="fill_parent" + android:layout_height="?android:attr/listPreferredItemHeight" />
\ No newline at end of file diff --git a/android/sdremote/res/values/dimens.xml b/android/sdremote/res/values/dimens.xml new file mode 100644 index 000000000000..61f9ded3591a --- /dev/null +++ b/android/sdremote/res/values/dimens.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + + <dimen name="padding_horizontal_list_item">8dp</dimen> + + <dimen name="text_size_list_item">18sp</dimen> + +</resources>
\ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/ComputersActivity.java b/android/sdremote/src/org/libreoffice/impressremote/ComputersActivity.java index 4675ca1c426c..e4e9d5fdec89 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/ComputersActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/ComputersActivity.java @@ -30,7 +30,8 @@ public class ComputersActivity extends SherlockFragmentActivity { } private ActionBar.Tab buildBluetoothServersTab() { - ComputersFragment aFragment = ComputersFragment.newInstance(); + ComputersFragment aFragment = ComputersFragment.newInstance( + ComputersFragment.Type.BLUETOOTH); return buildActionBarTab(aFragment, R.string.title_bluetooth); } @@ -45,7 +46,8 @@ public class ComputersActivity extends SherlockFragmentActivity { } private ActionBar.Tab buildWiFiServersTab() { - ComputersFragment aFragment = ComputersFragment.newInstance(); + ComputersFragment aFragment = ComputersFragment.newInstance( + ComputersFragment.Type.WIFI); return buildActionBarTab(aFragment, R.string.title_wifi); } diff --git a/android/sdremote/src/org/libreoffice/impressremote/ComputersAdapter.java b/android/sdremote/src/org/libreoffice/impressremote/ComputersAdapter.java new file mode 100644 index 000000000000..f3080f1c9f43 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/ComputersAdapter.java @@ -0,0 +1,65 @@ +/* -*- Mode: Java; 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.List; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import org.libreoffice.impressremote.communication.Server; + +public class ComputersAdapter extends ArrayAdapter<Server> { + private final LayoutInflater mLayoutInflater; + + public ComputersAdapter(Context aContext) { + super(aContext, R.layout.list_item); + + mLayoutInflater = (LayoutInflater) aContext + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public View getView(int aPosition, View aConvertView, ViewGroup aParentViewGroup) { + TextView aListItem = (TextView) getView(aConvertView, aParentViewGroup); + + aListItem.setText(buildListItemText(aPosition)); + + return aListItem; + } + + private View getView(View aConvertView, ViewGroup aParentViewGroup) { + if (aConvertView != null) { + return aConvertView; + } + + return mLayoutInflater + .inflate(R.layout.list_item, aParentViewGroup, false); + } + + private String buildListItemText(int aPosition) { + return getItem(aPosition).getName(); + } + + public void add(List<Server> aServers) { + setNotifyOnChange(false); + + for (Server aServer : aServers) { + add(aServer); + } + + notifyDataSetChanged(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/ComputersFragment.java b/android/sdremote/src/org/libreoffice/impressremote/ComputersFragment.java index 1e1d59d15b03..b776af5cae62 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/ComputersFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/ComputersFragment.java @@ -8,11 +8,206 @@ */ package org.libreoffice.impressremote; +import java.util.ArrayList; +import java.util.List; + +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 com.actionbarsherlock.app.SherlockListFragment; +import org.libreoffice.impressremote.communication.CommunicationService; +import org.libreoffice.impressremote.communication.Server; + +public class ComputersFragment extends SherlockListFragment implements ServiceConnection { + public static enum Type { + WIFI, BLUETOOTH + } + + private Type mType; + + private CommunicationService mCommunicationService; + private BroadcastReceiver mIntentsReceiver; + + public static ComputersFragment newInstance(Type aType) { + ComputersFragment aFragment = new ComputersFragment(); + + aFragment.setArguments(buildArguments(aType)); + + return aFragment; + } + + private static Bundle buildArguments(Type aType) { + Bundle aArguments = new Bundle(); + + aArguments.putSerializable("TYPE", aType); + + return aArguments; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mType = (Type) getArguments().getSerializable("TYPE"); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + bindService(); + } + + private void bindService() { + Intent aServiceIntent = new Intent(getActivity(), CommunicationService.class); + + getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE); + } + + @Override + public void onServiceConnected(ComponentName aComponentName, IBinder aBinder) { + CommunicationService.CBinder aServiceBinder = (CommunicationService.CBinder) aBinder; + + mCommunicationService = aServiceBinder.getService(); + + mCommunicationService.startSearch(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + unbindService(); + } + + private void unbindService() { + if (!isServiceBound()) { + return; + } + + getActivity().unbindService(this); + } + + private boolean isServiceBound() { + return mCommunicationService != null; + } + + @Override + public void onServiceDisconnected(ComponentName aComponentName) { + mCommunicationService = null; + } + + @Override + public void onResume() { + super.onResume(); + + registerIntentsReceiver(); + } + + private void registerIntentsReceiver() { + mIntentsReceiver = new IntentsReceiver(this); + IntentFilter aIntentFilter = buildIntentsReceiverFilter(); + + LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mIntentsReceiver, aIntentFilter); + } + + private static final class IntentsReceiver extends BroadcastReceiver { + private final ComputersFragment mComputersFragment; + + public IntentsReceiver(ComputersFragment aComputersFragment) { + mComputersFragment = aComputersFragment; + } + + @Override + public void onReceive(Context aContext, Intent aIntent) { + if (CommunicationService.MSG_SERVERLIST_CHANGED.equals(aIntent.getAction())) { + mComputersFragment.loadComputers(); + } + } + } + + private IntentFilter buildIntentsReceiverFilter() { + IntentFilter aIntentFilter = new IntentFilter(); + aIntentFilter.addAction(CommunicationService.MSG_SERVERLIST_CHANGED); + + return aIntentFilter; + } + + public void loadComputers() { + if (!isServiceBound()) { + return; + } + + if (getComputers().isEmpty()) { + return; + } + + if (!isComputersAdapterExist()) { + setUpComputersAdapter(); + } + + getComputersAdapter().clear(); + getComputersAdapter().add(getComputers()); + } + + private boolean isComputersAdapterExist() { + return getComputersAdapter() != null; + } + + private ComputersAdapter getComputersAdapter() { + return (ComputersAdapter) getListAdapter(); + } + + private void setUpComputersAdapter() { + setListAdapter(new ComputersAdapter(getActivity())); + } + + private List<Server> getComputers() { + List<Server> aComputers = new ArrayList<Server>(); + + for (Server aServer : mCommunicationService.getServers()) { + if (isComputerSupportsRequiredType(aServer)) { + aComputers.add(aServer); + } + } + + return aComputers; + } + + private boolean isComputerSupportsRequiredType(Server aServer) { + switch (mType) { + case WIFI: + return aServer.getProtocol() == Server.Protocol.TCP; + + case BLUETOOTH: + return aServer.getProtocol() == Server.Protocol.BLUETOOTH; + + default: + return false; + } + } + + @Override + public void onPause() { + super.onPause(); + + unregisterIntentsReceiver(); + } -public class ComputersFragment extends SherlockListFragment { - public static ComputersFragment newInstance() { - return new ComputersFragment(); + private void unregisterIntentsReceiver() { + try { + getActivity().unregisterReceiver(mIntentsReceiver); + } catch (IllegalArgumentException e) { + // Receiver not registered. + // Fixed in Honeycomb: Android’s issue #6191. + } } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index c523f6184065..f51e3525b28f 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -36,7 +36,7 @@ public class Server implements Parcelable { this.mTimeDiscovered = aTimeDiscovered; } - protected Server(Protocol aProtocol, String aAddress, String aName) { + public Server(Protocol aProtocol, String aAddress, String aName) { this.mProtocol = aProtocol; this.mAddress = aAddress; this.mName = aName; |