/* Copyright 2016 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.openmobilemappackage;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.mobilemappackage.MobileMapPackage;
import com.esri.arcgisruntime.mapping.view.MapView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private MapView mMapView;
    private static File extStorDir;
    private static String extSDCardDirName;
    private static String filename;

    protected static String TAG = "MMPK";
    protected static String FILE_EXTENSION = ".mmpk";


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

        // get sdcard resource name
        extStorDir = Environment.getExternalStorageDirectory();
        extSDCardDirName = this.getResources().getString(R.string.config_data_sdcard_offline_dir);
        filename = this.getResources().getString(R.string.config_mmpk_name);

        String mmpkFile = createMobileMapPackageFilePath();

        // retrieve the MapView from layout
        mMapView = (MapView) findViewById(R.id.mapView);
        final MobileMapPackage mapPackage = new MobileMapPackage(mmpkFile);
        mapPackage.loadAsync();

        mapPackage.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                if(mapPackage.getLoadStatus() == LoadStatus.FAILED_TO_LOAD){
                    Log.e(TAG, mapPackage.getLoadError().getMessage());
                }

                if(mapPackage.getMaps().size() > 0){
                    mMapView.setMap(mapPackage.getMaps().get(0));
                }
            }
        });
    }

    /**
     * Create the mobile map package file location and name structure
     */
    static String createMobileMapPackageFilePath(){
        return extStorDir.getAbsolutePath() + File.separator + extSDCardDirName + File.separator + filename + FILE_EXTENSION;
    }

    @Override
    protected void onPause(){
        super.onPause();
        mMapView.pause();
    }

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

@@ -1177,39 +1177,37 @@
 ame;%0A%0A    pr
-otec
+iva
 te
-d
  static 
 String TAG =
@@ -1190,24 +1190,30 @@
 vate static 
+final 
 String TAG =
@@ -1231,23 +1231,21 @@
   pr
-otected
+ivate
  static 
 Stri
@@ -1236,24 +1236,30 @@
 vate static 
+final 
 String FILE_
@@ -1281,16 +1281,17 @@
 mpk%22;%0A%0A%0A
+%0A
     @Ove
@@ -2171,93 +2171,114 @@
 tus.
-FAILED_TO_LOAD)%7B%0A                    Log.e(TAG, mapPackage.getLoadError().getMessage(
+LOADED && mapPackage.getMaps().size() %3E 0)%7B%0A                    mMapView.setMap(mapPackage.getMaps().get(0
 ));%0A
@@ -2290,25 +2290,29 @@
            %7D
-%0A
+else%7B
 %0A           
@@ -2320,107 +2320,97 @@
     
-if(mapPackage.getMaps().size() %3E 0)%7B%0A                    mMapView.setMap(mapPackage.getMaps().get(0
+    // Log the issue%0A                    Log.e(TAG, mapPackage.getLoadError().getMessage(
 ));%0A
@@ -2550,16 +2550,24 @@
  */%0A    
+private 
 static S

check map package is loaded and has at least one map