Skip to content

Commit 72c8d83

Browse files
author
Jarvis Luong
committed
initial commit
0 parents  commit 72c8d83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+9920
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# node.js
6+
#
7+
node_modules/
8+
npm-debug.log
9+
yarn-error.log
10+
11+
# Xcode
12+
#
13+
build/
14+
*.pbxuser
15+
!default.pbxuser
16+
*.mode1v3
17+
!default.mode1v3
18+
*.mode2v3
19+
!default.mode2v3
20+
*.perspectivev3
21+
!default.perspectivev3
22+
xcuserdata
23+
*.xccheckout
24+
*.moved-aside
25+
DerivedData
26+
*.hmap
27+
*.ipa
28+
*.xcuserstate
29+
project.xcworkspace
30+
31+
# Android/IntelliJ
32+
#
33+
build/
34+
.idea
35+
.gradle
36+
local.properties
37+
*.iml
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore

.npmignore

Whitespace-only changes.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# react-native-android-location-setting-enabler
2+
3+
## Getting started
4+
5+
`$ npm install react-native-android-location-setting-enabler --save`
6+
7+
### Mostly automatic installation
8+
9+
`$ react-native link react-native-android-location-setting-enabler`
10+
11+
### Manual installation
12+
13+
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
14+
- Add `import com.reactron.react.settingenabler.RNAndroidLocationSettingEnablerPackage;` to the imports at the top of the file
15+
- Add `new RNAndroidLocationSettingEnablerPackage()` to the list returned by the `getPackages()` method
16+
2. Append the following lines to `android/settings.gradle`:
17+
```
18+
include ':react-native-android-location-setting-enabler'
19+
project(':react-native-android-location-setting-enabler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-location-setting-enabler/android')
20+
```
21+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
22+
```
23+
compile project(':react-native-android-location-setting-enabler')
24+
```
25+
26+
27+
## Usage
28+
```javascript
29+
import RNAndroidLocationSettingEnabler from 'react-native-android-location-setting-enabler';
30+
31+
// TODO: What to do with the module?
32+
RNAndroidLocationSettingEnabler;
33+
```

android/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
README
2+
======
3+
4+
If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm:
5+
6+
1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed
7+
2. Be sure to have a `local.properties` file in this folder that points to the Android SDK and NDK
8+
```
9+
ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
10+
sdk.dir=/Users/{username}/Library/Android/sdk
11+
```
12+
3. Delete the `maven` folder
13+
4. Run `sudo ./gradlew installArchives`
14+
5. Verify that latest set of generated files is in the maven folder with the correct version number

android/build.gradle

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
buildscript {
2+
ext.safeExtGet = {prop, fallback ->
3+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
4+
}
5+
repositories {
6+
google()
7+
jcenter()
8+
}
9+
10+
dependencies {
11+
// Matches recent template from React Native (0.60)
12+
// https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L16
13+
classpath("com.android.tools.build:gradle:${safeExtGet('gradlePluginVersion', '3.4.1')}")
14+
}
15+
}
16+
17+
apply plugin: 'com.android.library'
18+
apply plugin: 'maven'
19+
20+
// Matches values in recent template from React Native (0.59)
21+
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L5-L9
22+
def DEFAULT_COMPILE_SDK_VERSION = 28
23+
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
24+
def DEFAULT_MIN_SDK_VERSION = 16
25+
def DEFAULT_TARGET_SDK_VERSION = 28
26+
27+
android {
28+
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
29+
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
30+
31+
defaultConfig {
32+
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
33+
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
34+
versionCode 1
35+
versionName "1.0"
36+
}
37+
lintOptions {
38+
abortOnError false
39+
}
40+
}
41+
42+
repositories {
43+
maven {
44+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
45+
// Matches recent template from React Native (0.59)
46+
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L30
47+
url "$projectDir/../node_modules/react-native/android"
48+
}
49+
mavenCentral()
50+
}
51+
52+
dependencies {
53+
implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}"
54+
implementation 'com.google.android.gms:play-services-location:+'
55+
}
56+
57+
def configureReactNativePom(def pom) {
58+
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
59+
60+
pom.project {
61+
name packageJson.title
62+
artifactId packageJson.name
63+
version = packageJson.version
64+
group = "com.reactlibrary"
65+
description packageJson.description
66+
url packageJson.repository.baseUrl
67+
68+
licenses {
69+
license {
70+
name packageJson.license
71+
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
72+
distribution 'repo'
73+
}
74+
}
75+
76+
developers {
77+
developer {
78+
id packageJson.author.username
79+
name packageJson.author.name
80+
}
81+
}
82+
}
83+
}
84+
85+
afterEvaluate { project ->
86+
87+
task androidJavadoc(type: Javadoc) {
88+
source = android.sourceSets.main.java.srcDirs
89+
classpath += files(android.bootClasspath)
90+
classpath += files(project.getConfigurations().getByName('compile').asList())
91+
include '**/*.java'
92+
}
93+
94+
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
95+
classifier = 'javadoc'
96+
from androidJavadoc.destinationDir
97+
}
98+
99+
task androidSourcesJar(type: Jar) {
100+
classifier = 'sources'
101+
from android.sourceSets.main.java.srcDirs
102+
include '**/*.java'
103+
}
104+
105+
android.libraryVariants.all { variant ->
106+
def name = variant.name.capitalize()
107+
task "jar${name}"(type: Jar, dependsOn: variant.javaCompile) {
108+
from variant.javaCompile.destinationDir
109+
}
110+
}
111+
112+
artifacts {
113+
archives androidSourcesJar
114+
archives androidJavadocJar
115+
}
116+
117+
task installArchives(type: Upload) {
118+
configuration = configurations.archives
119+
repositories.mavenDeployer {
120+
// Deploy to react-native-event-bridge/maven, ready to publish to npm
121+
repository url: "file://${projectDir}/../android/maven"
122+
123+
configureReactNativePom pom
124+
}
125+
}
126+
}

android/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.reactron.react.settingenabler">
3+
4+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.reactron.react.settingenabler;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.content.IntentSender;
6+
7+
import com.facebook.react.bridge.BaseActivityEventListener;
8+
import com.google.android.gms.common.api.ApiException;
9+
import com.google.android.gms.common.api.ResolvableApiException;
10+
import com.google.android.gms.location.LocationRequest;
11+
import com.google.android.gms.location.LocationServices;
12+
import com.google.android.gms.location.LocationSettingsRequest;
13+
14+
import com.facebook.react.bridge.Promise;
15+
import com.facebook.react.bridge.ReactApplicationContext;
16+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
17+
import com.facebook.react.bridge.ActivityEventListener;
18+
import com.facebook.react.bridge.ReactMethod;
19+
import com.facebook.react.module.annotations.ReactModule;
20+
import com.google.android.gms.location.LocationSettingsResponse;
21+
import com.google.android.gms.location.LocationSettingsStates;
22+
import com.google.android.gms.location.LocationSettingsStatusCodes;
23+
import com.google.android.gms.tasks.OnCompleteListener;
24+
import com.google.android.gms.tasks.Task;
25+
26+
import javax.annotation.Nonnull;
27+
28+
@ReactModule(name = RNAndroidLocationSettingEnablerModule.NAME)
29+
public class RNAndroidLocationSettingEnablerModule extends ReactContextBaseJavaModule {
30+
private static final int REQUEST_CHECK_SETTINGS = 1;
31+
32+
private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST";
33+
private static final String E_SETTINGS_CHANGE_UNAVAILABLE = "E_SETTINGS_CHANGE_UNAVAILABLE";
34+
private static final String E_USER_CANCELED = "E_USER_CANCELED";
35+
36+
private final ReactApplicationContext mContext;
37+
38+
private Promise mLocationSettingPromise;
39+
40+
public RNAndroidLocationSettingEnablerModule(@Nonnull ReactApplicationContext reactContext) {
41+
super(reactContext);
42+
mContext = reactContext;
43+
reactContext.addActivityEventListener(mActivityEventListener);
44+
}
45+
46+
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
47+
48+
@Override
49+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
50+
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
51+
if (requestCode == REQUEST_CHECK_SETTINGS && mLocationSettingPromise != null) {
52+
switch (resultCode) {
53+
case Activity.RESULT_OK:
54+
// All required changes were successfully made
55+
mLocationSettingPromise.resolve(null);
56+
57+
break;
58+
case Activity.RESULT_CANCELED:
59+
// The user was asked to change settings, but chose not to
60+
mLocationSettingPromise.reject(
61+
E_USER_CANCELED,
62+
"The user was asked to change settings, but chose not to");
63+
break;
64+
default:
65+
break;
66+
}
67+
mLocationSettingPromise = null;
68+
69+
}
70+
}
71+
72+
@Override
73+
public void onNewIntent(Intent intent) {
74+
super.onNewIntent(intent);
75+
}
76+
};
77+
78+
@ReactMethod()
79+
public void checkAndEnableGPSAndBLE(Promise promise) {
80+
Activity currentActivity = getCurrentActivity();
81+
82+
if (currentActivity == null) {
83+
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist");
84+
return;
85+
}
86+
87+
LocationRequest mLocationRequestHighAccuracy = LocationRequest
88+
.create()
89+
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
90+
LocationRequest mLocationRequestBalancedPowerAccuracy = LocationRequest
91+
.create()
92+
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
93+
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
94+
.addLocationRequest(mLocationRequestHighAccuracy)
95+
.addLocationRequest(mLocationRequestBalancedPowerAccuracy)
96+
.setNeedBle(true);
97+
Task<LocationSettingsResponse> task =
98+
LocationServices
99+
.getSettingsClient(mContext.getCurrentActivity())
100+
.checkLocationSettings(builder.build());
101+
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
102+
@Override
103+
public void onComplete(Task<LocationSettingsResponse> task) {
104+
try {
105+
LocationSettingsResponse response = task.getResult(ApiException.class);
106+
// All location settings are satisfied. The client can initialize location
107+
// requests here.
108+
promise.resolve(null);
109+
} catch (ApiException exception) {
110+
111+
switch (exception.getStatusCode()) {
112+
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
113+
// Location settings are not satisfied. But could be fixed by showing the
114+
// user a dialog.
115+
mLocationSettingPromise = promise;
116+
try {
117+
// Cast to a resolvable exception.
118+
ResolvableApiException resolvable = (ResolvableApiException) exception;
119+
// Show the dialog by calling startResolutionForResult(),
120+
// and check the result in onActivityResult().
121+
resolvable.startResolutionForResult(
122+
mContext.getCurrentActivity(),
123+
REQUEST_CHECK_SETTINGS);
124+
} catch (IntentSender.SendIntentException e) {
125+
// Ignore the error.
126+
} catch (ClassCastException e) {
127+
// Ignore, should be an impossible error.
128+
}
129+
break;
130+
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
131+
// Location settings are not satisfied. However, we have no way to fix the
132+
// settings so we won't show the dialog.
133+
promise.reject(
134+
E_SETTINGS_CHANGE_UNAVAILABLE,
135+
"Location settings are not satisfied. " +
136+
"However, we have no way to fix the settings " +
137+
"so we won't show the dialog.");
138+
break;
139+
}
140+
}
141+
}
142+
});
143+
}
144+
145+
146+
@Nonnull
147+
@Override
148+
public String getName() {
149+
return "RNAndroidLocationSettingEnablerModule";
150+
}
151+
}

0 commit comments

Comments
 (0)