Skip to content

Commit

Permalink
Updated layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
HiiYL committed Apr 25, 2015
1 parent 4bc0b2c commit 2f77087
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 221 deletions.
444 changes: 242 additions & 202 deletions .idea/workspace.xml

Large diffs are not rendered by default.

127 changes: 114 additions & 13 deletions app/src/main/java/com/github/hiiyl/mmuhub/DownloadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
Expand All @@ -23,10 +25,12 @@
import android.widget.ListView;
import android.widget.ProgressBar;

import com.gc.materialdesign.views.ButtonFlat;
import com.gc.materialdesign.widgets.SnackBar;
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.DownloadListViewScrollEvent;
import com.github.hiiyl.mmuhub.helper.FileOpen;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
Expand All @@ -43,6 +47,8 @@

import javax.net.ssl.HttpsURLConnection;

import de.greenrobot.event.EventBus;


public class DownloadActivity extends AppCompatActivity {
private static int mSubjectID;
Expand Down Expand Up @@ -95,6 +101,8 @@ public static class PlaceholderFragment extends Fragment {
private ListView download_list;
private MMUDbHelper mOpenHelper;
private MMLSDownloadAdapter adapter;
private ButtonFlat mDownloadAllButton;
private String mSubjectName;
private String mSubjectID;
private Bus bus;

Expand Down Expand Up @@ -137,11 +145,65 @@ public View onCreateView(LayoutInflater inflater, final ViewGroup container,
final String subject_id = Integer.toString(DownloadActivity.mSubjectID);
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 + " = ? ",
new String[] {subject_id}, null, null,null );
adapter = new MMLSDownloadAdapter(getActivity(), cursor);
download_list.setAdapter(adapter);

mDownloadAllButton = (ButtonFlat)rootView.findViewById(R.id.download_all_button);

// download_list.setRecyclerListener(new AbsListView.RecyclerListener() {
// @Override
// public void onMovedToScrapHeap(View view) {
// EventBus.getDefault().post(new DownloadListViewScrollEvent());
// }
// });

// mDownloadAllButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Cursor new_cursor = MySingleton.getInstance(getActivity()).getDatabase().query(MMUContract.FilesEntry.TABLE_NAME, null, MMUContract.FilesEntry.COLUMN_SUBJECT_KEY + " = ? ",
// new String[]{subject_id}, null, null, null);
// new_cursor.moveToFirst();
// int count = 0;
// while (!new_cursor.isAfterLast()) {
// String file_name = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_NAME));
// String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + Utility.DOWNLOAD_FOLDER + "/" + mSubjectName + "/" + file_name;
// String file_directory = Environment.getExternalStorageDirectory().getPath() + "/" + Utility.DOWNLOAD_FOLDER + "/" + mSubjectName + "/";
// File file = new File(file_path);
//
// if(!file.exists()) {
// File temp = new File(file_directory);
// temp.mkdirs();
// View view = getViewByPosition(count,download_list);
// FragmentActivity activity = (FragmentActivity) view.getContext();
// final DownloadTask downloadTask = new DownloadTask(getActivity(), activity);
// SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
// downloadTask.file_name = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_NAME));
// downloadTask.content_type = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_CONTENT_TYPE));
// downloadTask.content_id = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_CONTENT_ID));
// downloadTask.remote_file_path = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_REMOTE_FILE_PATH));
// downloadTask.token = prefs.getString("token", "");
// downloadTask.cookie = "laravel_session=" + prefs.getString("cookie", "");
// downloadTask.local_file_path = file_path;
// downloadTask.view = view;
// downloadTask.mDownloadList = download_list;
// downloadTask.mPosition = count;
// downloadTask.isDownloadAll = true;
//
//
// Log.d("APP", "Now downloading " + downloadTask.file_name);
//
// downloadTask.execute("https://mmls.mmu.edu.my/form-download-content");
// count++;
// }
// new_cursor.moveToNext();
// }
// }
// });


download_list.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Expand All @@ -152,7 +214,8 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
new_cursor.moveToPosition(position);

String file_name = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_NAME));
String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + file_name;
String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + Utility.DOWNLOAD_FOLDER + "/" + mSubjectName + "/" + file_name;
String file_directory = Environment.getExternalStorageDirectory().getPath() + "/" + Utility.DOWNLOAD_FOLDER + "/" + mSubjectName + "/";
Log.d("FILE NAME", file_name);
Log.d("FILE PATH", file_path);
File file = new File(file_path);
Expand All @@ -164,6 +227,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
}
else {

File temp = new File(file_directory);
temp.mkdirs();
view.setHasTransientState(true);
FragmentActivity activity = (FragmentActivity) view.getContext();
final DownloadTask downloadTask = new DownloadTask(getActivity(), activity);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
Expand All @@ -173,8 +240,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
downloadTask.remote_file_path = new_cursor.getString(new_cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_REMOTE_FILE_PATH));
downloadTask.token = prefs.getString("token", "");
downloadTask.cookie = "laravel_session=" + prefs.getString("cookie", "");
downloadTask.local_file_path = file_path;
downloadTask.view = view;


Log.d("APP", "Now downloading " + downloadTask.file_name);

new_cursor.close();
Expand All @@ -186,12 +255,26 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
});
return rootView;
}

}
// public static View getViewByPosition(int position, ListView listView) {
// final int firstListItemPosition = listView.getFirstVisiblePosition();
// final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
// View view;
//
// if (position < firstListItemPosition || position > lastListItemPosition ) {
// view = listView.getAdapter().getView(position, listView.getChildAt(position), listView);
// } else {
// final int childIndex = position - firstListItemPosition;
// view = listView.getChildAt(childIndex);
// }
// view.setHasTransientState(true);
// return view;
// }
private static class DownloadTask extends AsyncTask<String, Integer, String> {

private Context context;
private PowerManager.WakeLock mWakeLock;
private String local_file_path;
private String cookie;
private String content_id;
private String file_name;
Expand All @@ -202,8 +285,24 @@ private static class DownloadTask extends AsyncTask<String, Integer, String> {
private ProgressBar mProgressBar;
private Activity mActivity;
private HttpsURLConnection connection = null;
private ListView mDownloadList;
private int mPosition;
boolean isDownloadAll = false;

public void onEvent(DownloadListViewScrollEvent event) {
if(isDownloadAll) {
// Log.d("HEARING", "I HEAR THE SUMMONS");
// view = getViewByPosition(mPosition, mDownloadList);
mProgressBar = (ProgressBar) view.findViewById(R.id.listitem_download_mmls_progress_bar);
}
}





public DownloadTask(Context context, Activity activity) {
EventBus.getDefault().register(this);
this.context = context;
mActivity = activity;
}
Expand Down Expand Up @@ -286,10 +385,10 @@ protected String doInBackground(String... sUrl) {
// download the file
input = connection.getInputStream();

String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + file_name;
Log.d("FILE PATH", file_path);
// String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + file_name;
Log.d("FILE PATH", local_file_path);

output = new FileOutputStream(file_path);
output = new FileOutputStream(local_file_path);

byte data[] = new byte[4096];
long total = 0;
Expand Down Expand Up @@ -322,12 +421,15 @@ protected String doInBackground(String... sUrl) {
}
return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar = (ProgressBar) view.findViewById(R.id.listitem_download_mmls_progress_bar);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(true);
mProgressBar.getProgressDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
// view.setHasTransientState(true);

// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
Expand All @@ -346,24 +448,23 @@ protected void onPreExecute() {
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
Log.d("DOWNLOADING", Integer.toString(progress[0]));
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(false);
mProgressBar.setMax(100);
mProgressBar.setProgress(progress[0]);
}

@Override
protected void onPostExecute(String result) {
EventBus.getDefault().unregister(this);
mWakeLock.release();
// mProgressDialog.dismiss();
if (result != null) {
SnackBar new_snackbar = new SnackBar((Activity) context, "Download Failed",
"Retry", new View.OnClickListener() {
@Override
public void onClick(View v) {
execute();
}
});
mProgressBar.setIndeterminate(false);
mProgressBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
mProgressBar.setMax(100);
mProgressBar.setProgress(100);
SnackBar new_snackbar = new SnackBar((Activity) context, "Download Failed");
new_snackbar.show();
}
else {
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/github/hiiyl/mmuhub/MMLSDownloadAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -25,14 +26,21 @@ public MMLSDownloadAdapter(Context context, Cursor c) {
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.listitem_download_mmls,parent,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvTitle = (TextView) view.findViewById(R.id.file_download_name_textview);
TextView tvStatus = (TextView) view.findViewById(R.id.file_download_downloaded_status);
TextView tvHint = (TextView) view.findViewById(R.id.file_download_interaction_hint);
String file_name = cursor.getString(cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_NAME));
String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + file_name;
String subject_id = cursor.getString(cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_SUBJECT_KEY));
String sql = "SELECT " + MMUContract.SubjectEntry.TABLE_NAME + "." + MMUContract.SubjectEntry.COLUMN_NAME + " FROM " + MMUContract.SubjectEntry.TABLE_NAME + "," +
MMUContract.FilesEntry.TABLE_NAME + " WHERE " + MMUContract.SubjectEntry.TABLE_NAME + "." + MMUContract.SubjectEntry._ID + "=" + subject_id;

Cursor temp_cursor = MySingleton.getInstance(context).getDatabase().rawQuery(sql, null);
temp_cursor.moveToFirst();
String subject_name = temp_cursor.getString(temp_cursor.getColumnIndex(MMUContract.SubjectEntry.COLUMN_NAME));
String file_path = Environment.getExternalStorageDirectory().getPath() + "/" + Utility.DOWNLOAD_FOLDER + "/" + subject_name + "/" + file_name;
Log.d("SQL IS ", sql);
File file = new File(file_path);
tvTitle.setText(cursor.getString(cursor.getColumnIndex(MMUContract.FilesEntry.COLUMN_NAME)));
if(file.exists()) {
Expand All @@ -42,6 +50,7 @@ public void bindView(View view, Context context, Cursor cursor) {
tvStatus.setText("Not Downloaded");
tvHint.setText("Tap to Download");
}
temp_cursor.close();


}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/github/hiiyl/mmuhub/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Utility {
private static SharedPreferences prefs;
public static final String SYNC_FINISHED = "sync finish";
public static final String SYNC_BEGIN = "sync begin";
public static final String DOWNLOAD_FOLDER = "MMUHub Downloads";
public static String trimMessage(String json, String key){
String trimmedString = null;

Expand All @@ -49,6 +50,15 @@ public static int getSubjectCount(Context context) {
cursor.close();
return subject_count;
}
public static String getSubjectName(Context context, String subject_id) {
Cursor cursor = MySingleton.getInstance(context).getDatabase().query(MMUContract.SubjectEntry.TABLE_NAME,
null, MMUContract.SubjectEntry._ID + " = ?",
new String[]{subject_id},null,null,null);
cursor.moveToFirst();
String subject_name = cursor.getString(cursor.getColumnIndex(MMUContract.SubjectEntry.COLUMN_NAME));
cursor.close();
return subject_name;
}
public static void refreshToken(Context context) {
Context mContext = context;
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.hiiyl.mmuhub.helper;

/**
* Created by Hii on 4/25/15.
*/
public class DownloadListViewScrollEvent {
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/fragment_download.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
android:textSize="20sp"/>


<com.gc.materialdesign.views.ButtonFlat
android:id="@+id/download_all_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#1E88E5"
android:text="Download All" />

<ListView
android:layout_width="match_parent"
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/res/layout/listitem_download_mmls.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textStyle="bold"
android:typeface="serif"
android:padding="10dp"
android:id="@+id/file_download_name_textview"
android:text="File name placeholder"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="left"
android:layout_height="wrap_content"
android:id="@+id/file_download_downloaded_status"
android:text="Download Status"
/>
<TextView
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/file_download_interaction_hint"
android:gravity="right"
android:layout_gravity="right"
android:text="Tap to Download"/>
</LinearLayout>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 2f77087

Please sign in to comment.