Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRIVER-Android] Updated the develop branch #75

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified app/src/main/assets/models.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.worldbank.transport.driver.activities;

import android.Manifest;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Expand Down Expand Up @@ -51,6 +55,7 @@ public class RecordListActivity extends AppCompatActivity implements CheckSchema
PostRecordsTask.PostRecordsListener, UpdateSchemaTask.UpdateSchemaCallbackListener {

private static final String LOG_LABEL = "RecordListActivity";
private static final int WRITE_EXTERNAL_STORAGE_CODE = 1;

private static final DateFormat sourceDateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Expand Down Expand Up @@ -172,8 +177,25 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l
return true;
}
});

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_CODE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == WRITE_EXTERNAL_STORAGE_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showToast(getString(R.string.storage_permission_granted), false);
} else {
showToast(getString(R.string.storage_permission_not_granted), true);
}
}
}


@Override
protected void onPostResume() {
Log.d(LOG_LABEL, "in onPostResume for record list; refresh list");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package org.worldbank.transport.driver.controls;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

import com.azavea.androidvalidatedforms.controllers.ImageController;
import com.azavea.androidvalidatedforms.tasks.ResizeImageTask;

import org.jsonschema2pojo.media.SerializableMedia;
import org.worldbank.transport.driver.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Subclass image controller to get and set path on holder class used for Gson serialization.
*
* Created by kathrynkillebrew on 2/4/16.
*/
public class DriverImageController extends ImageController {

public DriverImageController(Context ctx, String name, String labelText, boolean isRequired) {
super(ctx, name, labelText, isRequired);
}
Expand All @@ -23,6 +41,11 @@ protected Object getModelValue() {
if (current != null && current.getClass().equals(SerializableMedia.class)) {
return ((SerializableMedia)current).path;
}
Bitmap placeholderBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.placeholder);
final int thumbSize = 64;
Bitmap thumbImage = ThumbnailUtils.extractThumbnail(placeholderBitmap, thumbSize, thumbSize);
File placeholderFile = storeImage(thumbImage);
setModelValue(placeholderFile.getAbsolutePath());

return null;
}
Expand All @@ -32,10 +55,129 @@ protected void setModelValue(String newImagePath) {
SerializableMedia media = null;

if (newImagePath != null && !newImagePath.isEmpty()) {
// Using custom resize Function
Bitmap bitmap = resizeBitMapImage(newImagePath, 768, 768);
Bitmap rotatedBitmap = rotateImage(bitmap, newImagePath);
File rescaledImageFile = storeImage(rotatedBitmap);
media = new SerializableMedia();
media.path = newImagePath;
media.path = rescaledImageFile.getAbsolutePath();
}

getModel().setValue(getName(), media);
}

private Bitmap rotateImage(Bitmap bitmap, String path) {
try {
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
// no rotation needed
return bitmap;
}

// rotate image
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;

} catch (IOException e) {
Log.e("TAG", "Failed to get EXIF information to rotate image");
e.printStackTrace();
return bitmap;
} catch (OutOfMemoryError e) {
Log.e("TAG", "Ran out of memory rotating image! Need to downscale further?");
e.printStackTrace();
return bitmap;
}
}


private Bitmap resizeBitMapImage(String filePath, int targetWidth, int targetHeight) {
Bitmap outputImage = null;
BitmapFactory.Options options = new BitmapFactory.Options();
outputImage = BitmapFactory.decodeFile(filePath, options);
int width = outputImage.getWidth();
int height = outputImage.getHeight();
if(width<= targetWidth && height<=targetHeight){
return outputImage;
}
if (width > height) { // LANDSCAPE IMAGE
Float widthRatio = (float)width/(float)targetWidth;
targetHeight = (int)(height/widthRatio);
} else { // PORTRAIT IMAGE
Float heightRatio = ((float)height/(float)targetHeight);
targetWidth = (int)(width/heightRatio);
}
outputImage = Bitmap.createScaledBitmap(outputImage, targetWidth, targetHeight, true);
return outputImage;
}

private File storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d("TAG",
"Error creating media file, check storage permissions: ");// e.getMessage());
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.e("TAG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.e("TAG", "Error accessing file: " + e.getMessage());
}
return pictureFile;
}

/** Create a File for saving an image or video */
private File getOutputMediaFile(){
String appName = getContext().getApplicationInfo().loadLabel(getContext().getPackageManager()).toString();

File picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
File mediaStorageDir = new File(picturePath, appName);

if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e("TAG", "failed to create directory");
return null;
}
}

String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public RecordDatabaseManager(Context context, boolean amTesting) {
writableDb = dbHelper.getWritableDatabase();
readableDb = dbHelper.getReadableDatabase();

storeDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
storeDateFormat.setTimeZone(TimeZone.getDefault());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,10 @@ public class DriverConstantFields {

public enum WeatherEnum {

CLEAR_DAY("clear-day"),
CLEAR_NIGHT("clear-night"),
CLOUDY("cloudy"),
NIGHT("fog"),
HAIL("hail"),
PARTLY_CLOUDY_DAY("partly-cloudy-day"),
PARTLY_CLOUDY_NIGHT("partly-cloudy-night"),
RAIN("rain"),
SLEET("sleet"),
SNOW("snow"),
THUNDERSTORM("thunderstorm"),
TORNADO("tornado"),
WIND("wind");
FAIR("Fair"),
RAIN("Rain"),
WIND("Wind"),
FOG("Fog");

private final String value;
private final static Map<String, DriverConstantFields.WeatherEnum> CONSTANTS = new HashMap<>();
Expand Down Expand Up @@ -107,10 +98,10 @@ public static DriverConstantFields.WeatherEnum fromValue(String value) {

public enum LightEnum {

DAWN("dawn"),
DAY("day"),
DUSK("dusk"),
NIGHT("night");
DAY("Day"),
DAWN_DUSK("Dawn_dusk"),
NIGHT_LIT("Night_lit"),
NIGHT_UNLIT("Night_unlit");

private final String value;
private final static Map<String, LightEnum> CONSTANTS = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.google.gson.Gson;
Expand All @@ -24,6 +25,7 @@

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Expand All @@ -32,6 +34,8 @@
import java.net.URL;
import java.util.Set;

import static org.worldbank.transport.driver.staticmodels.DriverApp.getContext;

/**
* Upload records to server, then delete them from the local database.
*
Expand Down Expand Up @@ -70,7 +74,7 @@ public PostRecordsTask(PostRecordsListener listener, DriverUserInfo userInfo) {
this(listener, userInfo, new UploadRecordUrlBuilder(), DriverApp.getDatabaseManager());
}

// Invoke this constructor directly in test.
// Invoke this constructor directly in test.
public PostRecordsTask(PostRecordsListener listener, DriverUserInfo userInfo,
UploadRecordUrl uploadRecordUrl, RecordDatabaseManager databaseManager) {

Expand Down Expand Up @@ -273,6 +277,22 @@ protected void onPostExecute(Integer failed) {
if (caller != null) {
caller.recordUploadFinished(failed);
}
// Delete the data images should all records be successfully uploaded
if (failed == 0) {
String appName = getContext().getApplicationInfo().loadLabel(getContext().getPackageManager()).toString();
File picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File mediaStorageDir = new File(picturePath, appName);
if (mediaStorageDir.isDirectory())
{
String[] children = mediaStorageDir.list();
for (String child : children) {
File file = new File(mediaStorageDir, child);
if (file.getName().contains("MI_")) {
file.delete();
}
}
}
}
}

@Override
Expand Down
Binary file added app/src/main/res/drawable-mdpi/placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 12 additions & 17 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,18 @@
<string name="Light">الإضاءه</string>

<!-- weather constants: names must match enum values, with underscores substituted for hyphens -->
<string name="clear_day">يوم صافي</string>
<string name="clear_night">ليله صافيه</string>
<string name="cloudy">غائم</string>
<string name="fog">ضباب</string>
<string name="hail">وابل</string>
<string name="partly_cloudy_day">نهار شبه غائم</string>
<string name="partly_cloudy_night">ليلة شبه غائمة</string>
<string name="rain">مطر</string>
<string name="sleet">مطر ثلجي</string>
<string name="snow">ثلج</string>
<string name="thunderstorm">عاصفة رعدية</string>
<string name="tornado">عاصفة</string>
<string name="wind">رياح</string>
<string name="Fair">Fair</string>
<string name="Rain">Rain</string>
<string name="Wind">Wind</string>
<string name="Fog">Fog</string>

<!-- light constants: names must match enum values -->
<string name="dawn">الفجر</string>
<string name="day">نهار</string>
<string name="dusk">الغسق</string>
<string name="night">ليل</string>
<string name="Day">Day</string>
<string name="Dawn_dusk">Dawn/Dusk</string>
<string name="Night_lit">Night (lit)</string>
<string name="Night_unlit">Night (unlit)</string>

<!--Storage Permission-->
<string name="storage_permission_granted">Storage Permission Granted</string>
<string name="storage_permission_not_granted">Storage Permission Not Granted! Please enable storage permission from settings</string>
</resources>
Loading