Skip to content

Commit

Permalink
Clean up for new camera work, new apk.
Browse files Browse the repository at this point in the history
Added constants for directories.
Unified file paths so everything is back into the camera directory.
Cleaned up some of the mess made by camera work changes.
Hid the attach images tab from create plant dialog.
  • Loading branch information
nonsensicalthinking committed Feb 9, 2018
1 parent 4804095 commit 1e5541d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 122 deletions.
Binary file modified plant_tracker-release.apk
Binary file not shown.
3 changes: 1 addition & 2 deletions plant_tracker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
applicationId "com.nonsense.planttracker"
minSdkVersion 24
Expand All @@ -19,7 +18,7 @@ android {
}

debug {
applicationIdSuffix ".debug"
// applicationIdSuffix ".debug"
debuggable true
}
}
Expand Down
8 changes: 2 additions & 6 deletions plant_tracker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nonsense.planttracker">

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera2.autofocus" />

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Expand All @@ -30,7 +26,7 @@

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.nonsense.planttracker.debug.fileprovider"
android:authorities="com.nonsense.planttracker.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class AndroidConstants {
public static final String INTENTKEY_AVAILABLE_GROUPS = "availableGroups";
public static final String INTENTKEY_BASE_DIRECTORY = "baseDir";

public static final String PATH_TRACKER_IMAGES = "camera";
public static final String PATH_TRACKER_SETTINGS = "settings";
public static final String PATH_TRACKER_DATA = "plants";



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -36,14 +34,9 @@
import com.nonsense.planttracker.android.Utility;
import com.nonsense.planttracker.tracker.impl.GenericRecord;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -60,7 +53,8 @@ public class CollectPlantData extends AppCompatActivity {
private boolean showNotes;
private TreeMap<String, Long> availableGroups;

private ArrayList<String> mImages = new ArrayList<>();
private Uri photoURI;
private ArrayList<String> images = new ArrayList<>();


@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -439,15 +433,16 @@ protected void onActivityResult(int requestCode, int resultCode, Intent returned
int selectedItems = returnedIntent.getClipData().getItemCount();

for(int x = 0; x < selectedItems; x++) {
Uri selected = returnedIntent.getClipData().getItemAt(x).getUri();
File f = new File(selected.getPath());
String filePath = getExternalFilesDir("camera") + "/" + f.getName() +
".jpg";
Log.d("SELECTED_URI_FOO", "Gallery Image Selected: " + filePath);
try {
Uri selected = returnedIntent.getClipData().getItemAt(x).getUri();
File f = new File(selected.getPath());
String filePath =
getExternalFilesDir(AndroidConstants.PATH_TRACKER_IMAGES) +
"/" + f.getName() + ".jpg";

InputStream is = getContentResolver().openInputStream(selected);
Utility.copyUriToLocation(is, filePath);
mImages.add(filePath);
images.add(filePath);
}
catch (Exception e) {
e.printStackTrace();
Expand All @@ -458,23 +453,18 @@ protected void onActivityResult(int requestCode, int resultCode, Intent returned

case AndroidConstants.ACTIVITY_PLANT_CAM:
if (resultCode == -1) {
mImages.add(photoURI.getPath());
File f = new File(photoURI.getPath());
String path = getExternalFilesDir(AndroidConstants.PATH_TRACKER_IMAGES) + "/" +
f.getName();
images.add(path);
dispatchTakePictureIntent();
}
break;

// case AndroidConstants.ACTIVITY_PLANT_CAM:
// if (resultCode == Activity.RESULT_OK) {
// ArrayList<String> selectedFiles = (ArrayList<String>)returnedIntent.
// getSerializableExtra(AndroidConstants.INTENTKEY_SELECTED_FILES);
//
// mImages.addAll(selectedFiles);
// }
// break;
}
}

private void dispatchTakePictureIntent() {
//TODO ask for permission to use camera, otherwise we just get an exception thrown about not having permissions!
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Expand All @@ -488,36 +478,28 @@ private void dispatchTakePictureIntent() {
}
// Continue only if the File was successfully created
if (photoFile != null) {
String path = getApplicationContext().getPackageName();
photoURI = FileProvider.getUriForFile(this,
"com.nonsense.planttracker.debug.fileprovider",
photoFile);
path + ".fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, AndroidConstants.ACTIVITY_PLANT_CAM);
}
}
}

private Uri photoURI;

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
//mCurrentPhotoPath = image.getAbsolutePath();
File storageDir = getExternalFilesDir(AndroidConstants.PATH_TRACKER_IMAGES);
File image = File.createTempFile(imageFileName,".jpg", storageDir);

return image;
}

private void cancelActivity() {
if (record.images != null) {
for(String s : mImages) {
for(String s : images) {
File f = new File(s);
f.delete();
}
Expand All @@ -542,7 +524,7 @@ private void endActivity() {
retInt.putExtra(AndroidConstants.INTENTKEY_GENERIC_RECORD, record);
retInt.putExtra(AndroidConstants.INTENTKEY_APPLY_TO_GROUP, applyToGroup);
retInt.putExtra(AndroidConstants.INTENTKEY_SELECTED_GROUP, selectedGroup);
retInt.putExtra(AndroidConstants.INTENTKEY_SELECTED_FILES, mImages);
retInt.putExtra(AndroidConstants.INTENTKEY_SELECTED_FILES, images);

setResult(Activity.RESULT_OK, retInt);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,9 @@ public boolean onNavigationItemSelected(MenuItem item) {
displayOperationInProgressDialog(PlantTrackerUi.this, "Export");

ArrayList<String> files = new ArrayList<>();
files.add(getExternalFilesDir("settings").getPath());
files.add(getExternalFilesDir("plants").getPath());
files.add(getExternalFilesDir("camera").getPath());
files.add(getExternalFilesDir(AndroidConstants.PATH_TRACKER_SETTINGS).getPath());
files.add(getExternalFilesDir(AndroidConstants.PATH_TRACKER_DATA).getPath());
files.add(getExternalFilesDir(AndroidConstants.PATH_TRACKER_IMAGES).getPath());

Zipper.compressTrackerData(files,
getExternalFilesDir("archive").getPath() +
Expand All @@ -833,7 +833,6 @@ public boolean onNavigationItemSelected(MenuItem item) {
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.setType("application/zip");
// startActivityForResult(intent, AndroidConstants.ACTIVITY_IMPORT_SELECT);
dispatchTakePictureIntent();
break;


Expand Down Expand Up @@ -874,51 +873,6 @@ public boolean onNavigationItemSelected(MenuItem item) {
return true;
}

static final int REQUEST_IMAGE_CAPTURE = 98;

String mCurrentPhotoPath;

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.nonsense.planttracker.debug.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}

private Uri photoURI;

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}

// Individual plant menu
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Expand Down Expand Up @@ -1074,7 +1028,7 @@ private void presentGenericEventDialog(String code, String displayName, final in
TabHost.TabSpec attachImagesTab = tabs.newTabSpec("Tab4");
attachImagesTab.setIndicator("Attach Images");
attachImagesTab.setContent(R.id.tab4);
tabs.addTab(attachImagesTab);
// tabs.addTab(attachImagesTab);

tabs.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
Expand Down Expand Up @@ -1428,24 +1382,6 @@ public void onClick(View view) {
protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
super.onActivityResult(requestCode, resultCode, returnedIntent);
switch (requestCode) {

case REQUEST_IMAGE_CAPTURE:
if (resultCode == -1) {
//TODO add image to list of images captured for session
mUriPaths.add(photoURI.getPath());
dispatchTakePictureIntent();
}
else {
if (mUriPaths.size() > 0) {
for(String path : mUriPaths) {
Log.d("Saved image paths", "Path: " + path);
}

mUriPaths.clear();
}
}
break;

case AndroidConstants.ACTIVITY_GENERIC_RECORD:
if (resultCode == Activity.RESULT_OK) {
GenericRecord record = (GenericRecord) returnedIntent.getSerializableExtra(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nonsense.planttracker.android.AndroidConstants;
import com.nonsense.planttracker.tracker.exceptions.PlantNotFoundException;
import com.nonsense.planttracker.tracker.impl.actions.PlantAction;
import com.nonsense.planttracker.tracker.interf.IPlantTrackerListener;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class PlantTracker implements IPlantUpdateListener, ISettingsChangedListe


public PlantTracker() {
this("plants/");
this(AndroidConstants.PATH_TRACKER_DATA);
}

public PlantTracker(String plantFolderPath) {
Expand Down Expand Up @@ -218,9 +219,6 @@ public void savePlant(Plant p) {
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
Gson g = new Gson();
String json = g.toJson(p.getPlantData());

System.out.println("plant json: " + json);

bw.write(json);
bw.close();
} catch (Exception e) {
Expand Down
4 changes: 1 addition & 3 deletions plant_tracker/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="plants/"/>
<external-path name="my_images" path="Android/data/com.nonsense.planttracker.debug/files/Pictures" />
<external-path name="old_images" path="Android/data/com.nonsense.planttracker.debug/files/camera" />

<external-path name="pt_images" path="Android/data/com.nonsense.planttracker/files/camera" />
</paths>

0 comments on commit 1e5541d

Please sign in to comment.