Skip to content

Commit

Permalink
Added bulletin auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
HiiYL committed Apr 30, 2015
1 parent f585303 commit f789970
Show file tree
Hide file tree
Showing 27 changed files with 1,037 additions and 430 deletions.
679 changes: 355 additions & 324 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
android:name=".LoginActivity"
android:excludeFromRecents="true"
android:label="@string/title_activity_login"
android:configChanges="keyboardHidden|orientation|screenSize"
android:noHistory="true"
android:theme="@style/LoginActivityTheme" >
</activity>
Expand Down

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion app/src/main/java/com/github/hiiyl/mmuhub/BulletinActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.hiiyl.mmuhub.data.MMUContract;
import com.github.hiiyl.mmuhub.data.MMUDbHelper;
import com.github.hiiyl.mmuhub.helper.DownloadCompleteEvent;
import com.github.hiiyl.mmuhub.helper.ViewEvent;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -111,6 +112,14 @@ public void onEventMainThread(DownloadCompleteEvent event) {
mAdapter.changeCursor(newCursor);
}
}
public void onEventMainThread(ViewEvent event) {
if(event.message.equals(Utility.VIEW_BULLETIN_EVENT)) {
Log.d("ANNOUNCEMENT", "BULLETIN VIEW EVENT RECEIVED");
Cursor newCursor = MySingleton.getInstance(getActivity()).getDatabase().query(
MMUContract.BulletinEntry.TABLE_NAME, null, null, null, null, null, null);
mAdapter.changeCursor(newCursor);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down Expand Up @@ -227,7 +236,6 @@ public void onClick(View v) {
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getInstance(getActivity()).addToRequestQueue(sr);

}
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/github/hiiyl/mmuhub/BulletinAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.hiiyl.mmuhub.data.MMUContract;
Expand All @@ -28,8 +29,17 @@ public void bindView(View view, Context context, Cursor cursor) {
TextView bulletin_title = (TextView) view.findViewById(R.id.bulletin_title);
TextView bulletin_posted = (TextView) view.findViewById(R.id.bulletin_posted_date);
TextView bulletin_author = (TextView) view.findViewById(R.id.bulletin_author);
ImageView ivHasSeen = (ImageView) view.findViewById(R.id.imageview_has_seen);

if(cursor.getInt(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_HAS_SEEN)) == 0) {
ivHasSeen.setVisibility(View.VISIBLE);
}else {
ivHasSeen.setVisibility(View.INVISIBLE);
}


bulletin_title.setText(cursor.getString(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_TITLE)));
bulletin_author.setText(Integer.toString(cursor.getInt(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_HAS_SEEN))));
bulletin_author.setText(cursor.getString(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_AUTHOR)));
bulletin_posted.setText(cursor.getString(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_POSTED_DATE)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.hiiyl.mmuhub;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand All @@ -13,6 +14,9 @@
import android.widget.TextView;

import com.github.hiiyl.mmuhub.data.MMUContract;
import com.github.hiiyl.mmuhub.helper.ViewEvent;

import de.greenrobot.event.EventBus;


public class BulletinDetailActivity extends AppCompatActivity {
Expand Down Expand Up @@ -76,6 +80,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
TextView bulletin_contents = (TextView) rootView.findViewById(R.id.bulletin_detail_contents);
TextView bulletin_posted_at = (TextView) rootView.findViewById(R.id.bulletin_posted_date);

ContentValues has_seen = new ContentValues();
has_seen.put(MMUContract.BulletinEntry.COLUMN_HAS_SEEN, true);
MySingleton.getInstance(getActivity()).getDatabase().update(MMUContract.BulletinEntry.TABLE_NAME, has_seen, MMUContract.AnnouncementEntry._ID + "=?", new String[]{bulletin_id});
EventBus.getDefault().post(new ViewEvent(Utility.VIEW_BULLETIN_EVENT));
Cursor cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.BulletinEntry.TABLE_NAME, null, MMUContract.BulletinEntry._ID + "=?",new String[] {bulletin_id},null,null,null);
cursor.moveToFirst();
bulletin_title.setText(cursor.getString(cursor.getColumnIndex(MMUContract.BulletinEntry.COLUMN_TITLE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Log.d("Subject ID is ", subject_id);
mSubjectID = subject_id;
mSubjectName = Utility.getSubjectName(getActivity(), mSubjectID);
final Cursor cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? ",
final Cursor cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? AND " + MMUContract.FilesEntry.COLUMN_ANNOUNCEMENT_KEY + " IS NULL",
new String[] {subject_id}, null, null,null );
adapter = new MMLSDownloadAdapter(getActivity(), cursor);
download_list.setAdapter(adapter);
Expand All @@ -164,7 +164,7 @@ public void onMovedToScrapHeap(View view) {
EventBus.getDefault().post(new DownloadListRecycleEvent(download_list));
}
});
Cursor new_cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? ",
Cursor new_cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? AND " + MMUContract.FilesEntry.COLUMN_ANNOUNCEMENT_KEY + " IS NULL",
new String[]{subject_id}, null, null, null);
new_cursor.moveToFirst();
int count = 0;
Expand Down Expand Up @@ -224,7 +224,7 @@ public void onMovedToScrapHeap(View view) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


Cursor new_cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? ",
Cursor new_cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? AND " + MMUContract.FilesEntry.COLUMN_ANNOUNCEMENT_KEY + " IS NULL",
new String[]{subject_id}, null, null, null);
new_cursor.moveToPosition(position);

Expand Down Expand Up @@ -386,6 +386,7 @@ protected String doInBackground(String... sUrl) {
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.d("CONNECT", " FAILED AND RESPONSE CODE IS " + connection.getResponseCode());
if(connection.getResponseCode() == 302) {
Log.d("WOW!", "REFRESHING TOKEN");
Utility.refreshToken(context);
}
return "Server returned HTTP " + connection.getResponseCode()
Expand Down Expand Up @@ -472,6 +473,7 @@ protected void onPostExecute(String result) {
mWakeLock.release();
String download_result;
if (result != null) {
Log.d("RESULT IS", result);
if(isVisible) {
mProgressBar.setIndeterminate(false);
mProgressBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/github/hiiyl/mmuhub/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public void onResponse(String response) {
// editor.putString("icems_password", icems_password.getText().toString());
editor.putString("name", name);
editor.putString("faculty", faculty);
editor.putString("sync_interval", "60");
editor.putBoolean("sync_enabled", true);
editor.apply();
MMUSyncAdapter.syncImmediately(LoginActivity.this);
Intent intent = new Intent(mContext, MMLSActivity.class);
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/github/hiiyl/mmuhub/MMLSActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_mmls);

super.onCreateDrawer();

mDownloadButton = (ButtonFloat)findViewById(R.id.lecture_notes_download);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {

@Override
public void run() {
if(subjectHasFiles(0)) {
Expand All @@ -69,9 +69,6 @@ public void run() {
}
}
}, 200);



// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/github/hiiyl/mmuhub/MMLSAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorTreeAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.hiiyl.mmuhub.data.MMUContract;
Expand Down Expand Up @@ -56,10 +57,19 @@ protected View newChildView(Context context, Cursor cursor, boolean isLastChild,
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
ImageView ivHasSeen = (ImageView) view.findViewById(R.id.imageview_has_seen);


TextView tvTitle = (TextView) view.findViewById(R.id.listitem_mmls_title);
TextView tvPostedAt = (TextView) view.findViewById(R.id.list_item_mmls_posted_at);
TextView tvAuthor = (TextView) view.findViewById(R.id.list_item_mmls_author);

if(cursor.getInt(cursor.getColumnIndex(MMUContract.AnnouncementEntry.COLUMN_HAS_SEEN)) == 0) {
ivHasSeen.setVisibility(View.VISIBLE);
}else {
ivHasSeen.setVisibility(View.INVISIBLE);
}

String title = cursor.getString(cursor.getColumnIndex(MMUContract.AnnouncementEntry.COLUMN_TITLE));
String posted_at = cursor.getString(cursor.getColumnIndex(MMUContract.AnnouncementEntry.COLUMN_POSTED_DATE));
String author = cursor.getString(cursor.getColumnIndex(MMUContract.AnnouncementEntry.COLUMN_AUTHOR));
Expand Down
47 changes: 45 additions & 2 deletions app/src/main/java/com/github/hiiyl/mmuhub/MMLSFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.hiiyl.mmuhub.data.MMUContract;
import com.github.hiiyl.mmuhub.data.MMUDbHelper;
import com.github.hiiyl.mmuhub.helper.SyncEvent;
import com.github.hiiyl.mmuhub.helper.ViewEvent;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -106,7 +107,12 @@ public void run() {
}
});
}

}
public void onEventMainThread(ViewEvent event) {
if(event.message.equals(Utility.VIEW_ANNOUNCEMENT_EVENT)) {
Log.d("ANNOUNCEMENT", "VIEW EVENT RECEIVED");
mAdapter.notifyDataSetChanged();
}
}


Expand Down Expand Up @@ -235,6 +241,7 @@ public void onResponse(String response) {
null,
null,
null);
long announcement_id;
if (!mCursor.moveToFirst()) {
Log.d("ANNOUNCEMENT VALUE", "NOT UNIQUE");
ContentValues announcementValues = new ContentValues();
Expand All @@ -244,8 +251,44 @@ public void onResponse(String response) {
announcementValues.put(MMUContract.AnnouncementEntry.COLUMN_AUTHOR, announcement_author);
announcementValues.put(MMUContract.AnnouncementEntry.COLUMN_POSTED_DATE, announcement_posted_date);
announcementValues.put(MMUContract.AnnouncementEntry.COLUMN_SUBJECT_KEY, subject_id);
long _id = MySingleton.getInstance(getActivity()).getDatabase().insert(MMUContract.AnnouncementEntry.TABLE_NAME, null, announcementValues);
announcement_id = MySingleton.getInstance(getActivity()).getDatabase().insert(MMUContract.AnnouncementEntry.TABLE_NAME, null, announcementValues);
}else {
announcement_id = mCursor.getLong(mCursor.getColumnIndex(MMUContract.AnnouncementEntry._ID));
}
JSONArray announcement_files = announcement.getJSONArray("subject_files");
for(int l = 0; l < announcement_files.length(); l++) {
Log.d("HELO THERE", "HAS FILES " + Long.toString(announcement_id) + " AND SUBJECT ID IS " + subject_id);
JSONObject file = announcement_files.getJSONObject(l);
String file_name = file.getString("file_name");
String token = file.getString("token");
String content_id = file.getString("content_id");
String content_type = file.getString("content_type");
String remote_file_path = file.getString("file_path");
String raw_sql = "SELECT * FROM " + MMUContract.FilesEntry.TABLE_NAME + ", " +
MMUContract.SubjectEntry.TABLE_NAME + " WHERE " +
MMUContract.FilesEntry.TABLE_NAME + "." +
MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = " +
MMUContract.AnnouncementEntry.TABLE_NAME + "." +
MMUContract.AnnouncementEntry._ID + " AND " +
MMUContract.FilesEntry.TABLE_NAME + "." +
MMUContract.FilesEntry.COLUMN_NAME + " = ? " + " AND " +
MMUContract.AnnouncementEntry.TABLE_NAME + "." +
MMUContract.AnnouncementEntry._ID + " = ? ;";
mCursor = MySingleton.getInstance(getActivity()).getDatabase().rawQuery(sql, new String[]{file_name, Long.toString(announcement_id)});
if(!mCursor.moveToFirst()) {
ContentValues fileValues = new ContentValues();
fileValues.put(MMUContract.FilesEntry.COLUMN_NAME, file_name);
fileValues.put(MMUContract.FilesEntry.COLUMN_TOKEN, token);
fileValues.put(MMUContract.FilesEntry.COLUMN_CONTENT_ID, content_id);
fileValues.put(MMUContract.FilesEntry.COLUMN_CONTENT_TYPE, content_type);
fileValues.put(MMUContract.FilesEntry.COLUMN_REMOTE_FILE_PATH, remote_file_path);
fileValues.put(MMUContract.FilesEntry.COLUMN_ANNOUNCEMENT_KEY, announcement_id);
fileValues.put(MMUContract.FilesEntry.COLUMN_SUBJECT_KEY, subject_id);
long _id = MySingleton.getInstance(getActivity()).getDatabase().insert(MMUContract.FilesEntry.TABLE_NAME, null, fileValues);
}

}

}
}
}
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/com/github/hiiyl/mmuhub/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@


public class SettingsActivity extends Activity {
public final String SYNC_INTERVAL = "sync_interval";
public final String SYNC_ENABLED = "sync_enabled";
SharedPreferences.OnSharedPreferenceChangeListener listener;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PrefsFragment()).commit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.d("ON CHANGE", "wow");
if(key.equals("sync_enabled")) {
boolean sync_enabled = prefs.getBoolean("sync_enabled", true);
if(key.equals(SYNC_ENABLED)) {
boolean sync_enabled = prefs.getBoolean(SYNC_ENABLED, true);
int sync_enabled_int = (sync_enabled) ? 1 : 0;
ContentResolver.setIsSyncable(MMUSyncAdapter.getSyncAccount(SettingsActivity.this), MMUProvider.getAuthority(), sync_enabled_int);
}
if(key.equals("sync_interval")) {
String interval = prefs.getString("sync_interval", "");
if(key.equals(SYNC_INTERVAL)) {
String interval = prefs.getString(SYNC_INTERVAL, "");
int interval_in_seconds = Integer.parseInt(interval) * 60;
Log.d("SYNC SETTING","NEW INTERVAL IS " + interval);
MMUSyncAdapter.configurePeriodicSync(SettingsActivity.this, interval_in_seconds, interval_in_seconds/3);
Expand Down

0 comments on commit f789970

Please sign in to comment.