This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
[WIP][RFC] Introduce a ContentProvider for HR Data #1138
Open
boun
wants to merge
21
commits into
Freeyourgadget:master
Choose a base branch
from
boun:runnerup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
86038a7
Fix Gradle Errors
1d1cd00
[WIP] Introduce a ContentProvider for HR Data
597076b
Fix Derp
7c3ee13
Squashed commit of the following:
8a03ff0
Inroduce a CONNECTING state in the state machine
e9b1a39
Extend the Contract Class
c32763d
Rename contentprovider
1fc49e9
Update Copyright Header
ac46be1
Change Logger to slf4j
c7331e5
Remove TODO -- copy it to pull request
9cd2f8e
Repeatedly punch .onEnableRealtimeHeartRateMeasurement
6269b41
[DO NOT MERGE] Comments
253c084
Rename Contentprovider + Introduce Permissions
f80e947
Fix Equals
d01e81b
Really Change Logger to slf4j :-)
26017dc
Use the correct broadcast receiver
89b0a35
Remove Android specific URI from ContractClass
3f451f2
Cleanup
7de5284
Revert "[DO NOT MERGE] Comments"
019edf3
Rename Methods
e51cf66
Remove onCreate guard, as Robolectric seems to have evolved
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* Copyright (C) 2016-2018 Andreas Shimokawa, Carsten Pfeiffer | ||
/* Copyright (C) 2018 Benedikt Elser | ||
|
||
This file is part of Gadgetbridge. | ||
|
||
|
@@ -31,6 +31,9 @@ | |
import android.support.v4.content.LocalBroadcastManager; | ||
import android.util.Log; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
import nodomain.freeyourgadget.gadgetbridge.GBApplication; | ||
|
@@ -42,21 +45,16 @@ | |
/** | ||
* A content Provider, which publishes read only RAW @see ActivitySample to other applications | ||
* <p> | ||
* TODO: | ||
* - Contract Class | ||
* - Permission System to read HR Data | ||
* - Fix Travis | ||
* - Check if the Device is really connected - connect and disconnect | ||
* (Is the Selected device the current connected device??) | ||
*/ | ||
public class HRContentProvider extends ContentProvider { | ||
private static final Logger LOG = LoggerFactory.getLogger(HRContentProvider.class); | ||
|
||
private static final int DEVICES_LIST = 1; | ||
private static final int REALTIME = 2; | ||
private static final int ACTIVITY_START = 3; | ||
private static final int ACTIVITY_STOP = 4; | ||
|
||
enum provider_state {ACTIVE, INACTIVE}; | ||
enum provider_state {ACTIVE, CONNECTING, INACTIVE}; | ||
provider_state state = provider_state.INACTIVE; | ||
|
||
private static final UriMatcher URI_MATCHER; | ||
|
@@ -82,17 +80,20 @@ enum provider_state {ACTIVE, INACTIVE}; | |
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
String action = intent.getAction(); | ||
//Log.i(HRContentProvider.class.getName(), "Received Event, aciton: " + action); | ||
//LOG.info(HRContentProvider.class.getName(), "Received Event, aciton: " + action); | ||
|
||
switch (action) { | ||
case GBDevice.ACTION_DEVICE_CHANGED: | ||
mGBDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); | ||
|
||
LOG.debug(HRContentProvider.class.toString(), "Got Device " + mGBDevice); | ||
// Rationale: If device was not connected | ||
// it should show up here after beeing connected | ||
// If the user wanted to switch on realtime traffic, but we first needed to connect it | ||
// we do it here | ||
if (state == provider_state.ACTIVE) { | ||
if (mGBDevice.isConnected() && state == provider_state.CONNECTING) { | ||
LOG.debug(HRContentProvider.class.toString(), "Device connected now, enabling realtime " + mGBDevice); | ||
|
||
state = provider_state.ACTIVE; | ||
GBApplication.deviceService().onEnableRealtimeSteps(true); | ||
GBApplication.deviceService().onEnableRealtimeHeartRateMeasurement(true); | ||
} | ||
|
@@ -117,7 +118,7 @@ public void onReceive(Context context, Intent intent) { | |
|
||
@Override | ||
public boolean onCreate() { | ||
Log.i(HRContentProvider.class.getName(), "Creating..."); | ||
LOG.info(HRContentProvider.class.getName(), "Creating..."); | ||
IntentFilter filterLocal = new IntentFilter(); | ||
|
||
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED); | ||
|
@@ -136,7 +137,7 @@ public boolean onCreate() { | |
|
||
@Override | ||
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | ||
//Log.i(HRContentProvider.class.getName(), "query uri " + uri.toString()); | ||
//LOG.info(HRContentProvider.class.getName(), "query uri " + uri.toString()); | ||
MatrixCursor mc; | ||
|
||
DeviceManager deviceManager; | ||
|
@@ -147,18 +148,20 @@ public Cursor query(@NonNull Uri uri, String[] projection, String selection, Str | |
if (l == null) { | ||
return null; | ||
} | ||
Log.i(HRContentProvider.class.getName(), String.format("listing %d devices", l.size())); | ||
LOG.info(HRContentProvider.class.getName(), String.format("listing %d devices", l.size())); | ||
|
||
mc = new MatrixCursor(HRContentProviderContract.deviceColumnNames); | ||
for (GBDevice dev : l) { | ||
mc.addRow(new Object[]{dev.getName(), dev.getModel(), dev.getAddress()}); | ||
} | ||
return mc; | ||
case ACTIVITY_START: | ||
this.state = provider_state.ACTIVE; | ||
|
||
this.state = provider_state.CONNECTING; | ||
LOG.info(HRContentProvider.class.getName(), "Get ACTIVTY START"); | ||
GBDevice targetDevice = getDevice((selectionArgs != null) ? selectionArgs[0] : ""); | ||
if (targetDevice != null && targetDevice.isConnected()) { | ||
Log.i(HRContentProvider.class.getName(), "Get ACTIVTY START"); | ||
this.state = provider_state.ACTIVE; | ||
GBApplication.deviceService().onEnableRealtimeSteps(true); | ||
GBApplication.deviceService().onEnableRealtimeHeartRateMeasurement(true); | ||
mc = new MatrixCursor(HRContentProviderContract.activityColumnNames); | ||
|
@@ -173,15 +176,15 @@ public Cursor query(@NonNull Uri uri, String[] projection, String selection, Str | |
case ACTIVITY_STOP: | ||
this.state = provider_state.INACTIVE; | ||
|
||
Log.i(HRContentProvider.class.getName(), "Get ACTIVITY STOP"); | ||
LOG.info(HRContentProvider.class.getName(), "Get ACTIVITY STOP"); | ||
GBApplication.deviceService().onEnableRealtimeSteps(false); | ||
GBApplication.deviceService().onEnableRealtimeHeartRateMeasurement(false); | ||
mc = new MatrixCursor(HRContentProviderContract.activityColumnNames); | ||
mc.addRow(new String[]{"OK", "No error"}); | ||
return mc; | ||
case REALTIME: | ||
//String sample_string = (buffered_sample == null) ? "" : buffered_sample.toString(); | ||
//Log.e(HRContentProvider.class.getName(), String.format("Get REALTIME buffered sample %s", sample_string)); | ||
//LOG.error(HRContentProvider.class.getName(), String.format("Get REALTIME buffered sample %s", sample_string)); | ||
mc = new MatrixCursor(HRContentProviderContract.realtimeColumnNames); | ||
if (buffered_sample != null) | ||
mc.addRow(new Object[]{"OK", buffered_sample.getHeartRate(), buffered_sample.getSteps(), mGBDevice != null ? mGBDevice.getBatteryLevel() : 99}); | ||
|
@@ -195,20 +198,27 @@ public Cursor query(@NonNull Uri uri, String[] projection, String selection, Str | |
@Nullable | ||
private GBDevice getDevice(String deviceAddress) { | ||
DeviceManager deviceManager; | ||
|
||
if (mGBDevice != null && mGBDevice.getAddress() == deviceAddress) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be equals(), not == |
||
LOG.info(HRContentProvider.class.getName(), String.format("Found device mGBDevice %s", mGBDevice)); | ||
|
||
return mGBDevice; | ||
} | ||
|
||
deviceManager = ((GBApplication) (this.getContext())).getDeviceManager(); | ||
for (GBDevice device : deviceManager.getDevices()) { | ||
if (deviceAddress.equals(device.getAddress())) { | ||
Log.i(HRContentProvider.class.getName(), String.format("Found device device %s", device)); | ||
LOG.info(HRContentProvider.class.getName(), String.format("Found device device %s", device)); | ||
return device; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the slf4j logger instead of Log.i, Log, e, etc. |
||
} | ||
} | ||
Log.i(HRContentProvider.class.getName(), String.format("Did not find device returning selected %s", deviceManager.getSelectedDevice())); | ||
LOG.info(HRContentProvider.class.getName(), String.format("Did not find device returning selected %s", deviceManager.getSelectedDevice())); | ||
return deviceManager.getSelectedDevice(); | ||
} | ||
|
||
@Override | ||
public String getType(@NonNull Uri uri) { | ||
Log.e(HRContentProvider.class.getName(), "getType uri " + uri); | ||
LOG.error(HRContentProvider.class.getName(), "getType uri " + uri); | ||
return null; | ||
} | ||
|
||
|
34 changes: 30 additions & 4 deletions
34
.../java/nodomain/freeyourgadget/gadgetbridge/contentprovider/HRContentProviderContract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
/* Copyright (C) 2018 Benedikt Elser | ||
|
||
This file is part of Gadgetbridge. | ||
|
||
Gadgetbridge is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
Gadgetbridge is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
|
||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
package nodomain.freeyourgadget.gadgetbridge.contentprovider; | ||
|
||
import android.net.Uri; | ||
|
||
public final class HRContentProviderContract { | ||
public static final String[] deviceColumnNames = new String[]{"Name", "Model", "Address"}; | ||
public static final String[] activityColumnNames = new String[]{"Status", "Message"}; | ||
public static final String[] realtimeColumnNames = new String[]{"Status", "Heartrate", "Steps", "Battery"}; | ||
|
||
static final String AUTHORITY = "com.gadgetbridge.heartrate.provider"; | ||
public static final String COLUMN_STATUS = "Status"; | ||
public static final String COLUMN_NAME = "Name"; | ||
public static final String COLUMN_ADDRESS = "Address"; | ||
public static final String COLUMN_MODEL = "Model"; | ||
public static final String COLUMN_MESSAGE = "Message"; | ||
public static final String COLUMN_HEARTRATE = "HeartRate"; | ||
public static final String COLUMN_STEPS = "Steps"; | ||
public static final String COLUMN_BATTERY = "Battery"; | ||
|
||
public static final String[] deviceColumnNames = new String[]{COLUMN_NAME, COLUMN_MODEL, COLUMN_ADDRESS}; | ||
public static final String[] activityColumnNames = new String[]{COLUMN_STATUS, COLUMN_MESSAGE}; | ||
public static final String[] realtimeColumnNames = new String[]{COLUMN_STATUS, COLUMN_HEARTRATE, COLUMN_STEPS, COLUMN_BATTERY}; | ||
|
||
static final String AUTHORITY = "org.gadgetbridge.realtimesamples.provider"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. com.gadgetbridge again.. |
||
static final String ACTIVITY_START_URL = "content://" + AUTHORITY + "/activity_start"; | ||
static final String ACTIVITY_STOP_URL = "content://" + AUTHORITY + "/activity_stop"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import org.junit.Test; | ||
|
||
|
@@ -36,9 +35,12 @@ | |
|
||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.shadows.ShadowContentResolver; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
public class SampleProviderTest extends TestBase { | ||
private static final Logger LOG = LoggerFactory.getLogger(SampleProviderTest.class); | ||
|
||
private GBDevice dummyGBDevice; | ||
private ContentResolver mContentResolver; | ||
|
@@ -53,7 +55,7 @@ public void setUp() throws Exception { | |
// Stuff context into provider | ||
provider.attachInfo(app.getApplicationContext(), null); | ||
|
||
ShadowContentResolver.registerProviderInternal("com.gadgetbridge.heartrate.provider", provider); | ||
ShadowContentResolver.registerProviderInternal("org.gadgetbridge.realtimesamples.provider", provider); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. com.gadgetbr... |
||
|
||
@Test | ||
|
@@ -204,7 +206,7 @@ private void generateSampleStream(MiBandSampleProvider sampleProvider) { | |
|
||
for (int i = 0; i < 10; i++) { | ||
MiBandActivitySample sample = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 100 + i * 50, 10, 60 + i * 5, 1000 * i, user, device); | ||
//Log.d(SampleProviderTest.class.getName(), "Sending sample " + sample.toString()); | ||
//LOG.debug(SampleProviderTest.class.getName(), "Sending sample " + sample.toString()); | ||
Intent intent = new Intent(DeviceService.ACTION_REALTIME_SAMPLES) | ||
.putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample); | ||
app.sendBroadcast(intent); | ||
|
@@ -286,15 +288,15 @@ class A1 extends ContentObserver { | |
@Override | ||
public void onChange(boolean selfChange, Uri uri) { | ||
super.onChange(selfChange, uri); | ||
//Log.e(SampleProviderTest.class.getName(), "Changed " + uri.toString()); | ||
//LOG.error(SampleProviderTest.class.getName(), "Changed " + uri.toString()); | ||
Cursor cursor = mContentResolver.query(HRContentProviderContract.REALTIME_URI, null, null, null, null); | ||
if (cursor.moveToFirst()) { | ||
do { | ||
String status = cursor.getString(0); | ||
int heartRate = cursor.getInt(1); | ||
assertEquals("OK", status); | ||
assertEquals(60 + 5*numObserved, heartRate); | ||
Log.i("test", "HeartRate " + heartRate); | ||
LOG.info("test", "HeartRate " + heartRate); | ||
} while (cursor.moveToNext()); | ||
} | ||
numObserved++; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, but I still don't get the rationale: why org.gadgetbridge instead of nodomain.freeyourgadget.gadgetbridge? I'm not totally opposed, but I'd like to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I misunderstood you. I'll finally change it to nodomain.freeyourgadget.gadgetbridge