Kaydet (Commit) 9ca3b536 authored tarafından Andrzej J.R. Hunt's avatar Andrzej J.R. Hunt

Saving of authorised remotes.

Change-Id: I4cf6542352ac4213e66e2bbd6cc54d4f9372de62
üst 571a7cba
...@@ -86,8 +86,7 @@ public class CommunicationService extends Service implements Runnable { ...@@ -86,8 +86,7 @@ public class CommunicationService extends Service implements Runnable {
mState = State.CONNECTING; mState = State.CONNECTING;
switch (mServerDesired.getProtocol()) { switch (mServerDesired.getProtocol()) {
case NETWORK: case NETWORK:
mClient = new NetworkClient( mClient = new NetworkClient(mServerDesired, this);
mServerDesired.getAddress(), this);
break; break;
case BLUETOOTH: case BLUETOOTH:
mClient = new BluetoothClient( mClient = new BluetoothClient(
......
...@@ -17,6 +17,8 @@ import java.util.Random; ...@@ -17,6 +17,8 @@ import java.util.Random;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
/** /**
...@@ -30,20 +32,16 @@ public class NetworkClient extends Client { ...@@ -30,20 +32,16 @@ public class NetworkClient extends Client {
private Socket mSocket; private Socket mSocket;
public NetworkClient(String ipAddress, Context aContext) { public NetworkClient(Server aServer, Context aContext) {
super(aContext); super(aContext);
try { try {
mSocket = new Socket(ipAddress, PORT); mSocket = new Socket(aServer.getAddress(), PORT);
mInputStream = mSocket.getInputStream(); mInputStream = mSocket.getInputStream();
mReader = new BufferedReader(new InputStreamReader(mInputStream, mReader = new BufferedReader(new InputStreamReader(mInputStream,
CHARSET)); CHARSET));
mOutputStream = mSocket.getOutputStream(); mOutputStream = mSocket.getOutputStream();
// Pairing. // Pairing.
Random aRandom = new Random(); String aPin = setupPin(aServer);
String aPin = "" + (aRandom.nextInt(9000) + 1000);
while (aPin.length() < 4) {
aPin = "0" + aPin; // Add leading zeros if necessary
}
Intent aIntent = new Intent( Intent aIntent = new Intent(
CommunicationService.MSG_PAIRING_STARTED); CommunicationService.MSG_PAIRING_STARTED);
aIntent.putExtra("PIN", aPin); aIntent.putExtra("PIN", aPin);
...@@ -82,6 +80,34 @@ public class NetworkClient extends Client { ...@@ -82,6 +80,34 @@ public class NetworkClient extends Client {
} }
private String setupPin(Server aServer) {
// Get settings
SharedPreferences aPreferences = mContext.getSharedPreferences(
"sdremote_authorisedremotes",
android.content.Context.MODE_PRIVATE);
if (aPreferences.contains(aServer.getName())) {
return aPreferences.getString(aServer.getName(), "");
} else {
String aPin = generatePin();
Editor aEdit = aPreferences.edit();
aEdit.putString(aServer.getName(), aPin);
aEdit.commit();
return aPin;
}
}
private String generatePin() {
Random aRandom = new Random();
String aPin = "" + (aRandom.nextInt(9000) + 1000);
while (aPin.length() < 4) {
aPin = "0" + aPin; // Add leading zeros if necessary
}
return aPin;
}
@Override @Override
public void closeConnection() { public void closeConnection() {
try { try {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<!--*********************************************************************** <!--***********************************************************************
* *
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* Copyright 2000, 2010 Oracle and/or its affiliates. * Copyright 2000, 2010 Oracle and/or its affiliates.
* *
* OpenOffice.org - a multi-platform office productivity suite * OpenOffice.org - a multi-platform office productivity suite
...@@ -90,6 +90,19 @@ ...@@ -90,6 +90,19 @@
</info> </info>
</prop> </prop>
</group> </group>
<group oor:name="AuthorisedRemote">
<info>
<author>ajrhunt</author>
<desc>
A Remote Control Device that has been authorised for connection.
</desc>
</info>
<prop oor:name="PIN" oor:type="xs:string">
<info>
<desc>The PIN used by the device for authentication.</desc>
</info>
</prop>
</group>
</templates> </templates>
<component> <component>
<group oor:name="Layout"> <group oor:name="Layout">
...@@ -655,6 +668,12 @@ ...@@ -655,6 +668,12 @@
<value>5000</value> <value>5000</value>
</prop> </prop>
</group> </group>
<set oor:name="AuthorisedRemotes" oor:node-type="AuthorisedRemote">
<info>
<author>ajrhunt</author>
<desc>Contains a list of remote control devices that can connect without user confirmation.</desc>
</info>
</set>
</group> </group>
<group oor:name="Snap"> <group oor:name="Snap">
<info> <info>
......
...@@ -11,7 +11,14 @@ ...@@ -11,7 +11,14 @@
#include <vector> #include <vector>
#include "officecfg/Office/Common.hxx" #include "officecfg/Office/Common.hxx"
#include "officecfg/Office/Impress.hxx"
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include <comphelper/processfactory.hxx> #include <comphelper/processfactory.hxx>
#include <comphelper/configuration.hxx>
#include "sddll.hxx" #include "sddll.hxx"
...@@ -25,8 +32,13 @@ ...@@ -25,8 +32,13 @@
using namespace std; using namespace std;
using namespace sd; using namespace sd;
using namespace ::com::sun::star; using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using rtl::OString; using rtl::OString;
using namespace ::osl; using namespace ::osl;
using namespace ::comphelper;
// struct ClientInfoInternal: // struct ClientInfoInternal:
// ClientInfo // ClientInfo
...@@ -89,10 +101,11 @@ void RemoteServer::execute() ...@@ -89,10 +101,11 @@ void RemoteServer::execute()
OUString aAddress = aClientAddr.getHostname(); OUString aAddress = aClientAddr.getHostname();
MutexGuard aGuard( mDataMutex ); MutexGuard aGuard( mDataMutex );
mAvailableClients.push_back( new ClientInfoInternal( ClientInfoInternal* pClient = new ClientInfoInternal(
OStringToOUString( aName, RTL_TEXTENCODING_UTF8 ), OStringToOUString( aName, RTL_TEXTENCODING_UTF8 ),
aAddress, pSocket, OStringToOUString( aPin, aAddress, pSocket, OStringToOUString( aPin,
RTL_TEXTENCODING_UTF8 ) ) ); RTL_TEXTENCODING_UTF8 ) );
mAvailableClients.push_back( pClient );
// Read off any additional non-empty lines // Read off any additional non-empty lines
do do
...@@ -100,6 +113,27 @@ void RemoteServer::execute() ...@@ -100,6 +113,27 @@ void RemoteServer::execute()
pSocket->readLine( aLine ); pSocket->readLine( aLine );
} }
while ( aLine.getLength() > 0 ); while ( aLine.getLength() > 0 );
// Check if we already have this server.
Reference< XNameAccess > xConfig = officecfg::Office::Impress::Misc::AuthorisedRemotes::get();
Sequence< OUString > aNames = xConfig->getElementNames();
for ( int i = 0; i < aNames.getLength(); i++ )
{
if ( aNames[i].equals( pClient->mName ) )
{
Reference<XNameAccess> xSetItem( xConfig->getByName(aNames[i]), UNO_QUERY );
Any axPin(xSetItem->getByName("PIN"));
OUString sPin;
axPin >>= sPin;
if ( ! sPin.equals( pClient->mPin ) ) {
break;
}
connectClient( pClient, sPin );
break;
}
}
} else { } else {
delete pSocket; delete pSocket;
} }
...@@ -180,6 +214,32 @@ sal_Bool RemoteServer::connectClient( ClientInfo* pClient, rtl::OUString aPin ) ...@@ -180,6 +214,32 @@ sal_Bool RemoteServer::connectClient( ClientInfo* pClient, rtl::OUString aPin )
ClientInfoInternal *apClient = (ClientInfoInternal*) pClient; ClientInfoInternal *apClient = (ClientInfoInternal*) pClient;
if ( apClient->mPin.equals( aPin ) ) if ( apClient->mPin.equals( aPin ) )
{ {
// Save in settings first
boost::shared_ptr< ConfigurationChanges > aChanges = ConfigurationChanges::create();
Reference< XNameContainer > xConfig = officecfg::Office::Impress::Misc::AuthorisedRemotes::get( aChanges );
Reference<XSingleServiceFactory> xChildFactory (
xConfig, UNO_QUERY);
Reference<XNameReplace> xChild( xChildFactory->createInstance(), UNO_QUERY);
Any aValue;
if (xChild.is())
{
// Check whether the client is already saved
Sequence< OUString > aNames = xConfig->getElementNames();
for ( int i = 0; i < aNames.getLength(); i++ )
{
if ( aNames[i].equals( apClient->mName ) )
xConfig->replaceByName( apClient->mName, makeAny( xChild ) );
else
xConfig->insertByName( apClient->mName, makeAny( xChild ) );
}
aValue <<= OUString( apClient->mPin );
xChild->replaceByName("PIN", aValue);
aChanges->commit();
}
Communicator* pCommunicator = new Communicator( apClient->mpStreamSocket ); Communicator* pCommunicator = new Communicator( apClient->mpStreamSocket );
MutexGuard aGuard( spServer->mDataMutex ); MutexGuard aGuard( spServer->mDataMutex );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment