Android Open Source - geoar-app Plugin Downloader






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
 *//from  w  ww .j av a2 s .c om
 * 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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.n52.geoar.GeoARApplication;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;

/**
 * 
 * @author Arne de Wall <a.dewall@52North.org>
 *
 */
public class PluginDownloader {

  public interface OnDataSourceResultListener {
    void onDataSourceResult(List<PluginDownloadHolder> dataSources);
  }

  private static DefaultHttpClient mHttpClient;
  private static DownloadManager mDownloadManager;
  private static List<Long> currentDownloads = new ArrayList<Long>(0);

  private static BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      synchronized (currentDownloads) {
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)
            && !currentDownloads.isEmpty()) {
          // long downloadId = intent.getLongExtra(
          // DownloadManager.EXTRA_DOWNLOAD_ID, 0);
          Query query = new Query();

          long[] downloads = new long[currentDownloads.size()];
          for (int i = 0, len = currentDownloads.size(); i < len; i++) {
            downloads[i] = currentDownloads.get(i);
          }
          query.setFilterById(downloads);
          query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
          Cursor cursor = mDownloadManager.query(query);
          boolean pluginAdded = false;
          if (cursor.moveToFirst()) {
            int columnId = cursor
                .getColumnIndex(DownloadManager.COLUMN_ID);
            do {
              currentDownloads.remove(cursor.getLong(columnId));
              pluginAdded = true;
            } while (cursor.moveToNext());
            if (pluginAdded) {
              PluginLoader.reloadPlugins();
            }
          }

        }
      }
    }
  };

  static {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory
        .getSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory
        .getSocketFactory(), 443));

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
    ClientConnectionManager cm = new ThreadSafeClientConnManager(
        httpParameters, registry);
    mHttpClient = new DefaultHttpClient(cm, httpParameters);

    mDownloadManager = (DownloadManager) GeoARApplication.applicationContext
        .getSystemService(Context.DOWNLOAD_SERVICE);
    GeoARApplication.applicationContext.registerReceiver(broadcastReceiver,
        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  }

  // TODO externalize string
  static final String SERVER_URL = "http://geoviqua.dev.52north.org/geoar/codebase";

  private static final String CODEBASE_ID = "id";
  private static final String CODEBASE_NAME = "name";
  private static final String CODEBASE_DESCRIPTION = "description";
  private static final String CODEBASE_VERSION = "version";
  private static final String CODEBASE_PUBLISHER = "publisher";

  private static Set<OnDataSourceResultListener> mCurrentListeners = new HashSet<OnDataSourceResultListener>();

  private static Set<PluginDownloadHolder> mDownloadableDataSources = new HashSet<PluginDownloadHolder>();

  private static class DownloadTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPostExecute(Void result) {
      List<PluginDownloadHolder> resultList = new ArrayList<PluginDownloadHolder>();
      resultList.addAll(mDownloadableDataSources);
      for (OnDataSourceResultListener listener : mCurrentListeners) {
        listener.onDataSourceResult(resultList);
      }
      mCurrentListeners.clear();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // TODO will not reflect removal of remote data sources

      HttpGet request = new HttpGet(SERVER_URL);
      request.setHeader("Accept", "application/json");
      request.setHeader("Content-Type", "application/json");

      try {
        HttpResponse response = mHttpClient.execute(request);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
          // convert Stream to String
          String res = convertStreamToString(entity.getContent());
          // create JSONObject
          JSONObject json = new JSONObject(res);
          // parse JSOBObject
          JSONArray datasourceArray = json
              .getJSONArray("datasources");
          for (int i = 0; i < datasourceArray.length(); i++) {
            PluginDownloadHolder holder = new PluginDownloadHolder();
            JSONObject currentObject = datasourceArray
                .getJSONObject(i);

            holder.setIdentifier(currentObject
                .getString(CODEBASE_ID));
            holder.setName(currentObject.getString(CODEBASE_NAME));
            holder.setVersion(currentObject
                .getLong(CODEBASE_VERSION));
            holder.setDescription(currentObject
                .getString(CODEBASE_DESCRIPTION));
            holder.setPublisher(currentObject
                .getString(CODEBASE_PUBLISHER));

            mDownloadableDataSources.add(holder);
          }
        }
      } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      return null;
    }

  };

  private static DownloadTask mDownloadTask = null;

  private static String convertStreamToString(InputStream is)
      throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null)
      sb.append(line);
    reader.close();
    is.close();
    return sb.toString();
  }

  public static void getDataSources(OnDataSourceResultListener listener,
      boolean force) {
    if (mDownloadTask == null
        || mDownloadTask.getStatus() != Status.FINISHED || force) {
      mCurrentListeners.add(listener);

      if (mDownloadTask == null
          || mDownloadTask.getStatus() == Status.FINISHED) {
        mDownloadTask = new DownloadTask();
        mDownloadTask.execute((Void) null);
      }
    } else {
      listener.onDataSourceResult(new ArrayList<PluginDownloadHolder>(
          mDownloadableDataSources));
    }
  }

  public static PluginHolder getPluginByIdentifier(String identifier) {
    for (PluginDownloadHolder plugin : mDownloadableDataSources) {
      if (plugin.getIdentifier().equals(identifier)) {
        return plugin;
      }
    }
    return null;
  }

  public static void getDataSources(OnDataSourceResultListener listener) {
    getDataSources(listener, false);
  }

  public static void downloadPlugin(PluginDownloadHolder dataSource) {

    Request request = new DownloadManager.Request(
        dataSource.getDownloadLink());
    request.setDestinationInExternalFilesDir(
        GeoARApplication.applicationContext, null,
        dataSource.getIdentifier() + ".apk");
    request.setTitle("GeoAR Data Souce Download");
    request.setMimeType("application/vnd.52north.datasources");
    request.setDescription(dataSource.getName());
    currentDownloads.add(mDownloadManager.enqueue(request));
  }

}




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