Portal Organizational Info

The purpose of this sample is to show how to create UserCredentials and a Portal object, and then use these to log in to a portal and fetch information about the portal and the organization.

Features

  • Portal API
  • User Credentials
  • Simple Async Task
  • Fetch Organization Thumbnail

Sample Design

This sample has one activity, OrganizationInfoActivity, which executes an AsyncTask to do all the work. A private class OrgInfoAsyncTask extends AsyncTask and overrides the following methods:

  • The preExecute() method displays a progress dialog on the UI thread.
  • The doInBackground() method, which runs on a background thread, logs in to the server and fetches information about the portal and organization. It also checks if there is an organization thumbnail and if so fetches it.
  • The onPostExecute() method then displays the information on the UI thread.

A Portal object is created using the constructor which takes portal URL and credentials. The fetchPortalInfo() method is used to log in to the portal. This returns a PortalInfo object that contains most of the portal and organization info. However, the organization thumbnail (if any) must be fetched by calling fetchOrganizationThumbnail(). The thumbnail data is converted to a BitmapDrawable for display.

Sample Code

package com.esri.arcgis.samples.organizationinfo;

import java.io.ByteArrayInputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.core.geometry.Envelope;
import com.esri.core.io.UserCredentials;
import com.esri.core.portal.Portal;
import com.esri.core.portal.PortalInfo;

/**
 * The purpose of this sample is to show how to create UserCredentials and a
 * Portal object, and then use these to log in to a portal and fetch information
 * about the portal and the organization.
 */
public class OrganizationInfoActivity extends Activity {

  private static final String TAG = "OrganizationInfoActivity";

  private static final String URL = "http://arcgis.com";

  private static final String USER_NAME = "democsf";

  private static final String PASSWORD = "devdemo";

  ProgressDialog mProgressDialog;

  byte[] mThumbnailData;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup progress dialog
    setContentView(R.layout.org_info_activity);
    mProgressDialog = new ProgressDialog(this) {
      @Override
      public void onBackPressed() {
        // Back key pressed - dismiss the dialog and finish the activity
        mProgressDialog.dismiss();
        finish();
      }
    };
    mProgressDialog.setMessage(getString(R.string.fetchingData));

    // Execute an async task to fetch and display organization info
    new OrgInfoAsyncTask().execute();
  }

  /**
   * This class provides an AsyncTask that fetches organization info from the
   * server on a background thread and displays it on the UI thread.
   */
  private class OrgInfoAsyncTask extends AsyncTask<Void, Void, PortalInfo> {
    private Exception mException;

    public OrgInfoAsyncTask() {
    }

    @Override
    protected void onPreExecute() {
      // Display progress dialog on UI thread
      mProgressDialog.show();
    }

    @Override
    protected PortalInfo doInBackground(Void... params) {
      // Fetch organization info on background thread
      mException = null;
      PortalInfo portalInfo = null;

      // Create UserCredentials with USER_NAME and PASSWORD. Anonymous
      // login is also possible.
      UserCredentials credentials = new UserCredentials();
      credentials.setUserAccount(USER_NAME, PASSWORD);

      // Create a Portal object
      Portal portal = new Portal(URL, credentials);

      try {
        // Fetch portal info from server. This logs in with the
        // credentials set above
        portalInfo = portal.fetchPortalInfo();

        // Fetch organization thumbnail (if any) from server
        if (portalInfo.getOrganizationThumbnailFileName() != null) {
          mThumbnailData = portalInfo.fetchOrganizationThumbnail();
        }
      } catch (Exception e) {
        mException = e;
      }
      return portalInfo;
    }

    @Override
    protected void onPostExecute(PortalInfo portalInfo) {
      // Display results on UI thread
      mProgressDialog.dismiss();
      if (mException != null) {
        Log.w(TAG, mException.toString());
        Toast.makeText(OrganizationInfoActivity.this, getString(R.string.fetchDataFailed), Toast.LENGTH_LONG).show();
        finish();
        return;
      }

      if (portalInfo != null) {
        TextView textView;

        textView = (TextView) findViewById(R.id.orgNameTextView);
        textView.setText(portalInfo.getOrganizationName());

        // Organization description needs a WebView because it contains
        // markup code
        WebView webView = (WebView) findViewById(R.id.orgDescriptionWebView);
        webView.loadDataWithBaseURL(null, portalInfo.getOrganizationDescription(), "text/html", "utf-8", null);

        textView = (TextView) findViewById(R.id.orgIdTextView);
        textView.setText(portalInfo.getOrganizationId());

        Envelope env = new Envelope();
        portalInfo.getDefaultExtent().getGeometry().queryEnvelope(env);
        textView = (TextView) findViewById(R.id.extentTextView);

        // build extent text
        StringBuilder extentXY = new StringBuilder();
        extentXY.append(env.getXMin());
        extentXY.append(" ");
        extentXY.append(env.getYMin());
        extentXY.append(" ");
        extentXY.append(env.getXMax());
        extentXY.append(" ");
        extentXY.append(env.getYMax());

        textView.setText(extentXY);

        textView = (TextView) findViewById(R.id.isSharedTextView);
        textView.setText(String.valueOf(portalInfo.isCanSharePublic()));

        textView = (TextView) findViewById(R.id.basemapTextView);
        textView.setText(portalInfo.getDefaultBaseMap().getTitle());

        if (mThumbnailData != null) {
          textView = (TextView) findViewById(R.id.thumbnailFilenameTextView);
          textView.setText(portalInfo.getOrganizationThumbnailFileName());

          ByteArrayInputStream bytes = new ByteArrayInputStream(mThumbnailData);
          BitmapDrawable bmd = new BitmapDrawable(getResources(), bytes);
          if (bmd != null) {
            ImageView imageView = (ImageView) findViewById(R.id.orgImageView);
            imageView.setImageBitmap(bmd.getBitmap());
          }
        }
      }
    }

  }

}
Feedback on this topic?