Skip to content

Commit

Permalink
Merge pull request #35 from sematext/rkuc_upgrade_to_work_with_newest…
Browse files Browse the repository at this point in the history
…_android_sdk

Upgrade the library to work with Android SDK 30
  • Loading branch information
Rafał Kuć authored Jun 25, 2021
2 parents f255b63 + 538dff8 commit 526d734
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 96 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Add the following gradle dependency to your Android application:
```
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.sematext:sematext-logsene-android:2.1.1'
compile 'com.github.sematext:sematext-logsene-android:3.3.0'
}
```

Expand Down Expand Up @@ -90,8 +90,7 @@ Add the following inside the application manifest (inside `<application>`):
* **LogseneRequiresUnmeteredNetwork**: if logs should be shipped only on unmetered network connection
* **LogseneRequiresDeviceIdle**: if logs should be shipped only when device is idle
* **LogseneRequiresBatteryNotLow**: if logs should be shipped only when battery is not low
* **LogseneAutomaticLocationEnabled**: if logs should be automatically enriched with device location information

* **LogseneAutomaticLocationEnabled**: if logs should be automatically enriched with device location information. See the **Enriching Logs with Location** section for more details.

Example Application
-------------------
Expand Down Expand Up @@ -216,7 +215,9 @@ logsene.info("Hello World with Location!", 53.08, 23.08);
It is also possible to tell the library to automatically retrieve location from the device. In such case the `Logsene` object instance needs to be created in the following way:

```
new Logsene(this, true);
Logsene.init(this);
Logsene logsene = Logsene.getInstance();
logsene.initializeLocationListener(context);
```

Because of the automatic retrieval of location from the device the `ACCESS_COARSE_LOCATION` and `ACCESS_FINE_LOCATION` permissions are needed:
Expand All @@ -226,6 +227,8 @@ Because of the automatic retrieval of location from the device the `ACCESS_COARS
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
```

It is also crucial to call the `initializeLocationListener(Context context)` method of the `Logsene` object after the user gives the application the permission to use location data. Otherwise it will not work. Example on how that can be done is included in the `com.sematext.logseneandroid.MainActivity` class.

### JUL

If your application uses JUL (java.util.logging) loggers, you can use the provided custom Handler for Logsene. You will need to configure it through code, since we need a reference to the `Context` object. If you configure your loggers to use the `LogseneHandler`, all log messages will be sent to Sematext for centralized logging.
Expand Down Expand Up @@ -286,14 +289,18 @@ public class TestApplication extends Application {

Don't forget to declare the custom application class in your manifest (with `android:name` on `application` element).

Migrating to version 3.1 from previous versions
-----------------------------------------------

Starting from version **3.1.0** Logsene Android SDK is no longer displaying the prompt to enable location services and provide appropriate rights. It is now the responsibility of the application itself.

Migrating to version 3.x from 2.x
---------------------------------

Starting from version **3.0.0** Logsene Android SDK contains backwards incompatible changes related to how it is initialized. You no longer need to create the `Logsene` object everytime you would like to use it for logging. You no longer create the `Logsene` object itself like this:

```java
Logsene logsene = new Logsene(context, true);

```

Instead you call the `init(Context)` method once and retrieve the instance of `Logsene` object later:
Expand Down
21 changes: 12 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 30
defaultConfig {
applicationId "com.sematext.logseneapplication"
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "3.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionName "3.3.0"
multiDexEnabled true
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -20,11 +21,13 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.core:core-ktx:1.0.0'

implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.core:core-ktx:1.5.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation project(path: ':logseneandroid')
}
9 changes: 4 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sematext.logseneapplication">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.sematext.logseneapplication;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sematext.logseneandroid.Logsene;
import com.sematext.logseneandroid.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {
public static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private int numberOfRepeatedMessages = 1000;
private Thread unlimitedLoop = null;
private Logsene logsene;
Expand All @@ -22,11 +33,16 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Logsene.init(this);
Logsene.init(getApplicationContext());
logsene = logsene.getInstance();

Log.e("INFO", "Android version: " + Build.VERSION.RELEASE);

// ask for location permissions
if (!Utils.checkLocationPermissions(getApplicationContext())) {
startLocationPermissionRequest();
}

try {
// Set some default meta properties to be sent with each message
JSONObject meta = new JSONObject();
Expand Down Expand Up @@ -140,4 +156,28 @@ public void onClick(View v) {
Log.e("myapp", "Unable to construct json", e);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions,
@NonNull @NotNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
Log.e("INFO", "Cancelled permissions request");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("INFO", "Location permissions granted");
logsene.initializeLocationListener(getApplicationContext());
} else {
Log.e("INFO", "Permissions not granted, location services not started");
}
}
}

private void startLocationPermissionRequest() {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}, REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
buildscript {
repositories {
google()
jcenter()
mavenCentral()

}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -18,7 +18,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
23 changes: 10 additions & 13 deletions logseneandroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.sematext'

android {
compileSdkVersion 28
compileSdkVersion 30

defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 4
versionName "3.0.0"
targetSdkVersion 30

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}

Expand All @@ -21,17 +19,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'androidx.work:work-runtime:2.0.1'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'androidx.work:work-runtime:2.5.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
2 changes: 1 addition & 1 deletion logseneandroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sematext.logseneandroid" >

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import androidx.work.Constraints;
Expand Down Expand Up @@ -103,9 +104,6 @@ public static void init(Context context) {
logsene.config(context);
logsene.preflightQueue = new SqliteObjectQueue(context, logsene.maxOfflineMessages);
logsene.lastScheduled = SystemClock.elapsedRealtime();
if (logsene.automaticLocationEnabled) {
logsene.locationListener = new LogseneLocationListener(context);
}
logsene.isActive = true;
logsene.schedulePeriodicWorker();

Expand Down Expand Up @@ -134,6 +132,16 @@ public static Logsene getInstance() {
throw new NullPointerException("Logsene is not initialized");
}

/**
* Enables location listener. Should be run after user gives permission for accessing location.
* @param context Context
*/
public void initializeLocationListener(Context context) {
if (automaticLocationEnabled) {
locationListener = new LogseneLocationListener(context);
}
}

/**
* Sets the default meta properties. These will be included with every request.
* @param metadata the default meta properties, use null to disable.
Expand Down Expand Up @@ -546,7 +554,7 @@ private void enrich(JSONObject obj) {
// create a location out of lat and lon fields
if (obj.has("lat") && obj.has("lon")) {
JSONObject geo = new JSONObject();
geo.put("location", String.format("%.2f,%.2f",
geo.put("location", String.format(Locale.ENGLISH, "%.2f,%.2f",
obj.getDouble("lat"), obj.getDouble("lon")));
obj.remove("lat");
obj.remove("lon");
Expand Down
Loading

0 comments on commit 526d734

Please sign in to comment.