Android Open Source - arcgis-runtime-samples-android User Content 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
 */*from   w w  w .  ja v  a 2 s.  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.arcgis.android.samples.oauth2sample;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.arcgis.android.samples.oauth2sample.MapFragment.OnFragmentInteractionListener_MapFragment;
import com.arcgis.android.samples.oauth2sample.UserContentFragment.OnFragmentInteractionListener;
import com.esri.core.io.UserCredentials;
import com.esri.core.portal.Portal;
import com.esri.core.portal.PortalItem;
import com.esri.core.portal.PortalItemType;
import com.esri.core.portal.PortalUser;
import com.esri.core.portal.PortalUserContent;

public class UserContentActivity extends FragmentActivity implements OnFragmentInteractionListener,
    OnFragmentInteractionListener_MapFragment {

  protected static final String TAG = "UserContentActivity";

  // ArcGIS components
  public static ArrayList<UserWebmaps> mUserPortalDataList;

  UserCredentials mValidLoginCredentials;

  public static Portal mMyPortal;

  // UI components
  AlertDialog mAlertDialog;

  static ProgressDialog mProgressDialog;

  protected static final int CLOSE_LOADING_WINDOW = 0;

  // Handler to close loading window
  final Handler uihandler = new HandlerExtension();

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

    Log.d("UserContentActivity", "inside UserContentActivity");

    mValidLoginCredentials = OAuth2Sample.getCredentials();

    setupQuitAppAlertDialog();

    mProgressDialog = ProgressDialog
        .show(this, "WebMaps from arcgis.com ", "Getting User WebMaps from portal ........");

    new GroupTask().execute();

  }

  private final static class HandlerExtension extends Handler {

    // default constructor
    public HandlerExtension() {
    }

    @Override
    public void handleMessage(Message msg) {
      switch ((msg.what)) {
      case CLOSE_LOADING_WINDOW:

        if (mProgressDialog != null)
          mProgressDialog.dismiss();
        break;

      default:
        break;
      }

    }
  }

  public class GroupTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
      try {
        getUserContentFromPortal();
      } catch (Exception e) {
        Log.e(TAG, "Exception while getting content from portal", e);
      }

      return null;
    }

  }

  public void getUserContentFromPortal() throws Exception {

    UserWebmaps userWebMaps;
    mMyPortal = new Portal(getResources().getString(R.string.portal_url), mValidLoginCredentials);
    mUserPortalDataList = new ArrayList<UserWebmaps>();

    // Fetch user from the portal and get user contents

    PortalUser user = mMyPortal.fetchUser();
    PortalUserContent puc = user.fetchContent();
    List<PortalItem> items = puc.getItems();

    if (items == null) {
      Log.e(TAG, "No items returned by Portal for the user");
      return;
    }

    // Get only the webmaps in the user account and add them in the ArrayList
    for (PortalItem item : items) {
      Log.i(TAG, "Item id = " + item.getTitle());
      if (item.getType() == PortalItemType.WEBMAP) {
        byte[] data = item.fetchThumbnail();
        Bitmap bitmap;
        if (data != null) {
          bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        } else {
          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.map);
        }
        userWebMaps = new UserWebmaps(item, bitmap);
        Log.i(TAG, "Item id = " + item.getTitle());
        mUserPortalDataList.add(userWebMaps);
      }

    }

    Log.d(TAG, "userPortalDataList" + mUserPortalDataList);
    uihandler.sendEmptyMessage(CLOSE_LOADING_WINDOW);
    UserContentActivity.this.runOnUiThread(new Runnable() {

      @Override
      public void run() {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        UserContentFragment ucf = UserContentFragment.newInstance();
        ft.add(android.R.id.content, ucf, "user_content_fragment");
        ft.addToBackStack("user_content_fragment");
        ft.commit();
      }
    });

  }

  public void setupQuitAppAlertDialog() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(UserContentActivity.this);

    // set title
    alertDialogBuilder.setTitle(getResources().getString(R.string.OAuth2_Sample));

    // set dialog message
    alertDialogBuilder.setMessage(getResources().getString(R.string.quit_app)).setCancelable(false)
        .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            // if this button is clicked, close
            // current activity
            Intent intent = new Intent(getApplicationContext(), OAuth2Sample.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra(OAuth2Sample.EXIT, true);
            startActivity(intent);
          }
        }).setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            dialog.cancel();
          }
        });

    // create alert dialog
    mAlertDialog = alertDialogBuilder.create();

    // show it

  }

  @Override
  public void onFragmentInteraction(String id) {

    Log.d(TAG, "on Fragment Interaction");

    MapFragment mapFragment = MapFragment.newInstance(id);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(android.R.id.content, mapFragment, "mapfragment");
    transaction.addToBackStack("mapfragment");

    // Commit the transaction
    transaction.commit();

  }

  // Fragment interaction listener for MapFragment
  // we don't do anything in this method as we don't want to perform any
  // action on the map
  @Override
  public void onFragmentInteraction(Uri uri) {

  }

  @Override
  public void onBackPressed() {

    Fragment f = getSupportFragmentManager().findFragmentByTag("user_content_fragment");
    if (f.isVisible()) {
      mAlertDialog.show();
      return;
    }
    super.onBackPressed();

  }

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

  @Override
  protected void onPause() {
    super.onPause();
    mAlertDialog.dismiss();
  }

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

}




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