Skip to content

Commit

Permalink
Added notification
Browse files Browse the repository at this point in the history
  • Loading branch information
HiiYL committed Apr 22, 2015
1 parent c4c1040 commit 725ca06
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 203 deletions.
389 changes: 195 additions & 194 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
if(extras != null) {
ANNOUNCEMENT_ID = extras.getString("ANNOUNCEMENT_ID");
Log.d("RECEIVED", "NOT NULL ANNOUNCEMENT_ID = " + ANNOUNCEMENT_ID);
} else {
Log.d("RECEIVED", "NULL ANNOUNCEMENT_ID = " + ANNOUNCEMENT_ID);
}

}
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/com/github/hiiyl/mmuhub/MMLSActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
Expand Down Expand Up @@ -36,6 +38,7 @@
import com.gc.materialdesign.views.ButtonFloat;
import com.github.hiiyl.mmuhub.data.MMUContract;
import com.github.hiiyl.mmuhub.data.MMUDbHelper;
import com.github.hiiyl.mmuhub.sync.MMUSyncAdapter;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -65,12 +68,13 @@ public class MMLSActivity extends ActionBarActivity{
private static int mPosition = 1;
private static RequestQueue queue;
private static String DOWNLOAD_TAG = "download_notes";

/**
* The {@link ViewPager} that will host the section contents.
*/
static ViewPager mViewPager;



@Override
protected void onCreate(Bundle savedInstanceState) {

Expand Down Expand Up @@ -138,6 +142,7 @@ private static boolean subjectHasFiles(int position) {
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down Expand Up @@ -250,6 +255,27 @@ public PlaceholderFragment() {
}


private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("RECEIVING STUFF", "FRAGMENT RECEIVES NOTIFICATION");
Cursor new_cursor = MainActivity.database.query(MMUContract.WeekEntry.TABLE_NAME, null, "subject_id = ?",new String[] {slide_str},null,null,null);
mAdapter.changeCursor(new_cursor);
mAdapter.notifyDataSetChanged();
}
};

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onStart();
getActivity().registerReceiver(syncFinishedReceiver, new IntentFilter(MMUSyncAdapter.SYNC_FINISHED));
}
@Override
public void onPause() {
getActivity().unregisterReceiver(syncFinishedReceiver);
super.onStop();
}



Expand Down Expand Up @@ -298,6 +324,7 @@ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition

return rootView;
}

private void refreshContent(Context context, String subject_id) {
updateSubject(context, subject_id);
}
Expand Down Expand Up @@ -534,7 +561,6 @@ public void refreshTokenAndRetry(final Context context, final String subject_id)
progressDialog.setMessage("Please Wait");
progressDialog.show();
String url = "https://mmu-api.herokuapp.com/refresh_token";
// String url = "https://mmu-api.herokuapp.com/login_test.json";
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/github/hiiyl/mmuhub/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -53,6 +55,9 @@ public class MainActivity extends ActionBarActivity {
private Button pager_button;
private MMUDbHelper mmuDbHelper;
public static SQLiteDatabase database;
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -64,7 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
SharedPreferences.Editor editor = prefs.edit();
mmuDbHelper = new MMUDbHelper(this);

MMUSyncAdapter.initializeSyncAdapter(this);

database = mmuDbHelper.getWritableDatabase();

Expand All @@ -75,6 +79,8 @@ protected void onCreate(Bundle savedInstanceState) {
finish();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}else {
MMUSyncAdapter.initializeSyncAdapter(this);
}
setContentView(R.layout.activity_main);
welcome_text = (TextView) findViewById(R.id.welcome_text);
Expand Down
42 changes: 36 additions & 6 deletions app/src/main/java/com/github/hiiyl/mmuhub/sync/MMUSyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SyncRequest;
import android.content.SyncResult;
Expand All @@ -17,6 +19,8 @@
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

import com.android.volley.AuthFailureError;
Expand All @@ -28,6 +32,7 @@
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.github.hiiyl.mmuhub.AnnouncementDetailActivity;
import com.github.hiiyl.mmuhub.MainActivity;
import com.github.hiiyl.mmuhub.R;
import com.github.hiiyl.mmuhub.Utility;
Expand All @@ -47,8 +52,10 @@
* Created by Hii on 4/22/15.
*/
public class MMUSyncAdapter extends AbstractThreadedSyncAdapter {
public static final String SYNC_FINISHED = "sync_finishedd";
public static final int SYNC_INTERVAL = 60 * 180;
public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3;
private static final int ANNOUNCEMENT_NOTIFICATION_ID = 3004;
SQLiteDatabase database;
MMUDbHelper helper;
private RequestQueue sync_queue;
Expand All @@ -62,6 +69,7 @@ public void onPerformSync(Account account, Bundle extras, String authority, Cont
Log.d(LOG_TAG, "onPerformSync Called.");
helper = new MMUDbHelper(getContext());
database = helper.getWritableDatabase();
String s = "1";
Cursor cursor= database.query(MMUContract.SubjectEntry.TABLE_NAME, null, null, null,null,null,null);
if(cursor.moveToFirst()) {
for (int i = 1; i < cursor.getCount() + 1; i++)
Expand Down Expand Up @@ -216,6 +224,24 @@ public void onResponse(String response) {
announcementValues.put(MMUContract.AnnouncementEntry.COLUMN_POSTED_DATE, announcement_posted_date);
announcementValues.put(MMUContract.AnnouncementEntry.COLUMN_SUBJECT_KEY, subject_id);
long _id = MainActivity.database.insert(MMUContract.AnnouncementEntry.TABLE_NAME, null, announcementValues);

Intent resultIntent = new Intent(getContext(), AnnouncementDetailActivity.class);
resultIntent.putExtra("ANNOUNCEMENT_ID",Long.toString(_id));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.ic_file_download_white_48dp)
.setContentTitle(announcement_title)
.setContentText(announcement_author);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager manager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(ANNOUNCEMENT_NOTIFICATION_ID, notificationBuilder.build());
}
}
}
Expand Down Expand Up @@ -250,6 +276,9 @@ public void onResponse(String response) {
fileValues.put(MMUContract.FilesEntry.COLUMN_REMOTE_FILE_PATH, remote_file_path);
fileValues.put(MMUContract.FilesEntry.COLUMN_SUBJECT_KEY, subject_id);
long _id = MainActivity.database.insert(MMUContract.FilesEntry.TABLE_NAME, null, fileValues);



}

}
Expand All @@ -258,6 +287,8 @@ public void onResponse(String response) {
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(SYNC_FINISHED);
getContext().sendBroadcast(i);
}
}, new Response.ErrorListener() {
String json = null;
Expand All @@ -271,8 +302,9 @@ public void onErrorResponse(VolleyError error) {
if (networkResponse != null && networkResponse.data != null) {
switch (networkResponse.statusCode) {
case 400:
sync_queue.stop();
refreshTokenAndRetry(context,subject_id);
Log.d("HELLO THERE~", "HTTTP 400");
sync_queue.cancelAll(SYNC_TAG);
refreshTokenAndRetry(context, subject_id);
break;
default:
}
Expand Down Expand Up @@ -306,9 +338,8 @@ public Map<String, String> getHeaders() throws AuthFailureError {
}
public void refreshTokenAndRetry(final Context context, final String subject_id) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final ProgressDialog progressDialog = new ProgressDialog(context);
String url = "https://mmu-api.herokuapp.com/refresh_token";
// String url = "https://mmu-api.herokuapp.com/login_test.json";
Log.d("HELLO THERE~", "PREPARING TO REFRESH TOKEN");
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Expand All @@ -326,7 +357,6 @@ public void onResponse(String response) {
String json = null;
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Log.d("Token", "Refresh unsuccessful");
}
}){
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
Expand Down Expand Up @@ -53,3 +58,17 @@


</RelativeLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_mmls.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
Expand Down Expand Up @@ -33,3 +39,4 @@
materialdesign:animate="true"
materialdesign:iconDrawable="@drawable/ic_file_download_white_48dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

0 comments on commit 725ca06

Please sign in to comment.