From 96577161813cacab5e47975942324feb92166e7c Mon Sep 17 00:00:00 2001 From: Javier Date: Fri, 26 Mar 2021 15:45:44 +0100 Subject: Support device removal by swiping left/right --- app/src/main/java/com/javispedro/rempe/Device.java | 15 +++-- .../java/com/javispedro/rempe/MainActivity.java | 64 ++++++++++++++++------ app/src/main/res/menu/menu_main.xml | 7 +-- app/src/main/res/values/strings.xml | 6 +- 4 files changed, 62 insertions(+), 30 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 mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver() { + private final AntPluginPcc.IPluginAccessResultReceiver mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver() { @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 mDeviceNumbers = new ArrayList(); private final ArrayList mDevices = new ArrayList(); @@ -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 mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver() { + private final AntPluginPcc.IPluginAccessResultReceiver mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver() { @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 list = new ArrayList(); + public void removeDeviceByPosition(int position) { + Log.d(TAG, "removeDeviceByPosition position=" + position); + List 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); } diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 5320159..b11af65 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -2,13 +2,10 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.javispedro.rempe.MainActivity"> - + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7db9d07..c6832b0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,16 +1,16 @@ Rempe - Remove all Settings - Add device... + Add device… Cannot connect to device: %1$s Device already on list + Device removed Device Status - -- + %1$s °C Last 24h: -- cgit v1.2.3