Skip to content

Commit

Permalink
removed EasyPreferences library. Instead by own implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hpsaturn committed Jan 27, 2025
1 parent aaff483 commit b221b82
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 66 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ dependencies {
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.iamhabib:easy-preference:1.0.1'

implementation 'com.github.MKergall:osmbonuspack:6.5.2'
implementation 'io.nlopez.smartlocation:library:3.3.3'
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
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
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.fonfon.geohash.GeoHash;
import com.google.gson.Gson;
import com.hpsaturn.tools.FileTools;
import com.hpsaturn.tools.Logger;
import com.hpsaturn.tools.UITools;
import com.iamhabib.easy_preference.EasyPreference;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import hpsaturn.pollutionreporter.AppData;
import hpsaturn.pollutionreporter.Config;
import hpsaturn.pollutionreporter.MainActivity;
import hpsaturn.pollutionreporter.R;
Expand All @@ -50,7 +45,6 @@ public class RecordTrackService extends Service {

private static final String TAG = RecordTrackService.class.getSimpleName();
private static final boolean VERBOSE = Config.DEBUG && false;
private EasyPreference.Builder prefBuilder;
private BLEHandler bleHandler;
private boolean isRecording;
private RecordTrackManager recordTrackManager;
Expand All @@ -64,15 +58,14 @@ public class RecordTrackService extends Service {
public void onCreate() {
super.onCreate();
Logger.i(TAG, "[BLE] Creating Service container..");
prefBuilder = AppData.getPrefBuilder(this);
isRecording = prefBuilder.getBoolean(Keys.SENSOR_RECORD, false);
isRecording = Storage.getBoolean(Keys.SENSOR_RECORD, false, this);
if (isRecording) restoreValues();
recordTrackManager = new RecordTrackManager(this, managerListener);
notificationSetup();
}

private void startConnection() {
if (prefBuilder.getBoolean(Keys.DEVICE_PAIR, false)) {
if (Storage.getBoolean(Keys.DEVICE_PAIR, false, this)) {
if (bleHandler == null) {
connect();
}
Expand All @@ -86,7 +79,7 @@ else if (bleHandler.isConnected()) {

private void connect() {
Logger.i(TAG, "[BLE] starting BLE connection..");
String macAddress = prefBuilder.getString(Keys.DEVICE_ADDRESS, "");
String macAddress = Storage.getString(Keys.DEVICE_ADDRESS, "", this);
Logger.i(TAG, "[BLE] deviceConnect to " + macAddress);
bleHandler = new BLEHandler(this, macAddress, bleListener);
bleHandler.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.hpsaturn.tools.Logger;
import com.hpsaturn.tools.UITools;
import com.iamhabib.easy_preference.EasyPreference;
import com.polidea.rxandroidble2.RxBleClient;
import com.polidea.rxandroidble2.exceptions.BleScanException;
import com.polidea.rxandroidble2.scan.ScanFilter;
Expand All @@ -33,6 +32,7 @@
import hpsaturn.pollutionreporter.PermissionUtil;
import hpsaturn.pollutionreporter.R;
import hpsaturn.pollutionreporter.common.Keys;
import hpsaturn.pollutionreporter.common.Storage;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

Expand Down Expand Up @@ -162,9 +162,9 @@ private void onDeviceConnectClick(ScanResult scanResults) {
String macAddress = scanResults.getBleDevice().getMacAddress();
String deviceName = scanResults.getBleDevice().getName();
Logger.i(TAG, "onDeviceConnectClick: "+deviceName+ " " + macAddress);
EasyPreference.Builder prefBuilder = AppData.getPrefBuilder(getContext());
prefBuilder.addString(Keys.DEVICE_ADDRESS, macAddress).save();
prefBuilder.addBoolean(Keys.DEVICE_PAIR, true).save();
// EasyPreference.Builder Storage = AppData.getPrefBuilder(getContext());
Storage.addString(Keys.DEVICE_ADDRESS, macAddress, requireContext());
Storage.addBoolean(Keys.DEVICE_PAIR, true, requireContext());
getMain().deviceConnect();
getMain().removeScanFragment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.hpsaturn.tools.Logger;
import com.hpsaturn.tools.UITools;
import com.iamhabib.easy_preference.EasyPreference;

import org.apache.commons.lang3.math.NumberUtils;

import java.util.TimerTask;

import hpsaturn.pollutionreporter.AppData;
import hpsaturn.pollutionreporter.Config;
import hpsaturn.pollutionreporter.R;
import hpsaturn.pollutionreporter.common.Keys;
Expand All @@ -29,6 +27,7 @@
import hpsaturn.pollutionreporter.models.ResponseConfig;
import hpsaturn.pollutionreporter.models.SensorConfig;
import hpsaturn.pollutionreporter.models.WifiConfig;
import hpsaturn.pollutionreporter.common.Storage;
import io.nlopez.smartlocation.SmartLocation;

/**
Expand Down Expand Up @@ -329,20 +328,18 @@ else if (geoHash.length() >=2 ) {
try {
summary = summary + geoHash;
ifxdbSwitch.setSummary(R.string.summary_key_enable_ifx_ready);
EasyPreference.Builder prefBuilder = AppData.getPrefBuilder(getContext());
String name = geoHash.substring(0,3);
String flavor = prefBuilder.getString(Keys.DEVICE_FLAVOR,"");
String flavor = Storage.getString(Keys.DEVICE_FLAVOR,"", requireContext());
if (flavor.length()>6) flavor = flavor.substring(0,7);
name = name + flavor;
name = name + prefBuilder.getString(Keys.DEVICE_ADDRESS,"").substring(10);
name = name + Storage.getString(Keys.DEVICE_ADDRESS,"", requireContext()).substring(10);
name = name.replace("_","");
name = name.replace(":","");
name = name.toUpperCase();
name = getString(R.string.summary_fixed_stations_map)+" "+name;
updateSummary(R.string.key_fixed_stations_map,name);
} catch (Exception e) {
EasyPreference.Builder prefBuilder = AppData.getPrefBuilder(getContext());
String flavor = prefBuilder.getString(Keys.DEVICE_FLAVOR,"");
String flavor = Storage.getString(Keys.DEVICE_FLAVOR,"", requireContext());
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
crashlytics.setCustomKey(Keys.DEVICE_FLAVOR,flavor);
crashlytics.setCustomKey("geohash",geoHash);
Expand Down
Loading

0 comments on commit b221b82

Please sign in to comment.