summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/rempe/MainActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/javispedro/rempe/MainActivity.java')
-rw-r--r--app/src/main/java/com/javispedro/rempe/MainActivity.java108
1 files changed, 77 insertions, 31 deletions
diff --git a/app/src/main/java/com/javispedro/rempe/MainActivity.java b/app/src/main/java/com/javispedro/rempe/MainActivity.java
index e4e6d6f..48fa5fd 100644
--- a/app/src/main/java/com/javispedro/rempe/MainActivity.java
+++ b/app/src/main/java/com/javispedro/rempe/MainActivity.java
@@ -18,11 +18,7 @@ import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
-import com.dsi.ant.plugins.antplus.pcc.AntPlusEnvironmentPcc;
-import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
-import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc;
-import com.dsi.ant.plugins.antplus.pccbase.PccReleaseHandle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
@@ -41,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout mSrlList;
private RecyclerView mList;
- private PccReleaseHandle<AntPlusEnvironmentPcc> mPccSearchHandle;
+ private Device mSearchDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -152,46 +148,67 @@ public class MainActivity extends AppCompatActivity {
}
};
- public void searchForNewDevice() {
- Log.d(TAG, "searchForNewDevice");
- if (mPccSearchHandle != null) {
- mPccSearchHandle.close();
- mPccSearchHandle = null;
- }
- mPccSearchHandle = AntPlusEnvironmentPcc.requestAccess(this, this, mResultReceiver, mDeviceStateChangeReceiver);
- }
-
- private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
+ private final Device.DeviceObserver mSearchDeviceObserver = new Device.DeviceObserver() {
@Override
- public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
- Log.d(TAG, "onResultReceived resultCode=" + resultCode);
- if (resultCode == RequestAccessResult.SUCCESS) {
- int deviceNumber = result.getAntDeviceNumber();
- result.releaseAccess();
- runOnUiThread(() -> addDevice(result.getAntDeviceNumber()));
- } else if (resultCode != RequestAccessResult.USER_CANCELLED) {
+ public void onDeviceSearchFinished(RequestAccessResult result) {
+ Log.d(TAG, "onDeviceSearchFinished result=" + result);
+ if (result == RequestAccessResult.SUCCESS) {
+ final Device device = mSearchDevice;
+ mSearchDevice.setObserver(null);
+ mSearchDevice = null; // Ownership of mSearchDevice passed to addDevice below
+ runOnUiThread(() -> addDevice(device));
+ } else if (result != RequestAccessResult.USER_CANCELLED) {
runOnUiThread(() -> {
- final String resultText = Device.connectionRequestAccessResultToString(MainActivity.this, resultCode);
+ final String resultText = Device.connectionRequestAccessResultToString(MainActivity.this, result);
Snackbar.make(findViewById(R.id.fabAddDevice),
getString(R.string.add_device_failed, resultText), Snackbar.LENGTH_INDEFINITE).show();
});
}
- mPccSearchHandle.close();
- mPccSearchHandle = null;
+ if (mSearchDevice != null) {
+ mSearchDevice.close();
+ mSearchDevice = null;
+ }
+ }
+
+ @Override
+ public void onDeviceInfoChanged() {
+
}
- };
- private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
@Override
- public void onDeviceStateChange(DeviceState newDeviceState) {
- Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
+ public void onDeviceStateChanged() {
+
+ }
+
+ @Override
+ public void onDeviceNewReading() {
+
+ }
+
+ @Override
+ public void onDeviceRssiChanged() {
+
}
};
+ public void searchForNewDevice() {
+ if (mSearchDevice != null) {
+ mSearchDevice.close();
+ }
+ mSearchDevice = new Device();
+ mSearchDevice.setObserver(mSearchDeviceObserver);
+ mSearchDevice.searchForDevice(this);
+ }
+
public void addDevice(int deviceNumber) {
- Log.d(TAG, "addDevice " + deviceNumber);
+ Log.d(TAG, "addDevice deviceNumber=" + deviceNumber);
+ if (deviceNumber == Device.INVALID_DEVICE) {
+ Log.e(TAG, "trying to add invalid device number; ignoring");
+ return;
+ }
List<Integer> list = Preferences.getDeviceNumbers(mPrefs);
if (list.contains(deviceNumber)) {
+ Log.w(TAG, "device already on list!");
Snackbar.make(findViewById(R.id.fabAddDevice),
getString(R.string.add_device_already), Snackbar.LENGTH_INDEFINITE).show();
return;
@@ -200,6 +217,33 @@ public class MainActivity extends AppCompatActivity {
Preferences.saveDeviceNumbers(mPrefs, list);
}
+ public void addDevice(Device device) {
+ final int deviceNumber = device.getDeviceNumber();
+ Log.d(TAG, "addDevice device.getDeviceNumber=" + deviceNumber);
+ if (deviceNumber == Device.INVALID_DEVICE) {
+ Log.e(TAG, "trying to add invalid device number; ignoring");
+ return;
+ }
+ if (mDeviceNumbers.contains(deviceNumber)) {
+ Log.w(TAG, "device already on list!");
+ Snackbar.make(findViewById(R.id.fabAddDevice),
+ getString(R.string.add_device_already), Snackbar.LENGTH_INDEFINITE).show();
+ device.close();
+ return;
+ }
+
+ mDevices.add(device);
+ mDeviceNumbers.add(deviceNumber);
+
+ if (mDeviceListAdapter != null) {
+ mDeviceListAdapter.notifyItemInserted(mDevices.size() - 1);
+ }
+
+ // Since we have manipulated mDeviceNumbers directly, even if we change the Prefs now
+ // Prefs listener should end up doing nothing
+ Preferences.saveDeviceNumbers(mPrefs, mDeviceNumbers);
+ }
+
public void removeDeviceByPosition(int position) {
Log.d(TAG, "removeDeviceByPosition position=" + position);
List<Integer> list = Preferences.getDeviceNumbers(mPrefs);
@@ -212,6 +256,7 @@ public class MainActivity extends AppCompatActivity {
Snackbar.make(mList,
getString(R.string.remove_device_done), Snackbar.LENGTH_SHORT).show();
+ // Prefs listener will take care of removing the device from mDevices
Preferences.saveDeviceNumbers(mPrefs, list);
}
@@ -265,7 +310,8 @@ public class MainActivity extends AppCompatActivity {
public void onInserted(int position, int count) {
for (int i = 0; i < count; ++i) {
final int deviceNumber = newDeviceNumbers.get(position + i);
- Device device = new Device(deviceNumber);
+ final String deviceName = getString(R.string.device_unnamed, deviceNumber);
+ Device device = new Device(deviceNumber, deviceName);
mDeviceNumbers.add(position + i, deviceNumber);
mDevices.add(position + i, device);
}