summaryrefslogtreecommitdiff
path: root/app/src/main/java/com
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2021-03-26 15:45:44 +0100
committerJavier <dev.git@javispedro.com>2021-03-26 15:45:44 +0100
commit96577161813cacab5e47975942324feb92166e7c (patch)
treeb5f0455373fa836ad262fbf405fb9a9e7d2068af /app/src/main/java/com
parent4fafdb37fb23d12469cc1ac4b23efe9583f87407 (diff)
downloadrempe-96577161813cacab5e47975942324feb92166e7c.tar.gz
rempe-96577161813cacab5e47975942324feb92166e7c.zip
Support device removal by swiping left/right
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r--app/src/main/java/com/javispedro/rempe/Device.java15
-rw-r--r--app/src/main/java/com/javispedro/rempe/MainActivity.java64
2 files changed, 57 insertions, 22 deletions
diff --git a/app/src/main/java/com/javispedro/rempe/Device.java b/app/src/main/java/com/javispedro/rempe/Device.java
index d3a0d05..fed7291 100644
--- a/app/src/main/java/com/javispedro/rempe/Device.java
+++ b/app/src/main/java/com/javispedro/rempe/Device.java
@@ -59,13 +59,16 @@ public class Device {
public void close() {
Log.d(TAG, "close (" + mDeviceNumber + ")");
+ if (mEnvPcc != null) {
+ mEnvPcc.subscribeTemperatureDataEvent(null);
+ mEnvPcc.subscribeRssiEvent(null);
+ mEnvPcc.releaseAccess();
+ mEnvPcc = null;
+ }
if (mEnvPccHandle != null) {
mEnvPccHandle.close();
mEnvPccHandle = null;
}
- if (mEnvPcc != null) {
- mEnvPcc = null;
- }
mConnectResult = RequestAccessResult.SUCCESS;
mCurState = DeviceState.DEAD;
if (mObserver != null) {
@@ -88,6 +91,8 @@ public class Device {
private void setEnvPcc(AntPlusEnvironmentPcc envPcc) {
if (mEnvPcc != null) {
+ mEnvPcc.subscribeTemperatureDataEvent(null);
+ mEnvPcc.subscribeRssiEvent(null);
mEnvPcc.releaseAccess();
mEnvPcc = null;
}
@@ -130,7 +135,7 @@ public class Device {
return mConnectResult;
}
- private AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
+ private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
@Override
public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
Log.d(TAG, "onResultReceived resultCode=" + resultCode + " initialDeviceState=" + initialDeviceState);
@@ -149,7 +154,7 @@ public class Device {
}
};
- private AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
+ private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
@Override
public void onDeviceStateChange(DeviceState newDeviceState) {
Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
diff --git a/app/src/main/java/com/javispedro/rempe/MainActivity.java b/app/src/main/java/com/javispedro/rempe/MainActivity.java
index 2c29bc4..e4e6d6f 100644
--- a/app/src/main/java/com/javispedro/rempe/MainActivity.java
+++ b/app/src/main/java/com/javispedro/rempe/MainActivity.java
@@ -6,11 +6,13 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
+import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
@@ -31,16 +33,6 @@ public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
private SharedPreferences mPrefs = null;
- private SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
- @Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- switch (key) {
- case Preferences.PREFS_DEVICES:
- refreshDevices();
- break;
- }
- }
- };
private final ArrayList<Integer> mDeviceNumbers = new ArrayList<Integer>();
private final ArrayList<Device> mDevices = new ArrayList<Device>();
@@ -72,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
mDeviceListAdapter = new DeviceListRecyclerViewListAdapter();
mList.setAdapter(mDeviceListAdapter);
+ new ItemTouchHelper(mItemTouchHelperCallback).attachToRecyclerView(mList);
+
refreshDevices();
}
@@ -113,9 +107,6 @@ public class MainActivity extends AppCompatActivity {
int id = item.getItemId();
switch (id) {
- case R.id.action_remove_all:
- removeAllDevices();
- return true;
case R.id.action_settings:
return true;
}
@@ -132,6 +123,35 @@ public class MainActivity extends AppCompatActivity {
mSrlList.setRefreshing(false);
}
+ private final ItemTouchHelper.Callback mItemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
+ @Override
+ public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
+ final int from = viewHolder.getAdapterPosition();
+ final int to = viewHolder.getAdapterPosition();
+
+ Log.d(TAG, "onMove from=" + from + " to=" + to);
+
+ return false;
+ }
+
+ @Override
+ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
+ final int position = viewHolder.getAdapterPosition();
+ removeDeviceByPosition(position);
+ }
+ };
+
+ private final SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ switch (key) {
+ case Preferences.PREFS_DEVICES:
+ refreshDevices();
+ break;
+ }
+ }
+ };
+
public void searchForNewDevice() {
Log.d(TAG, "searchForNewDevice");
if (mPccSearchHandle != null) {
@@ -141,7 +161,7 @@ public class MainActivity extends AppCompatActivity {
mPccSearchHandle = AntPlusEnvironmentPcc.requestAccess(this, this, mResultReceiver, mDeviceStateChangeReceiver);
}
- private AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
+ private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
@Override
public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
Log.d(TAG, "onResultReceived resultCode=" + resultCode);
@@ -161,7 +181,7 @@ public class MainActivity extends AppCompatActivity {
}
};
- private AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
+ private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
@Override
public void onDeviceStateChange(DeviceState newDeviceState) {
Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
@@ -180,8 +200,18 @@ public class MainActivity extends AppCompatActivity {
Preferences.saveDeviceNumbers(mPrefs, list);
}
- public void removeAllDevices() {
- List<Integer> list = new ArrayList<Integer>();
+ public void removeDeviceByPosition(int position) {
+ Log.d(TAG, "removeDeviceByPosition position=" + position);
+ List<Integer> list = Preferences.getDeviceNumbers(mPrefs);
+ try {
+ list.remove(position);
+ } catch (IndexOutOfBoundsException ex) {
+ Log.w(TAG, ex);
+ }
+
+ Snackbar.make(mList,
+ getString(R.string.remove_device_done), Snackbar.LENGTH_SHORT).show();
+
Preferences.saveDeviceNumbers(mPrefs, list);
}