From 6ea9537d38e66f9c7173ae3f664774859502e0ea Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 1 Jun 2015 16:57:45 +0200 Subject: [PATCH] Start sleep activity with context menu (long press on a device) This allows to see sleep data even if devices are not connected. --- .../gadgetbridge/AppManagerActivity.java | 5 --- .../gadgetbridge/ControlCenter.java | 32 +++++++++++++++++++ .../gadgetbridge/SleepMonitorActivity.java | 22 ++++++++++++- .../main/res/menu/controlcenter_context.xml | 6 ++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 app/src/main/res/menu/controlcenter_context.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java index 6cb867614..cabc7c2b6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter; -import nodomain.freeyourgadget.gadgetbridge.pebble.MorpheuzSupport; public class AppManagerActivity extends Activity { @@ -73,10 +72,6 @@ public class AppManagerActivity extends Activity { startAppIntent.setAction(BluetoothCommunicationService.ACTION_STARTAPP); startAppIntent.putExtra("app_uuid", uuid.toString()); startService(startAppIntent); - if (MorpheuzSupport.uuid.equals(uuid)) { - Intent startIntent = new Intent(AppManagerActivity.this, SleepMonitorActivity.class); - startActivity(startIntent); - } } }); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index e01d9be9e..cfa192d28 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -11,6 +11,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; +import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -39,6 +40,8 @@ public class ControlCenter extends Activity { TextView hintTextView; ListView deviceListView; GBDeviceAdapter mGBDeviceAdapter; + private GBDevice selectedDevice = null; + final List deviceList = new ArrayList<>(); private BroadcastReceiver mReceiver = new BroadcastReceiver() { @@ -96,6 +99,8 @@ public class ControlCenter extends Activity { } }); + registerForContextMenu(deviceListView); + IntentFilter filterLocal = new IntentFilter(); filterLocal.addAction(ACTION_QUIT); filterLocal.addAction(ACTION_REFRESH_DEVICELIST); @@ -140,6 +145,33 @@ public class ControlCenter extends Activity { startService(versionInfoIntent); } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + getMenuInflater().inflate( + R.menu.controlcenter_context, menu); + AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo; + selectedDevice = deviceList.get(acmi.position); + menu.setHeaderTitle(selectedDevice.getName()); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.controlcenter_start_sleepmonitor: + if (selectedDevice != null) { + Intent startIntent = new Intent(ControlCenter.this, SleepMonitorActivity.class); + startIntent.putExtra("device", selectedDevice); + startActivity(startIntent); + } + return true; + default: + return super.onContextItemSelected(item); + } + } + + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java index 60e0231db..becefb4f4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java @@ -38,6 +38,7 @@ public class SleepMonitorActivity extends Activity implements SurfaceHolder.Call private int mSmartAlarmTo = -1; private int mTimestampFrom = -1; private int mSmartAlarmGoneOff = -1; + private GBDevice mGBDevice = null; private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -56,12 +57,25 @@ public class SleepMonitorActivity extends Activity implements SurfaceHolder.Call }; private void refresh() { + if (mGBDevice == null) { + return; + } + if (mTimestampFrom == -1) { Long ts = System.currentTimeMillis(); mTimestampFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours } - ArrayList samples = GBApplication.getActivityDatabaseHandler().getGBActivitySamples(mTimestampFrom, -1, (byte) 1); + byte provider = -1; + switch (mGBDevice.getType()) { + case MIBAND: + provider = GBActivitySample.PROVIDER_MIBAND; + break; + case PEBBLE: + provider = GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ; // FIXME + break; + } + ArrayList samples = GBApplication.getActivityDatabaseHandler().getGBActivitySamples(mTimestampFrom, -1, provider); Calendar cal = Calendar.getInstance(); Date date; String dateStringFrom = ""; @@ -129,6 +143,12 @@ public class SleepMonitorActivity extends Activity implements SurfaceHolder.Call @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + Bundle extras = getIntent().getExtras(); + if (extras != null) { + mGBDevice = extras.getParcelable("device"); + } + setContentView(R.layout.activity_sleepmonitor); textView = (TextView) findViewById(R.id.textView); diff --git a/app/src/main/res/menu/controlcenter_context.xml b/app/src/main/res/menu/controlcenter_context.xml new file mode 100644 index 000000000..b9e35f9d1 --- /dev/null +++ b/app/src/main/res/menu/controlcenter_context.xml @@ -0,0 +1,6 @@ + + + + \ 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 e494086a6..d297f632e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ Settings Debug Quit + Sleep Monitor (ALPHA) Debug