Skip to content

Commit

Permalink
"FIX": Temporary Workaround for MessageService: Mocking some devices
Browse files Browse the repository at this point in the history
ADD: Google API Key is now wrapped in resources. Read api_keys_template.xml for more info
ADD: Devices can now be shown on a map
  • Loading branch information
scholzs committed Nov 21, 2017
1 parent 5c4e028 commit ff9c9da
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@
/captures
**/*.iml
**/build
cormorant-framework/src/main/res/values/api_keys.xml
1 change: 1 addition & 0 deletions cormorant-framework/build.gradle
Expand Up @@ -45,6 +45,7 @@ dependencies {
implementation 'org.igniterealtime.smack:smack-extensions:4.2.1-SNAPSHOT'
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.journeyapps:zxing-android-embedded:3.4.0'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
implementation 'com.google.android.gms:play-services-nearby:11.6.0'
testImplementation 'junit:junit:4.12'
Expand Down
15 changes: 8 additions & 7 deletions cormorant-framework/src/main/AndroidManifest.xml
Expand Up @@ -3,7 +3,7 @@
package="at.usmile.cormorant.framework">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand All @@ -18,10 +18,15 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.nearby.messages.API_KEY"
android:value="" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/api_key_google_maps" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<service
android:name=".AuthenticationFrameworkService"
Expand All @@ -35,7 +40,6 @@
android:name=".lock.LockService"
android:enabled="true"
android:exported="false" />

<service
android:name=".group.GroupService"
android:enabled="true"
Expand All @@ -55,7 +59,6 @@
<data android:scheme="package" />
</intent-filter>
</receiver>

<receiver
android:name=".AdminReceiver"
android:enabled="true"
Expand All @@ -77,17 +80,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".group.BarcodeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/device_id"
android:theme="@style/FullscreenTheme" />

<activity
android:name=".group.GroupListActivity"
android:label="Trusted Devices" />

<activity
android:name=".group.DialogPinEnterActivity"
android:label="Enter PIN"
Expand All @@ -99,6 +99,7 @@
<activity
android:name=".group.DialogRemoveDeviceActivity"
android:theme="@style/Theme.AppCompat.Dialog" />
<activity android:name=".group.GroupMapActivity"></activity>
</application>

</manifest>
@@ -0,0 +1,21 @@
package at.usmile.cormorant.framework.common;

import at.usmile.cormorant.framework.R;

/**
* Created by fhdwsse
*/

public class CommonUtils {
public static int getIconByScreenSize(double screenSize, boolean blue) {
if (screenSize >= 7) {
return blue ? R.drawable.ic_computer_blue_24dp : R.drawable.ic_computer_black_24dp;
}
if (screenSize < 3) {
return blue ? R.drawable.ic_watch_blue_24dp : R.drawable.ic_watch_black_24dp;
} else {
return blue ? R.drawable.ic_phone_android_blue_24dp : R.drawable.ic_phone_android_black_24dp;
}
}

}
@@ -1,17 +1,17 @@
/**
* Copyright 2016 - 2017
*
* <p>
* Daniel Hintze <daniel.hintze@fhdw.de>
* Sebastian Scholz <sebastian.scholz@fhdw.de>
* Rainhard D. Findling <rainhard.findling@fh-hagenberg.at>
* Muhammad Muaaz <muhammad.muaaz@usmile.at>
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -37,6 +37,7 @@
import android.widget.TextView;

import at.usmile.cormorant.framework.R;
import at.usmile.cormorant.framework.common.CommonUtils;
import at.usmile.cormorant.framework.common.TypedServiceConnection;
import at.usmile.cormorant.framework.lock.DeviceLockCommand;
import at.usmile.cormorant.framework.messaging.MessagingService;
Expand All @@ -46,6 +47,20 @@ public class GroupListActivity extends AppCompatActivity implements GroupChangeL
public static TrustedDevice deviceToRemove;

private ListView listview;
private TypedServiceConnection<MessagingService> messagingService = new TypedServiceConnection<>();
private TypedServiceConnection<GroupService> groupService = new TypedServiceConnection<GroupService>() {

@Override
public void onServiceConnected(GroupService service) {
createArrayAdapter();
service.addGroupChangeListener(GroupListActivity.this);
}

@Override
public void onServiceDisconnected(GroupService service) {
service.removeGroupChangeListener(GroupListActivity.this);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -114,6 +129,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menuAddDeviceToGroup:
startActivity(new Intent(this, BarcodeActivity.class));
return true;
case R.id.menuShowMap:
startActivity(new Intent(this, GroupMapActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -156,57 +174,29 @@ public View getView(int position, View contentView, ViewGroup viewGroup) {

TrustedDevice p = getItem(position);

if(groupService.get().getSelf().equals(p)){
((TextView) view.findViewById(R.id.activity_group_list_text3)).setVisibility(View.GONE);
((TextView) view.findViewById(R.id.activity_group_list_text4)).setVisibility(View.GONE);
}

((TextView) view.findViewById(R.id.activity_group_list_text1)).setText(p.getId());
((TextView) view.findViewById(R.id.activity_group_list_text2)).setText(p.getDevice());
((TextView) view.findViewById(R.id.activity_group_list_text3)).setText("GPS distance: " + p.getDistanceToOtherDeviceGps() + "m");
((TextView) view.findViewById(R.id.activity_group_list_text4)).setText("BT distance: " + p.getDistanceToOtherDeviceBluetooth());
((ImageView) view.findViewById(R.id.activity_group_list_icon)).setImageResource(getIconByScreenSize(p.getScreenSize(), groupService.get().getSelf().equals(p)));
((ImageView) view.findViewById(R.id.activity_group_list_icon)).setImageResource(
CommonUtils.getIconByScreenSize(p.getScreenSize(), groupService.get().getSelf().equals(p)));

return view;
}

};
listview.setAdapter(adapter);
}

@Override
public void groupChanged() {
runOnUiThread(new Runnable() {
@Override
public void run() {
((ArrayAdapter) listview.getAdapter()).notifyDataSetChanged();
}
});
}

private int getIconByScreenSize(double screenSize, boolean blue) {
if (screenSize >= 7) {
return blue ? R.drawable.ic_computer_blue_24dp : R.drawable.ic_computer_black_24dp;
}
if (screenSize < 3) {
return blue ? R.drawable.ic_watch_blue_24dp : R.drawable.ic_watch_black_24dp;
} else {
return blue ? R.drawable.ic_phone_android_blue_24dp : R.drawable.ic_phone_android_black_24dp;
}
runOnUiThread(() -> ((ArrayAdapter) listview.getAdapter()).notifyDataSetChanged());
}

private TypedServiceConnection<MessagingService> messagingService = new TypedServiceConnection<>();

private TypedServiceConnection<GroupService> groupService = new TypedServiceConnection<GroupService>() {

@Override
public void onServiceConnected(GroupService service) {
createArrayAdapter();
service.addGroupChangeListener(GroupListActivity.this);
}

@Override
public void onServiceDisconnected(GroupService service) {
service.removeGroupChangeListener(GroupListActivity.this);
}
};

private TrustedDevice getDeviceFromGroupListView(View view) {
int position = listview.getPositionForView(view);
return (TrustedDevice) listview.getItemAtPosition(position);
Expand Down
@@ -0,0 +1,126 @@
package at.usmile.cormorant.framework.group;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.support.annotation.DrawableRes;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.security.acl.Group;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import at.usmile.cormorant.framework.R;
import at.usmile.cormorant.framework.common.CommonUtils;
import at.usmile.cormorant.framework.common.TypedServiceConnection;

/**
* Created by fhdwsse
*/
public class GroupMapActivity extends AppCompatActivity implements OnMapReadyCallback, GroupChangeListener {
public static final double MAP_PADDING_FACTOR = 0.15;
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_map);

bindService(new Intent(this, GroupService.class), groupService, Context.BIND_AUTO_CREATE);

MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
protected void onDestroy() {
if (groupService.isBound()) unbindService(groupService);
super.onDestroy();
}

@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
if(groupService.isBound()) setupMap(addMarkerForDevices(groupService.get().getGroup()));
}

private List<Marker> addMarkerForDevices(List<TrustedDevice> devices) {
List<Marker> createdMarkers = new LinkedList<>();
devices.forEach(eachDevice -> {
int iconByScreenSize = CommonUtils.getIconByScreenSize(eachDevice.getScreenSize(),
groupService.get().getSelf().equals(eachDevice));
BitmapDescriptor markerIcon = convertVectorToBitmap(iconByScreenSize);
Location deviceLoc = eachDevice.getLocation();

Marker mapMarker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(deviceLoc.getLatitude(), deviceLoc.getLongitude()))
.icon(markerIcon)
.anchor(0.5f, 0.5f)
.snippet(eachDevice.getDistanceToOtherDeviceGps() + "m")
.title(eachDevice.getDevice()));
createdMarkers.add(mapMarker);
});
return createdMarkers;
}

private void setupMap(List<Marker> markers) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

int padding = (int) (getResources().getDisplayMetrics().widthPixels * MAP_PADDING_FACTOR);
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

googleMap.setOnMapLoadedCallback(() -> googleMap.animateCamera(cu));
}

private BitmapDescriptor convertVectorToBitmap(@DrawableRes int id) {
Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null);
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}

private TypedServiceConnection<GroupService> groupService = new TypedServiceConnection<GroupService>() {

@Override
public void onServiceConnected(GroupService service) {
if(googleMap != null) setupMap(addMarkerForDevices(service.getGroup()));
service.addGroupChangeListener(GroupMapActivity.this);
}

@Override
public void onServiceDisconnected(GroupService service) {
service.removeGroupChangeListener(GroupMapActivity.this);
}
};

@Override
public void groupChanged() {
//TODO refresh Map
}
}

0 comments on commit ff9c9da

Please sign in to comment.