001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jppf.android.node;
019
020import android.app.Activity;
021import android.util.Log;
022import android.view.View;
023
024import org.jppf.node.event.DefaultLifeCycleErrorHandler;
025import org.jppf.node.event.NodeIntegrationAdapter;
026import org.jppf.node.event.NodeLifeCycleErrorHandler;
027import org.jppf.node.event.NodeLifeCycleEvent;
028import org.jppf.node.event.NodeLifeCycleListener;
029import org.jppf.utils.ExceptionUtils;
030
031/**
032 * This is the base event handler for an Android node.
033 * @author Laurent Cohen
034 */
035public abstract class AndroidNodeIntegrationAdapter extends NodeIntegrationAdapter<Activity> implements NodeLifeCycleErrorHandler {
036  /**
037   * Log tag for this class.
038   */
039  private static String LOG_TAG = AndroidNodeIntegrationAdapter.class.getSimpleName();
040  /**
041   * An activity holding the UI to update.
042   */
043  protected Activity activity;
044
045  /**
046   * Get the view to be displayed during a job execution, if any.
047   * @return a {@link View} which will replace the one in the main activity, or {@code null} to keep the existing view.
048   */
049  public abstract View getContentView();
050
051  @Override
052  public final void setUiComponent(final Activity uiComponent) {
053    setActivity(uiComponent);
054  }
055
056  /**
057   * Set the Android activity from which the node is launched.
058   * @param activity the activity to set.
059   */
060  void setActivity(Activity activity) {
061    this.activity = activity;
062  }
063
064  /**
065   * Get the Android activity from which the node is launched.
066   * @return an instance of {@link android.app.Activity}.
067   */
068  public final Activity getActivity() {
069    return activity;
070  }
071
072  @Override
073  public void handleError(final NodeLifeCycleListener listener, final NodeLifeCycleEvent event, final Throwable t) {
074    StringBuilder sb = new StringBuilder("error executing ");
075    sb.append(event == null ? "unknown" : DefaultLifeCycleErrorHandler.methodsNamesMap.get(event.getType()));
076    sb.append(" on an instance of ").append(listener == null ? "[unknown listener class]" : listener.getClass().getName());
077    sb.append(", nbTasks=").append(event.getTasks() == null ? -1 : event.getTasks().size()).append(" :\n");
078    Log.e(LOG_TAG, sb.append(ExceptionUtils.getStackTrace(t)).toString());
079  }
080}