From d44266fb459e0c38f09bc663a7b6255baad07f5f Mon Sep 17 00:00:00 2001 From: Jeremy Shore Date: Tue, 17 Dec 2013 23:19:23 -0600 Subject: [PATCH] Making small progress towards storing blob images. Updated everything for XE12. --- .idea/libraries/android_agent_3_239_0.xml | 9 + .idea/workspace.xml | 378 +++++++++++++---- GlassShare/GlassShare-GlassShare.iml | 3 +- GlassShare/build.gradle | 4 + GlassShare/src/main/AndroidManifest.xml | 5 +- .../glassshare/Classes/StorageService.java | 388 +++++++++++++++--- .../com/w9jds/glassshare/MainActivity.java | 149 +++---- 7 files changed, 720 insertions(+), 216 deletions(-) create mode 100644 .idea/libraries/android_agent_3_239_0.xml diff --git a/.idea/libraries/android_agent_3_239_0.xml b/.idea/libraries/android_agent_3_239_0.xml new file mode 100644 index 0000000..5b36b25 --- /dev/null +++ b/.idea/libraries/android_agent_3_239_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 02fa678..4ac78de 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -10,9 +10,6 @@ - - @@ -68,6 +65,23 @@ + + + + @@ -77,11 +91,14 @@ - + - + + + + @@ -89,23 +106,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - - - - - + + + + + + + + + + + @@ -501,13 +592,13 @@ @@ -549,8 +640,6 @@ - - @@ -727,6 +816,8 @@ + + @@ -750,6 +841,7 @@ + @@ -968,13 +1060,13 @@ - + - + @@ -1023,6 +1115,9 @@ + + + @@ -1032,7 +1127,11 @@ - + + + + + @@ -1041,11 +1140,6 @@ - - - - - @@ -1055,6 +1149,9 @@ + + + @@ -1064,7 +1161,11 @@ - + + + + + @@ -1073,11 +1174,6 @@ - - - - - @@ -1087,6 +1183,9 @@ + + + @@ -1096,7 +1195,11 @@ - + + + + + @@ -1105,11 +1208,6 @@ - - - - - @@ -1119,6 +1217,9 @@ + + + @@ -1128,7 +1229,11 @@ - + + + + + @@ -1137,11 +1242,6 @@ - - - - - @@ -1151,6 +1251,9 @@ + + + @@ -1160,7 +1263,11 @@ - + + + + + @@ -1169,11 +1276,6 @@ - - - - - @@ -1183,6 +1285,9 @@ + + + @@ -1192,7 +1297,11 @@ - + + + + + @@ -1201,11 +1310,6 @@ - - - - - @@ -1215,6 +1319,9 @@ + + + @@ -1230,6 +1337,9 @@ + + + @@ -1245,6 +1355,9 @@ + + + @@ -1255,6 +1368,9 @@ + + + @@ -1281,66 +1397,170 @@ - + + + + + + - + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - - - - - + + - + - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/GlassShare/GlassShare-GlassShare.iml b/GlassShare/GlassShare-GlassShare.iml index 0807b1f..eee25da 100644 --- a/GlassShare/GlassShare-GlassShare.iml +++ b/GlassShare/GlassShare-GlassShare.iml @@ -89,9 +89,10 @@ - + + diff --git a/GlassShare/build.gradle b/GlassShare/build.gradle index b461a8a..3ff20db 100644 --- a/GlassShare/build.gradle +++ b/GlassShare/build.gradle @@ -4,9 +4,11 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' + classpath 'com.newrelic.agent.android:agent-gradle-plugin:3.239.0' } } apply plugin: 'android' +apply plugin: 'newrelic' repositories { mavenCentral() @@ -35,6 +37,7 @@ android { dependencies { + compile 'com.newrelic.agent.android:android-agent:3.239.0' compile 'com.google.api-client:google-api-client:1.17.0-rc@jar' compile 'com.google.api-client:google-api-client-android:1.17.0-rc@jar' compile 'com.google.http-client:google-http-client:1.17.0-rc@jar' @@ -49,4 +52,5 @@ dependencies compile files('libs/google-play-services.jar') compile files('libs/gson-2.2.2.jar') compile files('libs/mobileservices-1.1.0.jar') + } \ No newline at end of file diff --git a/GlassShare/src/main/AndroidManifest.xml b/GlassShare/src/main/AndroidManifest.xml index f5fab2c..6428faf 100644 --- a/GlassShare/src/main/AndroidManifest.xml +++ b/GlassShare/src/main/AndroidManifest.xml @@ -19,7 +19,10 @@ + android:label="@string/app_name" + android:name="com.w9jds.glassshare.Classes.StorageApplication"> + + >(); + //Loop through the results and get the name of each table + for (int i = 0; i < results.size(); i ++) + { + JsonElement item = results.get(i); + Map map = new HashMap(); + map.put("TableName", item.getAsJsonObject().getAsJsonPrimitive("TableName").getAsString()); + mTables.add(map); + } + //Broadcast that tables have been loaded + Intent broadcast = new Intent(); + broadcast.setAction("tables.loaded"); + mContext.sendBroadcast(broadcast); + } + }); + } + + /*** + * Adds a new table + * @param tableName + */ + public void addTable(String tableName) + { + JsonObject newTable = new JsonObject(); + newTable.addProperty("tableName", tableName); + mTableTables.insert(newTable, new TableJsonOperationCallback() + { + @Override + public void onCompleted(JsonObject jsonObject, Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Refetch the tables from the server + getTables(); + } + }); + } + + /*** + * Handles deleting a table from storage + * @param tableName + */ + public void deleteTable(String tableName) + { + //Create the json Object we'll send over and fill it with the required + //id property - otherwise we'll get kicked back + JsonObject table = new JsonObject(); + table.addProperty("id", 0); + //Create parameters to pass in the table name. We do this with params + //because it would be stripped out if we put it on the table object + List> parameters = new ArrayList>(); + parameters.add(new Pair("tableName", tableName)); + mTableTables.delete(table, parameters, new TableDeleteCallback() + { + @Override + public void onCompleted(Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Refetch the tables from the server + getTables(); + } + }); + } + + + /*** + * Gets all of the rows for a specific table + * @param tableName + */ + public void getTableRows(String tableName) + { + //Executes a read request with parameters + //We have to do it in this way to ensure it shows up correctly on the server + mTableTableRows.execute(mTableTableRows.parameter("table", tableName), new TableJsonQueryCallback() { + @Override + public void onCompleted(JsonElement result, int count, Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Loop through the results and add them to our local collection + JsonArray results = result.getAsJsonArray(); + mTableRows = new ArrayList(); + for (int i = 0; i < results.size(); i ++) + { + JsonElement item = results.get(i); + mTableRows.add(item); + } + //Broadcast that table rows have been loaded + Intent broadcast = new Intent(); + broadcast.setAction("tablerows.loaded"); + mContext.sendBroadcast(broadcast); + } + }); + } + + /*** + * Delets an individual table row + * @param tableName + * @param partitionKey + * @param rowKey + */ + public void deleteTableRow(final String tableName, String partitionKey, String rowKey) + { + //Create the json Object we'll send over and fill it with the required + //id property - otherwise we'll get kicked back + JsonObject row = new JsonObject(); + row.addProperty("id", 0); + //Create parameters to pass in the table row details. We do this with params + //because it would be stripped out if we put it on the row object + List> parameters = new ArrayList>(); + parameters.add(new Pair("tableName", tableName)); + parameters.add(new Pair("rowKey", rowKey)); + parameters.add(new Pair("partitionKey", partitionKey)); + + mTableTableRows.delete(row, parameters, new TableDeleteCallback() + { + @Override + public void onCompleted(Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Refetch the table rows for the table + getTableRows(tableName); + } + }); + } + + /*** + * Adds a new row to a table + * @param tableName + * @param tableRowData + */ + public void addTableRow(final String tableName, List> tableRowData) + { + //Create a new json object with the key value pairs + JsonObject newRow = new JsonObject(); + for (Pair pair : tableRowData) + { + newRow.addProperty(pair.first, pair.second); + } + //Pass the table name over in parameters + List> parameters = new ArrayList>(); + parameters.add(new Pair("table", tableName)); + mTableTableRows.insert(newRow, parameters, new TableJsonOperationCallback() + { + @Override + public void onCompleted(JsonObject jsonObject, Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Refetch the table rows from the server + getTableRows(tableName); + } + }); + } + + /*** + * Updates an existing table row + * @param tableName + * @param tableRowData + */ + public void updateTableRow(final String tableName, List> tableRowData) + { + //Create a new json object with the key value pairs + JsonObject newRow = new JsonObject(); + for (Pair pair : tableRowData) + { + newRow.addProperty(pair.first, pair.second); + } + //Add ID Parameter since it's required on the server side + newRow.addProperty("id", 1); + //Pass the table name over in parameters + List> parameters = new ArrayList>(); + parameters.add(new Pair("table", tableName)); + mTableTableRows.update(newRow, parameters, new TableJsonOperationCallback() + { + @Override + public void onCompleted(JsonObject jsonObject, Exception exception, ServiceFilterResponse response) + { + if (exception != null) { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Refetch the table rows + getTableRows(tableName); + } + }); + } + /*** * Gets all of the containers from storage */ @@ -189,40 +411,78 @@ public void onCompleted(Exception exception, ServiceFilterResponse response) }); } + /*** + * Get all of the blobs for a container + * @param containerName + */ + public void getBlobsForContainer(String containerName) + { + //Pass the container name as a parameter + //We have to do it in this way for it to show up properly on the server + mTableBlobs.execute(mTableBlobs.parameter("container", containerName), new TableJsonQueryCallback() + { + @Override + public void onCompleted(JsonElement result, int count, Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + JsonArray results = result.getAsJsonArray(); + //Store a local array of both the JsonElements and the blob names + mBlobNames = new ArrayList>(); + mBlobObjects = new ArrayList(); + for (int i = 0; i < results.size(); i ++) + { + JsonElement item = results.get(i); + mBlobObjects.add(item); + Map map = new HashMap(); + map.put("BlobName", item.getAsJsonObject().getAsJsonPrimitive("name").getAsString()); + mBlobNames.add(map); + } + //Broadcast that blobs are loaded + Intent broadcast = new Intent(); + broadcast.setAction("blobs.loaded"); + mContext.sendBroadcast(broadcast); + } + }); + } + /*** * Handles deleting a blob * @param containerName * @param blobName */ -// public void deleteBlob(final String containerName, String blobName) { -// //Create the json Object we'll send over and fill it with the required -// //id property - otherwise we'll get kicked back -// JsonObject blob = new JsonObject(); -// blob.addProperty("id", 0); -// -// //Create parameters to pass in the blob details. We do this with params -// //because it would be stripped out if we put it on the blob object -// List> parameters = new ArrayList>(); -// -// parameters.add(new Pair("containerName", containerName)); -// parameters.add(new Pair("blobName", blobName)); -// -// mTableBlobs.delete(blob, parameters, new TableDeleteCallback() -// { -// @Override -// public void onCompleted(Exception exception, ServiceFilterResponse response) -// { -// if (exception != null) -// { -// Log.e(TAG, exception.getCause().getMessage()); -// return; -// } -// -// //Refetch the blobs from the server -//// getBlobsForContainer(containerName); -// } -// }); -// } + public void deleteBlob(final String containerName, String blobName) { + //Create the json Object we'll send over and fill it with the required + //id property - otherwise we'll get kicked back + JsonObject blob = new JsonObject(); + blob.addProperty("id", 0); + + //Create parameters to pass in the blob details. We do this with params + //because it would be stripped out if we put it on the blob object + List> parameters = new ArrayList>(); + + parameters.add(new Pair("containerName", containerName)); + parameters.add(new Pair("blobName", blobName)); + + mTableBlobs.delete(blob, parameters, new TableDeleteCallback() + { + @Override + public void onCompleted(Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + + //Refetch the blobs from the server +// getBlobsForContainer(containerName); + } + }); + } /*** * Gets a SAS URL for an existing blob @@ -231,36 +491,36 @@ public void onCompleted(Exception exception, ServiceFilterResponse response) * NOTE THIS IS DONE AS A SEPARATE METHOD FROM getSasForNewBlob BECAUSE IT * BROADCASTS A DIFFERENT ACTION */ -// public void getBlobSas(String containerName, String blobName) -// { -// //Create the json Object we'll send over and fill it with the required -// //id property - otherwise we'll get kicked back -// JsonObject blob = new JsonObject(); -// blob.addProperty("id", 0); -// //Create parameters to pass in the blob details. We do this with params -// //because it would be stripped out if we put it on the blob object -// List> parameters = new ArrayList>(); -// parameters.add(new Pair("containerName", containerName)); -// parameters.add(new Pair("blobName", blobName)); -// mTableBlobs.insert(blob, parameters, new TableJsonOperationCallback() -// { -// @Override -// public void onCompleted(JsonObject jsonObject, Exception exception, ServiceFilterResponse response) -// { -// if (exception != null) -// { -// Log.e(TAG, exception.getCause().getMessage()); -// return; -// } -// //Set the loaded blob -// mLoadedBlob = jsonObject; -// //Broadcast that the blob is loaded -// Intent broadcast = new Intent(); -// broadcast.setAction("blob.loaded"); -// mContext.sendBroadcast(broadcast); -// } -// }); -// } + public void getBlobSas(String containerName, String blobName) + { + //Create the json Object we'll send over and fill it with the required + //id property - otherwise we'll get kicked back + JsonObject blob = new JsonObject(); + blob.addProperty("id", 0); + //Create parameters to pass in the blob details. We do this with params + //because it would be stripped out if we put it on the blob object + List> parameters = new ArrayList>(); + parameters.add(new Pair("containerName", containerName)); + parameters.add(new Pair("blobName", blobName)); + mTableBlobs.insert(blob, parameters, new TableJsonOperationCallback() + { + @Override + public void onCompleted(JsonObject jsonObject, Exception exception, ServiceFilterResponse response) + { + if (exception != null) + { + Log.e(TAG, exception.getCause().getMessage()); + return; + } + //Set the loaded blob + mLoadedBlob = jsonObject; + //Broadcast that the blob is loaded + Intent broadcast = new Intent(); + broadcast.setAction("blob.loaded"); + mContext.sendBroadcast(broadcast); + } + }); + } /*** * Gets a SAS URL for a new blob so we can upload it to the server diff --git a/GlassShare/src/main/java/com/w9jds/glassshare/MainActivity.java b/GlassShare/src/main/java/com/w9jds/glassshare/MainActivity.java index 2a7d0f1..76cf16b 100644 --- a/GlassShare/src/main/java/com/w9jds/glassshare/MainActivity.java +++ b/GlassShare/src/main/java/com/w9jds/glassshare/MainActivity.java @@ -9,6 +9,7 @@ import android.database.Cursor; import android.net.ConnectivityManager; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; @@ -32,7 +33,12 @@ import com.w9jds.glassshare.Classes.StorageApplication; import com.w9jds.glassshare.Classes.StorageService; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -69,6 +75,7 @@ protected void onCreate(Bundle savedInstanceState) if (mcmCon.getActiveNetworkInfo().isConnected()) { + StorageApplication myApp = (StorageApplication) getApplication(); mStorageService = myApp.getStorageService(); } @@ -161,8 +168,10 @@ public boolean onCreateOptionsMenu(Menu menu) } @Override - public boolean onOptionsItemSelected(android.view.MenuItem iItem) { - switch (iItem.getItemId()) { + public boolean onOptionsItemSelected(android.view.MenuItem iItem) + { + switch (iItem.getItemId()) + { case R.id.delete_menu_item: //set the text as deleting iItem.setTitle(R.string.deleting_label); @@ -221,13 +230,11 @@ public boolean onOptionsItemSelected(android.view.MenuItem iItem) { sContainer = myAccounts[i].name; } -// item.image = BitmapFactory.decodeFile(mlsPaths.get(iPosition)); +// mStorageService.getTables(); + + mStorageService.addContainer(sContainer, false); +// mStorageService.getSasForNewBlob(sContainer, "testimage"); -// Intent blobDetailsIntent = new Intent(getApplicationContext(), ImageItem.class); -// blobDetailsIntent.putExtra("ContainerName", sContainer); -// blobDetailsIntent.putExtra("BlobName", "img1"); -// blobDetailsIntent.putExtra("BlobPosition", iPosition); -// startActivity(blobDetailsIntent); } return true; @@ -240,72 +247,72 @@ public boolean onOptionsItemSelected(android.view.MenuItem iItem) { /*** * Handles uploading an image to a specified url */ -// class ImageUploaderTask extends AsyncTask -// { -// private String mUrl; -// -// public ImageUploaderTask(String url) -// { -// mUrl = url; -// } -// -// @Override -// protected Boolean doInBackground(Void... params) { -// try { -// //Get the image data -// Cursor cursor = getContentResolver().query(mImageUri, null,null, null, null); + class ImageUploaderTask extends AsyncTask + { + private String mUrl; + + public ImageUploaderTask(String url) + { + mUrl = url; + } + + @Override + protected Boolean doInBackground(Void... params) { + try { + //Get the image data +// Cursor cursor = getContentResolver().query(mImageUri, null, null, null, null); // cursor.moveToFirst(); -// -// FileInputStream fiStream = new FileInputStream(mlsPaths.get(iPosition)); -// -// int bytesRead = 0; -// ByteArrayOutputStream bos = new ByteArrayOutputStream(); -// -// byte[] b = new byte[1024]; -// while ((bytesRead = fiStream.read(b)) != -1) -// { -// bos.write(b, 0, bytesRead); -// } -// byte[] bytes = bos.toByteArray(); -// -// // Post our image data (byte array) to the server -// HttpURLConnection urlConnection = (HttpURLConnection) new URL(mUrl.replace("\"", "")).openConnection(); -// urlConnection.setDoOutput(true); -// urlConnection.setRequestMethod("PUT"); -// urlConnection.addRequestProperty("Content-Type", "image/jpeg"); -// urlConnection.setRequestProperty("Content-Length", ""+ bytes.length); -// -// // Write image data to server -// DataOutputStream doStream = new DataOutputStream(urlConnection.getOutputStream()); -// doStream.write(bytes); -// doStream.flush(); -// doStream.close(); -// -// int response = urlConnection.getResponseCode(); -// -// //If we successfully uploaded, return true -// if (response == 201 && urlConnection.getResponseMessage().equals("Created")) -// return true; -// -// } -// -// catch (Exception ex) + + FileInputStream fiStream = new FileInputStream(mlsPaths.get(iPosition)); + + int bytesRead; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + byte[] b = new byte[1024]; + while ((bytesRead = fiStream.read(b)) != -1) + { + bos.write(b, 0, bytesRead); + } + byte[] bytes = bos.toByteArray(); + + // Post our image data (byte array) to the server + HttpURLConnection urlConnection = (HttpURLConnection) new URL(mUrl.replace("\"", "")).openConnection(); + urlConnection.setDoOutput(true); + urlConnection.setRequestMethod("PUT"); + urlConnection.addRequestProperty("Content-Type", "image/jpeg"); + urlConnection.setRequestProperty("Content-Length", ""+ bytes.length); + + // Write image data to server + DataOutputStream doStream = new DataOutputStream(urlConnection.getOutputStream()); + doStream.write(bytes); + doStream.flush(); + doStream.close(); + + int response = urlConnection.getResponseCode(); + + //If we successfully uploaded, return true + if (response == 201 && urlConnection.getResponseMessage().equals("Created")) + return true; + + } + + catch (Exception ex) + { + Log.e("GlassShareDebug", ex.getMessage()); + } + return false; + } + + @Override + protected void onPostExecute(Boolean uploaded) + { +// if (uploaded) // { -// Log.e("GlassShareDebug", ex.getMessage()); +// mAlertDialog.cancel(); +// mStorageService.getBlobsForContainer(mContainerName); // } -// return false; -// } -// -// @Override -// protected void onPostExecute(Boolean uploaded) -// { -//// if (uploaded) -//// { -//// mAlertDialog.cancel(); -//// mStorageService.getBlobsForContainer(mContainerName); -//// } -// } -// } + } + } private void saveFileToDrive(String sPath) {