/*
* 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
*
*
*
*
param
*
info
*
*
*
username
*
the username
*
*
*
password
*
the password
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_SIGN_IN = 0;
/**
* Get gallery list
*
*
*
*
param
*
info
*
*
*
url
*
the url to get gallery list
*
*
*
source
*
the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_GALLERY_LIST = 1;
/**
* Get gallery list with japanese title
*
*
*
*
param
*
info
*
*
*
url
*
the url to get gallery list
*
*
*
source
*
the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_GALLERY_LIST_JPN = 2;
/**
* Get popular
*
*
*
*
param
*
info
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_POPULAR = 3;
/**
* Get popular
*
*
*
*
param
*
info
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_POPULAR_JPN = 4;
/**
* Get gallery detail
*
*
*
*
param
*
info
*
*
*
url
*
the url to get gallery detail
*
*
*
source
*
the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_GALLERY_DETAIL = 5;
/**
* Get gallery preview set
*
*
*
*
param
*
info
*
*
*
url
*
the url to get gallery preview set
*
*
*
source
*
the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_GET_PREVIEW_SET = 6;
/**
* Rate gallery
*
*
*
*
param
*
info
*
*
*
gid
*
gid of the gallery
*
*
*
token
*
token of the gallery
*
*
*
rating
*
your rating of the gallery
*
*
*
source
*
the source, one of {@link EhUrl#SOURCE_G},
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
*
*
*
listener
*
the listener for callback
*
*
*/
public static final int METHOD_RATE_GALLERY = 7;
private final ThreadPoolExecutor mRequestThreadPool;
private final HttpClient mHttpClient;
public EhClient(HttpClient httpClient) {
int poolSize = 3;
BlockingQueue 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 {
public abstract void onSuccess(E result);
public abstract void onFailure(Exception e);
public abstract void onCanceled();
}
class Task extends AsyncTask