Android Open Source - arcgis-runtime-samples-android Export Tile Cache Task Sample 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 2014 ESRI
 *// www .j a v a  2 s .  co  m
 * 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.arcgis.android.samples.ExportTileCacheTask;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISLocalTiledLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.map.CallbackListener;
import com.esri.core.tasks.tilecache.ExportTileCacheParameters;
import com.esri.core.tasks.tilecache.ExportTileCacheParameters.ExportBy;
import com.esri.core.tasks.tilecache.ExportTileCacheStatus;
import com.esri.core.tasks.tilecache.ExportTileCacheTask;

public class ExportTileCacheTaskSampleActivity extends Activity {

  static final String TAG = "ExportTileCacheTaskSampleActivity";

  // Map elements
  MapView mMapView;
  ArcGISLocalTiledLayer localTiledLayer;

  // action bar menu items
  MenuItem selectLevels;
  MenuItem download;
  MenuItem switchMaps;

  boolean isLocalLayerVisible = false;

  // The generated tile cache will be a compact cache
  boolean createAsTilePackage = false;

  double[] levels;

  final CharSequence[] items = { "Level ID:0", "Level ID:1", "Level ID:2",
      "Level ID:3", "Level ID:4", "Level ID:5", "Level ID:6",
      "Level ID:7", "Level ID:8", "Level ID:9", };

  double[] mapResolution = { 156543.03392800014, 78271.51696399994,
      39135.75848200009, 19567.87924099992, 9783.93962049996,
      4891.96981024998, 2445.98490512499, 1222.992452562495,
      611.4962262813797, 305.74811314055756 };

  boolean[] itemsChecked = new boolean[items.length];
  ArrayList<Double> levelsArraylist = new ArrayList<Double>();
  // path to persist data to disk
  static String DEFAULT_BASEMAP_PATH;
  // tile package url
  String tileURL;

  private static String defaultPath = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);

    DEFAULT_BASEMAP_PATH = getResources().getString(R.string.offline_dir);
    defaultPath = Environment.getExternalStorageDirectory().getPath()
        + DEFAULT_BASEMAP_PATH;

    // Initialize MapView and extent
    mMapView = (MapView) findViewById(R.id.mapView);

    // Add Tile layer to the MapView
    tileURL = getResources().getString(R.string.tileServiceURL);
    ArcGISTiledMapServiceLayer tilelayer = new ArcGISTiledMapServiceLayer(
        tileURL);
    mMapView.addLayer(tilelayer);

  }

  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    // menu items
    selectLevels = menu.getItem(0);
    selectLevels.setIcon(android.R.drawable.ic_menu_crop);
    download = menu.getItem(1);
    switchMaps = menu.getItem(2);

    return super.onCreateOptionsMenu(menu);
  }

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.select_levels:
      showDialog();
      return true;
    case R.id.download:
      downloadBasemap();
      return true;
    case R.id.switch_maps:
      if (isLocalLayerVisible) {
        switchToOnlineLayer();
      } else {
        switchToLocalLayer();
      }
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  /**
   * Launches the dialog to let the user select the Levels of Detail to be
   * downloaded from the tile service. Called when selectLevels action bar
   * icon is selected.
   * 
   **/
  public void showDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select the Levels of Detail");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
        // When ok button is pressed, we go through the array of
        // itemsChecked and add the selected
        // items to levelsArraylist
        for (int i = 0; i < items.length; i++) {
          if (itemsChecked[i]) {

            levelsArraylist.add((double) i);
            itemsChecked[i] = false;
          }
        }
      }
    });

    builder.setMultiChoiceItems(items, new boolean[] { false, false, false,
        false, false, false, false, false, false, false },
        new DialogInterface.OnMultiChoiceClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which,
              boolean isChecked) {
            itemsChecked[which] = isChecked;
          }
        });
    builder.show();
  }

  /**
   * Download the basemap for the level of details selected by the user Called
   * when download button is clicked.
   * 
   */
  @SuppressWarnings("boxing")
  public void downloadBasemap() {

    // Set the progressbar to VISIBLE
    setProgressBarIndeterminateVisibility(true);

    // Get the the extent covered by generated tile cache, here we are using
    // the area being displayed on screen
    Envelope extentForTPK = new Envelope();
    mMapView.getExtent().queryEnvelope(extentForTPK);

    // If the user does not select the Level of details
    // then give out the status message in a toast
    if (levelsArraylist.size() == 0) {
      Toast.makeText(this, "Please Select Levels of Detail",
          Toast.LENGTH_LONG).show();
      // Hide the progress bar
      setProgressBarIndeterminateVisibility(false);
      return;
    }

    levels = new double[levelsArraylist.size()];
    final String tileCachePath = defaultPath;

    // Specify all the Levels of details in an integer array
    for (int i = 0; i < levelsArraylist.size(); i++) {
      levels[i] = levelsArraylist.get(i);
    }

    // Create an instance of ExportTileCacheTask for the mapService that
    // supports the exportTiles() operation
    final ExportTileCacheTask exportTileCacheTask = new ExportTileCacheTask(
        tileURL, null);

    // Set up GenerateTileCacheParameters
    ExportTileCacheParameters params = new ExportTileCacheParameters(
        createAsTilePackage, levels, ExportBy.ID, extentForTPK,
        mMapView.getSpatialReference());

    // create tile cache
    createTileCache(params, exportTileCacheTask, tileCachePath);
  }

  /**
   * Creates tile Cache locally by calling generateTileCache
   * 
   * @param params
   * @param exportTileCacheTask
   * @param tileCachePath
   */
  private void createTileCache(ExportTileCacheParameters params,
      final ExportTileCacheTask exportTileCacheTask,
      final String tileCachePath) {

    // estimate tile cache size
    exportTileCacheTask.estimateTileCacheSize(params,
        new CallbackListener<Long>() {

          @Override
          public void onError(Throwable e) {
            Log.d("*** tilecachesize error: ", "" + e);
          }

          @Override
          public void onCallback(Long objs) {
            Log.d("*** tilecachesize: ", "" + objs);
            final long tilecachesize = objs / 1000;
            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                Toast.makeText(
                    getApplicationContext(),
                    "Approx. Tile Cache size to download : "
                        + tilecachesize + " KB",
                    Toast.LENGTH_LONG).show();
              }
            });

          }
        });

    // create status listener for generateTileCache
    CallbackListener<ExportTileCacheStatus> statusListener = new CallbackListener<ExportTileCacheStatus>() {

      @Override
      public void onError(Throwable e) {
        Log.d("*** tileCacheStatus error: ", "" + e);
      }

      @Override
      public void onCallback(ExportTileCacheStatus objs) {
        Log.d("*** tileCacheStatus : ", objs.getStatus().toString());
        final String status = objs.getStatus().toString();
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Toast.makeText(getApplicationContext(), status,
                Toast.LENGTH_SHORT).show();
          }
        });

      }
    };

    // Submit tile cache job and download
    exportTileCacheTask.generateTileCache(params, statusListener,
        new CallbackListener<String>() {
          private boolean errored = false;

          @Override
          public void onError(Throwable e) {
            errored = true;
            // print out the error message and disable the progress
            // bar
            Log.d("*** generateTileCache error: ", "" + e);
            final String error = e.toString();
            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                setProgressBarIndeterminateVisibility(false);
                Toast.makeText(getApplicationContext(),
                    "generateTileCache error: " + error,
                    Toast.LENGTH_LONG).show();
              }
            });
          }

          @Override
          public void onCallback(String path) {
            if (!errored) {
              Log.d("the Download Path = ", "" + path);

              // switch to the successfully downloaded local layer
              localTiledLayer = new ArcGISLocalTiledLayer(path);
              mMapView.addLayer(localTiledLayer);
              // initially setting the visibility to false,
              // turning it back on in the switchToLocalLayer()
              // method
              mMapView.getLayers()[1].setVisible(false);

              runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  // Hide the progress bar
                  setProgressBarIndeterminateVisibility(false);
                  Toast.makeText(
                      getApplicationContext(),
                      "TileCache successfully downloaded, Switching to Local Tiled Layer",
                      Toast.LENGTH_LONG).show();

                  switchToLocalLayer();
                }
              });
            }
          }
        }, tileCachePath);

  }

  private void switchToLocalLayer() {
    // Set the resolution of the map to the first level selected by user
    mMapView.setResolution(mapResolution[(int) levels[0]]);
    // set local layer true
    isLocalLayerVisible = true;
    // just hide the map service layer rather than removing it
    mMapView.getLayers()[0].setVisible(false);
    mMapView.getLayers()[1].setVisible(true);

  }

  private void switchToOnlineLayer() {
    // set local layer false
    isLocalLayerVisible = false;
    // hide the local layer, make the online layer visible
    mMapView.getLayers()[1].setVisible(false);
    mMapView.getLayers()[0].setVisible(true);
  }

  @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