Access the expiration information of an expired mobile map package.
Use case
The data contained within a mobile map package (MMPK) may only be relevant for a fixed period of time. Using ArcGIS Pro, the author of an MMPK can set an expiration date to ensure the user is aware the data is out of date.
As long as the author of an MMPK has set an expiration date, the expiration date can be read even if the MMPK has not yet expired. For example, developers could also use this API to warn app users that an MMPK may be expiring soon.
How to use the sample
Run the app. The author of the MMPK used in this sample chose to set the MMPK's map as still readable, even if it's expired. The app presents expiration information to the user.
How it works
- Create a
MobileMapPackage
using the URI to a local .mmpk file and load it. - Use
mobileMapPackage.getExpiration()
to get the expiration information. Get the expiration message withgetMessage()
and the expiration date withgetDate()
.
Relevant API
- Expiration
- MobileMapPackage
Tags
expiration, mmpk
Sample Code
/*
* Copyright 2019 Esri.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.esri.samples.honor_mobile_map_package_expiration_date;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.Expiration;
import com.esri.arcgisruntime.mapping.ExpirationType;
import com.esri.arcgisruntime.mapping.MobileMapPackage;
import com.esri.arcgisruntime.mapping.view.MapView;
public class HonorMobileMapPackageExpirationDateSample extends Application {
private MapView mapView;
private MobileMapPackage mobileMapPackage;
@Override
public void start(Stage stage) {
try {
// create stack pane and application scene
StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
scene.getStylesheets().add(getClass().getResource("/honor_mobile_map_package_expiration_date/style.css").toExternalForm());
// set title, size, and add scene to stage
stage.setTitle("Honor Mobile Map Package Expiration Date Sample");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(scene);
stage.show();
// create an overlay to display the expiration information
VBox expirationMessageVbox = new VBox(6);
expirationMessageVbox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
Insets.EMPTY)));
expirationMessageVbox.setPadding(new Insets(10.0));
expirationMessageVbox.setMaxSize(800, 150);
expirationMessageVbox.setAlignment(Pos.CENTER);
expirationMessageVbox.getStyleClass().add("panel-region");
// create a label to display the expiration message and expiration date
Label expirationDetailsLabel = new Label();
expirationMessageVbox.getStyleClass().add("label");
// add the labels to the overlay
expirationMessageVbox.getChildren().add(expirationDetailsLabel);
// create a map view
mapView = new MapView();
// load the mobile map package
File mmpkFile = new File(System.getProperty("data.dir"), "./samples-data/mmpk/LothianRiversAnno.mmpk");
mobileMapPackage = new MobileMapPackage(mmpkFile.getAbsolutePath());
mobileMapPackage.loadAsync();
mobileMapPackage.addDoneLoadingListener(() -> {
// check if the map package has expiration information and if so, has it expired yet
if (mobileMapPackage.getExpiration() != null && mobileMapPackage.getExpiration().isExpired()) {
// get the expiration of the mobile map package
Expiration expiration = mobileMapPackage.getExpiration();
// get the expiration message
String expirationMessage = expiration.getMessage();
// get the expiration date
SimpleDateFormat daysHoursFormat = new SimpleDateFormat("EEE',' d MMM yyyy 'at' hh:mm:ss a", Locale.US);
String expirationDate = daysHoursFormat.format(expiration.getDateTime().getTimeInMillis());
// set the expiration message to the label
expirationDetailsLabel.setText(expirationMessage + "\n Mobile map package expired on: " + expirationDate + ".");
// load the expired map if it is still accessible after expiration
if (mobileMapPackage.getExpiration().getType() == ExpirationType.ALLOW_EXPIRED_ACCESS && mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && !mobileMapPackage.getMaps().isEmpty()) {
// add the map from the mobile map package to the map view
mapView.setMap(mobileMapPackage.getMaps().get(0));
// show an alert if the mobile map package is not accessible after expiration
} else if (mobileMapPackage.getExpiration().getType() == ExpirationType.PREVENT_EXPIRED_ACCESS) {
new Alert(Alert.AlertType.ERROR, "The author of this mobile map package has disallowed access after the expiration date.").show();
}
// show the map if it is not expired
} else if (mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && !mobileMapPackage.getMaps().isEmpty()) {
// add the map from the mobile map package to the map view
mapView.setMap(mobileMapPackage.getMaps().get(0));
} else {
new Alert(Alert.AlertType.ERROR, "Failed to load the mobile map package.").show();
}
});
// add the map view and overlay to the stack pane
stackPane.getChildren().addAll(mapView, expirationMessageVbox);
StackPane.setAlignment(expirationMessageVbox, Pos.CENTER);
} catch (Exception e) {
// on any error, display the stack trace
e.printStackTrace();
}
}
/**
* Stops and releases all resources used in application.
*/
@Override
public void stop() {
if (mapView != null) {
mapView.dispose();
}
}
/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/
public static void main(String[] args) {
Application.launch(args);
}
}