Commit dafbe24f authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #28 from yvsriram/master

 	Added newsfeed table for offline events access and gcm notifications
parents 96935707 5ac4a72e
......@@ -10,6 +10,7 @@ public class DatabaseContract {
public static final String PATH_USER_PROFILE = "userProfile";
public static final String PATH_USER_FOLLOWERS = "userFollowers";
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 Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
......@@ -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 {
DatabaseContract.UserFollowsEntry.COLUMN_USER_NAME + " 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_USER_PROFILE);
db.execSQL(CREATE_TABLE_USER_FOLLOWERS);
db.execSQL(CREATE_TABLE_USER_FOLLOWS);
db.execSQL(CREATE_TABLE_NEWS_FEED);
}
@Override
......@@ -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.UserFollowersEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowsEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.NewsFeedEntry.TABLE_NAME);
onCreate(db);
}
}
......@@ -22,6 +22,8 @@ public class IITBAppContentProvider extends ContentProvider {
public static final int USER_FOLLOWER_WITH_ID = 301;
public static final int USER_FOLLOWS = 400;
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 DatabaseHelper databaseHelper;
......@@ -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_FOLLOWS, USER_FOLLOWS);
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;
}
......@@ -135,6 +139,26 @@ public class IITBAppContentProvider extends ContentProvider {
null,
sortOrder);
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:
throw new SQLException("Wrong Uri: " + uri);
......@@ -165,6 +189,12 @@ public class IITBAppContentProvider extends ContentProvider {
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS;
case USER_FOLLOWS_WITH_ID:
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:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
......@@ -207,6 +237,14 @@ public class IITBAppContentProvider extends ContentProvider {
} else
throw new SQLException("Failed to insert row into " + uri);
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:
throw new SQLException("Wrong uri: " + uri);
......@@ -301,6 +339,27 @@ public class IITBAppContentProvider extends ContentProvider {
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) {
getContext().getContentResolver().notifyChange(uri, null);
}
......@@ -309,6 +368,7 @@ public class IITBAppContentProvider extends ContentProvider {
default:
return super.bulkInsert(uri, values);
}
}
@Override
......@@ -347,6 +407,13 @@ public class IITBAppContentProvider extends ContentProvider {
selectionArgs);
break;
case NEWS_FEED:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.NewsFeedEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case LOC_WITH_ID:
......@@ -372,6 +439,12 @@ public class IITBAppContentProvider extends ContentProvider {
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserFollowsEntry.TABLE_NAME, "_id=?", new String[]{id});
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:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
......@@ -418,6 +491,12 @@ public class IITBAppContentProvider extends ContentProvider {
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserFollowsEntry.TABLE_NAME, values, "_id=?", new String[]{id});
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:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
......
package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
......@@ -8,12 +10,16 @@ import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import in.ac.iitb.gymkhana.iitbapp.Constants;
......@@ -25,6 +31,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.NewsFeedRequest;
import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse;
import in.ac.iitb.gymkhana.iitbapp.data.DatabaseContract;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -52,6 +59,37 @@ public class FeedFragment extends Fragment {
@Override
public void onStart() {
super.onStart();
Cursor cursor = getContext().getContentResolver().query(DatabaseContract.NewsFeedEntry.CONTENT_URI, null, null, null, null);
if (cursor.getCount() != 0) {
final 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) {
String eventJson = new Gson().toJson(events.get(position));
Bundle bundle = new Bundle();
bundle.putString(Constants.EVENT_JSON, eventJson);
EventFragment eventFragment = new EventFragment();
eventFragment.setArguments(bundle);
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.framelayout_for_fragment, eventFragment, eventFragment.getTag());
transaction.commit();
}
});
feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
feedRecyclerView.setAdapter(feedAdapter);
feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
updateFeed();
......@@ -91,6 +129,22 @@ public class FeedFragment extends Fragment {
feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
feedRecyclerView.setAdapter(feedAdapter);
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
feedSwipeRefreshLayout.setRefreshing(false);
......
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 in.ac.iitb.gymkhana.iitbapp.MainActivity;
import in.ac.iitb.gymkhana.iitbapp.R;
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