Skip to content

Commit

Permalink
Merge pull request #104 from learning-layers/integration
Browse files Browse the repository at this point in the history
v1.9.1 - Midnight
  • Loading branch information
melonmanchan authored Apr 24, 2017
2 parents 3606e06 + 9718f81 commit 5067299
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ manifest-merger-release-report.txt

# Android Studio heap captures
captures/
app/google-services.json
17 changes: 13 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ android {

useLibrary 'org.apache.http.legacy'

configurations {
compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
}

defaultConfig {
applicationId 'fi.aalto.legroup.achso'
minSdkVersion 16
Expand Down Expand Up @@ -84,10 +88,13 @@ dependencies {
compile 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0'

// Google Play Services APIs
compile 'com.google.android.gms:play-services-base:8.1.0'
compile 'com.google.android.gms:play-services-maps:8.1.0'
compile 'com.google.android.gms:play-services-location:8.1.0'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
compile 'com.google.android.gms:play-services-base:10.2.1'
compile 'com.google.android.gms:play-services-maps:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
compile 'com.google.android.gms:play-services-analytics:10.2.1'

// Push notifications
compile 'com.google.firebase:firebase-messaging:10.2.1'

// OAuth2 library for OpenID Connect
compile('com.google.oauth-client:google-oauth-client:1.19.0') {
Expand Down Expand Up @@ -129,3 +136,5 @@ dependencies {
// Decrypting the Layers Box URL
compile 'fi.aalto.legroup:cryptohelper:0.1.0'
}

apply plugin: 'com.google.gms.google-services'
19 changes: 18 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,24 @@
android:label="@string/choose_account"
android:parentActivityName=".browsing.BrowserActivity" />

<service android:name=".storage.local.ExportService" />
<service
android:enabled="true"
android:exported="true"
android:name=".utilities.AchsoFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service
android:enabled="true"
android:exported="true"
android:name=".utilities.AchsoFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

<service android:name=".storage.remote.upload.UploadService" />
<service android:name=".storage.remote.download.DownloadService" />
<service android:name=".storage.remote.SyncService" />
Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/fi/aalto/legroup/achso/app/App.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
package fi.aalto.legroup.achso.app;

import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.firebase.iid.FirebaseInstanceId;
import com.rollbar.android.Rollbar;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Timer;
import java.util.TimerTask;

import fi.aalto.legroup.achso.BuildConfig;
import fi.aalto.legroup.achso.R;
import fi.aalto.legroup.achso.authentication.AccountLoggedOutEvent;
import fi.aalto.legroup.achso.authentication.AuthenticatedHttpClient;
import fi.aalto.legroup.achso.authentication.LoginManager;
import fi.aalto.legroup.achso.authentication.LoginRequestEvent;
import fi.aalto.legroup.achso.authentication.LoginStateEvent;
import fi.aalto.legroup.achso.authentication.OIDCConfig;
import fi.aalto.legroup.achso.authoring.ExportHelper;
import fi.aalto.legroup.achso.authoring.LocationManager;
Expand Down Expand Up @@ -79,6 +91,7 @@ public void onCreate() {
setupPreferences();

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

layersBoxUrl = readLayersBoxUrl();
usePublicLayersBox = preferences.getBoolean(AppPreferences.USE_PUBLIC_LAYERS_BOX, false);
publicLayersBoxUrl = Uri.parse(getString(R.string.publicLayersBoxUrl));
Expand Down Expand Up @@ -139,6 +152,7 @@ public void onCreate() {

updateOIDCTokens(this);

bus.register(this);
bus.post(new LoginRequestEvent(LoginRequestEvent.Type.LOGIN));

// Trim the caches asynchronously
Expand Down Expand Up @@ -172,6 +186,64 @@ public void tokensRetrieved() {
}
}

public static void tokenUpdated(String notificationToken) {
if (loginManager.isLoggedIn()) {
App.registerToken(notificationToken);
}
}

@Subscribe
public static void onLoginStateEvent(final LoginStateEvent event) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
String token = FirebaseInstanceId.getInstance().getToken();

if (token == null) return;

if (event.getState() == LoginManager.LoginState.LOGGED_IN) {
App.registerToken(token);
}
}
}, 4000);
}

@Subscribe
public static void onAccountLoggedOutEvent(final AccountLoggedOutEvent event) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
String token = FirebaseInstanceId.getInstance().getToken();

if (token == null) return;

App.removeTokenFromAccount(event.getAccount(), token);
}
}, 10);
}

private static void registerToken(final String notificationToken) {
try {
achRails.registerToken(notificationToken);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void removeTokenFromAccount(Account account, String notificationToken) {
try {
achRails.unregisterToken(account, notificationToken);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static Uri getLayersBoxUrl() {
if (usePublicLayersBox) {
return publicLayersBoxUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fi.aalto.legroup.achso.authentication;

import android.accounts.Account;

/**
* Created by mat on 19/04/2017.
*/

public class AccountLoggedOutEvent {
Account account;

public AccountLoggedOutEvent(Account account) {
this.account = account;
}

public Account getAccount() {
return account;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.util.Log;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
Expand Down Expand Up @@ -67,6 +68,16 @@ public Response execute(Request request, Account account, boolean doRetry) throw
return response;
}

public void enqueue(Request request, Account account, Callback cb) {
AccountManager accountManager = AccountManager.get(context);

String token = getBearerToken(account);

request = request.newBuilder().header("Authorization", "Bearer " + token).build();

httpClient.newCall(request).enqueue(cb);
}

public boolean accessDenied(Response response) {
int code = response.code();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ public void login(Account account) {
new LoginTask().execute(account);
}

private void notifyAcccountLoggedOut(Account account) {
this.bus.post(new AccountLoggedOutEvent(account));
}

/**
* Logs out from the account and disables auto-login. Use this if the user manually logs out.
*/
public void logoutExplicitly() {
setState(LoginState.LOGGING_OUT, false);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

prefs.edit()
Expand All @@ -106,7 +112,12 @@ public void logoutExplicitly() {
* automatic (e.g. connectivity lost) and not initiated by the user.
*/
public void logout() {
if (getState() != LoginState.LOGGING_OUT) {
setState(LoginState.LOGGING_OUT, false);
}

setState(LoginState.LOGGED_OUT, true);
notifyAcccountLoggedOut(account);
account = null;
user = null;
}
Expand Down Expand Up @@ -259,7 +270,5 @@ protected void onPostExecute(String error) {

setState(LoginState.LOGGED_IN, true);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
Expand Down Expand Up @@ -143,27 +144,30 @@ public void onCreate(Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.mapFragment);

Location location = video.getLocation();
final Location location = video.getLocation();

if (location != null) {
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());

GoogleMap map = mapFragment.getMap();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());

map.addCircle(new CircleOptions()
.center(position)
.radius(location.getAccuracy())
.strokeWidth(3.0f)
.strokeColor(Color.WHITE)
.fillColor(Color.parseColor("#80ffffff")));
googleMap.addCircle(new CircleOptions()
.center(position)
.radius(location.getAccuracy())
.strokeWidth(3.0f)
.strokeColor(Color.WHITE)
.fillColor(Color.parseColor("#80ffffff")));

map.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14.5f));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14.5f));

findViewById(R.id.unknownLocationText).setVisibility(View.GONE);
findViewById(R.id.unknownLocationText).setVisibility(View.GONE);
}
});
}

initializeAddQRButton();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fi.aalto.legroup.achso.storage.remote;

import android.accounts.Account;
import android.net.Uri;

import org.json.JSONException;
Expand Down Expand Up @@ -57,4 +58,8 @@ public interface VideoHost {
* Finds a video by the video source uri.
*/
public Video findVideoByVideoUri(Uri videoUri) throws IOException;

public void registerToken(String notificationToken) throws JSONException, IOException;

public void unregisterToken(Account account, String notificationToken) throws JSONException, IOException;
}
Loading

0 comments on commit 5067299

Please sign in to comment.