Android Open Source - KISSmetrics-Android-SDK Sender Ready State






From Project

Back to project page KISSmetrics-Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project KISSmetrics-Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//
// KISSmetricsSDK
///*  w w  w  .j a  va 2s.  c om*/
// Copyright 2014 KISSmetrics
//
// 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.kissmetrics.sdk;

import java.util.concurrent.Executors;

public class SenderReadyState implements SenderState {

  Sender sender;
  
  public SenderReadyState(Sender sender) {
    this.sender = sender;
  }
  
  private void sendTopRecord() {
    
    // Assemble the full query string by prepending the current baseUrl as last archived.
    String apiQuery = ArchiverImpl.sharedArchiver().getBaseUrl()+ArchiverImpl.sharedArchiver().getQueryString(0);
    ConnectionImpl connection = sender.getNewConnection();
    connection.sendRecord(apiQuery, sender);
  }
  
  public void startSending() {

    // Ignore if we have nothing to send
    if (ArchiverImpl.sharedArchiver().getQueueCount() == 0) {
      return;
    }
    
    // Change to sendingState first so that sendingState is ready 
    // to handle ConnectionDelegate's connectionComplete.
    sender.setState(sender.getSendingState());
    
    // Begin sending
    Runnable runnable = new Runnable() { 
      @Override
      public void run() {
        sendTopRecord();
        }
    };
    
    sender.executorService = Executors.newFixedThreadPool(1);
    
    try {
      sender.executorService.execute(runnable);
    } catch(Exception e) {
      // Any failure to execute must place the Sender in a ready state;
      sender.setState(sender.getReadyState());
    }
  }
  
  public void disableSending() {

    // Switch to disabled state first so that ConnectionDelegate's 
    // connectionComplete is handled by the disabled state while we 
    // clear the send queue.
    sender.setState(sender.getDisabledState());
    
    // Empty the archived send queue
    ArchiverImpl.sharedArchiver().clearSendQueue();
  }
  
  public void enableSending() {
    // Ignored, already enabled
  }
  
  public void connectionComplete(String urlString, boolean success, boolean malformed) {
    
    // Ignored, connectionComplete should only be handled by the sending state.
    // The only way that this method could be called in this state is if a record 
    // is being sent at the time that the Sender is disabled and re-enabled.
    // We take no action because the queue would have already been emptied when 
    // switching to the disabled state.
  }
}




Java Source Code List

com.kissmetrics.api.MainActivity.java
com.kissmetrics.sdk.ArchiverImplActTest.java
com.kissmetrics.sdk.ArchiverImplTest.java
com.kissmetrics.sdk.ArchiverImpl.java
com.kissmetrics.sdk.Archiver.java
com.kissmetrics.sdk.ConnectionDelegate.java
com.kissmetrics.sdk.ConnectionImplTest.java
com.kissmetrics.sdk.ConnectionImpl.java
com.kissmetrics.sdk.Connection.java
com.kissmetrics.sdk.KISSmetricsAPITest.java
com.kissmetrics.sdk.KISSmetricsAPI.java
com.kissmetrics.sdk.QueryEncoderTest.java
com.kissmetrics.sdk.QueryEncoder.java
com.kissmetrics.sdk.SenderDisabledState.java
com.kissmetrics.sdk.SenderReadyState.java
com.kissmetrics.sdk.SenderSendingState.java
com.kissmetrics.sdk.SenderState.java
com.kissmetrics.sdk.Sender.java
com.kissmetrics.sdk.TestableConnectionImpl.java
com.kissmetrics.sdk.TestableVerificationImpl.java
com.kissmetrics.sdk.TrackingRunnablesNonTrackingState.java
com.kissmetrics.sdk.TrackingRunnablesTrackingState.java
com.kissmetrics.sdk.TrackingRunnables.java
com.kissmetrics.sdk.VerificationDelegate.java
com.kissmetrics.sdk.VerificationImplTest.java
com.kissmetrics.sdk.VerificationImpl.java
org.apache.cactus.mock.MockHttpURLConnection.java