Commit 781d0084 authored by yvsriram's avatar yvsriram

Added newsfeed table for offline events access and gcm notifications

parent e02df0a9
...@@ -10,6 +10,7 @@ public class DatabaseContract { ...@@ -10,6 +10,7 @@ public class DatabaseContract {
public static final String PATH_USER_PROFILE = "userProfile"; public static final String PATH_USER_PROFILE = "userProfile";
public static final String PATH_USER_FOLLOWERS = "userFollowers"; public static final String PATH_USER_FOLLOWERS = "userFollowers";
public static final String PATH_USER_FOLLOWS = "userFollows"; public static final String PATH_USER_FOLLOWS = "userFollows";
public static final String PATH_NEWS_FEED = "newsFeed";
public static final class MapEntry implements BaseColumns { public static final class MapEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon() public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
...@@ -68,5 +69,20 @@ public class DatabaseContract { ...@@ -68,5 +69,20 @@ public class DatabaseContract {
} }
public static final class NewsFeedEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_NEWS_FEED)
.build();
public static final String TABLE_NAME = "newsFeed";
public static final String COLUMN_EVENT_NAME = "event_name";
public static final String COLUMN_EVENT_DESCRIPTION = "event_description";
public static final String COLUMN_EVENT_IMAGE = "event_image";
public static final String COLUMN_EVENT_CREATOR_NAME = "event_creator_name";
public static final String COLUMN_EVENT_CREATOR_ID = "event_creator_id";
public static final String COLUMN_EVENT_GOING_STATUS = "event_going_status";
}
} }
...@@ -50,10 +50,20 @@ public class DatabaseHelper extends SQLiteOpenHelper { ...@@ -50,10 +50,20 @@ public class DatabaseHelper extends SQLiteOpenHelper {
DatabaseContract.UserFollowsEntry.COLUMN_USER_NAME + " TEXT NOT NULL, " + DatabaseContract.UserFollowsEntry.COLUMN_USER_NAME + " TEXT NOT NULL, " +
DatabaseContract.UserFollowsEntry.COLUMN_USER_PROFILE + " TEXT NOT NULL);"; DatabaseContract.UserFollowsEntry.COLUMN_USER_PROFILE + " TEXT NOT NULL);";
final String CREATE_TABLE_NEWS_FEED = "CREATE TABLE " + DatabaseContract.NewsFeedEntry.TABLE_NAME + " (" +
DatabaseContract.NewsFeedEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_NAME + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_ID + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_GOING_STATUS + " INTEGER NOT NULL);";
db.execSQL(CREATE_TABLE_MAP); db.execSQL(CREATE_TABLE_MAP);
db.execSQL(CREATE_TABLE_USER_PROFILE); db.execSQL(CREATE_TABLE_USER_PROFILE);
db.execSQL(CREATE_TABLE_USER_FOLLOWERS); db.execSQL(CREATE_TABLE_USER_FOLLOWERS);
db.execSQL(CREATE_TABLE_USER_FOLLOWS); db.execSQL(CREATE_TABLE_USER_FOLLOWS);
db.execSQL(CREATE_TABLE_NEWS_FEED);
} }
@Override @Override
...@@ -62,6 +72,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { ...@@ -62,6 +72,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserProfileEntry.TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserProfileEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowersEntry.TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowersEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowsEntry.TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowsEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.NewsFeedEntry.TABLE_NAME);
onCreate(db); onCreate(db);
} }
} }
...@@ -22,6 +22,8 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -22,6 +22,8 @@ public class IITBAppContentProvider extends ContentProvider {
public static final int USER_FOLLOWER_WITH_ID = 301; public static final int USER_FOLLOWER_WITH_ID = 301;
public static final int USER_FOLLOWS = 400; public static final int USER_FOLLOWS = 400;
public static final int USER_FOLLOWS_WITH_ID = 401; public static final int USER_FOLLOWS_WITH_ID = 401;
public static final int NEWS_FEED = 500;
public static final int NEWS_FEED_WITH_ID = 501;
private static final UriMatcher sUriMatcher = buildUriMatcher(); private static final UriMatcher sUriMatcher = buildUriMatcher();
private DatabaseHelper databaseHelper; private DatabaseHelper databaseHelper;
...@@ -35,6 +37,8 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -35,6 +37,8 @@ public class IITBAppContentProvider extends ContentProvider {
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWERS + "/#", USER_FOLLOWER_WITH_ID); matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWERS + "/#", USER_FOLLOWER_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS, USER_FOLLOWS); matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS, USER_FOLLOWS);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS + "/#", USER_FOLLOWS_WITH_ID); matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS + "/#", USER_FOLLOWS_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_NEWS_FEED, NEWS_FEED);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_NEWS_FEED + "/#", NEWS_FEED_WITH_ID);
return matcher; return matcher;
} }
...@@ -135,6 +139,26 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -135,6 +139,26 @@ public class IITBAppContentProvider extends ContentProvider {
null, null,
sortOrder); sortOrder);
break; break;
case NEWS_FEED:
cursor = db.query(DatabaseContract.NewsFeedEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.NewsFeedEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
default: default:
throw new SQLException("Wrong Uri: " + uri); throw new SQLException("Wrong Uri: " + uri);
...@@ -165,6 +189,12 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -165,6 +189,12 @@ public class IITBAppContentProvider extends ContentProvider {
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS; return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS;
case USER_FOLLOWS_WITH_ID: case USER_FOLLOWS_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS; return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS;
case NEWS_FEED:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_NEWS_FEED;
case NEWS_FEED_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_NEWS_FEED;
default: default:
throw new UnsupportedOperationException("Unknown uri: " + uri); throw new UnsupportedOperationException("Unknown uri: " + uri);
} }
...@@ -207,6 +237,14 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -207,6 +237,14 @@ public class IITBAppContentProvider extends ContentProvider {
} else } else
throw new SQLException("Failed to insert row into " + uri); throw new SQLException("Failed to insert row into " + uri);
break; break;
case NEWS_FEED:
id = db.insert(DatabaseContract.NewsFeedEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.NewsFeedEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
default: default:
throw new SQLException("Wrong uri: " + uri); throw new SQLException("Wrong uri: " + uri);
...@@ -301,6 +339,27 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -301,6 +339,27 @@ public class IITBAppContentProvider extends ContentProvider {
db.endTransaction(); db.endTransaction();
} }
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
case NEWS_FEED:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.NewsFeedEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) { if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(uri, null);
} }
...@@ -309,6 +368,7 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -309,6 +368,7 @@ public class IITBAppContentProvider extends ContentProvider {
default: default:
return super.bulkInsert(uri, values); return super.bulkInsert(uri, values);
} }
} }
@Override @Override
...@@ -347,6 +407,13 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -347,6 +407,13 @@ public class IITBAppContentProvider extends ContentProvider {
selectionArgs); selectionArgs);
break; break;
case NEWS_FEED:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.NewsFeedEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case LOC_WITH_ID: case LOC_WITH_ID:
...@@ -372,6 +439,12 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -372,6 +439,12 @@ public class IITBAppContentProvider extends ContentProvider {
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserFollowsEntry.TABLE_NAME, "_id=?", new String[]{id}); numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserFollowsEntry.TABLE_NAME, "_id=?", new String[]{id});
break; break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.NewsFeedEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
default: default:
throw new UnsupportedOperationException("Unknown uri: " + uri); throw new UnsupportedOperationException("Unknown uri: " + uri);
} }
...@@ -418,6 +491,12 @@ public class IITBAppContentProvider extends ContentProvider { ...@@ -418,6 +491,12 @@ public class IITBAppContentProvider extends ContentProvider {
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserFollowsEntry.TABLE_NAME, values, "_id=?", new String[]{id}); itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserFollowsEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break; break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.NewsFeedEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
default: default:
throw new UnsupportedOperationException("Unknown uri: " + uri); throw new UnsupportedOperationException("Unknown uri: " + uri);
} }
......
package in.ac.iitb.gymkhana.iitbapp.fragment; package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import in.ac.iitb.gymkhana.iitbapp.ItemClickListener; import in.ac.iitb.gymkhana.iitbapp.ItemClickListener;
...@@ -19,6 +23,7 @@ import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator; ...@@ -19,6 +23,7 @@ import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator;
import in.ac.iitb.gymkhana.iitbapp.api.model.Event; import in.ac.iitb.gymkhana.iitbapp.api.model.Event;
import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedRequest; import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedRequest;
import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse; import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse;
import in.ac.iitb.gymkhana.iitbapp.data.DatabaseContract;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
...@@ -45,6 +50,29 @@ public class FeedFragment extends Fragment { ...@@ -45,6 +50,29 @@ public class FeedFragment extends Fragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
Cursor cursor = getContext().getContentResolver().query(DatabaseContract.NewsFeedEntry.CONTENT_URI, null, null, null, null);
if (cursor.getCount() != 0) {
List<Event> events = new ArrayList<>();
while (cursor.moveToNext()) {
Event event = new Event(cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME)),
cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION)),
cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE)),
cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_NAME)),
cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_ID)),
cursor.getInt(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_GOING_STATUS)));
events.add(event);
}
FeedAdapter feedAdapter = new FeedAdapter(events, new ItemClickListener() {
@Override
public void onItemClick(View v, int position) {
// TODO: Launch EventFragment
}
});
feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
feedRecyclerView.setAdapter(feedAdapter);
feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
NewsFeedRequest newsFeedRequest = new NewsFeedRequest(NewsFeedRequest.FOLLOWED, 0, 20); NewsFeedRequest newsFeedRequest = new NewsFeedRequest(NewsFeedRequest.FOLLOWED, 0, 20);
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
...@@ -64,6 +92,22 @@ public class FeedFragment extends Fragment { ...@@ -64,6 +92,22 @@ public class FeedFragment extends Fragment {
feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view); feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
feedRecyclerView.setAdapter(feedAdapter); feedRecyclerView.setAdapter(feedAdapter);
feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
long itemsRemoved = getContext().getContentResolver().delete(DatabaseContract.NewsFeedEntry.CONTENT_URI, null, null);
Log.d("FeedFragment", itemsRemoved + " items removed.");
ContentValues contentValues[] = new ContentValues[events.size()];
for (int i = 0; i < events.size(); i++) {
ContentValues contentValues1 = new ContentValues();
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME, events.get(i).getEventName());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION, events.get(i).getEventDescription());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE, events.get(i).getEventImage());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_NAME, events.get(i).getEventCreatorName());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_ID, events.get(i).getEventCreatorId());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_GOING_STATUS, events.get(i).getEventEnthu());
contentValues[i] = contentValues1;
}
int insertCount = getContext().getContentResolver().bulkInsert(DatabaseContract.NewsFeedEntry.CONTENT_URI, contentValues);
Log.d("FeedFragment", Integer.toString(insertCount) + " elements inserted");
} }
//Server Error //Server Error
} }
......
package in.ac.iitb.gymkhana.iitbapp.gcm; package in.ac.iitb.gymkhana.iitbapp.gcm;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService; import com.google.android.gms.gcm.GcmListenerService;
import in.ac.iitb.gymkhana.iitbapp.MainActivity;
import in.ac.iitb.gymkhana.iitbapp.R;
public class MyGcmListenerService extends GcmListenerService { public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String s, Bundle bundle) {
String message = bundle.getString("message");
Log.d(TAG, "From: " + s);
Log.d(TAG, "Message: " + message);
sendNotification(message);
}
public void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//TODO Change the notification icon
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment