Files
instabug-reactnative/android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java
T

205 lines
6.2 KiB
Java
Raw Normal View History

2016-10-04 11:41:36 +02:00
package com.instabug.reactlibrary;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
2016-10-18 12:15:36 +02:00
import android.app.Application;
2016-10-04 11:41:36 +02:00
import com.instabug.library.Instabug;
2016-10-18 11:09:06 +02:00
import com.instabug.library.InstabugColorTheme;
import com.instabug.library.InstabugTrackingDelegate;
import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.invocation.InstabugInvocationEvent;
import com.instabug.library.invocation.InstabugInvocationMode;
import com.instabug.library.invocation.util.InstabugFloatingButtonEdge;
2016-10-04 11:41:36 +02:00
import java.util.Locale;
2016-10-20 16:24:17 +02:00
import java.util.HashMap;
2016-10-04 11:41:36 +02:00
import android.net.Uri;
2016-10-20 16:24:17 +02:00
import java.util.Map;
2016-10-04 11:41:36 +02:00
public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
private Instabug mInstabug;
2016-10-17 19:58:43 +02:00
private String mAndroidApplicationToken;
private Instabug.Builder mBuilder;
2016-10-18 14:10:19 +02:00
private Application androidApplication;
2016-10-04 11:41:36 +02:00
2016-10-18 14:10:19 +02:00
public RNInstabugReactnativeModule(ReactApplicationContext reactContext, Application androidApplication) {
2016-10-04 11:41:36 +02:00
super(reactContext);
2016-10-18 14:25:27 +02:00
this.androidApplication = androidApplication;
2016-10-04 11:41:36 +02:00
}
@Override
public String getName() {
2016-10-20 16:24:17 +02:00
return "Instabug";
2016-10-04 11:41:36 +02:00
}
2016-10-17 19:58:43 +02:00
/**
* start Instabug with default opetions
* default Invocation event Floating button
* @param androidApplicationToken
*/
@ReactMethod
2016-10-20 16:24:17 +02:00
public void startWithToken(String androidApplicationToken,InstabugInvocationEvent invocationEvent)
2016-10-17 19:58:43 +02:00
{
this.mAndroidApplicationToken = androidApplicationToken;
2016-10-18 14:34:15 +02:00
mInstabug = new Instabug.Builder(androidApplication, mAndroidApplicationToken)
.setEmailFieldRequired(false)
.setFloatingButtonOffsetFromTop(400)
.setTheme(InstabugColorTheme.InstabugColorThemeLight)
2016-10-20 16:24:17 +02:00
.setInvocationEvent(invocationEvent)
2016-10-18 14:34:15 +02:00
.setIntroMessageEnabled(false)
.build();
2016-10-17 19:58:43 +02:00
}
2016-10-04 11:41:36 +02:00
/**
* Adds tag(s) to issues before sending them
*
* @param tags
*/
@ReactMethod
public void addTags(String tags) {
try {
String[] result = tags.split(",");
mInstabug.resetTags(); //clear last commit tags
mInstabug.addTags(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Change Locale of Instabug UI elements(defaults to English)
*
* @param languageTag
*/
@ReactMethod
public void changeLocale(String languageTag) {
try {
switch (languageTag) {
case "CHINA":
case "CHINESE":
case "PRC":
case "SIMPLIFIED_CHINESE":
mInstabug.changeLocale(Locale.CHINESE);
break;
case "TAIWAN":
case "TRADITIONAL_CHINESE":
mInstabug.changeLocale(Locale.TAIWAN);
break;
case "ENGLISH":
mInstabug.changeLocale(Locale.ENGLISH);
break;
case "UK":
mInstabug.changeLocale(Locale.UK);
break;
case "US":
mInstabug.changeLocale(Locale.US);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@ReactMethod
public void report(String value) {
2016-10-17 20:43:28 +02:00
InstabugInvocationMode mode = InstabugInvocationMode.PROMPT_OPTION;
2016-10-04 11:41:36 +02:00
if (value.equals("bug")) {
2016-10-17 20:43:28 +02:00
mode = InstabugInvocationMode.NEW_BUG;
2016-10-04 11:41:36 +02:00
} else if (value.equals("feedback")) {
2016-10-17 20:43:28 +02:00
mode = InstabugInvocationMode.NEW_FEEDBACK;
2016-10-04 11:41:36 +02:00
}
mInstabug.invoke(mode);
}
/**
* The file at filePath will be uploaded along upcoming reports with the name fileNameWithExtension
*
* @param fileUri
* @param fileNameWithExtension
*/
@ReactMethod
public void setFileAttachment(String fileUri, String fileNameWithExtension) {
try {
Uri uri = Uri.parse(fileUri);
mInstabug.setFileAttachment(uri, fileNameWithExtension);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* If your app already acquires the user's email address and you provide it to this method,
* Instabug will pre-fill the user email in reports.
*
* @param userEmail
*/
@ReactMethod
public void setUserEmail(String userEmail) {
try {
mInstabug.setUserEmail(userEmail);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets the user name that is used in the dashboard's contacts.
*
* @param username
*/
@ReactMethod
public void setUsername(String username) {
try {
mInstabug.setUsername(username);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Adds specific user data that you need to be added to the reports
*
* @param userData
*/
@ReactMethod
public void setUserData(String userData) {
try {
mInstabug.setUserData(userData);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Call this method to display the discovery dialog explaining the shake gesture or the two
* finger swipe gesture, if you've enabled it.
* i.e: This method is automatically called on first run of the application
*/
@ReactMethod
public void showIntroMessage() {
try {
mInstabug.showIntroMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
2016-10-20 16:24:17 +02:00
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("invocationEventFloatingButton", InstabugInvocationEvent.FLOATING_BUTTON);
constants.put("invocationEventTwoFingersSwipeLeft", InstabugInvocationEvent.TWO_FINGER_SWIPE_LEFTt);
constants.put("invocationEventScreenshot", InstabugInvocationEvent.SCREENSHOT_GESTURE);
constants.put("invocationEventShake", InstabugInvocationEvent.SHAKE);
constants.put("invocationEventNone", InstabugInvocationEvent.NONE);
return constants;
}
2016-10-04 11:41:36 +02:00
}