Android Open Source - HockeySDK-Android Check Update Task With U I






From Project

Back to project page HockeySDK-Android.

License

The source code is released under:

Apache License

If you think the Android project HockeySDK-Android 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 net.hockeyapp.android.tasks;
/*from  w  w w  .j a va 2s.  co  m*/
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;

import net.hockeyapp.android.Constants;
import net.hockeyapp.android.Strings;
import net.hockeyapp.android.UpdateActivity;
import net.hockeyapp.android.UpdateFragment;
import net.hockeyapp.android.UpdateManagerListener;
import net.hockeyapp.android.utils.Util;
import net.hockeyapp.android.utils.VersionCache;

import org.json.JSONArray;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

/**
 * <h3>Description</h3>
 *
 * Internal helper class. Checks if a new update is available by
 * fetching version data from Hockeyapp.
 *
 * <h3>License</h3>
 *
 * <pre>
 * Copyright (c) 2011-2014 Bit Stadium GmbH
 *
 * 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.
 * </pre>
 *
 * @author Francisco Javier Fernandez
 **/
public class CheckUpdateTaskWithUI extends CheckUpdateTask {

  private Activity activity = null;
  private AlertDialog dialog = null;
  protected boolean isDialogRequired = false;

  public CheckUpdateTaskWithUI(WeakReference<Activity> weakActivity, String urlString, String appIdentifier, UpdateManagerListener listener, boolean isDialogRequired) {
    super(weakActivity, urlString, appIdentifier, listener);

    if (weakActivity != null) {
      activity = weakActivity.get();
    }

    this.isDialogRequired = isDialogRequired;
  }

  @Override
  public void detach() {
    super.detach();
    
    activity = null;
    
    if (dialog != null) {
      dialog.dismiss();
      dialog = null;
    }
  }

  @Override
  protected void onPostExecute(JSONArray updateInfo) {
    super.onPostExecute(updateInfo);
    if ((updateInfo != null) && (isDialogRequired)) {
      showDialog(updateInfo);
    }
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void showDialog(final JSONArray updateInfo) {
    if (getCachingEnabled()) {
      VersionCache.setVersionInfo(activity, updateInfo.toString());
    }

    if ((activity == null) || (activity.isFinishing())) {
      return;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(Strings.get(listener, Strings.UPDATE_DIALOG_TITLE_ID));

    if (!mandatory) {
      builder.setMessage(Strings.get(listener, Strings.UPDATE_DIALOG_MESSAGE_ID));

      builder.setNegativeButton(Strings.get(listener, Strings.UPDATE_DIALOG_NEGATIVE_BUTTON_ID), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          cleanUp();
        }
      });

      builder.setPositiveButton(Strings.get(listener, Strings.UPDATE_DIALOG_POSITIVE_BUTTON_ID), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          if (getCachingEnabled()) {
            VersionCache.setVersionInfo(activity, "[]");
          }

          WeakReference<Activity> weakActivity = new WeakReference<Activity>(activity);
          if ((Util.fragmentsSupported()) && (Util.runsOnTablet(weakActivity))) {
            showUpdateFragment(updateInfo);
          }
          else {
            startUpdateIntent(updateInfo, false);
          }
        }
      });

      dialog = builder.create();
      dialog.show();
    }
    else {
      Toast.makeText(activity, Strings.get(listener, Strings.UPDATE_MANDATORY_TOAST_ID), Toast.LENGTH_LONG).show();
      startUpdateIntent(updateInfo, true);
    }
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void showUpdateFragment(final JSONArray updateInfo) {
    if (activity != null) {
      FragmentTransaction fragmentTransaction = activity.getFragmentManager().beginTransaction();
      fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

      Fragment existingFragment = activity.getFragmentManager().findFragmentByTag("hockey_update_dialog");
      if (existingFragment != null) {
        fragmentTransaction.remove(existingFragment);
      }
      fragmentTransaction.addToBackStack(null);

      // Create and show the dialog
      Class<? extends UpdateFragment> fragmentClass = UpdateFragment.class;
      if (listener != null) {
        fragmentClass = listener.getUpdateFragmentClass();
      }

      try {
        Method method = fragmentClass.getMethod("newInstance", JSONArray.class, String.class);
        DialogFragment updateFragment = (DialogFragment)method.invoke(null, updateInfo, getURLString("apk"));
        updateFragment.show(fragmentTransaction, "hockey_update_dialog");
      }
      catch (Exception e) {
        Log.d(Constants.TAG, "An exception happened while showing the update fragment:");
        e.printStackTrace();
        Log.d(Constants.TAG, "Showing update activity instead.");
        startUpdateIntent(updateInfo, false);
      }
    }
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void startUpdateIntent(final JSONArray updateInfo, Boolean finish) {
    Class<?> activityClass = null;
    if (listener != null) {
      activityClass = listener.getUpdateActivityClass();
    }
    if (activityClass == null) {
      activityClass = UpdateActivity.class;
    }

    if (activity != null) {
      Intent intent = new Intent();
      intent.setClass(activity, activityClass);
      intent.putExtra(INTENT_EXTRA_JSON, updateInfo.toString());
      intent.putExtra(INTENT_EXTRA_URL, getURLString(APK));
      activity.startActivity(intent);

      if (finish) {
        activity.finish();
      }
    }

    cleanUp();
  }

  @Override
  protected void cleanUp() {
    super.cleanUp();
    activity = null;
    dialog = null;
  }
}




Java Source Code List

net.hockeyapp.android.Constants.java
net.hockeyapp.android.CrashManagerListener.java
net.hockeyapp.android.CrashManager.java
net.hockeyapp.android.ExceptionHandler.java
net.hockeyapp.android.ExpiryInfoActivity.java
net.hockeyapp.android.FeedbackActivityInterface.java
net.hockeyapp.android.FeedbackActivity.java
net.hockeyapp.android.FeedbackManagerListener.java
net.hockeyapp.android.FeedbackManager.java
net.hockeyapp.android.LocaleManager.java
net.hockeyapp.android.LoginActivity.java
net.hockeyapp.android.LoginManagerListener.java
net.hockeyapp.android.LoginManager.java
net.hockeyapp.android.PaintActivity.java
net.hockeyapp.android.StringListener.java
net.hockeyapp.android.Strings.java
net.hockeyapp.android.Tracking.java
net.hockeyapp.android.UpdateActivityInterface.java
net.hockeyapp.android.UpdateActivity.java
net.hockeyapp.android.UpdateFragment.java
net.hockeyapp.android.UpdateInfoListener.java
net.hockeyapp.android.UpdateManagerListener.java
net.hockeyapp.android.UpdateManager.java
net.hockeyapp.android.adapters.MessagesAdapter.java
net.hockeyapp.android.listeners.DownloadFileListener.java
net.hockeyapp.android.listeners.SendFeedbackListener.java
net.hockeyapp.android.objects.ErrorObject.java
net.hockeyapp.android.objects.FeedbackAttachment.java
net.hockeyapp.android.objects.FeedbackMessage.java
net.hockeyapp.android.objects.FeedbackResponse.java
net.hockeyapp.android.objects.Feedback.java
net.hockeyapp.android.tasks.AttachmentDownloader.java
net.hockeyapp.android.tasks.CheckUpdateTaskWithUI.java
net.hockeyapp.android.tasks.CheckUpdateTask.java
net.hockeyapp.android.tasks.DownloadFileTask.java
net.hockeyapp.android.tasks.GetFileSizeTask.java
net.hockeyapp.android.tasks.LoginTask.java
net.hockeyapp.android.tasks.ParseFeedbackTask.java
net.hockeyapp.android.tasks.SendFeedbackTask.java
net.hockeyapp.android.utils.AsyncTaskUtils.java
net.hockeyapp.android.utils.Base64.java
net.hockeyapp.android.utils.ConnectionManager.java
net.hockeyapp.android.utils.DeviceUtils.java
net.hockeyapp.android.utils.FeedbackParser.java
net.hockeyapp.android.utils.ImageUtils.java
net.hockeyapp.android.utils.PrefsUtil.java
net.hockeyapp.android.utils.SimpleMultipartEntity.java
net.hockeyapp.android.utils.UiThreadUtil.java
net.hockeyapp.android.utils.Util.java
net.hockeyapp.android.utils.VersionCache.java
net.hockeyapp.android.utils.VersionHelper.java
net.hockeyapp.android.utils.ViewHelper.java
net.hockeyapp.android.views.AttachmentListView.java
net.hockeyapp.android.views.AttachmentView.java
net.hockeyapp.android.views.ExpiryInfoView.java
net.hockeyapp.android.views.FeedbackMessageView.java
net.hockeyapp.android.views.FeedbackView.java
net.hockeyapp.android.views.LoginView.java
net.hockeyapp.android.views.PaintView.java
net.hockeyapp.android.views.UpdateView.java