Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #43 from thaliproject/story_001_tompaana_36
Browse files Browse the repository at this point in the history
Story 001 tompaana 36
  • Loading branch information
tompaana committed Feb 2, 2016
2 parents efbaac1 + c07e8e4 commit 80c9f78
Show file tree
Hide file tree
Showing 153 changed files with 5,448 additions and 1,442 deletions.
18 changes: 0 additions & 18 deletions BtConnectorLib/LICENSE

This file was deleted.

10 changes: 5 additions & 5 deletions BtConnectorLib/btconnectorlib2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ version = gradle.ext.version
group = gradle.ext.group

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -24,5 +24,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:appcompat-v7:23.1.1'
}
12 changes: 4 additions & 8 deletions BtConnectorLib/btconnectorlib2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<application android:allowBackup="true" android:label="@string/app_name">
</application>

<application android:allowBackup="true" android:label="@string/app_name" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public ConnectionManager(
mMyUuid = myUuid;
mMyName = myName;

mSettings = ConnectionManagerSettings.getInstance();
mSettings = ConnectionManagerSettings.getInstance(mContext);
mSettings.load();
mSettings.setListener(this);

mHandler = new Handler(mContext.getMainLooper());
Expand Down Expand Up @@ -155,7 +156,14 @@ public boolean start(String myPeerId, String myPeerName) {
* @return True, if started successfully or was already running. False otherwise.
*/
public boolean start(String myPeerName) {
return start(mBluetoothManager.getBluetoothAddress(), myPeerName);
String bluetoothMacAddress = getBluetoothMacAddress();
boolean wasStarted = false;

if (bluetoothMacAddress != null) {
wasStarted = start(getBluetoothMacAddress(), myPeerName);
}

return wasStarted;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
/* Copyright (c) 2015 Microsoft Corporation. This software is licensed under the MIT License.
/* Copyright (c) 2015-2016 Microsoft Corporation. This software is licensed under the MIT License.
* See the license file delivered with this project for further information.
*/
package org.thaliproject.p2p.btconnectorlib;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;
import org.thaliproject.p2p.btconnectorlib.internal.AbstractSettings;
import org.thaliproject.p2p.btconnectorlib.internal.bluetooth.BluetoothConnector;

/**
* Connection manager settings.
*/
public class ConnectionManagerSettings {
public class ConnectionManagerSettings extends AbstractSettings {
public interface Listener {
void onConnectionManagerSettingsChanged();
}

// Default settings
public static final long DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS = BluetoothConnector.DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS;
public static final int SYSTEM_DECIDED_INSECURE_RFCOMM_SOCKET_PORT = BluetoothConnector.SYSTEM_DECIDED_INSECURE_RFCOMM_SOCKET_PORT;
public static final int DEFAULT_ALTERNATIVE_INSECURE_RFCOMM_SOCKET_PORT = BluetoothConnector.DEFAULT_ALTERNATIVE_INSECURE_RFCOMM_SOCKET_PORT;
public static final int DEFAULT_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES = BluetoothConnector.DEFAULT_MAX_NUMBER_OF_RETRIES;

// Keys for shared preferences
private static final String KEY_CONNECTION_TIMEOUT = "connection_timeout";
private static final String KEY_PORT_NUMBER = "port_number";
private static final String KEY_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES = "max_number_of_connection_attempt_retries";

private static final String TAG = ConnectionManagerSettings.class.getName();
private static final int MAX_INSECURE_RFCOMM_SOCKET_PORT = 30;

Expand All @@ -29,11 +38,12 @@ public interface Listener {
private int mMaxNumberOfConnectionAttemptRetries = 0;

/**
* @param context The application context for the shared preferences.
* @return The singleton instance of this class.
*/
public static ConnectionManagerSettings getInstance() {
public static ConnectionManagerSettings getInstance(Context context) {
if (mInstance == null) {
mInstance = new ConnectionManagerSettings();
mInstance = new ConnectionManagerSettings(context);
}

return mInstance;
Expand All @@ -42,7 +52,8 @@ public static ConnectionManagerSettings getInstance() {
/**
* Private constructor.
*/
private ConnectionManagerSettings() {
private ConnectionManagerSettings(Context context) {
super(context); // Will create Shared preferences (and editor) instance
}

/**
Expand All @@ -65,10 +76,14 @@ public long getConnectionTimeout() {
* @param connectionTimeoutInMilliseconds The connection timeout in milliseconds.
*/
public void setConnectionTimeout(long connectionTimeoutInMilliseconds) {
mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds;
if (mConnectionTimeoutInMilliseconds != connectionTimeoutInMilliseconds) {
mConnectionTimeoutInMilliseconds = connectionTimeoutInMilliseconds;
mSharedPreferencesEditor.putLong(KEY_CONNECTION_TIMEOUT, mConnectionTimeoutInMilliseconds);
mSharedPreferencesEditor.apply();

if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
}
}
}

Expand All @@ -94,17 +109,21 @@ public int getInsecureRfcommSocketPortNumber() {
public boolean setInsecureRfcommSocketPortNumber(int insecureRfcommSocketPort) {
boolean wasSet = false;

if (insecureRfcommSocketPort >= -1 && insecureRfcommSocketPort <= MAX_INSECURE_RFCOMM_SOCKET_PORT) {
Log.i(TAG, "setInsecureRfcommSocketPortNumber: Will use port " + insecureRfcommSocketPort + " when trying to connect");
mInsecureRfcommSocketPortNumber = insecureRfcommSocketPort;
if (mInsecureRfcommSocketPortNumber != insecureRfcommSocketPort) {
if (insecureRfcommSocketPort >= -1 && insecureRfcommSocketPort <= MAX_INSECURE_RFCOMM_SOCKET_PORT) {
Log.i(TAG, "setInsecureRfcommSocketPortNumber: Will use port " + insecureRfcommSocketPort + " when trying to connect");
mInsecureRfcommSocketPortNumber = insecureRfcommSocketPort;
mSharedPreferencesEditor.putInt(KEY_PORT_NUMBER, mInsecureRfcommSocketPortNumber);
mSharedPreferencesEditor.apply();

if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
}
if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
}

wasSet = true;
} else {
Log.e(TAG, "setInsecureRfcommSocketPortNumber: Cannot set port, invalid port number: " + insecureRfcommSocketPort);
wasSet = true;
} else {
Log.e(TAG, "setInsecureRfcommSocketPortNumber: Cannot set port, invalid port number: " + insecureRfcommSocketPort);
}
}

return wasSet;
Expand All @@ -122,10 +141,36 @@ public int getMaxNumberOfConnectionAttemptRetries() {
* @param maxNumberOfRetries The maximum number of socket connection attempt retries for outgoing connections.
*/
public void setMaxNumberOfConnectionAttemptRetries(int maxNumberOfRetries) {
mMaxNumberOfConnectionAttemptRetries = maxNumberOfRetries;
if (mMaxNumberOfConnectionAttemptRetries != maxNumberOfRetries) {
mMaxNumberOfConnectionAttemptRetries = maxNumberOfRetries;
mSharedPreferencesEditor.putInt(KEY_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES, mMaxNumberOfConnectionAttemptRetries);
mSharedPreferencesEditor.apply();

if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
if (mListener != null) {
mListener.onConnectionManagerSettingsChanged();
}
}
}

@Override
public void load() {
mConnectionTimeoutInMilliseconds = mSharedPreferences.getLong(
KEY_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS);
mInsecureRfcommSocketPortNumber = mSharedPreferences.getInt(
KEY_PORT_NUMBER, SYSTEM_DECIDED_INSECURE_RFCOMM_SOCKET_PORT);
mMaxNumberOfConnectionAttemptRetries = mSharedPreferences.getInt(
KEY_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES, DEFAULT_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES);

Log.v(TAG, "load: "
+ "\n\tConnection timeout in milliseconds: " + mConnectionTimeoutInMilliseconds + ", "
+ "\n\tInsecure RFCOMM socket port number: " + mInsecureRfcommSocketPortNumber + ", "
+ "\n\tMaximum number of connection attempt retries: " + mMaxNumberOfConnectionAttemptRetries);
}

@Override
public void resetDefaults() {
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS);
setInsecureRfcommSocketPortNumber(SYSTEM_DECIDED_INSECURE_RFCOMM_SOCKET_PORT);
setMaxNumberOfConnectionAttemptRetries(DEFAULT_MAX_NUMBER_OF_CONNECTION_ATTEMPT_RETRIES);
}
}
Loading

0 comments on commit 80c9f78

Please sign in to comment.