Progressed. Getting closer to completing the contentprovider.

This commit is contained in:
Nils Norman Haukås 2015-04-18 22:58:48 +02:00
parent f8327b51f2
commit 59e1132d3e
9 changed files with 198 additions and 33 deletions

View File

@ -13,7 +13,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -1,21 +1,50 @@
package no.nilsnh.uibevents;
import android.app.Application;
import android.content.ContentValues;
import android.test.AndroidTestCase;
import android.test.ApplicationTestCase;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import no.nilsnh.uibevents.data.EventContract;
import no.nilsnh.uibevents.data.EventDbHelper;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
public class ApplicationTest extends AndroidTestCase {
public void setUp() {}
public void testFetchFromApi() throws Throwable {
EventDbHelper db = new EventDbHelper();
String result = db.fetchWebEventData();
assertNotNull(result);
}
public void testEventDb() throws Throwable {
public void testParseJsonEvent() {
ArrayList<ContentValues> events = null;
try {
events = EventDbHelper.parseJsonEvents(TestUtils.getExampleJson());
} catch (JSONException e) {
e.printStackTrace();
}
assertTrue(events.size() == 2);
}
public void testInsertIntoTextFile() {
ArrayList<ContentValues> events = null;
try {
events = EventDbHelper.parseJsonEvents(TestUtils.getExampleJson());
} catch (JSONException e) {
e.printStackTrace();
}
getContext().getContentResolver()
.insert(EventContract.EventEntry.CONTENT_URI, events.get(0));
EventDbHelper db = new EventDbHelper();
db.fetchWebEventData();
assertTrue(db.getStoredData().contains(events.get(0)));
}
}

View File

@ -0,0 +1,33 @@
package no.nilsnh.uibevents;
public class TestUtils {
public static String getExampleJson() {
return "{\n" +
" \"from_date\": \"2015-04-16T16:40:41+02:00\",\n" +
" \"events\": [\n" +
" {\n" +
" \"type\": \"exhibition\",\n" +
" \"title\": \" The Norwegian Constitution \\\"VI: 1814 - 2014\\\"\",\n" +
" \"date_from\": \"2014-05-11 22:00:00Z\",\n" +
" \"date_to\": \"2016-12-31 15:00:00Z\",\n" +
" \"location\": null,\n" +
" \"lead\": null,\n" +
" \"id\": 70450,\n" +
" \"path\": \"universitymuseum/70450/norwegian-constitution-vi-1814-2014\"\n" +
" },\n" +
" {\n" +
" \"type\": \"exhibition\",\n" +
" \"title\": \"Grunnlovsutstillingen \\\"Vi 1814-2014\\\"\",\n" +
" \"date_from\": \"2014-05-12 10:00:00Z\",\n" +
" \"date_to\": \"2016-12-31 11:00:00Z\",\n" +
" \"location\": \"De kulturhistoriske samlinger, Haakon Sheteligs plass 10\",\n" +
" \"lead\": \"En annerledes og nyskapende utstilling om demokrati for ungdom og voksne. \",\n" +
" \"id\": 48468,\n" +
" \"path\": \"grunnlovsjubileet2014/48468/grunnlovsutstillingen-vi-1814-2014\"\n" +
" }\n" +
" ]\n" +
"}";
}
}

View File

@ -18,6 +18,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="@string/content_authority"
android:name=".data.EventProvider"
android:exported="false"
android:syncable="true" />
</application>
</manifest>

View File

@ -0,0 +1,21 @@
package no.nilsnh.uibevents.data;
import android.content.ContentValues;
import android.net.Uri;
import java.util.Date;
public class Event {
private String id;
private String type;
private String title;
private Date dateFrom;
private Date dateTo;
private String location;
private String details;
private Uri url;
public Event(ContentValues values) {
}
}

View File

@ -2,6 +2,7 @@ package no.nilsnh.uibevents.data;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.BaseColumns;
@ -13,25 +14,27 @@ public class EventContract {
public static final String PATH_EVENT = "event";
public static final class EventEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI;
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_EVENT).build();
public static final String CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_EVENT;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_EVENT;
public static final Integer COLUMN_EVENT_ID = 1;
public static final Integer COLUMN_EVENT_TYPE = 2;
public static final Integer COLUMN_EVENT_TITLE = 3;
public static final Integer COLUMN_EVENT_DATE_FROM = 4;
public static final Integer COLUMN_EVENT_DATE_TO = 5;
public static final Integer COLUMN_EVENT_DATE_LOCATION = 6;
public static final Integer COLUMN_EVENT_DETAILS = 7;
public static final Integer COLUMN_EVENT_URL = 8;
public static final String TABLE_NAME = "event";
public static final String COLUMN_EVENT_ID = "id";
public static final String COLUMN_EVENT_TYPE = "type";
public static final String COLUMN_EVENT_TITLE = "title";
public static final String COLUMN_EVENT_DATE_FROM = "date_from";
public static final String COLUMN_EVENT_DATE_TO = "date_to";
public static final String COLUMN_EVENT_LOCATION = "location";
public static final String COLUMN_EVENT_DETAILS = "details";
public static final String COLUMN_EVENT_URL = "url";
public static Uri buildEventUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
}

View File

@ -4,7 +4,9 @@ import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@ -17,6 +19,8 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
public class EventDbHelper {
@ -34,7 +38,7 @@ public class EventDbHelper {
this.ctx = context;
}
public void fetchWebEventData() {
public String fetchWebEventData() {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
@ -55,7 +59,7 @@ public class EventDbHelper {
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return;
return eventJsonStr;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
@ -69,7 +73,7 @@ public class EventDbHelper {
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return;
return eventJsonStr;
}
eventJsonStr = buffer.toString();
} catch (IOException e) {
@ -88,7 +92,36 @@ public class EventDbHelper {
}
}
}
return;
return eventJsonStr;
}
public static ArrayList<ContentValues> parseJsonEvents(String data) throws JSONException {
ArrayList<ContentValues> parsedEvents = new ArrayList<>();
JSONArray events = new JSONObject(data).getJSONArray("events");
for (int i = 0; i < events.length(); i++) {
JSONObject event = events.getJSONObject(i);
Integer id = event.getInt("id");
String type = event.getString("type");
String title = event.getString("title");
String date_from = event.getString("date_from");
String date_to = event.getString("date_to");
String location = event.getString("location");
String details = event.getString("lead");
String url = event.getString("path");
ContentValues cValues = new ContentValues();
event.put(EventContract.EventEntry.COLUMN_EVENT_ID, id);
event.put(EventContract.EventEntry.COLUMN_EVENT_TYPE, type);
event.put(EventContract.EventEntry.COLUMN_EVENT_TITLE, title);
event.put(EventContract.EventEntry.COLUMN_EVENT_DATE_FROM, date_from);
event.put(EventContract.EventEntry.COLUMN_EVENT_DATE_TO, date_to);
event.put(EventContract.EventEntry.COLUMN_EVENT_LOCATION, location);
event.put(EventContract.EventEntry.COLUMN_EVENT_DETAILS, details);
event.put(EventContract.EventEntry.COLUMN_EVENT_URL, url);
parsedEvents.add(cValues);
}
return parsedEvents;
}
public void saveFile(String data) {
@ -104,40 +137,81 @@ public class EventDbHelper {
}
}
public String getStoredData() {
public ArrayList<ContentValues> getStoredData() {
File file = new File(filename);
if(file.exists()){
//Read text from file
StringBuilder text = new StringBuilder();
ArrayList<String> eventStrings = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('n');
eventStrings.add(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
return text.toString();
String[] eventDetailsString = null;
ContentValues eventDetailsCValue = new ContentValues();
ArrayList<ContentValues> events = new ArrayList<>();
for (String event: eventStrings) {
eventDetailsString = event.split(";");
eventDetailsCValue.put("id", eventDetailsString[0]);
eventDetailsCValue.put("type", eventDetailsString[1]);
eventDetailsCValue.put("title", eventDetailsString[2]);
eventDetailsCValue.put("date_from", eventDetailsString[3]);
eventDetailsCValue.put("date_to", eventDetailsString[4]);
eventDetailsCValue.put("location", eventDetailsString[5]);
eventDetailsCValue.put("details", eventDetailsString[6]);
eventDetailsCValue.put("url", eventDetailsString[7]);
events.add(eventDetailsCValue);
}
return events;
}
else {
return "Was not able to find file";
Log.d(LOG_TAG, "Was not able to find file");
return null;
}
}
public Long insert(ContentValues values) {
File file = new File(ctx.getFilesDir(),filename);
//First retrieve all data,then check if the value is already stored.
ArrayList<ContentValues> storedValues = getStoredData();
//If no data exist already create new arrayList
if (storedValues == null) storedValues = new ArrayList<>();
//If the new data is already there stop execution.
if (storedValues.contains(values)) return null;
storedValues.add(values);
Integer storedDataPosition = storedValues.indexOf(values);
//Write new data to textFile
try {
if (!file.exists()) file.createNewFile();
BufferedWriter buf = new BufferedWriter(new FileWriter(filename, true));
for (ContentValues event : storedValues) {
buf.write(event.getAsString("id") + ";");
buf.write(event.getAsString("type") + ";");
buf.write(event.getAsString("title") + ";");
buf.write(event.getAsString("date_from") + ";");
buf.write(event.getAsString("date_to") + ";");
buf.write(event.getAsString("location") + ";");
buf.write(event.getAsString("details") + ";");
buf.write(event.getAsString("url"));
buf.newLine();
}
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
return Long.valueOf(storedDataPosition);
}
}

View File

@ -6,13 +6,12 @@ import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
public class EventProvider extends ContentProvider{
public class EventProvider extends ContentProvider {
private EventDbHelper eventDbHelper;
private static final UriMatcher sUriMatcher = buildUriMatcher();
static final int EVENT = 100;
@Override
public boolean onCreate() {
eventDbHelper = new EventDbHelper(getContext());

View File

@ -3,4 +3,5 @@
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="content_authority">no.nilsnh.uibevents</string>
</resources>