Skip to content

Commit

Permalink
Merge pull request #149 from kike-canaries/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
hpsaturn authored Mar 22, 2022
2 parents d2232cf + c5ad286 commit 41635a4
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 90 deletions.
33 changes: 27 additions & 6 deletions app/src/main/java/hpsaturn/pollutionreporter/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hpsaturn.pollutionreporter;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
Expand All @@ -9,6 +10,8 @@
import android.view.MenuItem;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
Expand All @@ -24,20 +27,39 @@
public abstract class BaseActivity extends RxAppCompatActivity {

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

private void enableBLE() {
Logger.i(TAG,"[BLE] enableBLE: starting...");

ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {

if (result.getResultCode() == Activity.RESULT_OK) {
//Intent data = result.getData();
Logger.i(TAG,"[BLE] enableBLE: enabled!");
} else {
Logger.i(TAG,"[BLE] enableBLE: not enabled!");
}
});

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
someActivityResultLauncher.launch(enableBtIntent);
}

public void checkBluetoohtBle() {
public void checkBLE() {
// Use this check to determine whether BLE is supported on the device.
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.msg_ble_not_supported, Toast.LENGTH_SHORT).show();
} else if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 0);
this.enableBLE();
}
else Logger.i(TAG,"[BLE] checkBluetoohtBle: ready!");
else Logger.i(TAG,"[BLE] checkBLE: ready!");
}


public void showFragment(Fragment fragment){
if(fragment!=null) {
try {
Expand Down Expand Up @@ -187,8 +209,7 @@ public boolean isFragmentInStack(String tag) {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) return false;
FragmentManager fm = getSupportFragmentManager();
Fragment match = fm.findFragmentByTag(tag);
if (match != null) return true;
else return false;
return match != null;
} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void onCreate(Bundle savedInstanceState) {

startDataBase();
setSupportActionBar(toolbar);
checkBluetoohtBle();
checkBLE();
setupUI();
recordTrackManager = new RecordTrackManager(this, recordTrackListener);

Expand Down Expand Up @@ -433,7 +433,8 @@ private void startDataBase(){
}

private View.OnClickListener onFabClickListener = view -> {
if(!isGPSGranted() || !isBLEGranted()) showDisclosureFragment(R.string.msg_gps_title,R.string.msg_gps_desc,R.drawable.ic_bicycle);
if(!isGPSGranted() || !isBLEGranted())
showDisclosureFragment(R.string.msg_gps_title,R.string.msg_gps_desc,R.drawable.ic_bicycle);
else startPermissionsGPSFlow();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static void setSensorData(Context ctx, ArrayList<SensorData> items) {
}

public static ArrayList<SensorData> getSensorData(Context ctx) {
if (ctx == null)
return new ArrayList<>();

SharedPreferences preferences = ctx.getSharedPreferences(KEYS_TRACKS_PREFERENCES,0);
String ringJson = preferences.getString(Keys.SENSOR_DATA, "");
if (ringJson.equals("")) return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public String getDate() {
return date;
}

public void setDate(String email) {
this.date = email;
public void setDate(String date) {
this.date = date;
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import butterknife.BindView;
Expand Down Expand Up @@ -257,7 +258,7 @@ private void calculateReferenceTime(){
*/
private void addData(ArrayList<SensorData> data){
if(data==null){
chart.setNoDataText("No data.");
chart.setNoDataText(getString(R.string.msg_chart_no_data));
loadingData = false;
return;
}
Expand Down Expand Up @@ -349,19 +350,19 @@ private void updateMap() {

private void setTrackDescription(SensorTrack track, boolean isPublishedData){
chart_name.setText("ID:"+track.getName());
chart_name.setClickable(false); // we need fix it (heroku issue)
chart_name.setClickable(true); // we need fix it (heroku issue)
if(!isPublishedData) {
chart_name.setTextColor(getResources().getColor(R.color.black));
}
chart_date.setText(track.getDate());
chart_desc.setText("Points: "+track.size);
chart_desc.setText(getString(R.string.msg_chart_points)+": "+track.size);
if (track.kms != 0 || track.hours !=0 || track.mins !=0) {
String time = String.format("%02d:%02d:%02d",track.hours,track.mins,track.secs);
chart_loc.setText("Track: "+track.kms+" Kms ("+time+")");
chart_loc.setText(getString(R.string.msg_chart_track)+": "+track.kms+" Kms ("+time+")");
}
else
chart_loc.setVisibility(View.GONE);
chart_meta.setText("Metadata: "+ track.metadata);
chart_meta.setText(getString(R.string.msg_chart_metadata)+": "+ track.metadata);

rl_separator.setVisibility(View.VISIBLE);
}
Expand All @@ -370,9 +371,10 @@ private void setTrackDescription(SensorTrack track, boolean isPublishedData){
private View.OnClickListener onChartIdClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Logger.w(TAG, "[CHART] onChartIdClick");
if(recordId!=null) UITools.viewLink(
getActivity(),
getString(R.string.url_chart_get_data)+recordId+"?output=json"
requireContext(),
getString(R.string.url_chart_get_data)+recordId
);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class MapFragment extends Fragment {

public static final String TAG = MapFragment.class.getSimpleName();
private MapView mapView;
private static MapView mapView;

public static MapFragment newInstance() {
return new MapFragment();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hpsaturn.pollutionreporter.view;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -38,13 +39,16 @@ public class PostsFragment extends Fragment {

private FirebaseRecyclerAdapter<SensorTrackInfo, PostsViewHolder> mAdapter;

private Context ctx;

public static PostsFragment newInstance() {
PostsFragment fragment = new PostsFragment();
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.ctx=container.getContext();

View view = inflater.inflate(R.layout.fragment_records, container, false);
mEmptyMessage = view.findViewById(R.id.tv_records_empty_list);
Expand Down Expand Up @@ -87,14 +91,14 @@ protected void onBindViewHolder(@NonNull PostsViewHolder viewHolder, int positio
getMain().showTrackInfoFragment(recordId);
});
// Bind Post to ViewHolder, setting OnClickListener for the star button
viewHolder.bindToPost(trackInfo);
viewHolder.bindToPost(ctx, trackInfo);
}
};

mRecordsList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mAdapter.startListening();
mUpdateTimeTask.run(); // TODO: fucking workaround, firebase recycler wasn't update in fist time
mUpdateTimeTask.run(); // TODO: fucking workaround, firebase recycler wasn't update in first time

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hpsaturn.pollutionreporter.view;

import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

Expand All @@ -25,11 +27,11 @@ public PostsViewHolder(View itemView) {
record_location = itemView.findViewById(R.id.tv_record_location);
}

public void bindToPost(SensorTrackInfo sensorTrack){
public void bindToPost(Context ctx, SensorTrackInfo sensorTrack){
record_name.setText(sensorTrack.getName());
record_date.setText(sensorTrack.getDate());
// TODO: geocode inverse for location
record_location.setText(""+sensorTrack.getSize()+" points");
record_location.setText(""+sensorTrack.getSize()+" "+ctx.getString(R.string.text_unit_points));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;

import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.hpsaturn.tools.Logger;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import hpsaturn.pollutionreporter.R;
Expand All @@ -35,13 +43,19 @@ public RecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(RecordViewHolder holder, int position) {

java.text.DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(ctx);
final SensorTrack sensorTrack = mRecords.get(position);

holder.record_name.setText(sensorTrack.name);
holder.record_date.setText(sensorTrack.date);
try {
Date sensorTrackDate = new SimpleDateFormat("yyyyMMddkkmmss").parse(sensorTrack.getName());
holder.record_date.setText(dateFormat.format(sensorTrackDate));
} catch (Exception e) {
holder.record_date.setText("");
}
//holder.record_date.setText(sensorTrack.date);
// TODO: geocode inverse for location
holder.record_location.setText(""+sensorTrack.size+" points");
holder.record_location.setText(""+sensorTrack.size+" "+ctx.getString(R.string.text_unit_points));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hpsaturn.pollutionreporter.view;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class ScanFragment extends Fragment {
private RxBleClient rxBleClient;
private ScanResultsAdapter resultsAdapter;

private Context ctx;

public static ScanFragment newInstance() {
ScanFragment fragment = new ScanFragment();
Expand All @@ -65,6 +67,7 @@ public static ScanFragment newInstance() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

this.ctx=container.getContext();
View view = inflater.inflate(R.layout.fragment_devices, container, false);
ButterKnife.bind(this,view);
return view;
Expand Down Expand Up @@ -188,51 +191,63 @@ public void onPause() {
}

private void handleBleScanException(BleScanException bleScanException) {
final String text;
final String text, msg;

switch (bleScanException.getReason()) {
case BleScanException.BLUETOOTH_NOT_AVAILABLE:
text = "Bluetooth is not available";
msg = ctx.getString(R.string.msg_ble_scan_bluetooth_not_available);
break;
case BleScanException.BLUETOOTH_DISABLED:
text = "Enable bluetooth and try again";
msg = ctx.getString(R.string.msg_ble_scan_bluetooth_disabled);
break;
case BleScanException.LOCATION_PERMISSION_MISSING:
text = "On Android 6.0 location permission is required. Implement Runtime Permissions";
msg = ctx.getString(R.string.msg_ble_scan_location_permission_missing);
break;
case BleScanException.LOCATION_SERVICES_DISABLED:
text = "Location services needs to be enabled on Android 6.0";
msg = ctx.getString(R.string.msg_ble_scan_location_services_disabled);
break;
case BleScanException.SCAN_FAILED_ALREADY_STARTED:
text = "Scan with the same filters is already started";
msg = ctx.getString(R.string.msg_ble_scan_scan_failed_already_started);
break;
case BleScanException.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
text = "Failed to register application for bluetooth scan";
msg = ctx.getString(R.string.msg_ble_scan_scan_failed_application_registration_failed);
break;
case BleScanException.SCAN_FAILED_FEATURE_UNSUPPORTED:
text = "Scan with specified parameters is not supported";
msg = ctx.getString(R.string.msg_ble_scan_scan_failed_feature_unsupported);
break;
case BleScanException.SCAN_FAILED_INTERNAL_ERROR:
text = "Scan failed due to internal error";
msg = ctx.getString(R.string.msg_ble_scan_scan_failed_internal_error);
break;
case BleScanException.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES:
text = "Scan cannot start due to limited hardware resources";
msg = ctx.getString(R.string.msg_ble_scan_scan_failed_out_of_hardware_resources);
break;
case BleScanException.UNDOCUMENTED_SCAN_THROTTLE:
text = String.format(
Locale.getDefault(),
"Android 7+ does not allow more scans. Try in %d seconds",
secondsTill(bleScanException.getRetryDateSuggestion())
);
msg = ctx.getString(R.string.msg_ble_scan_undocumented_scan_throttle,
secondsTill(bleScanException.getRetryDateSuggestion()));
break;
case BleScanException.UNKNOWN_ERROR_CODE:
case BleScanException.BLUETOOTH_CANNOT_START:
default:
text = "Unable to start btnScanning";
msg = ctx.getString(R.string.msg_ble_scan_bluetooth_cannot_start);
break;
}
Logger.w(TAG, "EXCEPTION: " + text + " " + bleScanException.getMessage());
UITools.showToast(getActivity(), text);
UITools.showToast(getActivity(), msg);
}

private long secondsTill(Date retryDateSuggestion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ private SwitchPreference getStatusSwitch() {
}

void updateStatusSummary(boolean status){
updateSummary(R.string.key_device_status, status ? "Connected":"Disconnected");
updateSummary(R.string.key_device_status,
status ? getString(R.string.summary_key_device_status_connected) :
getString(R.string.summary_key_device_status_disconnected));
}

void updateSummary(int key){
Expand Down
Loading

0 comments on commit 41635a4

Please sign in to comment.