Android Open Source - arcgis-runtime-samples-android Create Runtime Geodatabase Activity






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-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

/* Copyright 2013 ESRI
 *//from  w  ww.j  ava  2s .  c om
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the Sample code usage restrictions document for further information.
 *
 */

package com.esri.arcgis.android.sample.runtimegeodb;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.esri.android.map.FeatureLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.core.ags.FeatureServiceInfo;
import com.esri.core.geodatabase.Geodatabase;
import com.esri.core.geodatabase.GeodatabaseFeatureTable;
import com.esri.core.map.CallbackListener;
import com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo;
import com.esri.core.tasks.geodatabase.GeodatabaseSyncTask;

public class CreateRuntimeGeodatabaseActivity extends Activity {

  static MapView mMapView;
  String fLayerUrl;
  String fServiceUrl;
  static ArcGISFeatureLayer wildfireFL;
  static GeodatabaseSyncTask gdbSyncTask;

  static ProgressDialog mProgressDialog;
  static TextView pathView;

  private static File demoDataFile;
  private static String offlineDataSDCardDirName;
  private static String filename;
  static String localGdbFilePath;
  
  protected static final String TAG = "CRGdb";
  protected static String OFFLINE_FILE_EXTENSION = ".geodatabase";
  
  private static Context mContext;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // set app context so it can be obtained to update progress
    CreateRuntimeGeodatabaseActivity.setContext(this);

    // get sdcard resource names
    demoDataFile = Environment.getExternalStorageDirectory();
    offlineDataSDCardDirName = this.getResources().getString(
        R.string.config_data_sdcard_offline_dir);
    filename = this.getResources().getString(
        R.string.config_geodatabase_name);

    // Retrieve the map and map options from XML layout
    mMapView = (MapView) findViewById(R.id.map);
    // create service layer
    fServiceUrl = this.getResources()
        .getString(R.string.featureservice_url);
    
    mProgressDialog = new ProgressDialog(CreateRuntimeGeodatabaseActivity.this);
    mProgressDialog.setTitle("Create local runtime geodatabase");
    
    // attribute app and pan across dateline
    addAttributes();
  }

  private void addAttributes() {
    // attribute ESRI logo to map
    mMapView.setEsriLogoVisible(true);
    // enable map to wrap around date line
    mMapView.enableWrapAround(true);

  }
  
  // methods to ensure context is available when updating the progress dialog
  public static Context getContext(){
    return mContext;
  }
  
  public static void setContext(Context context){
    mContext = context;
  }

  /*
   * Create the geodatabase file location and name structure
   */
  static String createGeodatabaseFilePath() {
    StringBuilder sb = new StringBuilder();
    sb.append(demoDataFile.getAbsolutePath());
    sb.append(File.separator);
    sb.append(offlineDataSDCardDirName);
    sb.append(File.separator);
    sb.append(filename);
    sb.append(OFFLINE_FILE_EXTENSION);
    return sb.toString();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // inflate action bar menu
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // handle menu item selection
    switch (item.getItemId()) {
    case R.id.action_download:
      downloadData(fServiceUrl);
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }

  }

  /**
   * Create the GeodatabaseTask from the feature service URL w/o credentials.
   */
  private void downloadData(String url) {
    Log.i(TAG, "Create GeoDatabase");
    // create a dialog to update user on progress
    mProgressDialog.show();
    // create the GeodatabaseTask

    gdbSyncTask = new GeodatabaseSyncTask(url, null);
    gdbSyncTask
        .fetchFeatureServiceInfo(new CallbackListener<FeatureServiceInfo>() {

          @Override
          public void onError(Throwable arg0) {
            Log.e(TAG, "Error fetching FeatureServiceInfo");
          }

          @Override
          public void onCallback(FeatureServiceInfo fsInfo) {
            if (fsInfo.isSyncEnabled()) {
              createGeodatabase(fsInfo);
            }
          }
        });

  }

  /**
   * Set up parameters to pass the the {@link #submitTask()} method. A
   * {@link CallbackListener} is used for the response.
   */
  private static void createGeodatabase(FeatureServiceInfo featureServerInfo) {
    // set up the parameters to generate a geodatabase
    GenerateGeodatabaseParameters params = new GenerateGeodatabaseParameters(
        featureServerInfo, mMapView.getExtent(),
        mMapView.getSpatialReference());

    // a callback which fires when the task has completed or failed.
    CallbackListener<String> gdbResponseCallback = new CallbackListener<String>() {
      @Override
      public void onError(final Throwable e) {
        Log.e(TAG, "Error creating geodatabase");
        mProgressDialog.dismiss();
      }

      @Override
      public void onCallback(String path) {
        Log.i(TAG, "Geodatabase is: " + path);
        mProgressDialog.dismiss();
        // update map with local feature layer from geodatabase
        updateFeatureLayer(path);
        // log the path to the data on device
        Log.i(TAG, "path to geodatabase: " + path);
      }
    };

    // a callback which updates when the status of the task changes
    GeodatabaseStatusCallback statusCallback = new GeodatabaseStatusCallback() {
      @Override
      public void statusUpdated(final GeodatabaseStatusInfo status) {
        // get current status
        String progress = status.getStatus().toString();
        // get activity context
        Context context = CreateRuntimeGeodatabaseActivity.getContext();
        // create activity from context
        CreateRuntimeGeodatabaseActivity activity = (CreateRuntimeGeodatabaseActivity) context;
        // update progress bar on main thread
        showProgressBar(activity, progress);

      }
    };

    // create the fully qualified path for geodatabase file
    localGdbFilePath = createGeodatabaseFilePath();

    // get geodatabase based on params
    submitTask(params, localGdbFilePath, statusCallback,
        gdbResponseCallback);
  }

  /**
   * Request database, poll server to get status, and download the file
   */
  private static void submitTask(GenerateGeodatabaseParameters params,
      String file, GeodatabaseStatusCallback statusCallback,
      CallbackListener<String> gdbResponseCallback) {
    // submit task
    gdbSyncTask.generateGeodatabase(params, file, false, statusCallback,
        gdbResponseCallback);
  }

  /**
   * Add feature layer from local geodatabase to map
   * 
   * @param featureLayerPath
   */
  private static void updateFeatureLayer(String featureLayerPath) {
    // create a new geodatabase
    Geodatabase localGdb = null;
    try {
      localGdb = new Geodatabase(featureLayerPath);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    // Geodatabase contains GdbFeatureTables representing attribute data
    // and/or spatial data. If GdbFeatureTable has geometry add it to
    // the MapView as a Feature Layer
    if (localGdb != null) {
      for (GeodatabaseFeatureTable gdbFeatureTable : localGdb
          .getGeodatabaseTables()) {
        if (gdbFeatureTable.hasGeometry()){
          mMapView.addLayer(new FeatureLayer(gdbFeatureTable));

        }
      }
    }
  }
  
  private static void showProgressBar(final CreateRuntimeGeodatabaseActivity activity, final String message){
    activity.runOnUiThread(new Runnable(){

      @Override
      public void run() {
        mProgressDialog.setMessage(message);
      }
      
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.unpause();
  }

}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java