Skip to content

Commit

Permalink
Merge pull request #168 from kike-canaries/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
hpsaturn authored Jan 28, 2025
2 parents 52f94fe + 90c1afa commit 5e3531d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 96 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Java
uses: actions/setup-java@v2
uses: actions/setup-java@v4
with:
distribution: "temurin"
distribution: "zulu"
java-version: 17
cache: 'gradle'

- name: Loading submodules
run: git submodule update --init --recursive
Expand Down
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ dependencies {
implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1'
implementation 'com.jakewharton.rx2:replaying-share:2.2.0'
implementation 'com.polidea.rxandroidble2:rxandroidble:1.17.2'
implementation 'com.polidea.rxandroidble2:rxandroidble:1.19.0'

implementation 'com.github.pwittchen:reactivewifi-rx2:0.3.0'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.iamhabib:easy-preference:1.0.1'

implementation 'com.github.MKergall:osmbonuspack:6.5.2'
implementation 'io.nlopez.smartlocation:library:3.3.3'
Expand All @@ -97,10 +96,10 @@ dependencies {


testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:core:1.5.0'
testImplementation 'androidx.test:core:1.6.1'
testImplementation 'org.mockito:mockito-core:3.3.3'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test:runner:1.6.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}

apply plugin: 'com.google.gms.google-services'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
Expand All @@ -29,7 +30,6 @@
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar.Launcher"
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/hpsaturn/pollutionreporter/AppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import androidx.multidex.MultiDexApplication;

import com.google.firebase.database.FirebaseDatabase;
import com.iamhabib.easy_preference.EasyPreference;
import com.polidea.rxandroidble2.RxBleClient;
import com.polidea.rxandroidble2.internal.RxBleLog;

Expand All @@ -18,16 +17,19 @@
public class AppData extends MultiDexApplication{

private RxBleClient rxBleClient;
private EasyPreference.Builder prefBuilder;

public static RxBleClient getRxBleClient(Context context) {
AppData application = (AppData) context.getApplicationContext();
return application.rxBleClient;
}

public static EasyPreference.Builder getPrefBuilder(Context context){
AppData application = (AppData) context.getApplicationContext();
return application.prefBuilder;
// public static EasyPreference.Builder getPrefBuilder(Context context){
// AppData application = (AppData) context.getApplicationContext();
// return application.prefBuilder;
// }

public Context getContext() {
return this.getContext();
}

@Override
Expand All @@ -42,7 +44,8 @@ public void onCreate() {
RxJavaPlugins.setErrorHandler(throwable -> {});
rxBleClient = RxBleClient.create(this);
RxBleClient.setLogLevel(RxBleLog.VERBOSE);
prefBuilder = EasyPreference.with(this,"KEYS_APP_PREFERENCES");

// prefBuilder = EasyPreference.with(this,"KEYS_APP_PREFERENCES");
// AqicnApiManager.getInstance().init(this);
super.onCreate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
Expand All @@ -25,7 +26,7 @@
/**
* Created by Antonio Vanegas @hpsaturn on 7/1/18.
*/
public abstract class BaseActivity extends RxAppCompatActivity {
public abstract class BaseActivity extends AppCompatActivity {

public static String TAG = BaseActivity.class.getSimpleName();

Expand Down
36 changes: 17 additions & 19 deletions app/src/main/java/hpsaturn/pollutionreporter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.firebase.database.FirebaseDatabase;
import com.hpsaturn.tools.Logger;
import com.hpsaturn.tools.UITools;
import com.iamhabib.easy_preference.EasyPreference;
import com.yarolegovich.discretescrollview.DiscreteScrollView;
import com.yarolegovich.discretescrollview.transform.ScaleTransformer;

Expand All @@ -40,6 +39,7 @@
import butterknife.BindView;
import butterknife.ButterKnife;
import hpsaturn.pollutionreporter.common.Keys;
import hpsaturn.pollutionreporter.common.Storage;
import hpsaturn.pollutionreporter.models.ResponseConfig;
import hpsaturn.pollutionreporter.models.SensorData;
import hpsaturn.pollutionreporter.models.SensorTrackInfo;
Expand Down Expand Up @@ -90,7 +90,6 @@ public class MainActivity extends BaseActivity implements
DiscreteScrollView fragmentPicker;

private ScanFragment scanFragment;
private EasyPreference.Builder prefBuilder;
private ChartFragment chartFragment;
private RecordTrackManager recordTrackManager;
private MapFragment mapFragment;
Expand All @@ -110,7 +109,6 @@ protected void onCreate(Bundle savedInstanceState) {
startRecordTrackService();

ButterKnife.bind(this);
prefBuilder = AppData.getPrefBuilder(this);

startDataBase();
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -198,7 +196,7 @@ public void onSensorConfigWrite(String config) {
};

public boolean isRecording(){
return prefBuilder.getBoolean(Keys.SENSOR_RECORD, false);
return Storage.getBoolean(Keys.SENSOR_RECORD, false, this);
}

private void buttonRecordingAction () {
Expand All @@ -221,14 +219,14 @@ public void shareAction(String metadata, boolean isShare){

private void stopRecord() {
showSnackMessage(R.string.msg_record_stop);
prefBuilder.addBoolean(Keys.SENSOR_RECORD, false).save();
Storage.addBoolean(Keys.SENSOR_RECORD, false, this);
recordTrackManager.serviceRecordStop();
fabUpdateLayout();
}

private void startRecord() {
showSnackMessage(R.string.msg_record);
prefBuilder.addBoolean(Keys.SENSOR_RECORD, true).save();
Storage.addBoolean(Keys.SENSOR_RECORD, true, this);
recordTrackManager.serviceRecord();
fabUpdateLayout();
}
Expand Down Expand Up @@ -328,7 +326,7 @@ private void scrollToRecordsFragment() {

private void fabUpdateLayout() {
fab.hide();
if (prefBuilder.getBoolean(Keys.SENSOR_RECORD, false)) {
if (Storage.getBoolean(Keys.SENSOR_RECORD, false, this)) {
fab.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color.color_state_record_stop));
fab.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_stop_white_24dp));
} else {
Expand Down Expand Up @@ -420,7 +418,7 @@ public void stopRecordTrackService() {
}

public void deviceConnect() {
if (prefBuilder.getBoolean(Keys.DEVICE_PAIR, false)) {
if (Storage.getBoolean(Keys.DEVICE_PAIR, false, this)) {
startRecordTrackService();
showSnackMessage(R.string.msg_device_connecting);
}
Expand Down Expand Up @@ -462,15 +460,15 @@ public boolean isStorageGranted(){
int permissionState = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
return permissionState == PackageManager.PERMISSION_GRANTED;
// return prefBuilder.getBoolean(Keys.PERMISSION_STORAGE,false);
// return Storage.getBoolean(Keys.PERMISSION_STORAGE,false);
}

public boolean isGPSGranted(){
return prefBuilder.getBoolean(Keys.PERMISSION_BACKGROUND,false);
return Storage.getBoolean(Keys.PERMISSION_BACKGROUND,false, this);
}

public boolean isBLEGranted(){
return prefBuilder.getBoolean(Keys.PERMISSION_BLE,false);
return Storage.getBoolean(Keys.PERMISSION_BLE,false, this);
}

public void checkPreviousPermission() {
Expand Down Expand Up @@ -560,16 +558,16 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
break;
}
Log.i(TAG, "[PERM] User granted background location permission");
prefBuilder.addBoolean(Keys.PERMISSION_BACKGROUND, true).save();
Storage.addBoolean(Keys.PERMISSION_BACKGROUND, true, this);
buttonRecordingAction();
break;
case STORAGE:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
prefBuilder.addBoolean(Keys.PERMISSION_STORAGE, false).save();
Storage.addBoolean(Keys.PERMISSION_STORAGE, false, this);
Log.i(TAG, "[PERM] User denied WRITE_EXTERNAL_STORAGE permission.");
} else {
Log.i(TAG, "[PERM] User granted WRITE_EXTERNAL_STORAGE permission.");
prefBuilder.addBoolean(Keys.PERMISSION_STORAGE, true).save();
Storage.addBoolean(Keys.PERMISSION_STORAGE, true, this);
}
break;
case BLUETOOTH:
Expand All @@ -588,7 +586,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
break;
}
Log.i(TAG, "[PERM] User granted scan nearby devices permission");
prefBuilder.addBoolean(Keys.PERMISSION_BLE, true).save();
Storage.addBoolean(Keys.PERMISSION_BLE, true, this);
requestLocationPermission();

default:
Expand All @@ -597,7 +595,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}

public void performBLEScan() {
if(!isBLEGranted())prefBuilder.addBoolean(Keys.PERMISSION_BLE, true).save();
if(!isBLEGranted())Storage.addBoolean(Keys.PERMISSION_BLE, true, this);
if(scanFragment!=null)scanFragment.executeScan();
}

Expand All @@ -609,8 +607,8 @@ void actionUnPair() {
Logger.i(TAG, "[BLE] unpaired..");
stopRecord();
recordTrackManager.stop();
prefBuilder.addString(Keys.DEVICE_ADDRESS,"").save();
prefBuilder.addBoolean(Keys.DEVICE_PAIR, false).save();
Storage.addString(Keys.DEVICE_ADDRESS,"", this);
Storage.addBoolean(Keys.DEVICE_PAIR, false, this);
fab.hide();
if (chartFragment != null) chartFragment.clearData();
removeFragment(chartFragment);
Expand Down Expand Up @@ -638,7 +636,7 @@ protected void onDestroy() {
}

private boolean isPaired(){
return prefBuilder.getBoolean(Keys.DEVICE_PAIR, false);
return Storage.getBoolean(Keys.DEVICE_PAIR, false, this);
}

public boolean isDeviceConnected() {
Expand Down
29 changes: 14 additions & 15 deletions app/src/main/java/hpsaturn/pollutionreporter/common/BLEHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,32 @@

public class BLEHandler {

private static String TAG = BLEHandler.class.getSimpleName();
private static final String TAG = BLEHandler.class.getSimpleName();

private Context ctx;
private OnBLEConnectionListener listener;
private String address;
private final Context ctx;
private final OnBLEConnectionListener listener;
private final String address;

private UUID serviceId = UUID.fromString("c8d1d262-861f-4082-947e-f383a259aaf3");
private UUID charactSensorDataUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae01");
private UUID charactConfigUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae02");
private UUID charactStatusUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae03");
private final UUID serviceId = UUID.fromString("c8d1d262-861f-4082-947e-f383a259aaf3");
private final UUID charactSensorDataUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae01");
private final UUID charactConfigUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae02");
private final UUID charactStatusUuid = UUID.fromString("b0f332a8-a5aa-4f3f-bb43-f99e7791ae03");

private PublishSubject<Boolean> disconnectTriggerSubject = PublishSubject.create();
private final PublishSubject<Boolean> disconnectTriggerSubject = PublishSubject.create();
private Subscription connectionSubscription;
private Observable<RxBleConnection> connectionObservable;
private Disposable scanDisposable;
private RxBleClient rxBleClient;
private RxBleDevice bleDevice;
private final CompositeDisposable compositeDisposable = new CompositeDisposable();

public BLEHandler(Context ctx, String address, OnBLEConnectionListener listener) {
this.ctx = ctx;
this.address = address;
this.listener = listener;
RxBleClient rxBleClient = AppData.getRxBleClient(ctx);
bleDevice = rxBleClient.getBleDevice(address);
connectionObservable = prepareConnectionObservable();

}

public interface OnBLEConnectionListener {
Expand Down Expand Up @@ -75,10 +78,6 @@ public interface OnBLEConnectionListener {


public void connect() {
rxBleClient = AppData.getRxBleClient(ctx);
bleDevice = rxBleClient.getBleDevice(address);
connectionObservable = prepareConnectionObservable();


if (isConnected()) {
triggerDisconnect();
Expand Down Expand Up @@ -116,7 +115,7 @@ public void setupNotification() {
if (isConnected()) {
final Disposable disposable = connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(charactSensorDataUuid))
.doOnNext(notificationObservable -> { notificationHasBeenSetUp(); })
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
Expand Down
24 changes: 23 additions & 1 deletion app/src/main/java/hpsaturn/pollutionreporter/common/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -115,4 +114,27 @@ public static List<String> getTempAPList(Context ctx) {
}
}

public static boolean getBoolean(String key, boolean value, Context ctx) {
SharedPreferences preferences = ctx.getSharedPreferences(KEYS_TRACKS_PREFERENCES,0);
return preferences.getBoolean(key, value);
}

public static void addBoolean(String key, boolean value, Context ctx) {
SharedPreferences preferences = ctx.getSharedPreferences(KEYS_TRACKS_PREFERENCES,0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}

public static void addString(String key, String value, Context ctx) {
SharedPreferences preferences = ctx.getSharedPreferences(KEYS_TRACKS_PREFERENCES,0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}

public static String getString(String key, String value, Context ctx) {
SharedPreferences preferences = ctx.getSharedPreferences(KEYS_TRACKS_PREFERENCES,0);
return preferences.getString(key, value);
}
}
Loading

0 comments on commit 5e3531d

Please sign in to comment.