Skip to content

Commit f126be4

Browse files
authored
Merge pull request #12 from freshbits/release/2.3.4
Release 2.3.4.post0
2 parents 1dcca63 + 096408c commit f126be4

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

android-example-app/app-kotlin/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
plugins {
22
id 'com.android.application'
33
id 'kotlin-android'
4-
id 'kotlin-android-extensions'
54
}
65

76
android {

android-example-app/app-kotlin/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
android:theme="@style/AppTheme" >
1212
<activity
1313
android:name="ch.freshbits.pathshare.example.MainActivity"
14-
android:label="@string/app_name"
1514
android:exported="true">
1615
<intent-filter>
1716
<action android:name="android.intent.action.MAIN" />

android-example-app/app-kotlin/src/main/java/ch/freshbits/pathshare/example/MainActivity.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package ch.freshbits.pathshare.example
22

3+
import android.app.AlarmManager
34
import android.content.Context
5+
import android.content.Intent
46
import android.content.SharedPreferences
7+
import android.os.Build
58
import android.os.Bundle
9+
import android.provider.Settings
610
import android.util.Log
711
import android.view.View
812
import android.widget.Button
@@ -146,10 +150,11 @@ class MainActivity : AppCompatActivity() {
146150
private fun joinSession() {
147151
if (session.isExpired) return
148152

149-
if (hasLocationPermission()) {
153+
if (hasLocationPermission() && hasAlarmPermission()) {
150154
performJoinSession()
151155
} else {
152-
requestLocationPermission()
156+
if (!hasLocationPermission()) { requestLocationPermission() }
157+
if (!hasAlarmPermission()) { requestAlarmPermission() }
153158
}
154159
}
155160

@@ -222,6 +227,26 @@ class MainActivity : AppCompatActivity() {
222227
return PermissionRequester.hasPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
223228
}
224229

230+
private fun requestAlarmPermission() {
231+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
232+
return
233+
}
234+
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
235+
startActivity(intent)
236+
}
237+
238+
private fun hasAlarmPermission(): Boolean {
239+
return Build.VERSION.SDK_INT < Build.VERSION_CODES.S || getAlarmManager().canScheduleExactAlarms()
240+
}
241+
242+
private fun getAlarmManager(): AlarmManager {
243+
return getContext().getSystemService(ALARM_SERVICE) as AlarmManager
244+
}
245+
246+
private fun getContext(): Context {
247+
return Pathshare.client().context
248+
}
249+
225250
private fun saveSessionIdentifier() {
226251
val editor = preferences().edit()
227252
editor.putString(SESSION_ID_KEY, session.identifier)

android-example-app/app/src/main/java/ch/freshbits/pathshare/example/MainActivity.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import android.Manifest;
44
import android.app.Activity;
5+
import android.app.AlarmManager;
56
import android.content.Context;
7+
import android.content.Intent;
68
import android.content.SharedPreferences;
79
import android.content.pm.PackageManager;
10+
import android.os.Build;
811
import android.os.Bundle;
12+
import android.provider.Settings;
913
import android.util.Log;
1014
import android.widget.Button;
1115
import android.widget.Toast;
@@ -131,10 +135,11 @@ public void onError(@NonNull Throwable throwable) {
131135
private void joinSession() {
132136
if (getSession().isExpired()) { return; }
133137

134-
if (hasLocationPermission()) {
138+
if (hasLocationPermission() && hasAlarmPermission()) {
135139
performJoinSession();
136140
} else {
137-
requestLocationPermission();
141+
if (!hasLocationPermission()) { requestLocationPermission(); }
142+
if (!hasAlarmPermission()) { requestAlarmPermission(); }
138143
}
139144
}
140145

@@ -171,6 +176,27 @@ private boolean hasLocationPermission() {
171176
return PermissionRequester.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
172177
}
173178

179+
private void requestAlarmPermission() {
180+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
181+
return;
182+
}
183+
184+
Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
185+
startActivity(intent);
186+
}
187+
188+
private boolean hasAlarmPermission() {
189+
return Build.VERSION.SDK_INT < Build.VERSION_CODES.S || getAlarmManager().canScheduleExactAlarms();
190+
}
191+
192+
private AlarmManager getAlarmManager() {
193+
return (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
194+
}
195+
196+
private Context getContext() {
197+
return Pathshare.client().getContext();
198+
}
199+
174200
@Override
175201
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
176202
if (requestCode == TAG_PERMISSIONS_REQUEST_LOCATION_ACCESS) {

android-example-app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.7.22'
4+
ext.kotlin_version = '1.9.22'
55

66
repositories {
77
mavenCentral()
@@ -24,6 +24,6 @@ allprojects {
2424
}
2525
}
2626

27-
task clean(type: Delete) {
27+
tasks.register('clean', Delete) {
2828
delete rootProject.buildDir
2929
}

0 commit comments

Comments
 (0)