[android] expose offline database merge API
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package com.mapbox.mapboxsdk.testapp.offline
|
||||
|
||||
import android.R
|
||||
import android.content.Context
|
||||
import android.support.test.espresso.Espresso.onView
|
||||
import android.support.test.espresso.IdlingRegistry
|
||||
import android.support.test.espresso.UiController
|
||||
import android.support.test.espresso.assertion.ViewAssertions.matches
|
||||
import android.support.test.espresso.idling.CountingIdlingResource
|
||||
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
|
||||
import android.support.test.espresso.matcher.ViewMatchers.withId
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap
|
||||
import com.mapbox.mapboxsdk.offline.OfflineManager
|
||||
import com.mapbox.mapboxsdk.offline.OfflineRegion
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke
|
||||
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest
|
||||
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class OfflineManagerTest : BaseActivityTest() {
|
||||
|
||||
companion object {
|
||||
private const val TEST_DB_FILE_NAME = "offline.db"
|
||||
}
|
||||
|
||||
private val context: Context by lazy { rule.activity }
|
||||
|
||||
private lateinit var offlineIdlingResource: CountingIdlingResource
|
||||
|
||||
override fun getActivityClass(): Class<*> {
|
||||
return EspressoTestActivity::class.java
|
||||
}
|
||||
|
||||
override fun beforeTest() {
|
||||
super.beforeTest()
|
||||
offlineIdlingResource = CountingIdlingResource("idling_resource")
|
||||
IdlingRegistry.getInstance().register(offlineIdlingResource)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun offlineMergeListDeleteTest() {
|
||||
validateTestSetup()
|
||||
|
||||
invoke(mapboxMap) { _: UiController, _: MapboxMap ->
|
||||
offlineIdlingResource.increment()
|
||||
copyAsset(context)
|
||||
OfflineManager.getInstance(context).mergeOfflineRegions(
|
||||
context.filesDir.absolutePath + "/" + TEST_DB_FILE_NAME,
|
||||
object : OfflineManager.MergeOfflineRegionsCallback {
|
||||
override fun onMerge(offlineRegions: Array<out OfflineRegion>?) {
|
||||
assert(offlineRegions?.size == 1)
|
||||
offlineIdlingResource.decrement()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to merge external offline database. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
invoke(mapboxMap) { _: UiController, _: MapboxMap ->
|
||||
offlineIdlingResource.increment()
|
||||
OfflineManager.getInstance(context).listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
|
||||
override fun onList(offlineRegions: Array<out OfflineRegion>?) {
|
||||
assert(offlineRegions?.size == 1)
|
||||
if (offlineRegions != null) {
|
||||
for (region in offlineRegions) {
|
||||
offlineIdlingResource.increment()
|
||||
region.delete(object : OfflineRegion.OfflineRegionDeleteCallback {
|
||||
override fun onDelete() {
|
||||
offlineIdlingResource.decrement()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to delete region with ID: ${region.id}. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
throw RuntimeException("Unable to find merged region.")
|
||||
}
|
||||
offlineIdlingResource.decrement()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to obtain offline regions list. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// waiting for offline idling resource
|
||||
onView(withId(R.id.content)).check(matches(isDisplayed()))
|
||||
}
|
||||
|
||||
override fun afterTest() {
|
||||
super.afterTest()
|
||||
IdlingRegistry.getInstance().unregister(offlineIdlingResource)
|
||||
}
|
||||
|
||||
private fun copyAsset(context: Context) {
|
||||
val bufferSize = 1024
|
||||
val assetManager = context.assets
|
||||
val inputStream = assetManager.open(TEST_DB_FILE_NAME)
|
||||
val outputStream = FileOutputStream(File(context.filesDir.absoluteFile, TEST_DB_FILE_NAME))
|
||||
|
||||
try {
|
||||
inputStream.copyTo(outputStream, bufferSize)
|
||||
} finally {
|
||||
inputStream.close()
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
package="com.mapbox.mapboxsdk.testapp">
|
||||
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:name=".MapboxApplication"
|
||||
@@ -815,6 +816,17 @@
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity" />
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.offline.MergeOfflineRegionsActivity"
|
||||
android:description="@string/description_offline_merge"
|
||||
android:label="@string/activity_offline_merge">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_offline" />
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity" />
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.location.LocationMapChangeActivity"
|
||||
android:description="@string/description_location_map_change"
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,129 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.offline
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.widget.Toast
|
||||
import com.mapbox.mapboxsdk.Mapbox
|
||||
import com.mapbox.mapboxsdk.constants.Style
|
||||
import com.mapbox.mapboxsdk.log.Logger
|
||||
import com.mapbox.mapboxsdk.offline.OfflineManager
|
||||
import com.mapbox.mapboxsdk.offline.OfflineRegion
|
||||
import com.mapbox.mapboxsdk.testapp.R
|
||||
import kotlinx.android.synthetic.main.activity_merge_offline_regions.*
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
class MergeOfflineRegionsActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
private const val TEST_DB_FILE_NAME = "offline.db"
|
||||
private const val PERMISSIONS_REQUEST_CODE = 11
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_merge_offline_regions)
|
||||
|
||||
// forcing offline state
|
||||
Mapbox.setConnected(false)
|
||||
|
||||
mapView.setStyleUrl(Style.SATELLITE)
|
||||
|
||||
mapView.onCreate(savedInstanceState)
|
||||
load_region_btn.setOnClickListener {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSIONS_REQUEST_CODE)
|
||||
} else {
|
||||
mergeDb()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (requestCode == PERMISSIONS_REQUEST_CODE) {
|
||||
for (result in grantResults) {
|
||||
if (result != PackageManager.PERMISSION_GRANTED) {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
mergeDb()
|
||||
}
|
||||
}
|
||||
|
||||
private fun mergeDb() {
|
||||
// copy db asset to internal memory
|
||||
copyAsset()
|
||||
|
||||
OfflineManager.getInstance(this).mergeOfflineRegions(
|
||||
this.filesDir.absolutePath + "/" + TEST_DB_FILE_NAME,
|
||||
object : OfflineManager.MergeOfflineRegionsCallback {
|
||||
override fun onMerge(offlineRegions: Array<OfflineRegion>) {
|
||||
mapView.setStyleUrl(Style.SATELLITE)
|
||||
Toast.makeText(
|
||||
this@MergeOfflineRegionsActivity,
|
||||
String.format("Merged %d regions.", offlineRegions.size),
|
||||
Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
override fun onError(error: String) {
|
||||
Logger.e("MBGL_OFFLINE_DB_MERGE", error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun copyAsset() {
|
||||
val bufferSize = 1024
|
||||
val assetManager = this.assets
|
||||
val inputStream = assetManager.open(TEST_DB_FILE_NAME)
|
||||
val outputStream = FileOutputStream(File(this.filesDir, TEST_DB_FILE_NAME))
|
||||
|
||||
try {
|
||||
inputStream.copyTo(outputStream, bufferSize)
|
||||
} finally {
|
||||
inputStream.close()
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
mapView.onStart()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
mapView.onResume()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
mapView.onPause()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
mapView.onStop()
|
||||
}
|
||||
|
||||
override fun onLowMemory() {
|
||||
super.onLowMemory()
|
||||
mapView.onLowMemory()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
mapView.onDestroy()
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
mapView.onSaveInstanceState(outState)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
@@ -10,6 +9,6 @@
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</LinearLayout>
|
||||
</RelativeLayout>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context=".activity.offline.MergeOfflineRegionsActivity">
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/load_region_btn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="MERGE OFFLINE DB" />
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -70,6 +70,7 @@
|
||||
<string name="description_hillshade">Example raster-dem source and hillshade layer</string>
|
||||
<string name="description_heatmaplayer">Use HeatmapLayer to visualise earthquakes</string>
|
||||
<string name="description_gesture_detector">Manipulate gestures detector\'s settings</string>
|
||||
<string name="description_offline_merge">Merge external offline database</string>
|
||||
<string name="description_draggable_marker">Click to add a marker, long-click to drag</string>
|
||||
<string name="description_location_map_change">Change map\'s style while location is displayed</string>
|
||||
<string name="description_location_modes">Showcases location render and tracking modes</string>
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
<string name="activity_hillshade">Hillshade</string>
|
||||
<string name="activity_heatmaplayer">Heatmap layer</string>
|
||||
<string name="activity_gesture_detector">Gestures detector</string>
|
||||
<string name="activity_offline_merge">Offline DB merge</string>
|
||||
<string name="activity_draggable_maker">Draggable marker</string>
|
||||
<string name="activity_location_map_change">Simple Location Activity</string>
|
||||
<string name="activity_location_modes">Location Modes Activity</string>
|
||||
|
||||
Reference in New Issue
Block a user