Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

LocalFileSource #6497

Merged
merged 4 commits into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(MBGL_TEST_FILES
# storage
test/storage/asset_file_source.test.cpp
test/storage/default_file_source.test.cpp
test/storage/local_file_source.test.cpp
test/storage/headers.test.cpp
test/storage/http_file_source.test.cpp
test/storage/offline.test.cpp
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class DefaultFileSource : public FileSource {
private:
const std::unique_ptr<util::Thread<Impl>> thread;
const std::unique_ptr<FileSource> assetFileSource;
const std::unique_ptr<FileSource> localFileSource;
};

} // namespace mbgl
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@
android:name="@string/category"
android:value="@string/category_style" />
</activity>
<activity
android:name=".activity.style.StyleFileActivity"
android:description="@string/description_style_file"
android:label="@string/activity_style_file">
<meta-data
android:name="@string/category"
android:value="@string/category_style" />
</activity>

<activity
android:name=".activity.imagegenerator.PrintActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.mapbox.mapboxsdk.testapp.activity.style;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RawRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;

/**
* Example on how to use a file:// resource for the style.json
*/
public class StyleFileActivity extends AppCompatActivity {
private static final String TAG = StyleFileActivity.class.getSimpleName();

private MapboxMap mapboxMap;
private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_style_file);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setColorFilter(ContextCompat.getColor(StyleFileActivity.this, R.color.primary));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "Loading style file");
new CreateStyleFileTask().execute();
}
});
}
});
}

/**
* Task to write a style file to local disk and load it in the map view
*/
private class CreateStyleFileTask extends AsyncTask<Void, Integer, Long> {
private File cacheStyleFile;

@Override
protected Long doInBackground(Void... params) {
try {
cacheStyleFile = File.createTempFile("my-", ".style.json");
cacheStyleFile.createNewFile();
Log.i(TAG, "Writing style file to: " + cacheStyleFile.getAbsolutePath());
writeToFile(cacheStyleFile, readRawResource(R.raw.local_style));
} catch (Exception e) {
Toast.makeText(StyleFileActivity.this, "Could not create style file in cache dir", Toast.LENGTH_SHORT).show();
}
return 1l;
}

protected void onPostExecute(Long result) {
//Actual file:// usage
mapboxMap.setStyleUrl("file://" + cacheStyleFile.getAbsolutePath());
}

private String readRawResource(@RawRes int rawResource) throws IOException {
InputStream is = getResources().openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}

return writer.toString();
}

private void writeToFile(File file, String contents) throws IOException {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(contents);
} finally {
if (writer != null) {
writer.close();
}
}
}
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
app:center_latitude="52.519003"
app:center_longitude="13.400972"
app:style_url="@string/style_mapbox_streets"
app:zoom="16"
android:layout_height="match_parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add_24dp"
app:backgroundTint="@android:color/white" />

</RelativeLayout>
Loading