Android Open Source - banshee-remote Banshee Server Check Task






From Project

Back to project page banshee-remote.

License

The source code is released under:

GNU General Public License

If you think the Android project banshee-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

package de.viktorreiser.bansheeremote.data;
/* www.j  av  a2 s .c  o  m*/
import java.lang.ref.WeakReference;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import de.viktorreiser.bansheeremote.R;

/**
 * General asynchronous banshee server availability checker.<br>
 * <br>
 * An activity should implement {@link OnBansheeServerCheck} and start the check with
 * {@link #BansheeServerCheckTask(BansheeServer, Activity)}. A dialog will be shown and dismissed on
 * finish. The task will call the callback on the given activity.<br>
 * <br>
 * See {@link #showDialog(Activity)} for some more information.
 * 
 * @author Viktor Reiser &lt;<a href="mailto:viktorreiser@gmx.de">viktorreiser@gmx.de</a>&gt;
 */
public class BansheeServerCheckTask extends AsyncTask<Void, Void, Integer> {
  
  // PRIVATE ====================================================================================
  
  private WeakReference<Activity> mCallback;
  private ProgressDialog mDialog;
  private BansheeServer mServer;
  
  // PUBLIC =====================================================================================
  
  public interface OnBansheeServerCheck {
    
    /**
     * Server test request response.
     * 
     * @param success
     *            {@code -1} not reachable, {@code 0} reachable but wrong password, {@code 1}
     *            everything okay
     */
    public void onBansheeServerCheck(Integer success);
  }
  
  
  /**
   * Create a banshee server check task.
   * 
   * @param server
   *            banshee server to check
   * @param callback
   *            the callback which will be informed when check task finishes - this object is a
   *            activity also so it can be used to construct the progress dialog
   */
  public <T extends Activity & OnBansheeServerCheck> BansheeServerCheckTask(BansheeServer server,
      T callback) {
    mServer = server;
    showDialog(callback);
    execute();
  }
  
  /**
   * Update the callback and show dialog again.<br>
   * <br>
   * This is a helper to manage orientation changes as long the check task is still running. You
   * call {@link #dismissDialog()} when the activity is destroy, retain this task, load it again
   * in when the new activity is created and call then this method to indicate that the task is
   * still running.
   * 
   * @param callback
   *            the callback which will be informed when check task finishes - this object is a
   *            activity also so it can be used to construct the progress dialog
   */
  public <T extends Activity & OnBansheeServerCheck> void showDialog(T callback) {
    mCallback = new WeakReference<Activity>(callback);
    
    mDialog = new ProgressDialog(callback);
    mDialog.setMessage(callback.getResources().getString(R.string.checking_server));
    mDialog.setIndeterminate(true);
    mDialog.setCancelable(false);
    mDialog.show();
  }
  
  /**
   * Dismiss the displayed dialog.
   * 
   * @see #showDialog(Activity)
   */
  public void dismissDialog() {
    mDialog.dismiss();
  }
  
  /**
   * Get the checked banshee server.
   * 
   * @return checked banshee server
   */
  public BansheeServer getServer() {
    return mServer;
  }
  
  // OVERRIDDEN =================================================================================
  
  @Override
  protected Integer doInBackground(Void... params) {
    return BansheeConnection.checkConnection(mServer);
  }
  
  @Override
  protected void onPostExecute(Integer result) {
    try {
      mDialog.dismiss();
    } catch (Exception e) {
      // this is causing a IllegalArgumentException (view not attached to window manager)
      // I can't tell why, always when returning to application
      e.printStackTrace();
    }
    
    Activity a = mCallback.get();
    
    if (a == null) {
      return;
    }
    
    ((OnBansheeServerCheck) a).onBansheeServerCheck(result);
  }
}




Java Source Code List

de.viktorreiser.bansheeremote.activity.AlbumActivity.java
de.viktorreiser.bansheeremote.activity.ArtistActivity.java
de.viktorreiser.bansheeremote.activity.CurrentSongActivity.java
de.viktorreiser.bansheeremote.activity.NewOrEditServerActivity.java
de.viktorreiser.bansheeremote.activity.PlaylistActivity.java
de.viktorreiser.bansheeremote.activity.PlaylistOverviewActivity.java
de.viktorreiser.bansheeremote.activity.ServerListActivity.java
de.viktorreiser.bansheeremote.activity.SettingsActivity.java
de.viktorreiser.bansheeremote.activity.TrackActivity.java
de.viktorreiser.bansheeremote.data.App.java
de.viktorreiser.bansheeremote.data.BansheeConnection.java
de.viktorreiser.bansheeremote.data.BansheeDatabase.java
de.viktorreiser.bansheeremote.data.BansheeServerCheckTask.java
de.viktorreiser.bansheeremote.data.BansheeServer.java
de.viktorreiser.bansheeremote.data.CoverCache.java
de.viktorreiser.toolbox.content.NetworkStateBroadcast.java
de.viktorreiser.toolbox.os.LruCache.java
de.viktorreiser.toolbox.preference.NumberPickerPreference.java
de.viktorreiser.toolbox.util.AndroidUtils.java
de.viktorreiser.toolbox.util.L.java
de.viktorreiser.toolbox.util.StringUtils.java
de.viktorreiser.toolbox.widget.HiddenQuickActionSetup.java
de.viktorreiser.toolbox.widget.NumberPicker.java
de.viktorreiser.toolbox.widget.SwipeableHiddenView.java
de.viktorreiser.toolbox.widget.SwipeableListItem.java
de.viktorreiser.toolbox.widget.SwipeableListView.java