Skip to content

Commit 2b07ab3

Browse files
committed
initial commit
0 parents  commit 2b07ab3

Some content is hidden

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

41 files changed

+1193
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
.idea
4+
/local.properties
5+
/.idea/workspace.xml
6+
/.idea/libraries
7+
.DS_Store
8+
/build
9+
/captures
10+
.externalNativeBuild

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "26.0.2"
6+
defaultConfig {
7+
applicationId "com.pubnub.tutorials.map.android.pubnubmaptutorialandroid"
8+
minSdkVersion 23
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
packagingOptions {
21+
exclude 'META-INF/DEPENDENCIES.txt'
22+
exclude 'META-INF/LICENSE.txt'
23+
exclude 'META-INF/NOTICE.txt'
24+
exclude 'META-INF/NOTICE'
25+
exclude 'META-INF/LICENSE'
26+
exclude 'META-INF/DEPENDENCIES'
27+
exclude 'META-INF/notice.txt'
28+
exclude 'META-INF/license.txt'
29+
exclude 'META-INF/dependencies.txt'
30+
exclude 'META-INF/LGPL2.1'
31+
}
32+
}
33+
34+
dependencies {
35+
compile fileTree(dir: 'libs', include: ['*.jar'])
36+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
37+
exclude group: 'com.android.support', module: 'support-annotations'
38+
exclude group: 'com.google.code.findbugs', module: 'jsr305'
39+
})
40+
compile group: 'com.pubnub', name: 'pubnub-gson', version: '4.12.0'
41+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.2'
42+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.2'
43+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.2'
44+
compile group: 'com.google.guava', name: 'guava', version: '23.2-android'
45+
compile 'com.google.android.gms:play-services:11.0.4'
46+
compile 'com.android.support:appcompat-v7:25.3.1'
47+
compile 'com.android.support:design:25.3.1'
48+
compile 'com.android.support:support-v4:25.3.1'
49+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
50+
testCompile 'junit:junit:4.12'
51+
}

app/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/sunny/android-sdks/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.pubnub.tutorials.map.android.pubnubmaptutorialandroid">
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.ACCESS_FINE_LOCATION"/>
8+
9+
<application
10+
android:allowBackup="true"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/AppTheme">
16+
<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_MAPS_KEY"/>
17+
<activity android:name=".MainActivity">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
24+
<activity android:name=".LoginActivity"
25+
android:screenOrientation="portrait"
26+
android:configChanges="orientation|keyboardHidden">
27+
</activity>
28+
</application>
29+
30+
</manifest>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.pubnub.tutorials.map.android.pubnubmaptutorialandroid;
2+
3+
public class Constants {
4+
public static final String PUBNUB_PUBLISH_KEY = "YOUR_PUB_KEY";
5+
public static final String PUBNUB_SUBSCRIBE_KEY = "YOUR_SUB_KEY";
6+
7+
public static final String SUBSCRIBE_CHANNEL_NAME = "basic_location_subscribe";
8+
public static final String PUBLISH_CHANNEL_NAME = "basic_location_publish";
9+
public static final String FLIGHTPATHS_CHANNEL_NAME = "basic_flightpaths_subscribe";
10+
11+
public static final String DATASTREAM_PREFS = "com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.DATASTREAM_PREFS";
12+
public static final String DATASTREAM_UUID = "com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.DATASTREAM_UUID";
13+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.pubnub.tutorials.map.android.pubnubmaptutorialandroid;
2+
3+
import android.content.Intent;
4+
import android.content.SharedPreferences;
5+
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.view.View;
8+
import android.widget.EditText;
9+
10+
public class LoginActivity extends AppCompatActivity {
11+
private EditText mUsername;
12+
13+
private static boolean isValid(String username) {
14+
return username.matches("^[a-zA-Z0-9_]+$");
15+
}
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_login);
21+
22+
mUsername = (EditText) findViewById(R.id.usernameEdit);
23+
}
24+
25+
public void joinChat(View view) {
26+
String username = mUsername.getText().toString();
27+
28+
if (!isValid(username)) {
29+
return;
30+
}
31+
32+
SharedPreferences sp = getSharedPreferences(Constants.DATASTREAM_PREFS, MODE_PRIVATE);
33+
SharedPreferences.Editor edit = sp.edit();
34+
edit.putString(Constants.DATASTREAM_UUID, username);
35+
edit.apply();
36+
37+
Intent intent = new Intent(this, MainActivity.class);
38+
startActivity(intent);
39+
}
40+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.pubnub.tutorials.map.android.pubnubmaptutorialandroid;
2+
3+
import android.content.Intent;
4+
import android.content.SharedPreferences;
5+
import android.os.Bundle;
6+
import android.support.annotation.NonNull;
7+
import android.support.design.widget.TabLayout;
8+
import android.support.v4.view.ViewPager;
9+
import android.support.v7.app.AppCompatActivity;
10+
11+
import com.pubnub.api.PNConfiguration;
12+
import com.pubnub.api.PubNub;
13+
14+
import java.util.Random;
15+
16+
public class MainActivity extends AppCompatActivity {
17+
private SharedPreferences mSharedPrefs;
18+
private PubNub pubNub;
19+
private String userName;
20+
private Random random;
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
26+
mSharedPrefs = getSharedPreferences(Constants.DATASTREAM_PREFS, MODE_PRIVATE);
27+
if (!mSharedPrefs.contains(Constants.DATASTREAM_UUID)) {
28+
Intent toLogin = new Intent(this, LoginActivity.class);
29+
startActivity(toLogin);
30+
return;
31+
}
32+
33+
setContentView(R.layout.activity_main);
34+
35+
this.random = new Random();
36+
this.userName = mSharedPrefs.getString(Constants.DATASTREAM_UUID, "anonymous_" + random.nextInt(10000));
37+
this.pubNub = initPubNub(this.userName);
38+
39+
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
40+
tabLayout.addTab(tabLayout.newTab().setText("1_Marker"));
41+
tabLayout.addTab(tabLayout.newTab().setText("2_Live_Location"));
42+
tabLayout.addTab(tabLayout.newTab().setText("3_Flightpath"));
43+
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
44+
45+
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
46+
final MainActivityTabManager adapter = new MainActivityTabManager
47+
(getSupportFragmentManager(), tabLayout.getTabCount(), pubNub);
48+
49+
viewPager.setAdapter(adapter);
50+
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
51+
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
52+
@Override
53+
public void onTabSelected(TabLayout.Tab tab) {
54+
viewPager.setCurrentItem(tab.getPosition());
55+
}
56+
57+
@Override
58+
public void onTabUnselected(TabLayout.Tab tab) {
59+
}
60+
61+
@Override
62+
public void onTabReselected(TabLayout.Tab tab) {
63+
}
64+
});
65+
}
66+
67+
@NonNull
68+
private PubNub initPubNub(String userName) {
69+
PNConfiguration pnConfiguration = new PNConfiguration();
70+
pnConfiguration.setPublishKey(Constants.PUBNUB_PUBLISH_KEY);
71+
pnConfiguration.setSubscribeKey(Constants.PUBNUB_SUBSCRIBE_KEY);
72+
pnConfiguration.setSecure(true);
73+
pnConfiguration.setUuid(userName);
74+
75+
return new PubNub(pnConfiguration);
76+
}
77+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.pubnub.tutorials.map.android.pubnubmaptutorialandroid;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.support.v4.app.FragmentManager;
5+
import android.support.v4.app.FragmentStatePagerAdapter;
6+
7+
import com.google.common.collect.ImmutableList;
8+
import com.pubnub.api.PubNub;
9+
import com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.flightpaths.FlightPathsTabContentFragment;
10+
import com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.locationpublish.LocationPublishTabContentFragment;
11+
import com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.locationsubscribe.LocationSubscribeTabContentFragment;
12+
13+
public class MainActivityTabManager extends FragmentStatePagerAdapter {
14+
private final LocationSubscribeTabContentFragment locationSubscribe;
15+
private final LocationPublishTabContentFragment locationPublish;
16+
private final FlightPathsTabContentFragment flightPathsTabContentFragment;
17+
18+
private ImmutableList<Fragment> items;
19+
20+
public MainActivityTabManager(FragmentManager fm, int NumOfTabs, PubNub pubNub) {
21+
super(fm);
22+
23+
this.locationSubscribe = new LocationSubscribeTabContentFragment();
24+
this.locationSubscribe.setPubNub(pubNub);
25+
26+
this.locationPublish = new LocationPublishTabContentFragment();
27+
this.locationPublish.setPubNub(pubNub);
28+
29+
this.flightPathsTabContentFragment = new FlightPathsTabContentFragment();
30+
this.flightPathsTabContentFragment.setPubNub(pubNub);
31+
32+
this.items = ImmutableList.of((Fragment) locationSubscribe, (Fragment) locationPublish, (Fragment) flightPathsTabContentFragment);
33+
}
34+
35+
@Override
36+
public Fragment getItem(int position) {
37+
return this.items.get(position);
38+
}
39+
40+
@Override
41+
public int getCount() {
42+
return this.items.size();
43+
}
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.pubnub.tutorials.map.android.pubnubmaptutorialandroid.flightpaths;
2+
3+
import android.app.Activity;
4+
import android.util.Log;
5+
6+
import com.google.android.gms.maps.CameraUpdateFactory;
7+
import com.google.android.gms.maps.GoogleMap;
8+
import com.google.android.gms.maps.model.LatLng;
9+
import com.google.android.gms.maps.model.Polyline;
10+
import com.google.android.gms.maps.model.PolylineOptions;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
public class FlightPathsMapAdapter {
17+
private static final String TAG = FlightPathsMapAdapter.class.getName();
18+
19+
private Polyline polyline;
20+
private Activity activity;
21+
private GoogleMap map;
22+
23+
private List<LatLng> polylinePoints;
24+
25+
public FlightPathsMapAdapter(Activity activity, GoogleMap map) {
26+
this.activity = activity;
27+
this.map = map;
28+
this.polylinePoints = new ArrayList<>();
29+
}
30+
31+
public void locationUpdated(final Map<String, String> newLocation) {
32+
if (newLocation.containsKey("lat") && newLocation.containsKey("lng")) {
33+
String lat = newLocation.get("lat");
34+
String lng = newLocation.get("lng");
35+
36+
doUiUpdate(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
37+
} else {
38+
Log.w(TAG, "message ignored: " + newLocation.toString());
39+
}
40+
}
41+
42+
private void doUiUpdate(final LatLng location) {
43+
activity.runOnUiThread(new Runnable() {
44+
@Override
45+
public void run() {
46+
polylinePoints.add(location);
47+
48+
if (polyline != null) {
49+
polyline.setPoints(polylinePoints);
50+
} else {
51+
polyline = map.addPolyline(new PolylineOptions().addAll(polylinePoints));
52+
}
53+
54+
map.moveCamera(CameraUpdateFactory.newLatLng(location));
55+
}
56+
});
57+
}
58+
}

0 commit comments

Comments
 (0)