Skip to content

Commit

Permalink
support for posting json, exclude source from proguard; updated to v0…
Browse files Browse the repository at this point in the history
….11.3
  • Loading branch information
PureDark committed Apr 24, 2017
1 parent dcb64d6 commit ea913ec
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 42 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ android {
resValue "string", "app_name", "H-Viewer"
minSdkVersion 17
targetSdkVersion 25
versionCode 86
versionName "0.11.2"
versionCode 87
versionName "0.11.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
//multiDexEnabled true
Expand Down
4 changes: 2 additions & 2 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
-keep class sun.misc.Unsafe { *; }
-dontwarn sun.misc.Unsafe

-keep class ml.puredark.hviewer.beans.** { *; }
-dontwarn ml.puredark.hviewer.beans.**
-keep class ml.puredark.hviewer.** { *; }
-dontwarn ml.puredark.hviewer.**

-keep class com.umeng.** {*; }
-dontwarn com.umeng.**
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/ml/puredark/hviewer/beans/Site.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public String getGalleryUrl(String inUrl, String idCode, int page, List<Picture>
return RuleParser.parseUrl(inUrl, page, idCode, "", array);
}

public static String getJsonParams(String url, int page, String keyword, List<Collection> collections) {
Object[] array = (collections != null) ? collections.toArray() : null;
return RuleParser.parseUrl(url, page, "", keyword, array, true);
}

public boolean isFirstLoadSecondLevelGallery(List<Picture> pictures) {
return (pictures != null && pictures.size() > 0 && this.hasFlag(Site.FLAG_SECOND_LEVEL_GALLERY)
&& !Picture.hasPicPosfix(pictures.get(0).url) && this.extraRule != null);
Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/ml/puredark/hviewer/core/RuleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ public static Map<String, String> parseUrl(String url) {
while (matcher2.find()) {
map.put(matcher2.group(1), matcher2.group(2));
}
Pattern pattern3 = Pattern.compile("\\{(json):(.*)\\}", DOTALL);
Matcher matcher3 = pattern3.matcher(url);
while (matcher3.find()) {
map.put(matcher3.group(1), matcher3.group(2));
}
return map;
}

public static String parseUrl(String url, int page, String idCode, String keyword, Object[] objs) {
public static String parseUrl(String url, int page, String idCode, String keyword, Object[] objs){
return parseUrl(url, page, idCode, keyword, objs, false);
}

public static String parseUrl(String url, int page, String idCode, String keyword, Object[] objs, boolean getJsonParams) {
Map<String, String> matchResult = RuleParser.parseUrl(url);
if(getJsonParams && !matchResult.containsKey("json"))
return null;
String result = (getJsonParams) ? matchResult.get("json") : url;
String pageStr = matchResult.get("page");
int startPage = 0;
int pageStep = 1;
Expand Down Expand Up @@ -102,7 +114,7 @@ else if (obj instanceof Picture)
if (page < startPage)
page = startPage;
int realPage = page + (page - startPage) * (pageStep - 1);
url = url.replaceAll("\\{pageStr:(.*?\\{.*?\\}.*?)\\}", (realPage == startPage) ? "" : matchResult.get("pageStr"))
result = result.replaceAll("\\{pageStr:(.*?\\{.*?\\}.*?)\\}", (realPage == startPage) ? "" : matchResult.get("pageStr"))
.replaceAll("\\{page:.*?\\}", "" + realPage)
.replaceAll("\\{keyword:.*?\\}", keyword)
.replaceAll("\\{idCode:\\}", idCode);
Expand All @@ -125,7 +137,7 @@ else if (obj instanceof Picture)
dateFormat = new SimpleDateFormat(dateStr);
}
String currDate = dateFormat.format(calendar.getTime());
url = url.replaceAll("\\{date:.*?\\}", currDate);
result = result.replaceAll("\\{date:.*?\\}", currDate);
}
if (matchResult.containsKey("time")) {
String timeStr = matchResult.get("time");
Expand All @@ -146,9 +158,9 @@ else if (obj instanceof Picture)
dateFormat = new SimpleDateFormat(timeStr);
}
String currTime = dateFormat.format(calendar.getTime());
url = url.replaceAll("\\{time:.*?\\}", currTime);
result = result.replaceAll("\\{time:.*?\\}", currTime);
}
return url;
return result;
}

public static boolean isJson(String string) {
Expand Down
31 changes: 18 additions & 13 deletions app/src/main/java/ml/puredark/hviewer/http/HViewerHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import ml.puredark.hviewer.HViewerApplication;
import ml.puredark.hviewer.beans.Site;
import ml.puredark.hviewer.helpers.Logger;
import okhttp3.Call;
import okhttp3.Callback;
Expand All @@ -35,6 +33,7 @@
import okhttp3.Response;

public class HViewerHttpClient {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static Handler mHandler = new Handler(Looper.getMainLooper());
private static DownloadUtil downloadUtil;
private static HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null, null);
Expand Down Expand Up @@ -141,8 +140,9 @@ public static void get(String url, boolean disableHProxy, List<Pair<String, Stri
if (!post)
get(url, disableHProxy, headers, false, null, callback);
else {
String params = (url == null) ? "" : (url.indexOf('?') > 0) ? url.substring(url.indexOf('?')) : url;
post(url, disableHProxy, params, headers, callback);
String jsonParam = Site.getJsonParams(url, 0, "", null);
String params = (jsonParam != null) ? jsonParam : url.substring(url.indexOf('?'));
post(url, disableHProxy, params, (jsonParam != null), headers, callback);
}
}

Expand Down Expand Up @@ -183,15 +183,20 @@ void onResponse(String contentType, Object body) {
}
}

public static void post(String url, boolean disableHProxy, String paramsString, List<Pair<String, String>> headers, final OnResponseListener callback) {
String[] paramStrings = paramsString.split("&");
FormBody.Builder formBody = new FormBody.Builder();
for (String paramString : paramStrings) {
String[] pram = paramString.split("=");
if (pram.length != 2) continue;
formBody.add(pram[0], pram[1]);
public static void post(String url, boolean disableHProxy, String params, boolean isPostJson, List<Pair<String, String>> headers, final OnResponseListener callback) {
RequestBody requestBody;
if(isPostJson){
requestBody = RequestBody.create(JSON, params);
} else {
String[] paramStrings = params.split("&");
FormBody.Builder formBody = new FormBody.Builder();
for (String paramString : paramStrings) {
String[] pram = paramString.split("=");
if (pram.length != 2) continue;
formBody.add(pram[0], pram[1]);
}
requestBody = formBody.build();
}
RequestBody requestBody = formBody.build();
get(url, disableHProxy, headers, true, requestBody, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ protected void onCreate(Bundle savedInstanceState) {
reportShortcutUsed(this, "scdownload");

Intent intent = getIntent();
if(intent!=null){
if (intent != null) {
Logger.d("ShortcutTest", "DownloadActivity");
Logger.d("ShortcutTest", intent.toString());
String action = intent.getAction();
if(HViewerApplication.INTENT_SHORTCUT.equals(action) && LockActivity.isSetLockMethod(this)){
if (HViewerApplication.INTENT_SHORTCUT.equals(action) && LockActivity.isSetLockMethod(this)) {
Intent lockIntent = new Intent(DownloadActivity.this, LockActivity.class);
lockIntent.setAction(HViewerApplication.INTENT_FROM_DOWNLOAD);
startActivity(lockIntent);
Expand All @@ -122,7 +122,7 @@ protected void onCreate(Bundle savedInstanceState) {

initTabAndViewPager();
initDownloadedTask();
distinguishDownloadTasks();
new Thread(() -> distinguishDownloadTasks()).start();

}

Expand All @@ -134,7 +134,7 @@ void back() {
@Override
public void onResume() {
super.onResume();
distinguishDownloadTasks();
new Thread(() -> distinguishDownloadTasks()).start();
}

@Override
Expand Down Expand Up @@ -210,7 +210,7 @@ public boolean onItemLongClick(View v, int position) {
.setView(view)
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {
manager.deleteDownloadTask(task);
distinguishDownloadTasks();
new Thread(() -> distinguishDownloadTasks()).start();
if (checkBoxDeleteFile.isChecked()) {
String rootPath = task.path.substring(0, task.path.lastIndexOf("/"));
String dirName = task.path.substring(task.path.lastIndexOf("/") + 1, task.path.length());
Expand All @@ -226,7 +226,7 @@ public boolean onItemLongClick(View v, int position) {
});
}

private void initDownloadedTask(){
private void initDownloadedTask() {
ExpandableDataProvider dataProvider = new ExpandableDataProvider(new ArrayList<>());
mRecyclerViewExpandableItemManager = new RecyclerViewExpandableItemManager(null);

Expand Down Expand Up @@ -400,7 +400,7 @@ public void onItemRemoved(int groupPosition, int childPosition) {
.setView(view)
.setPositiveButton("确定", (dialog, which) -> {
manager.deleteDownloadTask(task);
distinguishDownloadTasks();
new Thread(() -> distinguishDownloadTasks()).start();
if (checkBoxDeleteFile.isChecked()) {
String rootPath = task.path.substring(0, task.path.lastIndexOf("/"));
String dirName = task.path.substring(task.path.lastIndexOf("/") + 1, task.path.length());
Expand All @@ -425,18 +425,16 @@ private void updateGroupItemIndex(int groupPosition) {
});
}

private void distinguishDownloadTasks() {
new Thread(()->{
manager.checkDownloadedTask();
List<DownloadTask> downloadingTasks = manager.getDownloadingTasks();
List<Pair<CollectionGroup, List<DownloadTask>>> downloadedTasks = manager.getDownloadedTasks();
downloadingTaskAdapter.getDataProvider().setDataSet(downloadingTasks);
downloadedTaskAdapter.getDataProvider().setDataSet(downloadedTasks);
runOnUiThread(()->{
downloadingTaskAdapter.notifyDataSetChanged();
downloadedTaskAdapter.notifyDataSetChanged();
});
}).start();
private synchronized void distinguishDownloadTasks() {
manager.checkDownloadedTask();
List<DownloadTask> downloadingTasks = manager.getDownloadingTasks();
List<Pair<CollectionGroup, List<DownloadTask>>> downloadedTasks = manager.getDownloadedTasks();
downloadingTaskAdapter.getDataProvider().setDataSet(downloadingTasks);
downloadedTaskAdapter.getDataProvider().setDataSet(downloadedTasks);
runOnUiThread(() -> {
downloadingTaskAdapter.notifyDataSetChanged();
downloadedTaskAdapter.notifyDataSetChanged();
});
}

private void startNextTaskInQueue() {
Expand Down Expand Up @@ -466,7 +464,7 @@ public void onReceive(Context context, Intent intent) {
downloadingTaskAdapter.notifyDataSetChanged();
} else if (intent.getAction().equals(DownloadService.ON_COMPLETE)) {
showSnackBar("任务下载成功");
distinguishDownloadTasks();
new Thread(() -> distinguishDownloadTasks()).start();
startNextTaskInQueue();
}
Log.d("MyDownloadReceiver", intent.getAction());
Expand Down

0 comments on commit ea913ec

Please sign in to comment.