Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/bin
/gen
local.properties
res/raw/config.properties
4 changes: 4 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@
android:configChanges="orientation|keyboardHidden|screenSize"
android:launchMode="singleTask" >
</activity>

<!-- Notifications activities -->
<activity
android:name="org.wordpress.android.ui.notifications.NotificationsActivity" />

<!-- Services -->
<service
Expand Down
2 changes: 2 additions & 0 deletions project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ apk-configurations=
android.library=false
android.library.reference.1=../ActionBarSherlock/library
android.library.reference.2=../../../Downloads/android-menudrawer-master/library
android.library.reference.3=../wordpress-rest-android

33 changes: 33 additions & 0 deletions res/layout/note_list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
style="@style/WordPressListRowBackground" >
<ImageView
android:id="@+id/note_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp" />
<TextView
android:id="@+id/note_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/note_icon" />
<TextView
android:id="@+id/note_detail"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/note_label"
android:layout_alignLeft="@+id/note_label"
android:layout_marginTop="8dp"
android:maxLines="3"
android:textColor="#999999"
android:text="Hello World" />
</RelativeLayout>
17 changes: 17 additions & 0 deletions res/layout/notifications.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<fragment
android:name="org.wordpress.android.ui.notifications.NotificationsListFragment"
android:id="@+id/notes_list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
5 changes: 5 additions & 0 deletions res/raw/config.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Add your WordPress App credentials here
# Get you App ID from http://developer.wordpress.com
oauth.app_id=
oauth.app_secret=
oauth.redirect_uri=
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@
<!-- reader -->
<string name="reader">Reader</string>
<string name="topics">Topics</string>

<!-- notifications -->
<string name="notifications">Notifications</string>

<!-- Post Formats -->
<string-array name="post_formats_array">
Expand Down
25 changes: 24 additions & 1 deletion src/org/wordpress/android/WordPress.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package org.wordpress.android;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.util.Log;

import org.wordpress.android.models.Blog;
import org.wordpress.android.models.Comment;
import org.wordpress.android.models.Post;
import org.wordpress.android.util.WPRestClient;

public class WordPress extends Application {

Expand All @@ -24,16 +28,22 @@ public class WordPress extends Application {
public static OnPostUploadedListener onPostUploadedListener = null;
public static boolean postsShouldRefresh;
public static boolean shouldRestoreSelectedActivity;
public static WPRestClient restClient;
public static Properties config;

public static final String TAG="WordPress";

@Override
public void onCreate() {
loadProperties();
versionName = getVersionName();
wpDB = new WordPressDB(this);

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
if (settings.getInt("wp_pref_last_activity", -1) >= 0)
shouldRestoreSelectedActivity = true;


restClient = new WPRestClient(config, settings);
super.onCreate();
}

Expand Down Expand Up @@ -153,4 +163,17 @@ public static Blog setCurrentBlog(int id) {

return currentBlog;
}
/**
* Load res/raw/config.properties into a Properties object
*/
private void loadProperties(){
config = new Properties();
InputStream stream = getResources().openRawResource(R.raw.config);
try {
config.load(stream);
} catch(java.io.IOException error){
config = null;
Log.e(TAG, "Could not load config.properties", error);
}
}
}
147 changes: 147 additions & 0 deletions src/org/wordpress/android/models/Note.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Note represents a single WordPress.com notification
*/
package org.wordpress.android.models;

import android.text.Html;
import android.util.Log;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

public class Note {
protected static final String TAG="NoteModel";
public static final String UNKNOWN_TYPE="unknown";
public static final String COMMENT_TYPE="comment";
public static final String LIKE_TYPE="like";

private static String QUERY_SEPERATOR=".";
private static String QUERY_ARRAY_INDEX_START="[";
private static String QUERY_ARRAY_INDEX_END="]";
private static String QUERY_ARRAY_FIRST="first";
private static String QUERY_ARRAY_LAST="last";

private JSONObject mNoteJSON;
/**
* Create a note using JSON from REST API
*/
public Note(JSONObject noteJSON){
mNoteJSON = noteJSON;
Log.d(TAG, String.format("Built note of type: %s", getType()));
}

public String toString(){
String labelText = queryJSON("subject.text", "");
String label = Html.fromHtml(labelText.trim()).toString();
return label;
}

public JSONObject toJSONObject(){
return mNoteJSON;
}

public String getType(){
return queryJSON("type", UNKNOWN_TYPE);
}
public Boolean isType(String type){
return getType().equals(type);
}
public Boolean isCommentType(){
return isType(COMMENT_TYPE);
}
public String getIconURL(){
return (String) queryJSON("subject.icon", "");
}
protected Object queryJSON(String query){
Object defaultObject = "";
return queryJSON(query, defaultObject);
}
/**
* Rudimentary system for pulling an item out of a JSON object hierarchy
*/
public <U extends Object> U queryJSON(String query, U defaultObject){
int offset = 0;
JSONObject queryJSON = mNoteJSON;
do {
// find the next . or [
int next_seperator = query.indexOf(QUERY_SEPERATOR, offset);
boolean has_next_seperator = next_seperator > -1;
int start_array = query.indexOf(QUERY_ARRAY_INDEX_START, offset);
// pull out the key up to the seperator
if (next_seperator == -1 && start_array == -1) {
try {
return (U) queryJSON.get(query.substring(offset));
} catch (JSONException e) {
Log.e(TAG, String.format("Failed to query %s", query), e);
return defaultObject;
}
}
String key;
if (start_array == -1 || (next_seperator > -1 && start_array > next_seperator )) {
try {
queryJSON = queryJSON.getJSONObject(query.substring(offset, next_seperator));
} catch (JSONException e) {
Log.e(TAG, String.format("Failed to query key %s", query), e);
return defaultObject;
}
offset = next_seperator+1;
continue;
}
if (next_seperator == -1 || start_array == -1) {
key = query.substring(offset, Math.max(next_seperator, start_array));
} else {
key = query.substring(offset, Math.min(next_seperator, start_array));
}
if (start_array > -1 && (start_array < next_seperator || !has_next_seperator)) {
// time to pull off arrays
try {
JSONArray arrayJSON = queryJSON.getJSONArray(key);
do {
int end_array = query.indexOf(QUERY_ARRAY_INDEX_END, start_array);
if (end_array <= start_array) break;
offset = end_array;
String index = query.substring(start_array+1, end_array);
int i;
if (index.equals(QUERY_ARRAY_FIRST)) {
i = 0;
} else if (index.equals(QUERY_ARRAY_LAST)) {
i = -1;
} else {
i = Integer.parseInt(index);
}
if (i < 0)
i = arrayJSON.length() + i;
start_array = query.indexOf(QUERY_ARRAY_INDEX_START, end_array);
// no more arrays and no seperator, end of query, return object at index
// e.g. key[0][0]
if (start_array == -1 && !has_next_seperator) {
return (U) arrayJSON.get(i);
}
// no more arrays but there's a seperator, we must have a JSONObject
if (start_array == -1 && has_next_seperator) {
queryJSON = arrayJSON.getJSONObject(i);
break;
}
// theres more query but this is the last array in this section
// eg key[0][0][0].something[0]
arrayJSON = arrayJSON.getJSONArray(i);
// the next item is an array, so pull the array off and continue
} while(start_array < next_seperator);
offset = next_seperator + 1;
continue;
} catch (JSONException e) {
Log.e(TAG, String.format("Failed to query array %s", query), e);
return defaultObject;
}
}
Log.d(TAG, String.format("We need to remove the key from %s", query.substring(offset, next_seperator)));
key = query.substring(offset, next_seperator);

if(true) return defaultObject;

} while(offset > 0);
return defaultObject;
}

}
Loading