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
Load 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
passing in the path to the mobile map package's location on the device. - Load the mobile map package.
- Present
Expiration
information to the user with:
- Use
getMessage()
to get the expiration message set by the author of the MMPK. - Use
getDate()
to get the expiration date set by the author of the MMPK.
Relevant API
- Expiration
- MobileMapPackage
Offline Data
- Download the data from ArcGIS Online.
- Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
- Push the data into the scoped storage of the sample app:
adb push LothianRiversAnno.mmpk /Android/data/com.esri.arcgisruntime.sample.honormobilemappackageexpirationdate/files/LothianRiversAnno.mmpk
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.arcgisruntime.sample.honormobilemappackageexpirationdate;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.mapping.ExpirationType;
import com.esri.arcgisruntime.mapping.MobileMapPackage;
import com.esri.arcgisruntime.mapping.view.MapView;
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private TextView mExpirationMessageTextView;
// objects that implement Loadable must be class fields to prevent being garbage collected before loading
private MobileMapPackage mMobileMapPackage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get a reference to the map view
mMapView = findViewById(R.id.mapView);
// get a reference to the expiration text view
mExpirationMessageTextView = findViewById(R.id.expirationMessageTextView);
// create a mobile map package from a local mmpk
mMobileMapPackage = new MobileMapPackage(getExternalFilesDir(null) + getString(R.string.path_to_expired_mmpk));
// wait for the map package to load
mMobileMapPackage.addDoneLoadingListener(() -> {
// check if the map package has expiration information and if so, has it expired yet
if (mMobileMapPackage.getExpiration() != null && mMobileMapPackage.getExpiration().isExpired()) {
// define a format for the date
SimpleDateFormat daysHoursFormat = new SimpleDateFormat("yyyy-MM-dd' at 'hh:mm:ss", Locale.US);
// show the expiration text view
mExpirationMessageTextView.setVisibility(View.VISIBLE);
// set the expiration message and expiration date to the text view
mExpirationMessageTextView.setText(getString(R.string.expiration_text,
mMobileMapPackage.getExpiration().getMessage(),
daysHoursFormat.format(mMobileMapPackage.getExpiration().getDateTime().getTime())));
if (mMobileMapPackage.getExpiration().getType() == ExpirationType.ALLOW_EXPIRED_ACCESS) {
// add the map to the map view
mMapView.setMap(mMobileMapPackage.getMaps().get(0));
} else if (mMobileMapPackage.getExpiration().getType() == ExpirationType.PREVENT_EXPIRED_ACCESS) {
Toast.makeText(this, "The author of this mobile map package has disallowed access after the expiration date.",
Toast.LENGTH_LONG).show();
}
}
});
mMobileMapPackage.loadAsync();
}
@Override
protected void onPause() {
mMapView.pause();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}
@Override
protected void onDestroy() {
mMapView.dispose();
super.onDestroy();
}
}