public abstract class SonicSession
extends java.lang.Object
implements android.os.Handler.Callback
SonicSession
s are used to manage the entire process,include
obtain the latest data from the server, provide local and latest
data to kernel, separate html to template and data, build template
and data to html and so on. Each url involves one session at a time,
that session will be destroyed when the page is destroyed.限定符和类型 | 类和说明 |
---|---|
static interface |
SonicSession.Callback
The interface is used to inform the listeners that the state of the
session has changed.
|
限定符和类型 | 字段和说明 |
---|---|
static java.lang.String |
CHROME_FILE_THREAD
Name of chrome file thread
|
protected static int |
CLIENT_MSG_NOTIFY_RESULT
The message to record sonic mode.
|
protected static int |
CLIENT_MSG_ON_WEB_READY
The message of page ready, its means page want to get the latest session data.
|
protected java.util.concurrent.atomic.AtomicBoolean |
clientIsReady
Whether the client is ready.
|
protected java.util.concurrent.atomic.AtomicBoolean |
clientIsReload
Indicate current session is reload or not.
|
protected static int |
COMMON_MSG_BEGIN |
protected static int |
COMMON_MSG_END |
SonicSessionConfig |
config |
long |
createdTime
The time of current session created.
|
static java.lang.String |
DATA_UPDATE_BUNDLE_PARAMS_DIFF |
protected SonicDiffDataCallback |
diffDataCallback |
protected static int |
FILE_THREAD_MSG_BEGIN |
protected static int |
FILE_THREAD_SAVE_CACHE_ON_SERVER_CLOSE
The message of saving sonic cache while server close.
|
protected static int |
FILE_THREAD_SAVE_CACHE_ON_SESSION_FINISHED
The message of saving sonic cache while session finish.
|
protected android.os.Handler |
fileHandler |
protected int |
finalResultCode
Sonic final mode.
|
java.lang.String |
id |
protected android.content.Intent |
intent
This intent saves all of the initialization param.
|
protected boolean |
isPreload
Whether current session is preload.
|
protected java.util.concurrent.atomic.AtomicBoolean |
isWaitingForDestroy
Whether the session is waiting for destroy.
|
protected java.util.concurrent.atomic.AtomicBoolean |
isWaitingForSaveFile
Whether it is waiting for the file to be saved.
|
protected java.util.concurrent.atomic.AtomicBoolean |
isWaitingForSessionThread
Whether the session is waiting for data.
|
protected android.os.Handler |
mainHandler |
static java.lang.String |
OFFLINE_MODE_FALSE
The value of "cache-offline" in http(s) response headers.
|
static java.lang.String |
OFFLINE_MODE_HTTP
The value of "cache-offline" in http(s) response headers.
|
static java.lang.String |
OFFLINE_MODE_STORE
The value of "cache-offline" in http(s) response headers.
|
static java.lang.String |
OFFLINE_MODE_TRUE
The value of "cache-offline" in http(s) response headers.
|
protected java.lang.String |
pendingDiffData
The difference data between local and server data.
|
protected java.io.InputStream |
pendingWebResourceStream
The response for client interception.
|
protected java.util.List<java.lang.String> |
preloadLinks |
protected static int |
RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD
Resource Intercept State : intercepting in file thread
|
protected static int |
RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD
Resource Intercept State : intercepting in other thread(may be IOThread or other else)
|
protected static int |
RESOURCE_INTERCEPT_STATE_NONE
Resource Intercept State : none
|
protected SonicDownloadEngine |
resourceDownloaderEngine
Sonic sub resource downloader
|
protected java.util.concurrent.atomic.AtomicInteger |
resourceInterceptState
Resource intercept state, include
RESOURCE_INTERCEPT_STATE_NONE ,
RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD ,
RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD
More about it at {https://codereview.chromium.org/1350553005/#ps20001} |
protected SonicServer |
server
Sonic server
|
protected static int |
SESSION_MSG_FORCE_DESTROY
The message of forced to destroy the session.
|
protected java.util.concurrent.CopyOnWriteArrayList<java.lang.ref.WeakReference<com.tencent.sonic.sdk.SonicSessionCallback>> |
sessionCallbackList |
protected SonicSessionClient |
sessionClient |
protected java.util.concurrent.atomic.AtomicInteger |
sessionState
Session state, include
STATE_NONE , STATE_RUNNING ,
STATE_READY and STATE_DESTROY . |
long |
sId
The integer id of current session
|
protected static long |
sNextSessionLogId
Log id
|
static int |
SONIC_RESULT_CODE_DATA_UPDATE
Sonic mode : data update.
|
static int |
SONIC_RESULT_CODE_FIRST_LOAD
Sonic mode : first load.
|
static int |
SONIC_RESULT_CODE_HIT_CACHE
Sonic mode : 304.
|
static int |
SONIC_RESULT_CODE_TEMPLATE_CHANGE
Sonic mode : template change.
|
static int |
SONIC_RESULT_CODE_UNKNOWN
Sonic mode : unknown.
|
protected int |
srcResultCode
Sonic original mode.
|
java.lang.String |
srcUrl
The original url
|
static int |
STATE_DESTROY
Session state : destroyed.
|
static int |
STATE_NONE
Session state : original.
|
static int |
STATE_READY
Session state : ready.
|
static int |
STATE_RUNNING
Session state : running.
|
protected java.util.concurrent.CopyOnWriteArrayList<java.lang.ref.WeakReference<SonicSession.Callback>> |
stateChangedCallbackList |
protected SonicSessionStatistics |
statistics
Session statics var
|
static java.lang.String |
TAG
Log filter
|
protected java.util.concurrent.atomic.AtomicBoolean |
wasInterceptInvoked
Whether the client initiates a resource interception.
|
protected java.util.concurrent.atomic.AtomicBoolean |
wasOnPageFinishInvoked
Whether the local html is loaded, it is used only the template changes.
|
static java.lang.String |
WEB_RESPONSE_CODE
The result keyword to page : the value is
finalResultCode |
static java.lang.String |
WEB_RESPONSE_DATA
The all data keyword to page
|
static java.lang.String |
WEB_RESPONSE_EXTRA |
static java.lang.String |
WEB_RESPONSE_LOCAL_REFRESH_TIME |
static java.lang.String |
WEB_RESPONSE_SRC_CODE
The result keyword to page : the value is
srcResultCode |
限定符和类型 | 方法和说明 |
---|---|
boolean |
addSessionCallback(com.tencent.sonic.sdk.SonicSessionCallback callback) |
protected boolean |
addSessionStateChangedCallback(SonicSession.Callback callback) |
boolean |
bindClient(SonicSessionClient client) |
protected boolean |
canDestroy() |
protected void |
clearSessionData() |
protected android.content.Intent |
createConnectionIntent(com.tencent.sonic.sdk.SonicDataHelper.SessionData sessionData) |
void |
destroy() |
protected void |
destroy(boolean force) |
protected void |
doSaveSonicCache(SonicServer sonicServer,
java.lang.String htmlString) |
protected java.util.HashMap<java.lang.String,java.lang.String> |
getCacheHeaders()
Get header info from local cache headers
|
protected java.lang.String |
getCharsetFromHeaders()
Get the charset from the latest response http header.
|
java.lang.String |
getCharsetFromHeaders(java.util.Map<java.lang.String,java.lang.String> headers) |
java.lang.String |
getCurrentUrl() |
int |
getFinalResultCode() |
protected java.util.HashMap<java.lang.String,java.lang.String> |
getHeaders()
Get header info with the original url of current session.
|
SonicSessionClient |
getSessionClient() |
int |
getSrcResultCode() |
SonicSessionStatistics |
getStatistics() |
protected void |
handleFlow_Connection(boolean hasCache,
com.tencent.sonic.sdk.SonicDataHelper.SessionData sessionData)
Initiate a network request to obtain server data.
|
protected abstract void |
handleFlow_DataUpdate(java.lang.String serverRsp)
Handle data update
SONIC_RESULT_CODE_DATA_UPDATE logic. |
protected abstract void |
handleFlow_FirstLoad()
Handle sonic first
SONIC_RESULT_CODE_FIRST_LOAD logic. |
protected abstract void |
handleFlow_HttpError(int responseCode) |
protected abstract void |
handleFlow_LoadLocalCache(java.lang.String cacheHtml) |
protected void |
handleFlow_NotModified() |
protected abstract void |
handleFlow_ServiceUnavailable() |
protected abstract void |
handleFlow_TemplateChange(java.lang.String newHtml)
Handle template update
SONIC_RESULT_CODE_TEMPLATE_CHANGE logic. |
boolean |
handleMessage(android.os.Message msg)
Subclasses must implement this to receive messages.
|
boolean |
isDestroyedOrWaitingForDestroy() |
boolean |
isMatchCurrentUrl(java.lang.String url)
Whether the incoming url matches the current url,it will
ignore url parameters
|
boolean |
isPreload() |
protected void |
notifyStateChange(int oldState,
int newState,
android.os.Bundle extraData)
When the session state changes, notify the listeners.
|
boolean |
onClientPageFinished(java.lang.String url) |
boolean |
onClientReady()
Client informs sonic that it is ready.
|
java.lang.Object |
onClientRequestResource(java.lang.String url)
When the webview initiates a resource interception, the client invokes the method to retrieve the data
|
protected java.lang.Object |
onRequestResource(java.lang.String url)
When the webview initiates a main resource interception, the client invokes this method to retrieve the data
|
void |
onServerClosed(SonicServer sonicServer,
boolean readComplete)
If the kernel obtain inputStream from a
SonicSessionStream , the inputStream
will be closed when the kernel reads the data.This method is invoked when the sonicSessionStream
close. |
boolean |
onWebReady(SonicDiffDataCallback diffDataCallback)
Client will call this method to obtain the update data when the page shows the content.
|
protected boolean |
postForceDestroyIfNeed()
Destroy the session if it is waiting for destroy and it is can be destroyed.
|
protected void |
postTaskToSaveSonicCache(java.lang.String htmlString) |
boolean |
refresh() |
boolean |
removeSessionCallback(com.tencent.sonic.sdk.SonicSessionCallback callback) |
protected boolean |
removeSessionStateChangedCallback(SonicSession.Callback callback) |
protected boolean |
setCookiesFromHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headers,
boolean executeInNewThread)
Set cookies to webview from headers
|
protected void |
setResult(int srcCode,
int finalCode,
boolean notify)
Record the sonic mode, notify the result to page if necessary.
|
protected boolean |
shouldSetCookieAsynchronous()
Whether should set cookie asynchronous or not , if
onClientRequestResource is calling
in IOThread, it should not call set cookie synchronous which will handle in IOThread as it may
cause deadlock
More about it see {https://issuetracker.google.com/issues/36989494#c8}
Fix VasSonic issue {https://github.com/Tencent/VasSonic/issues/90} |
void |
start()
Start the sonic process
|
protected boolean |
switchState(int fromState,
int toState,
boolean notify) |
public static final java.lang.String TAG
public static final java.lang.String WEB_RESPONSE_SRC_CODE
srcResultCode
public static final java.lang.String WEB_RESPONSE_CODE
finalResultCode
public static final java.lang.String WEB_RESPONSE_EXTRA
public static final java.lang.String WEB_RESPONSE_DATA
public static final java.lang.String DATA_UPDATE_BUNDLE_PARAMS_DIFF
public static final java.lang.String WEB_RESPONSE_LOCAL_REFRESH_TIME
public static final java.lang.String CHROME_FILE_THREAD
public static final int STATE_NONE
This state means session has not start.
public static final int STATE_RUNNING
This state means session has begun to request data from the server and is processing the data.
public static final int STATE_READY
This state means session data is available when the page initiates a resource interception. In other stats the client(kernel) will wait.
public static final int STATE_DESTROY
This state means the session is destroyed.
public static final java.lang.String OFFLINE_MODE_HTTP
This value means sonic server unavailable, the terminal
does not take sonic logic for the next period of time,the
value of time is defined in SonicConfig.SONIC_UNAVAILABLE_TIME
public static final java.lang.String OFFLINE_MODE_STORE
This value means sonic will save the latest data, but not refresh page.For example, when sonic mode is data update, sonic will not provide the difference data between local and server to page to refresh the content.
public static final java.lang.String OFFLINE_MODE_TRUE
This value means sonic will save the latest data and refresh page content.
public static final java.lang.String OFFLINE_MODE_FALSE
This value means sonic will refresh page content but not save date, sonic will remove the local data also.
public static final int SONIC_RESULT_CODE_UNKNOWN
public static final int SONIC_RESULT_CODE_FIRST_LOAD
public static final int SONIC_RESULT_CODE_TEMPLATE_CHANGE
public static final int SONIC_RESULT_CODE_DATA_UPDATE
public static final int SONIC_RESULT_CODE_HIT_CACHE
protected int srcResultCode
For example, when local data does not exist, the value is
SONIC_RESULT_CODE_FIRST_LOAD
protected int finalResultCode
For example, when local data does not exist, the srcResultCode
value is SONIC_RESULT_CODE_FIRST_LOAD
. If the server data is read
finished, sonic will provide the latest data to kernel when the kernel
initiates a resource interception.This effect is the same as loading local data,
so the sonic mode will be set as SONIC_RESULT_CODE_HIT_CACHE
protected static final int COMMON_MSG_BEGIN
protected static final int CLIENT_MSG_NOTIFY_RESULT
protected static final int CLIENT_MSG_ON_WEB_READY
protected static final int SESSION_MSG_FORCE_DESTROY
protected static final int COMMON_MSG_END
protected static final int FILE_THREAD_MSG_BEGIN
protected static final int FILE_THREAD_SAVE_CACHE_ON_SERVER_CLOSE
protected static final int FILE_THREAD_SAVE_CACHE_ON_SESSION_FINISHED
protected static final int RESOURCE_INTERCEPT_STATE_NONE
protected static final int RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD
protected static final int RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD
protected final java.util.concurrent.atomic.AtomicInteger sessionState
STATE_NONE
, STATE_RUNNING
,
STATE_READY
and STATE_DESTROY
.protected final java.util.concurrent.atomic.AtomicBoolean wasInterceptInvoked
protected final java.util.concurrent.atomic.AtomicBoolean clientIsReady
protected final java.util.concurrent.atomic.AtomicBoolean isWaitingForSaveFile
protected final java.util.concurrent.atomic.AtomicBoolean isWaitingForDestroy
protected final java.util.concurrent.atomic.AtomicBoolean isWaitingForSessionThread
protected final java.util.concurrent.atomic.AtomicBoolean wasOnPageFinishInvoked
protected final java.util.concurrent.atomic.AtomicInteger resourceInterceptState
RESOURCE_INTERCEPT_STATE_NONE
,
RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD
,
RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD
More about it at {https://codereview.chromium.org/1350553005/#ps20001}protected final java.util.concurrent.atomic.AtomicBoolean clientIsReload
protected SonicSessionStatistics statistics
protected volatile SonicServer server
protected volatile SonicDownloadEngine resourceDownloaderEngine
protected volatile java.io.InputStream pendingWebResourceStream
protected java.lang.String pendingDiffData
protected static long sNextSessionLogId
public final SonicSessionConfig config
public final java.lang.String id
protected boolean isPreload
public long createdTime
public final long sId
public java.lang.String srcUrl
protected volatile SonicSessionClient sessionClient
protected final android.os.Handler mainHandler
protected final java.util.concurrent.CopyOnWriteArrayList<java.lang.ref.WeakReference<SonicSession.Callback>> stateChangedCallbackList
protected SonicDiffDataCallback diffDataCallback
protected final android.os.Handler fileHandler
protected java.util.List<java.lang.String> preloadLinks
protected final java.util.concurrent.CopyOnWriteArrayList<java.lang.ref.WeakReference<com.tencent.sonic.sdk.SonicSessionCallback>> sessionCallbackList
protected final android.content.Intent intent
public boolean handleMessage(android.os.Message msg)
handleMessage
在接口中 android.os.Handler.Callback
public void start()
public boolean refresh()
protected android.content.Intent createConnectionIntent(com.tencent.sonic.sdk.SonicDataHelper.SessionData sessionData)
protected void handleFlow_Connection(boolean hasCache, com.tencent.sonic.sdk.SonicDataHelper.SessionData sessionData)
hasCache
- Indicates local sonic cache is exist or not.sessionData
- SessionData holds eTag templateTagprotected abstract void handleFlow_LoadLocalCache(java.lang.String cacheHtml)
protected abstract void handleFlow_FirstLoad()
SONIC_RESULT_CODE_FIRST_LOAD
logic.protected abstract void handleFlow_DataUpdate(java.lang.String serverRsp)
SONIC_RESULT_CODE_DATA_UPDATE
logic.serverRsp
- Server response data.protected abstract void handleFlow_TemplateChange(java.lang.String newHtml)
SONIC_RESULT_CODE_TEMPLATE_CHANGE
logic.newHtml
- new Html string from web-serverprotected abstract void handleFlow_HttpError(int responseCode)
protected abstract void handleFlow_ServiceUnavailable()
protected void handleFlow_NotModified()
public boolean isPreload()
public SonicSessionStatistics getStatistics()
protected boolean addSessionStateChangedCallback(SonicSession.Callback callback)
protected boolean removeSessionStateChangedCallback(SonicSession.Callback callback)
public boolean addSessionCallback(com.tencent.sonic.sdk.SonicSessionCallback callback)
public boolean removeSessionCallback(com.tencent.sonic.sdk.SonicSessionCallback callback)
public java.lang.String getCurrentUrl()
public int getFinalResultCode()
public int getSrcResultCode()
public boolean isDestroyedOrWaitingForDestroy()
protected boolean postForceDestroyIfNeed()
protected boolean canDestroy()
protected boolean switchState(int fromState, int toState, boolean notify)
public void onServerClosed(SonicServer sonicServer, boolean readComplete)
SonicSessionStream
, the inputStream
will be closed when the kernel reads the data.This method is invoked when the sonicSessionStream
close.
If the html is read complete, sonic will separate the html to template and data, and save these data.
sonicServer
- The actual server connection of current SonicSession.readComplete
- Whether the html is read complete.protected void postTaskToSaveSonicCache(java.lang.String htmlString)
protected void doSaveSonicCache(SonicServer sonicServer, java.lang.String htmlString)
protected void notifyStateChange(int oldState, int newState, android.os.Bundle extraData)
oldState
- The old state.newState
- The nex state.extraData
- The extra data.protected void setResult(int srcCode, int finalCode, boolean notify)
srcCode
- The original mode.finalCode
- The final mode.notify
- Whether notify te result to page.public boolean bindClient(SonicSessionClient client)
public boolean onClientReady()
public final java.lang.Object onClientRequestResource(java.lang.String url)
url
- The url of this sessionprotected boolean shouldSetCookieAsynchronous()
onClientRequestResource
is calling
in IOThread, it should not call set cookie synchronous which will handle in IOThread as it may
cause deadlock
More about it see {https://issuetracker.google.com/issues/36989494#c8}
Fix VasSonic issue {https://github.com/Tencent/VasSonic/issues/90}protected boolean setCookiesFromHeaders(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headers, boolean executeInNewThread)
headers
- headers from server responseexecuteInNewThread
- whether execute in new thread or notprotected java.lang.Object onRequestResource(java.lang.String url)
url
- The url of this sessionpublic boolean onWebReady(SonicDiffDataCallback diffDataCallback)
diffDataCallback
- Sonic provides the latest data to the page through this callbackpublic boolean onClientPageFinished(java.lang.String url)
public boolean isMatchCurrentUrl(java.lang.String url)
url
- The incoming url.protected java.util.HashMap<java.lang.String,java.lang.String> getHeaders()
protected java.lang.String getCharsetFromHeaders()
public java.lang.String getCharsetFromHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
protected java.util.HashMap<java.lang.String,java.lang.String> getCacheHeaders()
public SonicSessionClient getSessionClient()
public void destroy()
protected void destroy(boolean force)
protected void clearSessionData()