Android Open Source - Boxee-Thumb-Remote Server State Poller






From Project

Back to project page Boxee-Thumb-Remote.

License

The source code is released under:

Apache License

If you think the Android project Boxee-Thumb-Remote 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

/* The following code was written by Menny Even Danan
 * and is released under the APACHE 2.0 license
 *//from w w w  . j  a v  a  2 s.  com
 * http://www.apache.org/licenses/LICENSE-2.0
 */
package net.evendanan.android.thumbremote;

import java.util.Random;

import net.evendanan.android.thumbremote.network.HttpRequest;
import net.evendanan.android.thumbremote.network.Response;


import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log;

public final class ServerStatePoller {
  private static final String TAG = "ServerStatePoller";
  
  private static final int MAX_NETWORK_ERRORS = 3;
  
  private static final long REGULAR_DELAY = 1000;
  private static final long BACKGROUND_DELAY = 5000;
  
  private final Object mWaiter = new Object();
  private final ServerStateUrlsProvider mUrlsProvider;
  private final WifiManager mWifi;
  
  private boolean mInBackground = false;
  private int mErrorsAllowedLeft = MAX_NETWORK_ERRORS;
  private int mServerErrorCode = 200;
  
  private boolean mRun;

  public ServerStatePoller(ServerStateUrlsProvider provider, Context context) {
    mUrlsProvider = provider;
    mWifi =  (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mRun = true;
  }
  private final Thread mPollerThread = new Thread()
  {  
    
    private WifiLock mWifiLock = null;

    public void run() {
      Log.d(TAG, "Starting ServerStatePoller.");
      mWifiLock  = mWifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "ThumbRemote"+(new Random()).nextInt());
      mWifiLock.acquire();
      while(mRun)
      {
        
        try
        {
          mUrlsProvider.startOver();
          while(mUrlsProvider.requiresServerStateData())
          {
            String[] responses = getResponsesFromServer(mUrlsProvider.getServerStateUrls());
            if (responses != null)
            {
              mServerErrorCode = 200;
              mErrorsAllowedLeft = MAX_NETWORK_ERRORS;
              mUrlsProvider.onServerStateResponsesAvailable(responses);
            }
            else
            {
              mErrorsAllowedLeft--;
              if (mErrorsAllowedLeft == 0)
              {
                mUrlsProvider.onServerStateRetrievalError(mServerErrorCode);
              }
            }
          }
        }
        finally
        {
          if (mRun)
          {
            synchronized (mWaiter) {
              //refreshing state every 500 ms
              try {
                mWaiter.wait(mInBackground? BACKGROUND_DELAY : REGULAR_DELAY );
              } catch (InterruptedException e) {
                e.printStackTrace();
                mRun = false;
              }
            }
          }
        }
      }
      mWifiLock.release();
      Log.d(TAG, "ServerStatePoller ended.");
    }
  };
  
  public void poll() {
    mRun = true;
    mInBackground = false;
    mPollerThread.start();
  }
  
  public void checkStateNow()
  {
    synchronized (mWaiter) {
      mWaiter.notifyAll();
    }
  }
  
  public void moveToBackground() {
    mInBackground = true;
  }
  
  public void comeBackToForeground() {
    mInBackground = false;
  }
  
  
  public void stop() {
    mRun = false;
    synchronized (mWaiter) {
      mWaiter.notifyAll();
    }
  }
  
  private String[] getResponsesFromServer(String[] urls) {
    String[] responses = new String[urls.length];
    for(int i=0; i<urls.length; i++)
    {
      final String url = urls[i];
      Response r = HttpRequest.getHttpResponse(url);
      if (!r.success())
      {
        mServerErrorCode = r.responseCode();
        return null;
      }
      responses[i] = r.response();
    }
    
    return responses;
  }
}




Java Source Code List

.HttpClientBlocking.java
com.example.android.actionbarcompat.ActionBarHelperBase.java
com.example.android.actionbarcompat.ActionBarHelperCompat.java
com.example.android.actionbarcompat.ActionBarHelperHoneycomb.java
com.example.android.actionbarcompat.ActionBarHelperICS.java
com.example.android.actionbarcompat.SimpleMenuItem.java
com.example.android.actionbarcompat.SimpleMenu.java
iharder.base64.Base64.java
net.evendanan.android.thumbremote.MediaStateListener.java
net.evendanan.android.thumbremote.RemoteApplication.java
net.evendanan.android.thumbremote.ServerAddress.java
net.evendanan.android.thumbremote.ServerConnectionListener.java
net.evendanan.android.thumbremote.ServerConnector.java
net.evendanan.android.thumbremote.ServerRemote.java
net.evendanan.android.thumbremote.ServerStatePoller.java
net.evendanan.android.thumbremote.ServerStateUrlsProvider.java
net.evendanan.android.thumbremote.ServerState.java
net.evendanan.android.thumbremote.Settings.java
net.evendanan.android.thumbremote.ShakeListener.java
net.evendanan.android.thumbremote.UiView.java
net.evendanan.android.thumbremote.boxee.BoxeeConnector.java
net.evendanan.android.thumbremote.boxee.BoxeeDiscovererThread.java
net.evendanan.android.thumbremote.network.HttpBlocking.java
net.evendanan.android.thumbremote.network.HttpRequest.java
net.evendanan.android.thumbremote.network.Response.java
net.evendanan.android.thumbremote.network.ReusableHttpClientBlocking.java
net.evendanan.android.thumbremote.service.DoServerRemoteAction.java
net.evendanan.android.thumbremote.service.ServerRemoteService.java
net.evendanan.android.thumbremote.service.State.java
net.evendanan.android.thumbremote.ui.FixedViewFlipper.java
net.evendanan.android.thumbremote.ui.FragmentAlertDialogSupport.java
net.evendanan.android.thumbremote.ui.HelpUiActivity.java
net.evendanan.android.thumbremote.ui.RemoteUiActivity.java
net.evendanan.android.thumbremote.ui.SettingsActivity.java