Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright (c) 2012, 2013, 2014 Countly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package ly.count.android.api;
import android.content.Context;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* ConnectionQueue queues session and event data and periodically sends that data to
* a Count.ly server on a background thread.
*
* None of the methods in this class are synchronized because access to this class is
* controlled by the Countly singleton, which is synchronized.
*
* NOTE: This class is only public to facilitate unit testing, because
* of this bug in dexmaker: https://code.google.com/p/dexmaker/issues/detail?id=34
*/
public class ConnectionQueue {
private CountlyStore store_;
private ExecutorService executor_;
private String appKey_;
private Context context_;
private String serverURL_;
private Future> connectionProcessorFuture_;
// Getters are for unit testing
String getAppKey() {
return appKey_;
}
void setAppKey(final String appKey) {
appKey_ = appKey;
}
Context getContext() {
return context_;
}
void setContext(final Context context) {
context_ = context;
}
String getServerURL() {
return serverURL_;
}
void setServerURL(final String serverURL) {
serverURL_ = serverURL;
}
CountlyStore getCountlyStore() {
return store_;
}
void setCountlyStore(final CountlyStore countlyStore) {
store_ = countlyStore;
}
/**
* Checks internal state and throws IllegalStateException if state is invalid to begin use.
* @throws java.lang.IllegalStateException if context, app key, store, or server URL have not been set
*/
void checkInternalState() {
if (context_ == null) {
throw new IllegalStateException("context has not been set");
}
if (appKey_ == null || appKey_.length() == 0) {
throw new IllegalStateException("app key has not been set");
}
if (store_ == null) {
throw new IllegalStateException("countly store has not been set");
}
if (serverURL_ == null || !Countly.isValidURL(serverURL_)) {
throw new IllegalStateException("server URL is not valid");
}
}
/**
* Records a session start event for the app and sends it to the server.
* @throws java.lang.IllegalStateException if context, app key, store, or server URL have not been set
*/
void beginSession() {
checkInternalState();
final String data = "app_key=" + appKey_
+ "×tamp=" + Countly.currentTimestamp()
+ "&sdk_version=" + Countly.COUNTLY_SDK_VERSION_STRING
+ "&begin_session=1"
+ "&metrics=" + DeviceInfo.getMetrics(context_);
store_.addConnection(data);
tick();
}
/**
* Records a session duration event for the app and sends it to the server. This method does nothing
* if passed a negative or zero duration.
* @param duration duration in seconds to extend the current app session, should be more than zero
* @throws java.lang.IllegalStateException if context, app key, store, or server URL have not been set
*/
void updateSession(final int duration) {
checkInternalState();
if (duration > 0) {
final String data = "app_key=" + appKey_
+ "×tamp=" + Countly.currentTimestamp()
+ "&session_duration=" + duration;
store_.addConnection(data);
tick();
}
}
/**
* Records a session end event for the app and sends it to the server. Duration is only included in
* the session end event if it is more than zero.
* @param duration duration in seconds to extend the current app session
* @throws java.lang.IllegalStateException if context, app key, store, or server URL have not been set
*/
void endSession(final int duration) {
checkInternalState();
String data = "app_key=" + appKey_
+ "×tamp=" + Countly.currentTimestamp()
+ "&end_session=1";
if (duration > 0) {
data += "&session_duration=" + duration;
}
store_.addConnection(data);
tick();
}
/**
* Records the specified events and sends them to the server.
* @param events URL-encoded JSON string of event data
* @throws java.lang.IllegalStateException if context, app key, store, or server URL have not been set
*/
void recordEvents(final String events) {
checkInternalState();
final String data = "app_key=" + appKey_
+ "×tamp=" + Countly.currentTimestamp()
+ "&events=" + events;
store_.addConnection(data);
tick();
}
/**
* Ensures that an executor has been created for ConnectionProcessor instances to be submitted to.
*/
void ensureExecutor() {
if (executor_ == null) {
executor_ = Executors.newSingleThreadExecutor();
}
}
/**
* Starts ConnectionProcessor instances running in the background to
* process the local connection queue data.
* Does nothing if there is connection queue data or if a ConnectionProcessor
* is already running.
*/
void tick() {
if (!store_.isEmptyConnections() && (connectionProcessorFuture_ == null || connectionProcessorFuture_.isDone())) {
ensureExecutor();
connectionProcessorFuture_ = executor_.submit(new ConnectionProcessor(serverURL_, store_));
}
}
// for unit testing
ExecutorService getExecutor() { return executor_; }
void setExecutor(final ExecutorService executor) { executor_ = executor; }
Future> getConnectionProcessorFuture() { return connectionProcessorFuture_; }
void setConnectionProcessorFuture(final Future> connectionProcessorFuture) { connectionProcessorFuture_ = connectionProcessorFuture; }
}