Skip to content

Commit

Permalink
Added example for positioning in the background using Android foregro…
Browse files Browse the repository at this point in the history
…und service
  • Loading branch information
vatolvan committed Apr 24, 2018
1 parent 2cb06cc commit a17c193
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Basic/build.gradle
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.application'


android {
compileSdkVersion 23
buildToolsVersion '25.0.0'
compileSdkVersion 26
buildToolsVersion '26.0.3'

// note: use gradle.properties in project root level to set your api credentials
def apiKey = project.properties['indoorAtlasApiKey'] ?: "api-key-not-set";
Expand All @@ -15,7 +15,7 @@ android {
defaultConfig {
applicationId "com.indooratlas.android.sdk.examples"
minSdkVersion 19
targetSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
// avoid getting errors from malformed string resources if secret contains '%' chars
Expand Down Expand Up @@ -50,16 +50,16 @@ android {
}

dependencies {
compile "com.indooratlas.android:indooratlas-android-sdk:2.7.0-beta-848@aar"
compile 'com.indooratlas.android:indooratlas-android-wayfinding:2.7.0-beta-9'
compile "com.indooratlas.android:indooratlas-android-sdk:2.7.0@aar"
compile 'com.indooratlas.android:indooratlas-android-wayfinding:2.7.1'

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.android.gms:play-services-maps:8.1.0'
compile 'com.google.maps.android:android-maps-utils:0.3.+'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.2.0'
compile 'com.pubnub:pubnub-android:3.7.5'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:design:26.1.0'

// for Open Street Map support
compile 'org.osmdroid:osmdroid-android:5.6.4'
Expand Down
9 changes: 9 additions & 0 deletions Basic/src/main/AndroidManifest.xml
Expand Up @@ -100,6 +100,15 @@
android:label="@string/example_googlemaps_indoor_title"
android:screenOrientation="portrait" />

<activity
android:name=".foregroundservice.MainActivity"
android:label="@string/example_foregroundservice_title"
android:screenOrientation="portrait" />

<service
android:name=".foregroundservice.ForegroundService"
android:exported="false" />

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.indooratlas.android.sdk.examples.fileprovider"
Expand Down
@@ -0,0 +1,154 @@
package com.indooratlas.android.sdk.examples.foregroundservice;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.indooratlas.android.sdk.IALocation;
import com.indooratlas.android.sdk.IALocationManager;
import com.indooratlas.android.sdk.IALocationRequest;
import com.indooratlas.android.sdk.examples.R;

public class ForegroundService extends Service {

public static final String MAIN_ACTION = "main_action";
public static final String PAUSE_ACTION = "pause_positioning_action";
public static final String START_ACTION = "start_positioning_action";
public static final String STOP_ACTION = "stop_positioning_action";
public static final String STARTFOREGROUND_ACTION = "startforeground";
public static final String STOPFOREGROUND_ACTION = "stopforeground";

public static final int NOTIFICATION_ID = 101;
private static final String NOTIFICATION_CHANNEL_ID = "example_notification_channel";

private static final String LOG_TAG = "IAForegroundExample";

private NotificationCompat.Builder mBuilder;

@Override
public void onCreate() {
super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel =
new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications",
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(notificationChannel);
}
mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle("IndoorAtlas Foreground Service Example")
.setTicker("IndoorAtlas Foreground Service Example")
.setSmallIcon(R.drawable.ic_launcher);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IALocation location = IALocation.from(intent);
if (location != null) {
Log.i(LOG_TAG, "Got IA Location: " + location);
mBuilder.setContentText(location.getLatitude() + ", " + location.getLongitude());
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
return START_STICKY;
}


if (intent.getAction().equals(ForegroundService.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(ForegroundService.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

Intent previousIntent = new Intent(this, ForegroundService.class);
previousIntent.setAction(ForegroundService.PAUSE_ACTION);
PendingIntent ppauseIntent = PendingIntent.getService(this, 0,
previousIntent, 0);

Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(ForegroundService.START_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);

Intent nextIntent = new Intent(this, ForegroundService.class);
nextIntent.setAction(ForegroundService.STOP_ACTION);
PendingIntent pstopIntent = PendingIntent.getService(this, 0,
nextIntent, 0);

Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

Notification notification = mBuilder
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_pause,
"Pause", ppauseIntent)
.addAction(android.R.drawable.ic_media_play, "Start",
pplayIntent)
.addAction(android.R.drawable.ic_media_ff, "Stop",
pstopIntent).build();


startForeground(NOTIFICATION_ID, notification);

} else if (intent.getAction().equals(ForegroundService.PAUSE_ACTION)) {
IALocationManager manager = IALocationManager.create(this);
PendingIntent stopIntent = PendingIntent.getService(this, 0,
new Intent(this, ForegroundService.class), 0);
manager.removeLocationUpdates(stopIntent);
manager.destroy();

Log.i(LOG_TAG, "Clicked Pause ");
} else if (intent.getAction().equals(ForegroundService.START_ACTION)) {
IALocationManager manager = IALocationManager.create(this);
PendingIntent requestIntent = PendingIntent.getService(this, 0,
new Intent(this, ForegroundService.class), 0);
manager.requestLocationUpdates(IALocationRequest.create(), requestIntent);
manager.destroy();

Log.i(LOG_TAG, "Clicked Start");
} else if (intent.getAction().equals(ForegroundService.STOP_ACTION)) {
stopForeground(true);
stopSelf();

Log.i(LOG_TAG, "Clicked Stop");
} else if (intent.getAction().equals(ForegroundService.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
return START_STICKY;
}

@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}


@Override
public IBinder onBind(Intent intent) {
// Used only in case if services are bound (Bound Services).
return null;
}
}
@@ -0,0 +1,66 @@
package com.indooratlas.android.sdk.examples.foregroundservice;

import android.Manifest;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.indooratlas.android.sdk.examples.R;
import com.indooratlas.android.sdk.examples.SdkExample;

@SdkExample(description = R.string.example_foregroundservice_description)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private final int CODE_PERMISSIONS = 1;

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

String[] neededPermissions = {
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
};
ActivityCompat.requestPermissions( this, neededPermissions, CODE_PERMISSIONS );

Button startButton = findViewById(R.id.button1);
Button stopButton = findViewById(R.id.button2);

startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
startIntent.setAction(ForegroundService.STARTFOREGROUND_ACTION);
startService(startIntent);
break;
case R.id.button2:
Intent stopIntent = new Intent(MainActivity.this, ForegroundService.class);
stopIntent.setAction(ForegroundService.STOPFOREGROUND_ACTION);
startService(stopIntent);
break;

default:
break;
}

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

}
}
3 changes: 3 additions & 0 deletions Basic/src/main/res/values/strings.xml
Expand Up @@ -95,5 +95,8 @@
<string name="example_wayfinding_description">Demonstrates usage of IndoorAtlas Wayfinding</string>
<string name="example_wayfinding_title">12. Wayfinding</string>

<string name="example_foregroundservice_description">Demonstrates IndoorAtlas background positioning using a foreground service.</string>
<string name="example_foregroundservice_title">14. Foreground Service</string>


</resources>
8 changes: 8 additions & 0 deletions build.gradle
Expand Up @@ -3,6 +3,10 @@ buildscript {

repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}

dependencies {
Expand All @@ -28,5 +32,9 @@ allprojects {
// needed for OSMBonusPack (Open Street Map example)
url "https://jitpack.io"
}
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
}

0 comments on commit a17c193

Please sign in to comment.