Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map integration #5

Merged
merged 8 commits into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.github.erdalgns:FloatingView:1.7b'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
5 changes: 5 additions & 0 deletions client/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

<application
Expand All @@ -25,6 +26,10 @@
android:name=".HUD"
android:enabled="true"
android:exported="true"></service>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>

</manifest>
121 changes: 63 additions & 58 deletions client/app/src/main/java/com/xbenjii/pokemock/HUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;

import jp.co.recruit_lifestyle.android.floatingview.FloatingViewListener;
import jp.co.recruit_lifestyle.android.floatingview.FloatingViewManager;
Expand All @@ -28,18 +34,19 @@ public class HUD extends Service implements FloatingViewListener {

private static final String TAG = "HUD";

static final int MSG_REGISTER_CLIENT = 1;
static final int MSG_SAVE_DATA = 2;
static final int MSG_LOAD_DATA = 3;

private String mockLocationProvider = LocationManager.GPS_PROVIDER;
private LocationManager mLocationManager;
private Location currentLocation = new Location(mockLocationProvider);

private boolean isSystemApp = false;

private FloatingViewManager mFloatingViewManager;

@Override
public IBinder onBind(Intent intent) {
return null;
}
private SharedPreferencesStorage storage;

private LatLng lastCoordinates;

private final LocationListener locationListener = new LocationListener() {
@Override
Expand All @@ -61,8 +68,8 @@ public void onProviderDisabled(String s) {}
public void onCreate() {
super.onCreate();

isSystemApp = (getApplicationInfo().flags & (ApplicationInfo.FLAG_SYSTEM
| ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
this.storage = new SharedPreferencesStorage(this);
lastCoordinates = storage.getLastCoordinates();

int density = getResources().getDisplayMetrics().densityDpi;

Expand All @@ -76,21 +83,10 @@ public void onCreate() {

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if(!isSystemApp) {
try {
mLocationManager.addTestProvider(mockLocationProvider, true, true, true, false, true,
true, true, 0, 5);
mLocationManager.setTestProviderEnabled(mockLocationProvider, true);
mLocationManager.setTestProviderStatus(mockLocationProvider, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
} catch (Exception e) {
Toast.makeText(HUD.this, "You haven't set PokeMock as the active mock location app.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return;
}
}

mLocationManager.addTestProvider(mockLocationProvider, true, true, true, false, true,
true, true, 0, 5);
mLocationManager.setTestProviderEnabled(mockLocationProvider, true);
mLocationManager.setTestProviderStatus(mockLocationProvider, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(mockLocationProvider, 5, 15, locationListener);
Expand All @@ -100,8 +96,8 @@ public void onCreate() {
currentLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
currentLocation.setAccuracy(50);
currentLocation.setAltitude(5);
currentLocation.setLatitude(53.7282337);
currentLocation.setLongitude(-1.8642777);
currentLocation.setLatitude(lastCoordinates.latitude);
currentLocation.setLongitude(lastCoordinates.longitude);

final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Expand All @@ -120,52 +116,61 @@ public void onCreate() {
public void OnMoved(double diffLongitude, double diffLatitude) {
currentLocation.setLongitude(currentLocation.getLongitude() + diffLongitude);
currentLocation.setLatitude(currentLocation.getLatitude() + diffLatitude);
currentLocation.setTime(System.currentTimeMillis());
currentLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
if(isSystemApp) {
int value = setMockLocationSettings();
try {
mLocationManager.setTestProviderLocation(mockLocationProvider, currentLocation);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
restoreMockLocationSettings(value);
}
} else {
mLocationManager.setTestProviderLocation(mockLocationProvider, currentLocation);
}
}

@Override
public void OnReleased() {

}
});

new Thread(new Runnable() {
@Override
public void run() {
while (true) {
currentLocation.setTime(System.currentTimeMillis());
currentLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
mLocationManager.setTestProviderLocation(mockLocationProvider, currentLocation);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}

@Override
public void onFinishFloatingView() {

}

private int setMockLocationSettings() {
int value = 1;
try {
value = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION);
Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 1);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return value;
}
final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler.


private void restoreMockLocationSettings(int restoreValue) {
try {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, restoreValue);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
class IncomingHandler extends Handler { // Handler of incoming messages from clients.
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REGISTER_CLIENT:
//currentLocation
break;
case MSG_SAVE_DATA:
storage.saveCoordinates(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()));
break;
case MSG_LOAD_DATA:
lastCoordinates = storage.getLastCoordinates();
currentLocation.setLongitude(lastCoordinates.longitude);
currentLocation.setLatitude(lastCoordinates.latitude);
break;
default:
super.handleMessage(msg);
}
}
}
}
Loading