Skip to content

Commit

Permalink
Localization plugin (#74)
Browse files Browse the repository at this point in the history
* Basic map localization support added

* fixed missing junit dependency

* use regrex to replace only the part needing change
  • Loading branch information
Langston Smith authored and Cameron Mace committed Feb 21, 2018
1 parent 4cb4a74 commit 401b669
Show file tree
Hide file tree
Showing 21 changed files with 1,039 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ MBGL_ANDROID_PLUGINS += building;plugin-building
MBGL_ANDROID_PLUGINS += cluster;plugin-cluster
MBGL_ANDROID_PLUGINS += offline;plugin-offline
MBGL_ANDROID_PLUGINS += places;plugin-places
MBGL_ANDROID_PLUGINS += localization;plugin-localization

sonarqube:
./gradlew test
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ dependencies {
implementation project(':plugin-cluster')
implementation project(':plugin-places')
implementation project(':plugin-offline')
implementation project(':plugin-localization')
}

sonarqube {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<activity
android:name=".activity.LocalizationActivity"
android:description="@string/description_localization"
android:label="@string/title_localization">
<meta-data
android:name="@string/category"
android:value="@string/category_localization"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<activity android:name="com.mapbox.maboxsdk.plugins.SingleFragmentActivity"/>

<service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.mapbox.mapboxsdk.plugins.testapp.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
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.plugins.localization.LocalizationPlugin;
import com.mapbox.mapboxsdk.plugins.localization.MapLocale;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.mapboxsdk.plugins.testapp.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LocalizationActivity extends AppCompatActivity implements OnMapReadyCallback {

@BindView(R.id.mapView)
MapView mapView;

private LocalizationPlugin localizationPlugin;
private MapboxMap mapboxMap;
private boolean mapIsLocalized;

private static final MapLocale[] LOCALES = new MapLocale[] {
MapLocale.CANADA,
MapLocale.GERMANY,
MapLocale.CHINA,
MapLocale.US,
MapLocale.CANADA_FRENCH,
MapLocale.ITALY,
MapLocale.JAPAN,
MapLocale.KOREA,
MapLocale.FRANCE
};

private static int index;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localization);
ButterKnife.bind(this);
mapIsLocalized = true;
Toast.makeText(this, R.string.change_language_instruction, Toast.LENGTH_LONG).show();
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
localizationPlugin = new LocalizationPlugin(mapView, mapboxMap);
localizationPlugin.matchMapLanguageWithDeviceDefault();
}

@OnClick(R.id.localize_fab)
public void localizeToggleFab() {
if (mapIsLocalized) {
localizationPlugin.setMapLanguage(new MapLocale(MapLocale.FRENCH));
Toast.makeText(this, R.string.map_not_localized, Toast.LENGTH_SHORT).show();
mapIsLocalized = false;
} else {
localizationPlugin.matchMapLanguageWithDeviceDefault();
Toast.makeText(this, R.string.map_localized, Toast.LENGTH_SHORT).show();
mapIsLocalized = true;
}
}

@OnClick(R.id.camera_localization)
public void localizeCameraFab() {
MapLocale locale = getNextMapLocale();
localizationPlugin.setMapLanguage(locale);
localizationPlugin.setCameraToLocaleCountry(locale);
}

@OnClick(R.id.change_map_style)
public void changeMapStyleFab() {
if (mapboxMap != null) {
mapboxMap.setStyleUrl(Utils.getNextStyle());
}
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

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

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

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

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

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_languages, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.english:
localizationPlugin.setMapLanguage(MapLocale.ENGLISH);
return true;
case R.id.spanish:
localizationPlugin.setMapLanguage(MapLocale.SPANISH);
return true;
case R.id.french:
localizationPlugin.setMapLanguage(MapLocale.FRENCH);
return true;
case R.id.german:
localizationPlugin.setMapLanguage(MapLocale.GERMAN);
return true;
case R.id.russian:
localizationPlugin.setMapLanguage(MapLocale.RUSSIAN);
return true;
case R.id.chinese:
localizationPlugin.setMapLanguage(MapLocale.CHINESE);
return true;
case R.id.simplified_chinese:
localizationPlugin.setMapLanguage(MapLocale.SIMPLIFIED_CHINESE);
return true;
case R.id.portuguese:
localizationPlugin.setMapLanguage(MapLocale.PORTUGUESE);
return true;
case R.id.arabic:
localizationPlugin.setMapLanguage(MapLocale.ARABIC);
return true;
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

public static MapLocale getNextMapLocale() {
index++;
if (index == LOCALES.length) {
index = 0;
}
return LOCALES[index];
}
}

12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path
android:fillColor="#FF000000"
android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_translate_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M12.87,15.07l-2.54,-2.51 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53L17,6L17,4h-7L10,2L8,2v2L1,4v1.99h11.17C11.5,7.92 10.44,9.75 9,11.35 8.07,10.32 7.3,9.19 6.69,8h-2c0.73,1.63 1.73,3.17 2.98,4.56l-5.09,5.02L4,19l5,-5 3.11,3.11 0.76,-2.04zM18.5,10h-2L12,22h2l1.12,-3h4.75L21,22h2l-4.5,-12zM15.88,17l1.62,-4.33L19.12,17h-3.24z"/>
</vector>
65 changes: 65 additions & 0 deletions app/src/main/res/layout/activity_localization.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="51.505300"
app:mapbox_cameraTargetLng="-0.075073"
app:mapbox_cameraTilt="20"
app:mapbox_cameraZoom="2.5"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets">

</com.mapbox.mapboxsdk.maps.MapView>

<android.support.design.widget.FloatingActionButton
android:id="@+id/localize_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_translate_white_24dp"
app:backgroundTint="@color/colorPrimary"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/change_map_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_layers"
android:tint="@android:color/white"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/camera_localization"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/camera_localization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_camera"
android:tint="@android:color/white"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/localize_fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>
52 changes: 52 additions & 0 deletions app/src/main/res/menu/menu_languages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/english"
android:title="English"
app:showAsAction="never"/>

<item
android:id="@+id/spanish"
android:title="Spanish"
app:showAsAction="never"/>

<item
android:id="@+id/french"
android:title="French"
app:showAsAction="never"/>

<item
android:id="@+id/german"
android:title="German"
app:showAsAction="never"/>

<item
android:id="@+id/russian"
android:title="Russian"
app:showAsAction="never"/>

<item
android:id="@+id/chinese"
android:title="Chinese"
app:showAsAction="never"/>

<item
android:id="@+id/simplified_chinese"
android:title="Simplified Chinese"
app:showAsAction="never"/>


<item
android:id="@+id/portuguese"
android:title="Portuguese"
app:showAsAction="never"/>

<item
android:id="@+id/arabic"
android:title="Arabic"
app:showAsAction="never"/>


</menu>
Loading

0 comments on commit 401b669

Please sign in to comment.