Skip to content

Commit

Permalink
Added preference to vibrate when receiving data from scale
Browse files Browse the repository at this point in the history
Defaults to true
 User configurable in general preferences
 Added 2 strings info_bluetooth_retrieve_data_successful and label_vibrate_on_measurement
 Added permission android.permission.VIBRATE
Fixes oliexdev#1097
  • Loading branch information
jontyms committed Jan 1, 2025
1 parent a44df16 commit 853176b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions android_app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.VIBRATE" />

<!-- Permission to allow read of data from the database through a ContentProvider.
Marked "dangerous" so that explicit user approval is required to read this data, not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.text.Editable;
Expand Down Expand Up @@ -116,6 +118,7 @@ public class MainActivity extends AppCompatActivity
private NavigationView navigationView;
private BottomNavigationView navigationBottomView;


private boolean settingsActivityRunning = false;

public static Context createBaseContext(Context context) {
Expand Down Expand Up @@ -715,7 +718,7 @@ public void handleMessage(Message msg) {
case RETRIEVE_SCALE_DATA:
setBluetoothStatusIcon(R.drawable.ic_bluetooth_connection_success);
ScaleMeasurement scaleBtData = (ScaleMeasurement) msg.obj;

Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_retrieve_data_successful), Toast.LENGTH_SHORT).show();
OpenScale openScale = OpenScale.getInstance();

if (prefs.getBoolean("mergeWithLastMeasurement", true)) {
Expand All @@ -725,6 +728,22 @@ public void handleMessage(Message msg) {
}
}


if (prefs.getBoolean("vibrateOnMeasurement", true)) {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
// Check Android version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// New vibrate method for API 26 and above
VibrationEffect effect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
} else {
// Deprecated vibrate method for API 25 and below
vibrator.vibrate(500);
}
}
}

openScale.addScaleMeasurement(scaleBtData, true);
break;
case INIT_PROCESS:
Expand Down
2 changes: 2 additions & 0 deletions android_app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<string name="info_bluetooth_connection_lost">Bluetooth connection lost</string>
<string name="info_bluetooth_no_device">No Bluetooth device found</string>
<string name="info_bluetooth_no_device_retrying">Could not establish connection, retrying…</string>
<string name="info_bluetooth_retrieve_data_successful">Successfully retrieved data from scale</string>">
<string name="info_bluetooth_no_device_set">Select a Bluetooth device</string>
<string name="info_bluetooth_connection_successful">Connected</string>
<string name="info_bluetooth_init">Initialize Bluetooth device</string>
Expand All @@ -114,6 +115,7 @@
<string name="question_really_delete_user">Delete user?</string>
<string name="label_bluetooth_title">Bluetooth</string>
<string name="label_bluetooth_enable">Connect to scale on startup</string>
<string name="label_vibrate_on_measurement">Vibrate on Measurement</string>
<string name="label_mergeWithLastMeasurement">Merge with last measurement</string>
<string name="label_bluetooth_searching">Searching for your Bluetooth scale</string>
<string name="label_bluetooth_searching_finished">Search finished</string>
Expand Down
10 changes: 8 additions & 2 deletions android_app/app/src/main/res/xml/general_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
app:useSimpleSummaryProvider="true"
android:title="@string/label_theme" />
<CheckBoxPreference
android:defaultValue="true"
android:key="deleteConfirmationEnable"
android:summaryOff="@string/info_is_not_enable"
android:summaryOn="@string/info_is_enable"
android:title="@string/label_delete_confirmation" />
android:title="@string/label_delete_confirmation"
app:defaultValue="true" />
<CheckBoxPreference
android:defaultValue="true"
android:key="vibrateOnMeasurement"
android:summaryOff="@string/info_is_not_enable"
android:summaryOn="@string/info_is_enable"
android:title="@string/label_vibrate_on_measurement" />
</PreferenceScreen>

0 comments on commit 853176b

Please sign in to comment.