Android Open Source - arcgis-runtime-samples-android Main 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
 */*w w w.ja  v  a2s .  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.esri.arcgis.android.samples.standardlicense;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.esri.android.map.MapOptions;
import com.esri.android.map.MapOptions.MapType;
import com.esri.android.map.MapView;
import com.esri.android.oauth.OAuthView;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.esri.core.portal.LicenseInfo;
import com.esri.core.portal.Portal;
import com.esri.core.portal.PortalInfo;
import com.esri.core.runtime.LicenseLevel;
import com.esri.core.runtime.LicenseResult;

/**
 * This sample shows how to set the license level of your ArcGIS application to Standard. Setting the license level to
 * Standard allows you to use features of the ArcGIS SDK for Android that require Standard license level. In order to
 * set the license level to Standard you need to edit this code and assign a valid client id string to the CLIENT_ID
 * constant.<p>
 * When you release your app, you should ensure that the client id, and license string (if used), are encrypted
 * and saved to the device in a secure manner; this sample uses a hardcoded string instead for simplicity of example 
 * code.<p>
 * Follow these steps:
 * <ol>
 * <li>Browse to https://developers.arcgis.com.</li>
 * <li>Sign in with your ArcGIS developer account.</li>
 * <li>Create an application. This will give you access to a client id string.</li>
 * <li>Initialize the CLIENT_ID constant with the client id string and run the sample. If the client id has been
 * successfully set a OAuth sign in UI is shown which allows the user to sign in to a portal. After successful sign in
 * the license level is set to standard based on the LicenseInfo retrieved from the authenticated portal.</li>
 * </ol>
 * Alternatively you can explore how to set Standard license level via a license string obtained from Esri customer
 * service. In this case edit the code to initialize the LICENSE_STRING constant with a license string and set
 * USE_LICENSE_INFO to false.<p> 
 * <b>NOTE:</b> When you release your app, you should ensure that the client id and license string (if used) are 
 * encrypted and saved to the device in a secure manner; the code here uses hardcoded strings instead for simplicity
 * of example code.
 */
public class MainActivity extends Activity {

  // TODO: initialize CLIENT_ID with a valid client id string
  // NOTE: When you release your app, you should ensure that the client id is encrypted and saved to the device 
  // in a secure manner. 
  private static final String CLIENT_ID = null;

  // TODO: initialize LICENSE_STRING with a valid license string and set USE_LICENSE_INFO to false if you want to unlock
  // Standard license level via a license string. A license string needs to be obtained from Esri customer service.
  // NOTE: When you release your app, you should ensure that the license string is encrypted and saved to the device
  // in a secure manner. 
  private static final String LICENSE_STRING = null;

  private static final boolean USE_LICENSE_INFO = true;

  private static final String PORTAL_URL = "https://www.arcgis.com";

  private static final int OAUTH_EXPIRATION_NEVER = -1;

  private FrameLayout mViewContainer;

  private OAuthView mOAuthView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewContainer = (FrameLayout) findViewById(R.id.main_activity_view_container);

    // Set the client id string on the ArcGISRuntime class. This will set the license level to Basic.
    // ArcGISRuntime.setClientId() needs to be called before any other calls to the ArcGIS SDK for Android are made.
    //
    LicenseResult licenseResult = ArcGISRuntime.setClientId(CLIENT_ID);

    LicenseLevel licenseLevel = ArcGISRuntime.License.getLicenseLevel();

    if (licenseResult == LicenseResult.VALID && licenseLevel == LicenseLevel.BASIC) {
      if (USE_LICENSE_INFO) {
        signInWithOAuth();
      } else {
        setStandardLicenseWithLicenseString();
      }
    } else {
      MessageDialogFragment.showMessage(getString(R.string.valid_client_id_required), getFragmentManager());

      showMap();
    }
  }

  /**
   * Sets up a sign in UI that allows the user to sign in to a portal via OAuth. OAuth is the recommended authentication
   * method to sign in to a portal. After successful sign in the obtained UserCredentials are used to set the license
   * level to standard by retrieving a LicenseInfo object from an authenticated Portal instance (see
   * setStandardLicenseWithLicenseInfo() method).
   */
  private void signInWithOAuth() {
    mOAuthView = new OAuthView(this, PORTAL_URL, CLIENT_ID, OAUTH_EXPIRATION_NEVER,
        new CallbackListener<UserCredentials>() {

          @Override
          public void onError(Throwable e) {
            MessageDialogFragment.showMessage(getString(R.string.oauth_login_failed), getFragmentManager());
          }

          @Override
          public void onCallback(UserCredentials credentials) {
            if (credentials != null) {
              setStandardLicenseWithLicenseInfo(credentials);
            } else {
              MessageDialogFragment.showMessage(getString(R.string.oauth_login_failed), getFragmentManager());
            }
          }
        });

    mViewContainer.addView(mOAuthView);
  }

  /**
   * This method attempts to set the license level of the application to LicenseLevel.Standard. Standard license level
   * is set by retrieving a LicenseInfo object from an authenticated Portal instance. The authenticated Portal instance
   * is created with the UserCredentials obtained from the previous OAuth sign in step. The steps to set standard
   * license level with a LicenseInfo object are:
   * <ol>
   * <li>Sign in to a portal via OAuth to retrieve UserCredentials.</li>
   * <li>Create an authenticated Portal instance from the UserCredentials.</li>
   * <li>Fetch the PortalInfo from the Portal instance and retrieve the LicenseInfo.</li>
   * <li>Initialize the license with the LicenseInfo to activate Standard license level.</li>
   * </ol>
   * This approach to set standard license level is suitable for applications that connect to an ArcGIS portal. For
   * applications that don't connect to an ArcGIS portal see {@link setStandardLicenseFromLicenseString} method.
   */
  private void setStandardLicenseWithLicenseInfo(UserCredentials credentials) {
    Portal portal = new Portal(PORTAL_URL, credentials);
    PortalInfo portalInfo = null;

    try {
      portalInfo = portal.fetchPortalInfo();
    } catch (Exception e) {
      MessageDialogFragment.showMessage(getString(R.string.standard_license_failed), getFragmentManager());

      return;
    }

    LicenseInfo licenseInfo = portalInfo.getLicenseInfo();

    LicenseResult licenseResult = ArcGISRuntime.License.setLicense(licenseInfo);
    LicenseLevel licenseLevel = ArcGISRuntime.License.getLicenseLevel();

    if (licenseResult == LicenseResult.VALID && licenseLevel == LicenseLevel.STANDARD) {
      MessageDialogFragment.showMessage(getString(R.string.standard_license_succeeded), getFragmentManager());
    } else {
      MessageDialogFragment.showMessage(getString(R.string.standard_license_failed), getFragmentManager());
    }

    showMap();
  }

  /**
   * This method attempts to set the license level of the application to LicenseLevel.Standard from a previously
   * obtained license string. The license string needs to be obtained from Esri customer service. This approach to set
   * standard license level is suitable for applications that never connect to an ArcGIS portal. For applications that
   * do connect to an ArcGIS portal see setStandardLicenseWithLicenseInfo() method.
   */
  private void setStandardLicenseWithLicenseString() {
    LicenseResult licenseResult = ArcGISRuntime.License.setLicense(LICENSE_STRING);
    LicenseLevel licenseLevel = ArcGISRuntime.License.getLicenseLevel();

    if (licenseResult == LicenseResult.VALID && licenseLevel == LicenseLevel.STANDARD) {
      MessageDialogFragment.showMessage(getString(R.string.standard_license_succeeded), getFragmentManager());
    } else {
      MessageDialogFragment.showMessage(getString(R.string.standard_license_failed), getFragmentManager());
    }

    showMap();
  }

  /**
   * Shows a map in the MainActivity's layout.
   */
  private void showMap() {
    runOnUiThread(new Runnable() {

      @Override
      public void run() {
        if (mOAuthView != null) {
          // remove the OAuthView so we can show the map
          //
          mViewContainer.removeView(mOAuthView);
          mOAuthView = null;
        }

        MapView mapView = new MapView(MainActivity.this, new MapOptions(MapType.TOPO, 34.056215, -117.195668, 16));
        mViewContainer.addView(mapView);
      }
    });
  }
}




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