Last active
December 15, 2015 13:38
-
-
Save ChrisRisner/5268441 to your computer and use it in GitHub Desktop.
Android Mobile Services Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addContainer(String containerName, boolean isPublic) { | |
//Creating a json object with the container name | |
JsonObject newContainer = new JsonObject(); | |
newContainer.addProperty("containerName", containerName); | |
//Passing over the public flag as a parameter | |
List<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("isPublic", isPublic ? "1" : "0")); | |
mTableContainers.insert(newContainer, 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 containers from the server | |
getContainers(); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addTableRow(final String tableName, List<Pair<String,String>> tableRowData) { | |
//Create a new json object with the key value pairs | |
JsonObject newRow = new JsonObject(); | |
for (Pair<String,String> pair : tableRowData) { | |
newRow.addProperty(pair.first, pair.second); | |
} | |
//Pass the table name over in parameters | |
List<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public StorageService(Context context) { | |
mContext = context; | |
try { | |
mClient = new MobileServiceClient("https://mobileserviceurl.azure-mobile.net/", "applicationkey", mContext); | |
mTableTables = mClient.getTable("Tables"); | |
mTableTableRows = mClient.getTable("TableRows"); | |
mTableContainers = mClient.getTable("BlobContainers"); | |
mTableBlobs = mClient.getTable("BlobBlobs"); | |
} catch (MalformedURLException e) { | |
Log.e(TAG, "There was an error creating the Mobile Service. Verify the URL"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("containerName", containerName)); | |
parameters.add(new Pair<String, String>("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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void deleteContainer(String containerName) { | |
//Create the json Object we'll send over and fill it with the required | |
//id property - otherwise we'll get kicked back | |
JsonObject container = new JsonObject(); | |
container.addProperty("id", 0); | |
//Create parameters to pass in the container details. We do this with params | |
//because it would be stripped out if we put it on the container object | |
List<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("containerName", containerName)); | |
mTableContainers.delete(container, parameters, new TableDeleteCallback() { | |
@Override | |
public void onCompleted(Exception exception, ServiceFilterResponse response) { | |
if (exception != null) { | |
Log.e(TAG, exception.getCause().getMessage()); | |
return; | |
} | |
//Refetch containers from the server | |
getContainers(); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("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(); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("tableName", tableName)); | |
parameters.add(new Pair<String, String>("rowKey", rowKey)); | |
parameters.add(new Pair<String, String>("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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("containerName", containerName)); | |
parameters.add(new Pair<String, String>("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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Map<String, String>>(); | |
mBlobObjects = new ArrayList<JsonElement>(); | |
for (int i = 0; i < results.size(); i ++) { | |
JsonElement item = results.get(i); | |
mBlobObjects.add(item); | |
Map<String, String> map = new HashMap<String, String>(); | |
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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void getContainers() { | |
mTableContainers.where().execute(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 and build an array of container names | |
JsonArray results = result.getAsJsonArray(); | |
mContainers = new ArrayList<Map<String, String>>(); | |
for (int i = 0; i < results.size(); i ++) { | |
JsonElement item = results.get(i); | |
Map<String, String> map = new HashMap<String, String>(); | |
map.put("ContainerName", item.getAsJsonObject().getAsJsonPrimitive("name").getAsString()); | |
mContainers.add(map); | |
} | |
//Broadcast that the containers have been loaded | |
Intent broadcast = new Intent(); | |
broadcast.setAction("containers.loaded"); | |
mContext.sendBroadcast(broadcast); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void getSasForNewBlob(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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("containerName", containerName)); | |
parameters.add(new Pair<String, String>("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 we are ready to upload the blob data | |
Intent broadcast = new Intent(); | |
broadcast.setAction("blob.created"); | |
mContext.sendBroadcast(broadcast); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<JsonElement>(); | |
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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void getTables() { | |
mTableTables.where().execute(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(); | |
mTables = new ArrayList<Map<String, String>>(); | |
//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<String, String> map = new HashMap<String, String>(); | |
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); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void updateTableRow(final String tableName, List<Pair<String,String>> tableRowData) { | |
//Create a new json object with the key value pairs | |
JsonObject newRow = new JsonObject(); | |
for (Pair<String,String> 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<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>(); | |
parameters.add(new Pair<String, String>("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); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment