Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhehuaz committed Feb 29, 2016
2 parents a3d34fe + f8065b2 commit 814968c
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 120 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 15 additions & 25 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -65,56 +65,46 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/coverage-instrumented-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/debug" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/cardview-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/design/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/palette-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/preference-v14/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/preference-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/io.reactivex/rxandroid/1.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/ndk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="palette-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="gson-2.4" level="project" />
<orderEntry type="library" exported="" name="jsoup-1.8.3" level="project" />
<orderEntry type="library" exported="" name="eventbus-annotation-processor-3.0.0-beta1" level="project" />
<orderEntry type="library" exported="" name="cardview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="eventbus-3.0.0-beta1" level="project" />
<orderEntry type="library" exported="" name="preference-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="rxandroid-1.0.1" level="project" />
<orderEntry type="library" exported="" name="preference-v14-23.1.1" level="project" />
<orderEntry type="library" exported="" name="okhttp-2.7.1" level="project" />
<orderEntry type="library" exported="" name="eventbus-3.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="design-23.1.1" level="project" />
<orderEntry type="library" exported="" name="eventbus-3.0.0-beta1" level="project" />
<orderEntry type="library" exported="" name="rxjava-1.0.14" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="palette-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="rxjava-1.0.14" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="preference-v14-23.1.1" level="project" />
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="eventbus-annotation-processor-3.0.0-beta1" level="project" />
<orderEntry type="library" exported="" name="jsoup-1.8.3" level="project" />
<orderEntry type="library" exported="" name="okhttp-2.7.1" level="project" />
<orderEntry type="library" exported="" name="cardview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="eventbus-3.0.0" level="project" />
<orderEntry type="library" exported="" name="gson-2.4" level="project" />
<orderEntry type="library" exported="" name="preference-v7-23.1.1" level="project" />
<orderEntry type="module" module-name="uilibrary" exported="" />
</component>
</module>
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "me.zchang.onchart"
minSdkVersion 16
targetSdkVersion 23
versionCode 7
versionName "1.4.1"
versionCode 9
versionName "1.4.3"
}
buildTypes {
release {
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/me/zchang/onchart/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
public class ConfigManager {
private static String SETTING_FILE;

final static String CHART_FILE_NAME = "chart.js";

Context context;
SharedPreferences sp;
CourseSQLiteHelper courseSQLiteHelper;

OnConfigChangeListener configChangeListner;
OnConfigChangeListener configChangeListener;

private boolean firstLaunch = false;

Expand All @@ -48,12 +46,12 @@ public class ConfigManager {
};

public void registerListener(OnConfigChangeListener listener) {
configChangeListner = listener;
configChangeListener = listener;
sp.registerOnSharedPreferenceChangeListener(listener);
}

public void unRegisterListener(OnConfigChangeListener listener) {
configChangeListner = null;
configChangeListener = null;
sp.unregisterOnSharedPreferenceChangeListener(listener);
}

Expand Down Expand Up @@ -89,14 +87,14 @@ public ConfigManager deleteSchedule() {

public boolean insertCourse(Course course) {
boolean result = courseSQLiteHelper.insertCourse(course);
if (configChangeListner != null && result)
configChangeListner.onInsertCourse(course);
if (configChangeListener != null && result)
configChangeListener.onInsertCourse(course);
return result;
}

public void deleteCourse(long id) {
courseSQLiteHelper.deleteCourse(id);
configChangeListner.onDeleteCourse(id);
configChangeListener.onDeleteCourse(id);
}

public ConfigManager saveImgPathIndex(long key, int resIndex) {
Expand Down Expand Up @@ -174,7 +172,6 @@ public ConfigManager saveLastVersionCode(int code) {

public interface OnConfigChangeListener extends SharedPreferences.OnSharedPreferenceChangeListener {
void onInsertCourse(Course course);

void onDeleteCourse(long id);
}
}
49 changes: 37 additions & 12 deletions app/src/main/java/me/zchang/onchart/parser/StudentInfoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -55,6 +57,14 @@ public static List<Course> parseCourses(@NonNull String htmlText)
if(chartTable != null) {
Elements lessonEles = chartTable.select("tr");
List<Course> courses = new ArrayList<>();
Elements elements = doc.select("[selected=selected]");
StringBuilder semesterBuilder = new StringBuilder();
if (!elements.isEmpty()) {
semesterBuilder.append(elements.first().text().split("-")[0])
.append("-")
.append(elements.last().text());
}
String thisSemester = semesterBuilder.toString();

List<Course> dupCourses = new ArrayList<>();
for (Element e : lessonEles) {
Expand All @@ -63,8 +73,7 @@ public static List<Course> parseCourses(@NonNull String htmlText)
Course baseCourse = new LabelCourse();
Elements lessonInfo = e.getAllElements();

// TODO: 2/4/16 parse and add semester information
baseCourse.setSemester("2015-2");
baseCourse.setSemester(thisSemester);
baseCourse.setName(lessonInfo.get(1).text());
baseCourse.setCredit(Float.parseFloat(lessonInfo.get(2).text()));
baseCourse.setDepartment(lessonInfo.get(5).text());
Expand Down Expand Up @@ -137,6 +146,17 @@ public static List<Course> parseCourses(@NonNull String htmlText)
}
}

public static Map<String, String> parseParamsInCoursePage(String coursePageHtml) {
Map<String, String> params = new HashMap<>(5);

Document page = Jsoup.parse(coursePageHtml);
Elements paramEles = page.select("input");
for (Element element : paramEles) {
params.put(element.attr("name"), element.val());
}
return params;
}

/**
* Parse current week number.
* @param htmlText the source html text.
Expand All @@ -150,22 +170,27 @@ public static int parseWeek(@NonNull String htmlText) {
Element childElement;
if (rootElement != null) {
childElement = rootElement.select("b").get(0);
if (childElement != null)
return Integer.parseInt(childElement.text());
if (childElement != null) {
try {
return Integer.parseInt(childElement.text());
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
}
return -1;
}

/**
* Parse cur date from the homepage of jwc.
*
* @param htmlText The source html text.
* @return The date.
*/
public static void parseDate(@NonNull String htmlText, Integer year, Integer month, Integer day) {
/**
* Parse cur date from the homepage of jwc.
*
* @param htmlText The source html text.
* @return The date.
*/
public static void parseDate(@NonNull String htmlText, Integer year, Integer month, Integer day) {

}
}


/**
Expand Down
115 changes: 113 additions & 2 deletions app/src/main/java/me/zchang/onchart/session/BitJwcSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.zchang.onchart.parser.StudentInfoParser;
import me.zchang.onchart.session.events.HomepageFetchOverEvent;
import me.zchang.onchart.session.events.ScheduleFetchOverEvent;
import me.zchang.onchart.session.events.SessionErrorEvent;
import me.zchang.onchart.session.events.SessionStartOverEvent;
import me.zchang.onchart.session.events.HomepageFetchOverEvent;
import me.zchang.onchart.student.Course;
import me.zchang.onchart.student.Exam;
import me.zchang.onchart.ui.MainActivity;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/*
* Copyright 2015 Zhehua Chang
Expand Down Expand Up @@ -120,7 +128,7 @@ public void run() {
scheduleResponse = httpClient.newCall(scheduleRequest).execute();
if (scheduleResponse.isSuccessful()) {
EventBus.getDefault().post(
new ScheduleFetchOverEvent(StudentInfoParser.parseCourses(scheduleResponse.body().string()))
new ScheduleFetchOverEvent(StudentInfoParser.parseCourses(scheduleResponse.body().string()), "default")
);
Log.i(MainActivity.TAG, "post schedule fetch over");
}
Expand All @@ -133,6 +141,109 @@ public void run() {
}).start();
}

/**
* Fetch a schedule in the specific semester.
* @param yearSemester The format obeys "YYYY-N" according to {@link Course#semester}.
*/
public void fetchSchedule(final String yearSemester) {
StringBuilder semesterBuilder = new StringBuilder();
String[] splitSemester = yearSemester.split("-");
if (splitSemester.length == 2) {
semesterBuilder.append(splitSemester[0])
.append("-")
.append(Integer.parseInt(splitSemester[0]) + 1);
//fetchSchedule(semesterBuilder.toString(), splitSemester[1]);
} else {
Log.i(TAG, "year semester parse error");
return ;
}
final String year = semesterBuilder.toString();
final String semester = splitSemester[1];
Observable.create(new Observable.OnSubscribe<Map<String, String>>() {
@Override
public void call(Subscriber<? super Map<String, String>> subscriber) {
Log.i(TAG, "trying to fetch params");
String path = "/xskbcx.aspx?xh=" + stuNum + "&xm=%D5%C5%D5%DC%BB%AA&gnmkdm=N121603";
if (loginUrl != null) {
Request scheduleRequest = new Request.Builder()
.addHeader("Referer", loginUrl)
.get()
.url(loginUrl.substring(0, 43) + path)
.build();
Response scheduleResponse = null;
try {
scheduleResponse = httpClient.newCall(scheduleRequest).execute();
if (scheduleResponse.isSuccessful()) {
subscriber.onNext(
StudentInfoParser.parseParamsInCoursePage(scheduleResponse.body().string())
);
}
} catch (IOException e) {
e.printStackTrace();
subscriber.onError(e);
}
subscriber.onCompleted();
}
}
})
.subscribeOn(Schedulers.io())
.flatMap(new Func1<Map<String, String>, Observable<String>>() {
@Override
public Observable<String> call(final Map<String, String> params) {

Log.i(TAG, "params received.");
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
String path = "/xskbcx.aspx?xh=" + stuNum + "&xm=%D5%C5%D5%DC%BB%AA&gnmkdm=N121603";
FormEncodingBuilder builder = new FormEncodingBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
builder.add("xnd", year)
.add("xqd", semester);
Request scheduleRequest = new Request.Builder()
.addHeader("Referer", loginUrl)
.post(builder.build())
.url(loginUrl.substring(0, 43) + path)
.build();
try {
Log.i(TAG, "fetch schedule request sent");
Response response = httpClient.newCall(scheduleRequest).execute();
subscriber.onNext(response.body().string());
subscriber.onCompleted();
} catch (IOException e) {
e.printStackTrace();
subscriber.onError(e);
}
}
})
.subscribeOn(Schedulers.io());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
e.printStackTrace();
Log.i(TAG, "fetch schedule error ");
EventBus.getDefault().post(new SessionErrorEvent(ErrorCode.SESSION_EC_FETCH_SCHEDULE));
}

@Override
public void onNext(String s) {
Log.i(TAG, "get schedule");
EventBus.getDefault()
.post(new ScheduleFetchOverEvent(StudentInfoParser.parseCourses(s), yearSemester));
}
});
}

/**
* Fetch <a href="http://jwc.bit.edu.cn"/> homepage
* for week number and current date.
Expand Down
Loading

0 comments on commit 814968c

Please sign in to comment.