Skip to content

Commit

Permalink
first commit of Roundtrip2
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffWyche committed Apr 4, 2016
0 parents commit c5d9a4d
Show file tree
Hide file tree
Showing 56 changed files with 2,868 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions app/build.gradle
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.gxwtech.roundtrip2"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/geoff/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,13 @@
package com.gxwtech.roundtrip2;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
53 changes: 53 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gxwtech.roundtrip2">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".RoundtripService"
android:label="@string/roundtrip_service_name"
android:exported="false" >
<intent-filter>
<action android:name="com.gxwtech.roundtrip2.RoundtripService" />
<action android:name="com.gxwtech.roundtrip2.MainActivity" />
<action android:name="com.gxwtech.roundtrip2.DeviceScanActivity" />
</intent-filter>
</service>
<activity
android:name=".DeviceScanActivity"
android:label="@string/title_activity_device_scan"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.gxwtech.roundtrip2.DeviceScanActivity" />
<action android:name="com.gxwtech.roundtrip2.RoundtripService" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gxwtech.roundtrip2.MainActivity"
/>
</activity>
</application>

</manifest>
234 changes: 234 additions & 0 deletions app/src/main/java/com/gxwtech/roundtrip2/DeviceScanActivity.java
@@ -0,0 +1,234 @@
package com.gxwtech.roundtrip2;

import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class DeviceScanActivity extends ListActivity {
private static final String TAG="DeviceScanActivity";
private LeDeviceListAdapter mLeDeviceListAdapter;

private static final int STATE_startScan = 1;
private static final int STATE_scanning = 2;
private static final int STATE_noAddress = 3;
private static final int STATE_haveAddress = 4;
private int scanState = STATE_noAddress;

private Handler mHandler;

// For receiving and displaying log messages from the Service thread
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

if (RT2Const.INTENT_scanStarted.equals(intent.getAction())) {
if (scanState == STATE_startScan) {
scanState = STATE_scanning;
}
refillFields();
} else if (RT2Const.INTENT_scanStopped.equals(intent.getAction())) {
if (scanState == STATE_scanning) {
scanState = STATE_noAddress;
}
refillFields();
} else if (RT2Const.INTENT_deviceFound.equals(intent.getAction())) {
String address = intent.getStringExtra("address");
if (address == null) {
address = "(null)";
}
Log.i(TAG,"Received DeviceFound message, address="+address);
mLeDeviceListAdapter.addDevice(address);
mLeDeviceListAdapter.notifyDataSetChanged();
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_scan);
mLeDeviceListAdapter = new LeDeviceListAdapter();
mHandler = new Handler();
this.setListAdapter(mLeDeviceListAdapter);
}

protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RT2Const.INTENT_deviceFound);
intentFilter.addAction(RT2Const.INTENT_scanStarted);
intentFilter.addAction(RT2Const.INTENT_scanStopped);

// register our desire to receive broadcasts from RoundtripService
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(broadcastReceiver, intentFilter);
refillFields();
}

protected void refillFields() {
SharedPreferences prefs = getApplicationContext().getSharedPreferences(RT2Const.RT2Preferences, 0);
String currentRL = prefs.getString("Default RileyLink", "");
if ((currentRL != null) && (!"".equals(currentRL))) {
scanState = STATE_haveAddress;
}
Button button = (Button)findViewById(R.id.buttonScanOrForget);
TextView tv = (TextView) findViewById(R.id.textViewCurrentRL);
switch(scanState) {
case STATE_noAddress: {
button.setText("Scan");
tv.setText("(None)");
} break;
case STATE_startScan: {
button.setText("(Scan started)");
tv.setText("(None)");
} break;
case STATE_scanning: {
button.setText("Scanning...");
tv.setText("(None)");
} break;
case STATE_haveAddress: {
button.setText("Forget");
if ((currentRL != null) && (!"".equals(currentRL))) {
tv.setText(currentRL);
} else {
tv.setText("(error)");
}
}
}
}

protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(getApplicationContext())
.unregisterReceiver(broadcastReceiver);
}

private class LeDeviceListAdapter extends BaseAdapter {
public LeDeviceListAdapter() {
this.items = new ArrayList<>();
}

private ArrayList<String> items;

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int i) {
return items.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View V = view;
if (V == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.ble_devlist_row,null);
}
String address = items.get(i);
TextView name = (TextView)V.findViewById(R.id.ble_dev_name);
name.setText(address);
return V;
}

public void addDevice(String address) {
items.add(address);
}

public void toggleItem(int i) {
// NYI
}

public void removeAllItems() {
items.clear();
}
}

protected void scanTimeout() {
// Uh, oh. We asked the service to scan for RL devices,
// and it took too long. Try to recover.
scanState = STATE_noAddress;
refillFields();
}

public void onScanOrForgetButtonClicked(View view) {
SharedPreferences prefs = getApplicationContext().getSharedPreferences(RT2Const.RT2Preferences,0);
String currentRL = prefs.getString("Default RileyLink", "");
if (scanState == STATE_noAddress) {
// if ((currentRL==null) || ("".equals(currentRL))) {
// then scan
// whack current listView contents
mLeDeviceListAdapter.removeAllItems();
mLeDeviceListAdapter.notifyDataSetChanged();
// Tell RoundtripService to find BLE devices

Intent intent = new Intent(RT2Const.INTENT_startScan);
/* do not include a specific device to scan for...*/
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
Log.d(TAG, "Sent Intent_startScan");
scanState = STATE_startScan;

// set a timer, in case service never responds
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
scanTimeout();
}
},11000);

refillFields();
} else if (scanState == STATE_haveAddress) {
// then forget
prefs.edit().putString("Default RileyLink","").commit();
scanState = STATE_noAddress;
refillFields();
}
}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mLeDeviceListAdapter.toggleItem(position);
String deviceAddress = (String)mLeDeviceListAdapter.getItem(position);
// record the device chosen.
SharedPreferences prefs = getApplicationContext().getSharedPreferences(RT2Const.RT2Preferences,0);
prefs.edit().putString("Default RileyLink", deviceAddress).commit();
Toast.makeText(this, "New RileyLink: " + deviceAddress, Toast.LENGTH_SHORT).show();
scanState = STATE_haveAddress;
refillFields();
// send device found
Intent intent = new Intent(RT2Const.INTENT_deviceSelected).putExtra("address",deviceAddress);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
//finish();
}

}

0 comments on commit c5d9a4d

Please sign in to comment.