Android Open Source - geoar-app Data Source Instance Holder






From Project

Back to project page geoar-app.

License

The source code is released under:

Apache License

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

/**
 * Copyright 2012 52North Initiative for Geospatial Open Source Software GmbH
 */*  w w w .  j  av  a 2  s .  co m*/
 * 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 org.n52.geoar.newdata;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.SocketException;
import java.util.HashSet;
import java.util.Set;

import org.n52.geoar.GeoARApplication;
import org.n52.geoar.R;
import org.n52.geoar.newdata.CheckList.CheckManager;
import org.n52.geoar.newdata.CheckList.CheckedChangedListener;
import org.n52.geoar.newdata.DataSourceInstanceSettingsDialogActivity.SettingsResultListener;
import org.n52.geoar.settings.SettingsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcelable;

public class DataSourceInstanceHolder implements Parcelable {

  public interface DataSourceSettingsChangedListener {
    void onDataSourceSettingsChanged();
  }

  private static int nextId = 0;
  private static final int CLEAR_CACHE = 1;
  private static final int CLEAR_CACHE_AFTER_DEACTIVATION_DELAY = 10000;
  private static final Logger LOG = LoggerFactory
      .getLogger(DataSourceInstanceHolder.class);

  private Handler dataSourceHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
      if (msg.what == CLEAR_CACHE) {
        // Delayed clearing of cache after datasource has been
        // deactivated
        dataCache.clearCache();
        return true;
      }

      return false;
    }
  });
  private DataSource<? super Filter> dataSource;
  private boolean injected = false;
  private DataSourceHolder parentHolder;
  private final int id = nextId++;
  private DataCache dataCache;
  private Filter currentFilter;
  private Boolean hasSettings = null;
  @CheckManager
  private CheckList<DataSourceInstanceHolder>.Checker mChecker;
  private Exception lastError;
  private Set<DataSourceSettingsChangedListener> mSettingsChangedListeners = new HashSet<DataSourceInstanceHolder.DataSourceSettingsChangedListener>(
      0);

  public DataSourceInstanceHolder(DataSourceHolder parentHolder,
      DataSource<? super Filter> dataSource) {
    this.parentHolder = parentHolder;
    this.dataSource = dataSource;
    try {
      this.currentFilter = parentHolder.getFilterClass().newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(
          "Referenced filter has no appropriate constructor");
    } catch (IllegalAccessException e) {
      throw new RuntimeException(
          "Referenced filter has no appropriate constructor");
    }

    dataCache = new DataCache(this);
  }

  @CheckedChangedListener
  public void checkedChanged(boolean state) {
    if (state) {
      activate();
    } else {
      deactivate();
    }
  }

  /**
   * Prevents datasource from getting unloaded. Should be called when
   * datasource is added to map/ar.
   */
  public void activate() {
    LOG.info("Activating data source instance " + getName());

    // prevents clearing of cache by removing messages
    dataSourceHandler.removeMessages(CLEAR_CACHE);
    if (!injected) {
      parentHolder.perfomInjection(dataSource);
      injected = true;
    }
  }

  /**
   * Queues unloading of datasource and cached data
   */
  public void deactivate() {
    // Clears the cache 30s after calling this method
    LOG.info("Deactivating data source " + getName());
    dataSourceHandler.sendMessageDelayed(
        dataSourceHandler.obtainMessage(CLEAR_CACHE),
        CLEAR_CACHE_AFTER_DEACTIVATION_DELAY);
  }

  public DataCache getDataCache() {
    return dataCache;
  }

  public void createSettingsDialog(Context context) {
    SettingsResultListener resultListener = new SettingsResultListener() {
      @Override
      void onSettingsResult(int resultCode) {
        if (resultCode == Activity.RESULT_OK) {
          notifySettingsChanged();
        }
      }
    };

    Intent intent = new Intent(context,
        DataSourceInstanceSettingsDialogActivity.class);
    intent.putExtra("dataSourceInstance", this);
    intent.putExtra("resultListener", resultListener); // unsure whether
                              // Intent uses weak
                              // references too
    context.startActivity(intent);
  }

  public Filter getCurrentFilter() {
    return currentFilter;
  }

  public DataSourceHolder getParent() {
    return parentHolder;
  }

  public void addOnSettingsChangedListener(
      DataSourceSettingsChangedListener listener) {
    mSettingsChangedListeners.add(listener);
  }

  public void removeOnSettingsChangedListener(
      DataSourceSettingsChangedListener listener) {
    mSettingsChangedListeners.remove(listener);
  }

  /**
   * It does not only notify listeners, but also clears the current cache.
   * TODO
   */
  void notifySettingsChanged() {
    dataCache.setFilter(currentFilter);
    for (DataSourceSettingsChangedListener listener : mSettingsChangedListeners) {
      listener.onDataSourceSettingsChanged();
    }
  }

  public String getName() {
    if (parentHolder.getNameCallbackMethod() != null) {
      // try to use name callback
      try {
        return (String) parentHolder.getNameCallbackMethod().invoke(
            dataSource);
      } catch (Exception e) {
        LOG.warn("Data Source " + parentHolder.getName()
            + " NameCallback fails");
      }
    }

    if (parentHolder.instanceable()) {
      return parentHolder.getName() + " " + id;
    } else {
      return parentHolder.getName();
    }
  }

  public DataSource<? super Filter> getDataSource() {
    return dataSource;
  }

  public boolean hasSettings() {
    if (hasSettings == null) {
      hasSettings = SettingsHelper.hasSettings(getCurrentFilter())
          || SettingsHelper.hasSettings(getDataSource());
    }

    return hasSettings;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    // Parcel based on unique DataSourceHolder id
    dest.writeParcelable(parentHolder, 0);
    dest.writeInt(id);
  }

  public static final Parcelable.Creator<DataSourceInstanceHolder> CREATOR = new Parcelable.Creator<DataSourceInstanceHolder>() {
    public DataSourceInstanceHolder createFromParcel(Parcel in) {
      DataSourceHolder dataSource = in
          .readParcelable(DataSourceHolder.class.getClassLoader());
      int id = in.readInt();
      // Find DataSourceInstance with provided id
      for (DataSourceInstanceHolder instance : dataSource.getInstances()) {
        if (instance.id == id) {
          return instance;
        }
      }

      return null;
    }

    public DataSourceInstanceHolder[] newArray(int size) {
      return new DataSourceInstanceHolder[size];
    }
  };

  public boolean isChecked() {
    return mChecker.isChecked();
  }

  public void setChecked(boolean state) {
    mChecker.setChecked(state);
  }

  public void saveState(ObjectOutputStream objectOutputStream)
      throws IOException {

    // Store filter, serializable
    objectOutputStream.writeObject(currentFilter);

    // Store data source instance settings using settings framework
    SettingsHelper.storeSettings(objectOutputStream, this.dataSource);

    objectOutputStream.writeBoolean(isChecked());
  }

  public void restoreState(ObjectInputStream objectInputStream)
      throws IOException {
    try {
      // restore filter, serializable
      currentFilter = (Filter) objectInputStream.readObject();
      dataCache.setFilter(currentFilter);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // restore data source instance settings, settings framework
    SettingsHelper.restoreSettings(objectInputStream, this.dataSource);

    setChecked(objectInputStream.readBoolean());
  }

  public void reportError(Exception e) {
    lastError = e;
  }

  public void clearError() {
    lastError = null;
  }

  public String getErrorString() {
    if (lastError == null) {
      return null;
    }

    if (lastError instanceof SocketException) {
      return GeoARApplication.applicationContext
          .getString(R.string.connection_error);
    } else if (lastError instanceof PluginException) {
      return ((PluginException) lastError).getTitle();
    }

    return GeoARApplication.applicationContext
        .getString(R.string.unknown_error);
  }

  public boolean hasErrorMessage() {
    if (lastError == null) {
      return false;
    }
    return lastError instanceof PluginException;
  }

  public String getErrorMessage() {
    if (lastError == null || !(lastError instanceof PluginException)) {
      return null;
    }

    return lastError.toString();
  }
}




Java Source Code List

.DataSourcesOverlay.java
.VisualizationOverlayItem.java
org.n52.geoar.AboutDialog.java
org.n52.geoar.DataSourceListAdapter.java
org.n52.geoar.GeoARActivity.java
org.n52.geoar.GeoARApplication.java
org.n52.geoar.ar.view.ARFragment.java
org.n52.geoar.ar.view.ARObject.java
org.n52.geoar.ar.view.ARView.java
org.n52.geoar.ar.view.DataSourceVisualizationHandler.java
org.n52.geoar.ar.view.IntroController.java
org.n52.geoar.ar.view.IntroViewer.java
org.n52.geoar.ar.view.gl.ARSurfaceViewRenderer.java
org.n52.geoar.ar.view.gl.ARSurfaceView.java
org.n52.geoar.ar.view.gl.GLESCamera.java
org.n52.geoar.ar.view.gl.MultisampleConfigs.java
org.n52.geoar.ar.view.gl.SurfaceTopology.java
org.n52.geoar.ar.view.overlay.ARCanvasSurfaceView.java
org.n52.geoar.ar.view.overlay.GUIDrawable.java
org.n52.geoar.ar.view.overlay.Radar.java
org.n52.geoar.exception.UnsupportedGeometryType.java
org.n52.geoar.map.view.DataSourceOverlayHandler.java
org.n52.geoar.map.view.GeoARMapView.java
org.n52.geoar.map.view.MapActivityContext.java
org.n52.geoar.map.view.MapFragment.java
org.n52.geoar.map.view.overlay.DataSourceOverlay.java
org.n52.geoar.map.view.overlay.DataSourcePointOverlay.java
org.n52.geoar.map.view.overlay.DataSourcePolygonOverlay.java
org.n52.geoar.map.view.overlay.DataSourcePolylineOverlay.java
org.n52.geoar.map.view.overlay.DataSourcesOverlay.java
org.n52.geoar.map.view.overlay.OverlayType.java
org.n52.geoar.map.view.overlay.PointOverlayType.java
org.n52.geoar.map.view.overlay.PolygonOverlayType.java
org.n52.geoar.map.view.overlay.PolylineOverlayType.java
org.n52.geoar.newdata.CheckList.java
org.n52.geoar.newdata.DataCache.java
org.n52.geoar.newdata.DataSourceHolder.java
org.n52.geoar.newdata.DataSourceInstanceHolder.java
org.n52.geoar.newdata.DataSourceInstanceSettingsDialogActivity.java
org.n52.geoar.newdata.InstalledPluginHolder.java
org.n52.geoar.newdata.PluginActivityContext.java
org.n52.geoar.newdata.PluginContext.java
org.n52.geoar.newdata.PluginDialogFragment.java
org.n52.geoar.newdata.PluginDownloadHolder.java
org.n52.geoar.newdata.PluginDownloader.java
org.n52.geoar.newdata.PluginFragment.java
org.n52.geoar.newdata.PluginGridAdapter.java
org.n52.geoar.newdata.PluginHolder.java
org.n52.geoar.newdata.PluginLoader.java
org.n52.geoar.newdata.PluginLogger.java
org.n52.geoar.newdata.PluginStateInputStream.java
org.n52.geoar.newdata.Tile.java
org.n52.geoar.settings.DateTimeSettingsViewField.java
org.n52.geoar.settings.DateUtils.java
org.n52.geoar.settings.NumberSettingsViewField.java
org.n52.geoar.settings.SettingsException.java
org.n52.geoar.settings.SettingsHelper.java
org.n52.geoar.settings.SettingsViewField.java
org.n52.geoar.settings.SettingsView.java
org.n52.geoar.settings.SpinnerSettingsViewField.java
org.n52.geoar.settings.StringSettingsViewField.java
org.n52.geoar.tracking.camera.CameraView.java
org.n52.geoar.tracking.camera.RealityCamera.java
org.n52.geoar.tracking.location.AdaptiveLowPassSensorBuffer.java
org.n52.geoar.tracking.location.LocationHandler.java
org.n52.geoar.tracking.location.LowPassSensorBuffer.java
org.n52.geoar.tracking.location.MeanSensorBuffer.java
org.n52.geoar.tracking.location.SensorBuffer.java
org.n52.geoar.view.InfoView.java
org.n52.geoar.view.geoar.CalibrationControlView.java
org.n52.geoar.view.geoar.Settings.java
org.n52.geoar.view.geoar.gl.mode.BilligerColorShader.java
org.n52.geoar.view.geoar.gl.mode.BilligerLightShader.java
org.n52.geoar.view.geoar.gl.mode.BilligerTextureShader.java
org.n52.geoar.view.geoar.gl.mode.BoundingBox.java
org.n52.geoar.view.geoar.gl.mode.FeatureShader.java
org.n52.geoar.view.geoar.gl.mode.PhongFeatureShader.java
org.n52.geoar.view.geoar.gl.mode.RenderFeature2.java
org.n52.geoar.view.geoar.gl.mode.Spatial.java
org.n52.geoar.view.geoar.gl.mode.TextureFeatureShader.java
org.n52.geoar.view.geoar.gl.mode.Texture.java
org.n52.geoar.view.geoar.gl.mode.features.CubeFeature2.java
org.n52.geoar.view.geoar.gl.mode.features.FlatCircleFeature.java
org.n52.geoar.view.geoar.gl.mode.features.HeightMapFeature.java
org.n52.geoar.view.geoar.gl.mode.features.NewGridFeature.java
org.n52.geoar.view.geoar.gl.mode.features.ReferencedGridFeature.java
org.n52.geoar.view.geoar.gl.mode.features.SphereFeature.java
org.n52.geoar.view.geoar.gl.mode.features.TriangleFeature.java