Showing
8 changed files
with
625 additions
and
28 deletions
.idea/deploymentTargetDropDown.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project version="4"> | ||
| 3 | + <component name="deploymentTargetDropDown"> | ||
| 4 | + <runningDeviceTargetSelectedWithDropDown> | ||
| 5 | + <Target> | ||
| 6 | + <type value="RUNNING_DEVICE_TARGET" /> | ||
| 7 | + <deviceKey> | ||
| 8 | + <Key> | ||
| 9 | + <type value="SERIAL_NUMBER" /> | ||
| 10 | + <value value="R58M5262DCB" /> | ||
| 11 | + </Key> | ||
| 12 | + </deviceKey> | ||
| 13 | + </Target> | ||
| 14 | + </runningDeviceTargetSelectedWithDropDown> | ||
| 15 | + <timeTargetWasSelectedWithDropDown value="2023-06-27T13:21:52.853768900Z" /> | ||
| 16 | + </component> | ||
| 17 | +</project> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
| 3 | package="warp.ly.android_sdk"> | 3 | package="warp.ly.android_sdk"> |
| 4 | 4 | ||
| 5 | <uses-permission android:name="android.permission.INTERNET" /> | 5 | <uses-permission android:name="android.permission.INTERNET" /> |
| 6 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
| 6 | 7 | ||
| 7 | <!-- Restrict Tablets --> | 8 | <!-- Restrict Tablets --> |
| 8 | <!-- <supports-screens--> | 9 | <!-- <supports-screens--> |
| ... | @@ -23,6 +24,7 @@ | ... | @@ -23,6 +24,7 @@ |
| 23 | android:label="@string/app_name" | 24 | android:label="@string/app_name" |
| 24 | android:largeHeap="true" | 25 | android:largeHeap="true" |
| 25 | android:roundIcon="@mipmap/ic_launcher_round" | 26 | android:roundIcon="@mipmap/ic_launcher_round" |
| 27 | + android:requestLegacyExternalStorage="true" | ||
| 26 | android:theme="@style/AppTheme"> | 28 | android:theme="@style/AppTheme"> |
| 27 | <meta-data | 29 | <meta-data |
| 28 | android:name="com.google.android.geo.API_KEY" | 30 | android:name="com.google.android.geo.API_KEY" | ... | ... |
| ... | @@ -66,6 +66,12 @@ | ... | @@ -66,6 +66,12 @@ |
| 66 | android:theme="@style/SDKAppTheme" /> | 66 | android:theme="@style/SDKAppTheme" /> |
| 67 | 67 | ||
| 68 | <activity | 68 | <activity |
| 69 | + android:name="ly.warp.sdk.activities.TelematicsActivity" | ||
| 70 | + android:exported="false" | ||
| 71 | + android:screenOrientation="portrait" | ||
| 72 | + android:theme="@style/SDKAppTheme" /> | ||
| 73 | + | ||
| 74 | + <activity | ||
| 69 | android:name="ly.warp.sdk.activities.GiftsForYouActivity" | 75 | android:name="ly.warp.sdk.activities.GiftsForYouActivity" |
| 70 | android:exported="false" | 76 | android:exported="false" |
| 71 | android:screenOrientation="portrait" | 77 | android:screenOrientation="portrait" | ... | ... |
| 1 | +package ly.warp.sdk.activities; | ||
| 2 | + | ||
| 3 | +import android.Manifest; | ||
| 4 | +import android.app.Activity; | ||
| 5 | +import android.content.Context; | ||
| 6 | +import android.content.pm.PackageManager; | ||
| 7 | +import android.hardware.Sensor; | ||
| 8 | +import android.hardware.SensorEvent; | ||
| 9 | +import android.hardware.SensorEventListener; | ||
| 10 | +import android.hardware.SensorManager; | ||
| 11 | +import android.location.Location; | ||
| 12 | +import android.location.LocationListener; | ||
| 13 | +import android.location.LocationManager; | ||
| 14 | +import android.os.Bundle; | ||
| 15 | +import android.os.Environment; | ||
| 16 | +import android.os.Handler; | ||
| 17 | +import android.util.Log; | ||
| 18 | +import android.view.View; | ||
| 19 | +import android.view.WindowManager; | ||
| 20 | +import android.widget.ImageView; | ||
| 21 | +import android.widget.LinearLayout; | ||
| 22 | +import android.widget.TextView; | ||
| 23 | + | ||
| 24 | +import androidx.annotation.NonNull; | ||
| 25 | +import androidx.core.app.ActivityCompat; | ||
| 26 | +import androidx.core.content.ContextCompat; | ||
| 27 | + | ||
| 28 | +import com.google.android.material.snackbar.Snackbar; | ||
| 29 | + | ||
| 30 | +import org.json.JSONArray; | ||
| 31 | +import org.json.JSONException; | ||
| 32 | +import org.json.JSONObject; | ||
| 33 | + | ||
| 34 | +import java.io.File; | ||
| 35 | +import java.io.FileOutputStream; | ||
| 36 | +import java.io.IOException; | ||
| 37 | +import java.util.ArrayList; | ||
| 38 | +import java.util.Arrays; | ||
| 39 | + | ||
| 40 | +import io.github.inflationx.viewpump.ViewPumpContextWrapper; | ||
| 41 | +import ly.warp.sdk.R; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * Created by Panagiotis Triantafyllou on 26/June/2023. | ||
| 45 | + */ | ||
| 46 | +public class TelematicsActivity extends Activity implements View.OnClickListener, | ||
| 47 | + SensorEventListener, LocationListener { | ||
| 48 | + | ||
| 49 | + // =========================================================== | ||
| 50 | + // Constants | ||
| 51 | + // =========================================================== | ||
| 52 | + | ||
| 53 | + // =========================================================== | ||
| 54 | + // Fields | ||
| 55 | + // =========================================================== | ||
| 56 | + | ||
| 57 | + private ImageView mIvBack; | ||
| 58 | + private boolean mIsTripStarted = false; | ||
| 59 | + private LinearLayout mLlTripButton, mLlTelematicsMain; | ||
| 60 | + private TextView mTvTripButton, mTvSensorData, mTvVelocity, mTvAvgVelocity, mTvRecordsSaved; | ||
| 61 | + private SensorManager mSensorManager; | ||
| 62 | + private Sensor mSensor; | ||
| 63 | + private Handler mHandler, mLoctionHandler; | ||
| 64 | + private Runnable mRunnable, mLocationRunnable; | ||
| 65 | + private long lastUpdate = 0; | ||
| 66 | + private float lastX, lastY, lastZ; | ||
| 67 | + private float velocity = 0; | ||
| 68 | + private ArrayList<JSONObject> mAccelerationTimestamps = new ArrayList<>(); | ||
| 69 | + float mAcceleration = 0; | ||
| 70 | + | ||
| 71 | + private static final float ALPHA = 0.8f; // Filter factor | ||
| 72 | + private static final float STOP_THRESHOLD = 8.0f; // Stop threshold in m/s² | ||
| 73 | + private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 4000; | ||
| 74 | + private static final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 4001; | ||
| 75 | + JSONArray jsonArray = new JSONArray(); | ||
| 76 | + private LocationManager locationManager; | ||
| 77 | + private static final int LOCATION_UPDATE_INTERVAL = 5000; | ||
| 78 | + double mLatitude = 0; | ||
| 79 | + double mLongitude = 0; | ||
| 80 | + // Radius of the Earth in meters | ||
| 81 | + private static final double EARTH_RADIUS = 6371000.0; | ||
| 82 | + | ||
| 83 | + // =========================================================== | ||
| 84 | + // Methods for/from SuperClass/Interfaces | ||
| 85 | + // =========================================================== | ||
| 86 | + | ||
| 87 | + @Override | ||
| 88 | + public void onCreate(Bundle savedInstanceState) { | ||
| 89 | + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | ||
| 90 | + super.onCreate(savedInstanceState); | ||
| 91 | + setContentView(R.layout.activity_telematics); | ||
| 92 | + | ||
| 93 | + mIvBack = findViewById(R.id.iv_telematics_close); | ||
| 94 | + mLlTripButton = findViewById(R.id.ll_activate_button); | ||
| 95 | + mLlTripButton.setOnClickListener(this); | ||
| 96 | + mTvTripButton = findViewById(R.id.tv_trip_button); | ||
| 97 | + mLlTelematicsMain = findViewById(R.id.ll_telematics_main); | ||
| 98 | + mTvSensorData = findViewById(R.id.tv_sensor_data); | ||
| 99 | + mTvVelocity = findViewById(R.id.tv_velocity); | ||
| 100 | + mTvAvgVelocity = findViewById(R.id.tv_avg); | ||
| 101 | + mTvRecordsSaved = findViewById(R.id.tv_records); | ||
| 102 | + mIvBack.setOnClickListener(this); | ||
| 103 | + | ||
| 104 | + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | ||
| 105 | + | ||
| 106 | + initViews(); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + @Override | ||
| 110 | + public void onResume() { | ||
| 111 | + super.onResume(); | ||
| 112 | + requestLocationUpdates(); | ||
| 113 | +// WarplyAnalyticsManager.logTrackersEvent(this, "screen", "TelematicsActivity"); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + @Override | ||
| 117 | + protected void onPause() { | ||
| 118 | + super.onPause(); | ||
| 119 | + if (mIsTripStarted) | ||
| 120 | + unregisterStepSensor(); | ||
| 121 | + stopLocationUpdates(); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + @Override | ||
| 125 | + public void onSensorChanged(SensorEvent sensorEvent) { | ||
| 126 | + mTvSensorData.setText(Arrays.toString(sensorEvent.values)); | ||
| 127 | + | ||
| 128 | + float currentX = sensorEvent.values[0]; | ||
| 129 | + float currentY = sensorEvent.values[1]; | ||
| 130 | + float currentZ = sensorEvent.values[2]; | ||
| 131 | + | ||
| 132 | + long currentTime = System.currentTimeMillis(); | ||
| 133 | + long timeDifference = currentTime - lastUpdate; | ||
| 134 | + lastUpdate = currentTime; | ||
| 135 | + | ||
| 136 | + float time = timeDifference / 1000.0f; // Convert time difference to seconds | ||
| 137 | + | ||
| 138 | + // Apply low-pass filter | ||
| 139 | + float filteredX = ALPHA * lastX + (1 - ALPHA) * currentX; | ||
| 140 | + float filteredY = ALPHA * lastY + (1 - ALPHA) * currentY; | ||
| 141 | + float filteredZ = ALPHA * lastZ + (1 - ALPHA) * currentZ; | ||
| 142 | + | ||
| 143 | + // Calculate acceleration in m/s² using filtered values | ||
| 144 | + float accelerationX = (filteredX - lastX) / time; | ||
| 145 | + float accelerationY = (filteredY - lastY) / time; | ||
| 146 | + float accelerationZ = (filteredZ - lastZ) / time; | ||
| 147 | + | ||
| 148 | + // Calculate total acceleration | ||
| 149 | + float acceleration = (float) Math.sqrt(accelerationX * accelerationX + accelerationY * accelerationY + accelerationZ * accelerationZ); | ||
| 150 | + | ||
| 151 | + // If acceleration is below the stop threshold, assume we are in a stop | ||
| 152 | + if (acceleration < STOP_THRESHOLD) { | ||
| 153 | + velocity = 0; | ||
| 154 | + } else { | ||
| 155 | + // Update velocity | ||
| 156 | + velocity = acceleration * time; | ||
| 157 | + } | ||
| 158 | + // Convert velocity to km/h | ||
| 159 | + mAcceleration = velocity * 3.6f; // Convert to km/h | ||
| 160 | + mTvVelocity.setText(String.format("%.1f", mAcceleration) + " km/h"); | ||
| 161 | + // Update last values | ||
| 162 | + lastX = filteredX; | ||
| 163 | + lastY = filteredY; | ||
| 164 | + lastZ = filteredZ; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + @Override | ||
| 168 | + public void onAccuracyChanged(Sensor sensor, int i) { | ||
| 169 | + | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + @Override | ||
| 173 | + public void onClick(View view) { | ||
| 174 | + if (view.getId() == R.id.iv_telematics_close) { | ||
| 175 | + onBackPressed(); | ||
| 176 | + return; | ||
| 177 | + } | ||
| 178 | + if (view.getId() == R.id.ll_activate_button) { | ||
| 179 | + if (mIsTripStarted) { | ||
| 180 | + unregisterStepSensor(); | ||
| 181 | + stopLocationUpdates(); | ||
| 182 | + initViews(); | ||
| 183 | + mIsTripStarted = false; | ||
| 184 | + mTvTripButton.setText(R.string.cos_dlg_start_trip); | ||
| 185 | + } else { | ||
| 186 | + registerStepSensor(); | ||
| 187 | + mIsTripStarted = true; | ||
| 188 | + mTvTripButton.setText(R.string.cos_dlg_stop_trip); | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + @Override | ||
| 194 | + protected void attachBaseContext(Context newBase) { | ||
| 195 | + super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase)); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + @Override | ||
| 199 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
| 200 | + if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) { | ||
| 201 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
| 202 | + saveAccelerationDataToExternalStorage(jsonArray); | ||
| 203 | + } else { | ||
| 204 | + Snackbar.make(mLlTelematicsMain, "Storage Permission Denied", Snackbar.LENGTH_SHORT).show(); | ||
| 205 | + } | ||
| 206 | + return; | ||
| 207 | + } | ||
| 208 | + if (requestCode == PERMISSION_REQUEST_ACCESS_FINE_LOCATION) { | ||
| 209 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
| 210 | + startLocationUpdates(); | ||
| 211 | + } else { | ||
| 212 | + Snackbar.make(mLlTelematicsMain, "Location Permission Denied", Snackbar.LENGTH_SHORT).show(); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + @Override | ||
| 218 | + public void onLocationChanged(Location location) { | ||
| 219 | + //TODO: uncomment and remove setText if needs revert to handler implementation | ||
| 220 | + //TODO: LOCATION_UPDATE_INTERVAL = 300 when using location.getSpeed() | ||
| 221 | +// float speedKmph = (location.getSpeed() * 3600) / 1000; | ||
| 222 | +// mTvAvgVelocity.setText(String.format("%.1f", Math.floor(speedKmph)) + " km/h"); | ||
| 223 | + if (mLatitude != 0 && mLongitude != 0) | ||
| 224 | + requestLocationUpdatePeriodically(location); | ||
| 225 | + mLatitude = location.getLatitude(); | ||
| 226 | + mLongitude = location.getLongitude(); | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + @Override | ||
| 230 | + public void onStatusChanged(String provider, int status, Bundle extras) { | ||
| 231 | + // Handle status changes if needed | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + @Override | ||
| 235 | + public void onProviderEnabled(String provider) { | ||
| 236 | + // Handle provider enabled if needed | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + @Override | ||
| 240 | + public void onProviderDisabled(String provider) { | ||
| 241 | + // Handle provider disabled if needed | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + // =========================================================== | ||
| 245 | + // Methods | ||
| 246 | + // =========================================================== | ||
| 247 | + | ||
| 248 | + private void initViews() { | ||
| 249 | + mTvVelocity.setText("0.0 km/h"); | ||
| 250 | + mTvAvgVelocity.setText("0.0 km/h"); | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + private void requestLocationUpdates() { | ||
| 254 | + // Check if the ACCESS_FINE_LOCATION permission is granted | ||
| 255 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | ||
| 256 | + != PackageManager.PERMISSION_GRANTED) { | ||
| 257 | + // Permission is not granted, request it | ||
| 258 | + ActivityCompat.requestPermissions(this, | ||
| 259 | + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | ||
| 260 | + PERMISSION_REQUEST_ACCESS_FINE_LOCATION); | ||
| 261 | + } else { | ||
| 262 | + // Permission is granted, start requesting location updates | ||
| 263 | + startLocationUpdates(); | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + private void startLocationUpdates() { | ||
| 268 | + try { | ||
| 269 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, | ||
| 270 | + LOCATION_UPDATE_INTERVAL, // minimum time interval between location updates (in milliseconds) | ||
| 271 | + 0, // minimum distance between location updates (in meters) | ||
| 272 | + this); | ||
| 273 | + } catch (SecurityException e) { | ||
| 274 | + e.printStackTrace(); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + private void requestLocationUpdatePeriodically(Location location) { | ||
| 279 | + mLoctionHandler = new Handler(); | ||
| 280 | + mLocationRunnable = new Runnable() { | ||
| 281 | + @Override | ||
| 282 | + public void run() { | ||
| 283 | + double speed = calculateSpeed(mLatitude, mLongitude, location.getLatitude(), location.getLongitude(), (LOCATION_UPDATE_INTERVAL / 1000)); | ||
| 284 | + mTvAvgVelocity.setText(String.format("%.1f", Math.floor(speed)) + " km/h"); | ||
| 285 | + mLoctionHandler.postDelayed(this, LOCATION_UPDATE_INTERVAL); | ||
| 286 | + } | ||
| 287 | + }; | ||
| 288 | + mLoctionHandler.postDelayed(mLocationRunnable, LOCATION_UPDATE_INTERVAL); | ||
| 289 | + } | ||
| 290 | + | ||
| 291 | + private double calculateDistance(double lat1, double lon1, double lat2, double lon2) { | ||
| 292 | + double x = Math.toRadians(lon2 - lon1) * Math.cos(Math.toRadians((lat1 + lat2) / 2)); | ||
| 293 | + double y = Math.toRadians(lat2 - lat1); | ||
| 294 | + return Math.sqrt(x * x + y * y) * EARTH_RADIUS; | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + // Function to calculate speed in meters per second | ||
| 298 | + private double calculateSpeed(double lat1, double lon1, double lat2, double lon2, double timeDifferenceInSeconds) { | ||
| 299 | + double distance = calculateDistance(lat1, lon1, lat2, lon2); | ||
| 300 | + return (distance / timeDifferenceInSeconds) * 3.6f; // Convert to km/h; | ||
| 301 | + } | ||
| 302 | + | ||
| 303 | + private void requestSingleLocationUpdate() { | ||
| 304 | + try { | ||
| 305 | + locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null); | ||
| 306 | + } catch (SecurityException e) { | ||
| 307 | + e.printStackTrace(); | ||
| 308 | + Snackbar.make(mLlTelematicsMain, "requestSingleLocationUpdate Exception", Snackbar.LENGTH_SHORT).show(); | ||
| 309 | + } | ||
| 310 | + } | ||
| 311 | + | ||
| 312 | + private void stopLocationUpdates() { | ||
| 313 | + locationManager.removeUpdates(this); | ||
| 314 | + if (mLoctionHandler != null) | ||
| 315 | + mLoctionHandler.removeCallbacks(mLocationRunnable); | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + private void registerStepSensor() { | ||
| 319 | + mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | ||
| 320 | + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | ||
| 321 | + mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); | ||
| 322 | + Snackbar.make(mLlTelematicsMain, "Sensor Registered", Snackbar.LENGTH_SHORT).show(); | ||
| 323 | + | ||
| 324 | + final int delay = 1000; | ||
| 325 | + final int[] recordsCount = {0}; | ||
| 326 | + mTvRecordsSaved.setText(String.valueOf(recordsCount[0])); | ||
| 327 | + mHandler = new Handler(); | ||
| 328 | + mRunnable = new Runnable() { | ||
| 329 | + @Override | ||
| 330 | + public void run() { | ||
| 331 | + try { | ||
| 332 | + mAccelerationTimestamps.add(new JSONObject().putOpt(String.valueOf(System.currentTimeMillis()), mAcceleration)); | ||
| 333 | + recordsCount[0]++; | ||
| 334 | + mTvRecordsSaved.setText(String.valueOf(recordsCount[0])); | ||
| 335 | + } catch (JSONException e) { | ||
| 336 | + e.printStackTrace(); | ||
| 337 | + Snackbar.make(mLlTelematicsMain, "Runnable Failed", Snackbar.LENGTH_SHORT).show(); | ||
| 338 | + } | ||
| 339 | + mHandler.postDelayed(this, delay); | ||
| 340 | + } | ||
| 341 | + }; | ||
| 342 | + mHandler.postDelayed(mRunnable, delay); | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + private void unregisterStepSensor() { | ||
| 346 | + mSensorManager.unregisterListener(this); | ||
| 347 | + mTvVelocity.setText("0.0 km/h"); | ||
| 348 | + mTvAvgVelocity.setText("0.0 km/h"); | ||
| 349 | + Snackbar.make(mLlTelematicsMain, "Sensor Unregistered", Snackbar.LENGTH_SHORT).show(); | ||
| 350 | + mHandler.removeCallbacks(mRunnable); | ||
| 351 | + saveAccelerationDataToFile(); | ||
| 352 | + } | ||
| 353 | + | ||
| 354 | + private void saveAccelerationDataToFile() { | ||
| 355 | + jsonArray = new JSONArray(); | ||
| 356 | + | ||
| 357 | + // Convert each JSONObject in the array to JSON and add it to the JSONArray | ||
| 358 | + for (JSONObject jsonObject : mAccelerationTimestamps) { | ||
| 359 | + jsonArray.put(jsonObject); | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) | ||
| 363 | + != PackageManager.PERMISSION_GRANTED) { | ||
| 364 | + ActivityCompat.requestPermissions(this, | ||
| 365 | + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, | ||
| 366 | + PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); | ||
| 367 | + } else { | ||
| 368 | + saveAccelerationDataToExternalStorage(jsonArray); | ||
| 369 | + } | ||
| 370 | + } | ||
| 371 | + | ||
| 372 | + private void saveAccelerationDataToExternalStorage(JSONArray jsonArray) { | ||
| 373 | + String state = Environment.getExternalStorageState(); | ||
| 374 | + if (Environment.MEDIA_MOUNTED.equals(state)) { | ||
| 375 | + File documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); | ||
| 376 | + File file = new File(documentsDir, "telematics_data.json"); | ||
| 377 | + try { | ||
| 378 | + FileOutputStream fileOutputStream = new FileOutputStream(file); | ||
| 379 | + fileOutputStream.write(jsonArray.toString().getBytes()); | ||
| 380 | + fileOutputStream.close(); | ||
| 381 | + Snackbar.make(mLlTelematicsMain, "Success saving data to file", Snackbar.LENGTH_SHORT).show(); | ||
| 382 | + } catch (IOException e) { | ||
| 383 | + e.printStackTrace(); | ||
| 384 | + Snackbar.make(mLlTelematicsMain, "Error saving acceleration data to file", Snackbar.LENGTH_SHORT).show(); | ||
| 385 | + } | ||
| 386 | + } else { | ||
| 387 | + Snackbar.make(mLlTelematicsMain, "External storage is not accessible", Snackbar.LENGTH_SHORT).show(); | ||
| 388 | + } | ||
| 389 | + } | ||
| 390 | + | ||
| 391 | +// // Low-pass filter function using Exponential Moving Average (EMA) | ||
| 392 | +// private float lowPassFilter(float currentValue) { | ||
| 393 | +// float filteredValue = alpha * currentValue + (1 - alpha) * previousFilteredValue; | ||
| 394 | +// previousFilteredValue = filteredValue; | ||
| 395 | +// return filteredValue; | ||
| 396 | +// } | ||
| 397 | + | ||
| 398 | + // =========================================================== | ||
| 399 | + // Inner and Anonymous Classes | ||
| 400 | + // =========================================================== | ||
| 401 | + | ||
| 402 | +} |
| ... | @@ -22,6 +22,7 @@ import java.util.ArrayList; | ... | @@ -22,6 +22,7 @@ import java.util.ArrayList; |
| 22 | 22 | ||
| 23 | import ly.warp.sdk.R; | 23 | import ly.warp.sdk.R; |
| 24 | import ly.warp.sdk.activities.ActiveCouponsActivity; | 24 | import ly.warp.sdk.activities.ActiveCouponsActivity; |
| 25 | +import ly.warp.sdk.activities.TelematicsActivity; | ||
| 25 | import ly.warp.sdk.activities.WarpViewActivity; | 26 | import ly.warp.sdk.activities.WarpViewActivity; |
| 26 | import ly.warp.sdk.io.callbacks.CallbackReceiver; | 27 | import ly.warp.sdk.io.callbacks.CallbackReceiver; |
| 27 | import ly.warp.sdk.io.models.Campaign; | 28 | import ly.warp.sdk.io.models.Campaign; |
| ... | @@ -33,9 +34,7 @@ import ly.warp.sdk.views.adapters.HomeCampaignAdapter; | ... | @@ -33,9 +34,7 @@ import ly.warp.sdk.views.adapters.HomeCampaignAdapter; |
| 33 | 34 | ||
| 34 | public class HomeFragment extends Fragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener { | 35 | public class HomeFragment extends Fragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener { |
| 35 | 36 | ||
| 36 | - private RelativeLayout mOptionOne, mOptionTwo, mOptionThree; | 37 | + private RelativeLayout mOptionOne, mOptionTwo, mOptionThree, mRlDriving; |
| 37 | - private RecyclerView mRecyclerCampaigns; | ||
| 38 | - private HomeCampaignAdapter mAdapterCampaigns; | ||
| 39 | private LinearLayout mLlBillPayment; | 38 | private LinearLayout mLlBillPayment; |
| 40 | private TextView mTvUsername, mTvActiveCoupons; | 39 | private TextView mTvUsername, mTvActiveCoupons; |
| 41 | private ConstraintLayout mClActiveCoupons; | 40 | private ConstraintLayout mClActiveCoupons; |
| ... | @@ -86,17 +85,8 @@ public class HomeFragment extends Fragment implements View.OnClickListener, Swip | ... | @@ -86,17 +85,8 @@ public class HomeFragment extends Fragment implements View.OnClickListener, Swip |
| 86 | mTvUsername.setText(String.format(getResources().getString(R.string.welcome_user), | 85 | mTvUsername.setText(String.format(getResources().getString(R.string.welcome_user), |
| 87 | WarplyManagerHelper.getConsumer().getFirstName() + " " + WarplyManagerHelper.getConsumer().getLastName())); | 86 | WarplyManagerHelper.getConsumer().getFirstName() + " " + WarplyManagerHelper.getConsumer().getLastName())); |
| 88 | 87 | ||
| 89 | - mRecyclerCampaigns = view.findViewById(R.id.rv_home_campaigns); | 88 | + mRlDriving = view.findViewById(R.id.rl_driving); |
| 90 | - mRecyclerCampaigns.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); | 89 | + mRlDriving.setOnClickListener(this); |
| 91 | - mAdapterCampaigns = new HomeCampaignAdapter(getContext(), WarplyManagerHelper.getUniqueCampaignList().get("homescreen")); | ||
| 92 | - mRecyclerCampaigns.setAdapter(mAdapterCampaigns); | ||
| 93 | - mAdapterCampaigns.getPositionClicks() | ||
| 94 | - .doOnNext(campaign -> { | ||
| 95 | - startActivity(WarpViewActivity.createIntentFromURL(getContext(), WarplyManagerHelper.constructCampaignUrl(campaign))); | ||
| 96 | - }) | ||
| 97 | - .doOnError(error -> { | ||
| 98 | - }) | ||
| 99 | - .subscribe(); | ||
| 100 | } | 90 | } |
| 101 | 91 | ||
| 102 | @Override | 92 | @Override |
| ... | @@ -117,6 +107,11 @@ public class HomeFragment extends Fragment implements View.OnClickListener, Swip | ... | @@ -117,6 +107,11 @@ public class HomeFragment extends Fragment implements View.OnClickListener, Swip |
| 117 | Intent intent = new Intent(getContext(), ActiveCouponsActivity.class); | 107 | Intent intent = new Intent(getContext(), ActiveCouponsActivity.class); |
| 118 | intent.putExtra("couponlist", WarplyManagerHelper.getCouponList()); | 108 | intent.putExtra("couponlist", WarplyManagerHelper.getCouponList()); |
| 119 | startActivity(intent); | 109 | startActivity(intent); |
| 110 | + return; | ||
| 111 | + } | ||
| 112 | + if (view.getId() == R.id.rl_driving) { | ||
| 113 | + Intent intent = new Intent(getContext(), TelematicsActivity.class); | ||
| 114 | + startActivity(intent); | ||
| 120 | } | 115 | } |
| 121 | } | 116 | } |
| 122 | 117 | ... | ... |
| 1 | +<?xml version="1.0" encoding="utf-8"?> | ||
| 2 | +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| 3 | + xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| 4 | + android:id="@+id/ll_telematics_main" | ||
| 5 | + android:layout_width="match_parent" | ||
| 6 | + android:layout_height="match_parent" | ||
| 7 | + android:background="@color/cos_light_grey3" | ||
| 8 | + android:orientation="vertical"> | ||
| 9 | + | ||
| 10 | + <androidx.constraintlayout.widget.ConstraintLayout | ||
| 11 | + android:id="@+id/cl_bill_header" | ||
| 12 | + android:layout_width="match_parent" | ||
| 13 | + android:layout_height="64dp" | ||
| 14 | + android:background="@color/white"> | ||
| 15 | + | ||
| 16 | + <ImageView | ||
| 17 | + android:id="@+id/iv_telematics_close" | ||
| 18 | + android:layout_width="48dp" | ||
| 19 | + android:layout_height="48dp" | ||
| 20 | + android:layout_marginStart="16dp" | ||
| 21 | + android:scaleType="centerInside" | ||
| 22 | + android:src="@drawable/ic_back" | ||
| 23 | + app:layout_constraintBottom_toBottomOf="parent" | ||
| 24 | + app:layout_constraintStart_toStartOf="parent" | ||
| 25 | + app:layout_constraintTop_toTopOf="parent" /> | ||
| 26 | + | ||
| 27 | + <TextView | ||
| 28 | + android:id="@+id/textView3" | ||
| 29 | + fontPath="fonts/BTCosmo-Bold.ttf" | ||
| 30 | + android:layout_width="wrap_content" | ||
| 31 | + android:layout_height="wrap_content" | ||
| 32 | + android:gravity="center" | ||
| 33 | + android:text="@string/cos_telematics" | ||
| 34 | + android:textColor="@color/cos_light_black" | ||
| 35 | + android:textSize="19sp" | ||
| 36 | + app:layout_constraintBottom_toBottomOf="parent" | ||
| 37 | + app:layout_constraintEnd_toEndOf="parent" | ||
| 38 | + app:layout_constraintStart_toStartOf="parent" | ||
| 39 | + app:layout_constraintTop_toTopOf="parent" /> | ||
| 40 | + </androidx.constraintlayout.widget.ConstraintLayout> | ||
| 41 | + | ||
| 42 | + <RelativeLayout | ||
| 43 | + android:layout_width="match_parent" | ||
| 44 | + android:layout_height="match_parent" | ||
| 45 | + android:background="@color/cos_light_grey3" | ||
| 46 | + android:gravity="center_horizontal"> | ||
| 47 | + | ||
| 48 | + <TextView | ||
| 49 | + android:id="@+id/tv_sensor_data_label" | ||
| 50 | + fontPath="fonts/PeridotPE-Regular.ttf" | ||
| 51 | + android:layout_width="wrap_content" | ||
| 52 | + android:layout_height="wrap_content" | ||
| 53 | + android:layout_centerHorizontal="true" | ||
| 54 | + android:layout_marginTop="24dp" | ||
| 55 | + android:text="Sensor Data" | ||
| 56 | + android:textColor="@color/blue_dark" | ||
| 57 | + android:textSize="16sp" /> | ||
| 58 | + | ||
| 59 | + <TextView | ||
| 60 | + android:id="@+id/tv_sensor_data" | ||
| 61 | + fontPath="fonts/PeridotPE-Bold.ttf" | ||
| 62 | + android:layout_width="wrap_content" | ||
| 63 | + android:layout_height="wrap_content" | ||
| 64 | + android:layout_below="@+id/tv_sensor_data_label" | ||
| 65 | + android:layout_centerHorizontal="true" | ||
| 66 | + android:layout_marginTop="24dp" | ||
| 67 | + android:textColor="@color/blue_dark" | ||
| 68 | + android:textSize="16sp" /> | ||
| 69 | + | ||
| 70 | + <TextView | ||
| 71 | + android:id="@+id/tv_velocity_label" | ||
| 72 | + fontPath="fonts/PeridotPE-Regular.ttf" | ||
| 73 | + android:layout_width="wrap_content" | ||
| 74 | + android:layout_height="wrap_content" | ||
| 75 | + android:layout_centerHorizontal="true" | ||
| 76 | + android:layout_marginTop="24dp" | ||
| 77 | + android:text="Acceleration" | ||
| 78 | + android:layout_below="@+id/tv_sensor_data" | ||
| 79 | + android:textColor="@color/blue_dark" | ||
| 80 | + android:textSize="16sp" /> | ||
| 81 | + | ||
| 82 | + <TextView | ||
| 83 | + android:id="@+id/tv_velocity" | ||
| 84 | + fontPath="fonts/PeridotPE-Bold.ttf" | ||
| 85 | + android:layout_width="wrap_content" | ||
| 86 | + android:layout_height="wrap_content" | ||
| 87 | + android:layout_below="@+id/tv_velocity_label" | ||
| 88 | + android:layout_centerHorizontal="true" | ||
| 89 | + android:layout_marginTop="24dp" | ||
| 90 | + android:textColor="@color/blue_dark" | ||
| 91 | + android:textSize="16sp" /> | ||
| 92 | + | ||
| 93 | + <TextView | ||
| 94 | + android:id="@+id/tv_avg_label" | ||
| 95 | + fontPath="fonts/PeridotPE-Regular.ttf" | ||
| 96 | + android:layout_width="wrap_content" | ||
| 97 | + android:layout_height="wrap_content" | ||
| 98 | + android:layout_centerHorizontal="true" | ||
| 99 | + android:layout_marginTop="24dp" | ||
| 100 | + android:text="Velocity" | ||
| 101 | + android:layout_below="@+id/tv_velocity" | ||
| 102 | + android:textColor="@color/blue_dark" | ||
| 103 | + android:textSize="16sp" /> | ||
| 104 | + | ||
| 105 | + <TextView | ||
| 106 | + android:id="@+id/tv_avg" | ||
| 107 | + fontPath="fonts/PeridotPE-Bold.ttf" | ||
| 108 | + android:layout_width="wrap_content" | ||
| 109 | + android:layout_height="wrap_content" | ||
| 110 | + android:layout_below="@+id/tv_avg_label" | ||
| 111 | + android:layout_centerHorizontal="true" | ||
| 112 | + android:layout_marginTop="24dp" | ||
| 113 | + android:textColor="@color/blue_dark" | ||
| 114 | + android:textSize="16sp" /> | ||
| 115 | + | ||
| 116 | + <TextView | ||
| 117 | + android:id="@+id/tv_records_label" | ||
| 118 | + fontPath="fonts/PeridotPE-Regular.ttf" | ||
| 119 | + android:layout_width="wrap_content" | ||
| 120 | + android:layout_height="wrap_content" | ||
| 121 | + android:layout_centerHorizontal="true" | ||
| 122 | + android:layout_marginTop="24dp" | ||
| 123 | + android:text="Records Saved" | ||
| 124 | + android:layout_below="@+id/tv_avg" | ||
| 125 | + android:textColor="@color/blue_dark" | ||
| 126 | + android:textSize="16sp" /> | ||
| 127 | + | ||
| 128 | + <TextView | ||
| 129 | + android:id="@+id/tv_records" | ||
| 130 | + fontPath="fonts/PeridotPE-Bold.ttf" | ||
| 131 | + android:layout_width="wrap_content" | ||
| 132 | + android:layout_height="wrap_content" | ||
| 133 | + android:layout_below="@+id/tv_records_label" | ||
| 134 | + android:layout_centerHorizontal="true" | ||
| 135 | + android:layout_marginTop="24dp" | ||
| 136 | + android:textColor="@color/blue_dark" | ||
| 137 | + android:textSize="16sp" /> | ||
| 138 | + | ||
| 139 | + <LinearLayout | ||
| 140 | + android:id="@+id/ll_activate_button" | ||
| 141 | + android:layout_width="match_parent" | ||
| 142 | + android:layout_height="55dp" | ||
| 143 | + android:layout_alignParentBottom="true" | ||
| 144 | + android:layout_marginHorizontal="24dp" | ||
| 145 | + android:layout_marginBottom="56dp" | ||
| 146 | + android:background="@drawable/selector_button_green_border" | ||
| 147 | + android:gravity="center" | ||
| 148 | + android:orientation="horizontal"> | ||
| 149 | + | ||
| 150 | + <TextView | ||
| 151 | + android:id="@+id/tv_trip_button" | ||
| 152 | + fontPath="fonts/PeridotPE-SemiBold.ttf" | ||
| 153 | + android:layout_width="wrap_content" | ||
| 154 | + android:layout_height="wrap_content" | ||
| 155 | + android:gravity="center" | ||
| 156 | + android:includeFontPadding="false" | ||
| 157 | + android:text="@string/cos_dlg_start_trip" | ||
| 158 | + android:textColor="@color/blue_dark" | ||
| 159 | + android:textSize="16sp" /> | ||
| 160 | + </LinearLayout> | ||
| 161 | + </RelativeLayout> | ||
| 162 | +</LinearLayout> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -103,29 +103,39 @@ | ... | @@ -103,29 +103,39 @@ |
| 103 | android:background="@drawable/home_bg"> | 103 | android:background="@drawable/home_bg"> |
| 104 | 104 | ||
| 105 | <RelativeLayout | 105 | <RelativeLayout |
| 106 | - android:id="@+id/rl_home_campaigns" | 106 | + android:id="@+id/rl_driving" |
| 107 | android:layout_width="match_parent" | 107 | android:layout_width="match_parent" |
| 108 | - android:layout_height="wrap_content" | 108 | + android:layout_height="140dp" |
| 109 | - android:layout_marginTop="50dp"> | 109 | + android:layout_marginHorizontal="8dp" |
| 110 | + android:layout_marginTop="50dp" | ||
| 111 | + android:layout_marginBottom="16dp" | ||
| 112 | + android:background="@drawable/shape_cos_white"> | ||
| 110 | 113 | ||
| 111 | - <androidx.recyclerview.widget.RecyclerView | 114 | + <TextView |
| 112 | - android:id="@+id/rv_home_campaigns" | 115 | + android:layout_width="wrap_content" |
| 113 | - android:layout_width="match_parent" | ||
| 114 | android:layout_height="wrap_content" | 116 | android:layout_height="wrap_content" |
| 115 | - android:layout_marginBottom="8dp" | 117 | + android:layout_alignParentStart="true" |
| 116 | - android:background="#80FFFFFF" | 118 | + android:layout_centerVertical="true" |
| 117 | - android:clipToPadding="false" | 119 | + android:layout_marginStart="16dp" |
| 118 | - android:orientation="horizontal" | 120 | + android:text="@string/cos_telematics" |
| 119 | - android:paddingVertical="15dp" | 121 | + android:textColor="@color/blue_dark" |
| 120 | - android:paddingStart="10dp" | 122 | + android:textSize="20sp" /> |
| 121 | - android:paddingEnd="10dp" /> | 123 | + |
| 124 | + <ImageView | ||
| 125 | + android:id="@+id/iv_one_logo" | ||
| 126 | + android:layout_width="100dp" | ||
| 127 | + android:layout_height="30dp" | ||
| 128 | + android:layout_alignParentEnd="true" | ||
| 129 | + android:layout_centerVertical="true" | ||
| 130 | + android:layout_marginEnd="16dp" | ||
| 131 | + android:src="@drawable/cosmote_one" /> | ||
| 122 | </RelativeLayout> | 132 | </RelativeLayout> |
| 123 | 133 | ||
| 124 | <LinearLayout | 134 | <LinearLayout |
| 125 | android:id="@+id/rl_home_coupons" | 135 | android:id="@+id/rl_home_coupons" |
| 126 | android:layout_width="match_parent" | 136 | android:layout_width="match_parent" |
| 127 | android:layout_height="wrap_content" | 137 | android:layout_height="wrap_content" |
| 128 | - android:layout_below="@id/rl_home_campaigns" | 138 | + android:layout_below="@id/rl_driving" |
| 129 | android:visibility="gone"> | 139 | android:visibility="gone"> |
| 130 | 140 | ||
| 131 | <androidx.recyclerview.widget.RecyclerView | 141 | <androidx.recyclerview.widget.RecyclerView | ... | ... |
| ... | @@ -176,6 +176,9 @@ | ... | @@ -176,6 +176,9 @@ |
| 176 | <string name="lbl_take_photo_accept">Οκ</string> | 176 | <string name="lbl_take_photo_accept">Οκ</string> |
| 177 | <string name="lbl_take_photo_decline">Άκυρο</string> | 177 | <string name="lbl_take_photo_decline">Άκυρο</string> |
| 178 | <string name="lbl_gps_enabled">Θέλετε να ενεργοποιήσετε το GPS;</string> | 178 | <string name="lbl_gps_enabled">Θέλετε να ενεργοποιήσετε το GPS;</string> |
| 179 | + <string name="cos_telematics">Telematics Demo</string> | ||
| 180 | + <string name="cos_dlg_start_trip">Start Trip</string> | ||
| 181 | + <string name="cos_dlg_stop_trip">Stop Trip</string> | ||
| 179 | 182 | ||
| 180 | <string-array name="coupons_array"> | 183 | <string-array name="coupons_array"> |
| 181 | <item>Κουπόνια</item> | 184 | <item>Κουπόνια</item> | ... | ... |
-
Please register or login to post a comment