Skip to content

Commit 55c5459

Browse files
committed
Add source for demo android app
1 parent d4048c9 commit 55c5459

File tree

73 files changed

+4266
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+4266
-0
lines changed

android_demo/.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Built application files
2+
*/build/
3+
4+
# Local configuration file (sdk path, etc)
5+
local.properties
6+
7+
# IntelliJ configuration file
8+
android/android.iml
9+
10+
# Gradle generated files
11+
.gradle/
12+
13+
# Signing files
14+
.signing/
15+
16+
# User-specific configurations
17+
.idea/*
18+
19+
# OS-specific files
20+
.DS_Store
21+
._*
22+
.Trashes

android_demo/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Android Open Location Code Demonstrator
2+
=======================================
3+
4+
This is the source code for an Android app that uses
5+
[Open Location Codes](http://openlocationcode.com) and displays them on a map.
6+
7+
It displays the current location, computes the code for that location, and uses
8+
a bundled set of place names to shorten the code to a more convenient form.
9+
Currently all the place names exist within Cape Verde. Other place names can be
10+
added to the source.
11+
12+
Using a bundled set of places means that determining the current address, and
13+
locating an entered address will work offline.
14+
15+
This project has been structured to be used with
16+
[Android Studio](https://developer.android.com/studio/index.html).
17+
18+
An Open Location Code JAR file is in the directory android/libs. If the core library
19+
is updated, you will need to update this JAR file.

android_demo/android/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.neenbedankt.android-apt'
3+
4+
android {
5+
compileSdkVersion rootProject.compileSdkVersion
6+
buildToolsVersion rootProject.buildToolsVersion
7+
8+
defaultConfig {
9+
applicationId "com.openlocationcode.android"
10+
minSdkVersion rootProject.minSdkVersion
11+
targetSdkVersion rootProject.targetSdkVersion
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
testCompile 'junit:junit:4.12'
26+
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
27+
compile "com.android.support:design:$rootProject.supportLibraryVersion"
28+
29+
// Dagger dependencies
30+
apt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
31+
provided 'org.glassfish:javax.annotation:10.0-b28'
32+
compile "com.google.dagger:dagger:$rootProject.daggerVersion"
33+
34+
compile "com.android.volley:volley:$rootProject.volleyVersion"
35+
36+
compile "com.google.auto.factory:auto-factory:$rootProject.autoFactoryVersion"
37+
38+
39+
compile "com.google.android.gms:play-services-location:$rootProject.gmsVersion"
40+
compile "com.google.android.gms:play-services-base:$rootProject.gmsVersion"
41+
}
6.83 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Add project specific ProGuard rules here.
2+
# You can edit the include path and order by changing the proguardFiles
3+
# directive in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# Add any project specific keep options here:
9+
10+
# If your project uses WebView with JS, uncomment the following
11+
# and specify the fully qualified class name to the JavaScript interface
12+
# class:
13+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
14+
# public *;
15+
#}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.openlocationcode.android">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
8+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
9+
10+
<application
11+
android:supportsRtl="true"
12+
android:allowBackup="true"
13+
android:icon="@drawable/ic_launcher"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme">
16+
<activity
17+
android:name="com.openlocationcode.android.main.WelcomeActivity"
18+
android:label="@string/current_title"
19+
android:configChanges="orientation|screenSize">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
<activity
26+
android:name="com.openlocationcode.android.main.MainActivity"
27+
android:label="@string/current_title"
28+
android:configChanges="orientation|screenSize">
29+
<intent-filter>
30+
<action android:name="android.intent.action.VIEW" />
31+
<category android:name="android.intent.category.DEFAULT" />
32+
<data android:scheme="geo" />
33+
</intent-filter>
34+
</activity>
35+
36+
<meta-data
37+
android:name="com.google.android.maps.v2.API_KEY"
38+
android:value="AIzaSyAU9ShujnIg3IDQxtPr7Q1qOvFVdwNmWc4" >
39+
</meta-data>
40+
41+
</application>
42+
43+
</manifest>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.openlocationcode.android.code;
18+
19+
import com.openlocationcode.android.direction.Direction;
20+
21+
import com.google.openlocationcode.OpenLocationCode;
22+
23+
/**
24+
* The contract for the functionality displaying the code, in {@link
25+
* com.openlocationcode.android.main.MainActivity}.
26+
*/
27+
public interface CodeContract {
28+
29+
interface View {
30+
31+
/**
32+
* Implements displaying the {@code code}.
33+
*/
34+
void displayCode(OpenLocationCode code);
35+
}
36+
37+
interface ActionsListener {
38+
39+
/**
40+
* Call this when the code is requested for a new location (eg when the map is dragged).
41+
*
42+
* @param latitude
43+
* @param longitude
44+
* @param isCurrent
45+
* @return
46+
*/
47+
Direction codeLocationUpdated(double latitude, double longitude, boolean isCurrent);
48+
49+
/**
50+
* @return the code currently displayed to the user.
51+
*/
52+
OpenLocationCode getCurrentOpenLocationCode();
53+
}
54+
55+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.openlocationcode.android.code;
18+
19+
import android.content.Intent;
20+
import android.location.Location;
21+
import android.net.Uri;
22+
import android.provider.ContactsContract.Contacts;
23+
import android.provider.ContactsContract.Intents.Insert;
24+
import android.support.v7.widget.PopupMenu;
25+
import android.view.MenuInflater;
26+
import android.view.MenuItem;
27+
import android.view.View;
28+
import android.view.View.OnClickListener;
29+
30+
import com.openlocationcode.android.R;
31+
import com.openlocationcode.android.direction.Direction;
32+
import com.openlocationcode.android.direction.DirectionUtil;
33+
import com.openlocationcode.android.main.MainActivity;
34+
35+
import com.google.openlocationcode.OpenLocationCode;
36+
import com.google.openlocationcode.OpenLocationCode.CodeArea;
37+
38+
import java.util.Locale;
39+
40+
/**
41+
* Presents the code information, direction and handles the share menu.
42+
*/
43+
public class CodePresenter
44+
implements CodeContract.ActionsListener, PopupMenu.OnMenuItemClickListener {
45+
46+
private static final float MAX_WALKING_MODE_DISTANCE = 3000f; // 3 km
47+
48+
private final CodeView mView;
49+
50+
public CodePresenter(CodeView view) {
51+
mView = view;
52+
53+
mView.getShareButton()
54+
.setOnClickListener(
55+
new OnClickListener() {
56+
@Override
57+
public void onClick(View v) {
58+
openShareMenu();
59+
}
60+
});
61+
mView.getNavigateButton()
62+
.setOnClickListener(
63+
new OnClickListener() {
64+
@Override
65+
public void onClick(View v) {
66+
navigate();
67+
}
68+
});
69+
}
70+
71+
@Override
72+
public Direction codeLocationUpdated(double latitude, double longitude, boolean isCurrent) {
73+
OpenLocationCode code = OpenLocationCodeUtil.createOpenLocationCode(latitude, longitude);
74+
mView.displayCode(code);
75+
Location currentLocation = MainActivity.getMainPresenter().getCurrentLocation();
76+
77+
Direction direction;
78+
if (isCurrent) {
79+
direction = new Direction(code, null, 0, 0);
80+
} else if (currentLocation == null) {
81+
direction = new Direction(null, code, 0, 0);
82+
} else {
83+
direction = DirectionUtil.getDirection(currentLocation, code);
84+
}
85+
86+
return direction;
87+
}
88+
89+
@Override
90+
public OpenLocationCode getCurrentOpenLocationCode() {
91+
return mView.getLastFullCode();
92+
}
93+
94+
@Override
95+
public boolean onMenuItemClick(MenuItem item) {
96+
int itemId = item.getItemId();
97+
String code = mView.getCodeAndLocality();
98+
if (itemId == R.id.share_code) {
99+
openShareChooser(code);
100+
return true;
101+
} else if (itemId == R.id.save_to_contact) {
102+
saveCodeAsContact(code);
103+
return true;
104+
}
105+
return false;
106+
}
107+
108+
private void openShareMenu() {
109+
PopupMenu popup = new PopupMenu(mView.getContext(), mView.getShareButton());
110+
popup.setOnMenuItemClickListener(this);
111+
MenuInflater inflater = popup.getMenuInflater();
112+
inflater.inflate(R.menu.share_menu, popup.getMenu());
113+
popup.show();
114+
}
115+
116+
private void openShareChooser(String code) {
117+
Intent intent = new Intent(Intent.ACTION_SEND);
118+
intent.putExtra(Intent.EXTRA_TEXT, code);
119+
intent.setType("text/plain");
120+
mView.getContext()
121+
.startActivity(
122+
Intent.createChooser(
123+
intent,
124+
mView.getContext().getResources().getText(R.string.share_chooser_title)));
125+
}
126+
127+
private void saveCodeAsContact(String code) {
128+
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
129+
intent.setType(Contacts.CONTENT_ITEM_TYPE);
130+
intent.putExtra(Insert.POSTAL, code);
131+
mView.getContext().startActivity(intent);
132+
}
133+
134+
private void navigate() {
135+
OpenLocationCode code = mView.getLastFullCode();
136+
CodeArea codeArea = code.decode();
137+
Location currentLocation = MainActivity.getMainPresenter().getCurrentLocation();
138+
float[] results = new float[3];
139+
Location.distanceBetween(
140+
currentLocation.getLatitude(),
141+
currentLocation.getLongitude(),
142+
codeArea.getCenterLatitude(),
143+
codeArea.getCenterLongitude(),
144+
results);
145+
float distance = results[0];
146+
char navigationMode;
147+
if (distance <= MAX_WALKING_MODE_DISTANCE) {
148+
navigationMode = 'w';
149+
} else {
150+
navigationMode = 'd';
151+
}
152+
153+
Uri gmmIntentUri = Uri.parse(
154+
String.format(
155+
Locale.US,
156+
"google.navigation:q=%f,%f&mode=%s",
157+
codeArea.getCenterLatitude(),
158+
codeArea.getCenterLongitude(),
159+
navigationMode));
160+
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
161+
mView.getContext().startActivity(mapIntent);
162+
}
163+
}

0 commit comments

Comments
 (0)