Files
EhViewer/app/src/main/java/com/hippo/ehviewer/client/EhClient.java
T
2015-08-19 22:20:03 +08:00

380 lines
12 KiB
Java

/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.client;
import android.os.AsyncTask;
import android.os.Process;
import com.hippo.ehviewer.client.data.GalleryApiDetail;
import com.hippo.ehviewer.client.data.GalleryBase;
import com.hippo.ehviewer.network.EhHttpRequest;
import com.hippo.httpclient.HttpClient;
import com.hippo.httpclient.HttpRequest;
import com.hippo.yorozuya.PriorityThreadFactory;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public final class EhClient {
public static final String TAG = EhClient.class.getSimpleName();
/**
* Sign in
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>username</td>
* <td>the username</td>
* </tr>
* <tr>
* <td>password</td>
* <td>the password</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_SIGN_IN = 0;
/**
* Get gallery list
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>url</td>
* <td>the url to get gallery list</td>
* </tr>
* <tr>
* <td>source</td>
* <td>the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_GALLERY_LIST = 1;
/**
* Get gallery list with japanese title
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>url</td>
* <td>the url to get gallery list</td>
* </tr>
* <tr>
* <td>source</td>
* <td>the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_GALLERY_LIST_JPN = 2;
/**
* Get popular
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_POPULAR = 3;
/**
* Get popular
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_POPULAR_JPN = 4;
/**
* Get gallery detail
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>url</td>
* <td>the url to get gallery detail</td>
* </tr>
* <tr>
* <td>source</td>
* <td>the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_GALLERY_DETAIL = 5;
/**
* Get gallery preview set
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>url</td>
* <td>the url to get gallery preview set</td>
* </tr>
* <tr>
* <td>source</td>
* <td>the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_GET_PREVIEW_SET = 6;
/**
* Rate gallery
*
* <table>
* <tr>
* <th>param</th>
* <th>info</th>
* </tr>
* <tr>
* <td>gid</td>
* <td>gid of the gallery</td>
* </tr>
* <tr>
* <td>token</td>
* <td>token of the gallery</td>
* </tr>
* <tr>
* <td>rating</td>
* <td>your rating of the gallery</td>
* </tr>
* <tr>
* <td>source</td>
* <td>the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
* </tr>
* <tr>
* <td>listener</td>
* <td>the listener for callback</td>
* </tr>
* </table>
*/
public static final int METHOD_RATE_GALLERY = 7;
private final ThreadPoolExecutor mRequestThreadPool;
private final HttpClient mHttpClient;
public EhClient(HttpClient httpClient) {
int poolSize = 3;
BlockingQueue<Runnable> requestWorkQueue = new LinkedBlockingQueue<>();
ThreadFactory threadFactory = new PriorityThreadFactory(TAG,
Process.THREAD_PRIORITY_BACKGROUND);
mRequestThreadPool = new ThreadPoolExecutor(poolSize, poolSize,
1L, TimeUnit.SECONDS, requestWorkQueue, threadFactory);
mHttpClient = httpClient;
}
public void execute(EhRequest request) {
if (!request.isCanceled()) {
Task task = new Task(request.method, request.ehListener, request.ehConfig);
task.executeOnExecutor(mRequestThreadPool, request.args);
request.task = task;
} else {
request.ehListener.onCanceled();
}
}
public static abstract class EhListener<E> {
public abstract void onSuccess(E result);
public abstract void onFailure(Exception e);
public abstract void onCanceled();
}
class Task extends AsyncTask<Object, Void, Object> {
private int mMethod;
private EhListener mListener;
private EhHttpRequest mEhHttpRequest;
private boolean mStop;
public Task(int method, EhListener listener, EhConfig ehConfig) {
mMethod = method;
mListener = listener;
mEhHttpRequest = new EhHttpRequest();
mEhHttpRequest.setEhConfig(ehConfig);
}
public void stop() {
if (!mStop) {
mStop = true;
Status status = getStatus();
if (status == Status.PENDING) {
cancel(false);
// If it is pending, onPostExecute will not be called,
// need to call mListener.onCanceled here
mListener.onCanceled();
// Clear
mEhHttpRequest = null;
mListener = null;
} else if (status == Status.RUNNING) {
if (mEhHttpRequest != null) {
mEhHttpRequest.cancel();
}
}
}
}
private void fillJpnTitle(HttpRequest httpRequest, List<GalleryBase> list, int source) throws Exception {
int length = list.size();
int[] gids = new int[length];
String[] tokens = new String[length];
for (int i = 0; i < length; i++) {
GalleryBase gi = list.get(i);
gids[i] = gi.gid;
tokens[i] = gi.token;
}
List<GalleryApiDetail> galleryApiDetails = EhEngine.getGalleryApiDetail(
mHttpClient, httpRequest, gids, tokens, source);
for (int i = 0; i < length; i++) {
GalleryBase gi = list.get(i);
GalleryApiDetail gad = galleryApiDetails.get(i);
if (gi.gid == gad.gid) {
gi.titleJpn = gad.titleJpn;
}
}
}
@SuppressWarnings("unchecked")
@Override
protected Object doInBackground(Object... params) {
try {
switch (mMethod) {
case METHOD_SIGN_IN:
return EhEngine.signIn(mHttpClient, mEhHttpRequest, (String) params[0], (String) params[1]);
case METHOD_GET_GALLERY_LIST:
return EhEngine.getGalleryList(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]);
case METHOD_GET_GALLERY_LIST_JPN: {
GalleryListParser.Result result = EhEngine.getGalleryList(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]);
mEhHttpRequest.clear();
fillJpnTitle(mEhHttpRequest, (List) result.galleryInfos, (Integer) params[1]);
return result;
}
case METHOD_GET_POPULAR:
return EhEngine.getPopular(mHttpClient, mEhHttpRequest);
case METHOD_GET_POPULAR_JPN: {
PopularParser.Result result = EhEngine.getPopular(mHttpClient, mEhHttpRequest);
mEhHttpRequest.clear();
fillJpnTitle(mEhHttpRequest, (List) result.galleryInfos, (Integer) params[0]);
return result;
}
case METHOD_GET_GALLERY_DETAIL:
return EhEngine.getGalleryDetail(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]);
case METHOD_GET_PREVIEW_SET:
return EhEngine.getPreviewSet(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]);
case METHOD_RATE_GALLERY:
return EhEngine.rateGallery(mHttpClient, mEhHttpRequest, (Integer) params[0], (String) params[1], (Float) params[2], (Integer) params[3]);
default:
return new IllegalStateException("Can't detect method");
}
} catch (Exception e) {
return e;
}
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(Object result) {
if (result instanceof CanceledException) {
mListener.onCanceled();
} else if (result instanceof Exception) {
mListener.onFailure((Exception) result);
} else {
mListener.onSuccess(result);
}
// Clear
mEhHttpRequest = null;
mListener = null;
}
}
}