137 lines
4.4 KiB
Java
137 lines
4.4 KiB
Java
package com.avapp;
|
|
|
|
import static com.avapp.MainApplication.isAlfredEnabledFromFirebase;
|
|
|
|
import com.avapp.utils.AlfredFirebaseHelper;
|
|
import com.avapp.utils.FirebaseRemoteConfigHelper;
|
|
import com.facebook.react.ReactActivity;
|
|
import com.facebook.react.ReactActivityDelegate;
|
|
import com.facebook.react.ReactRootView;
|
|
import com.navi.alfred.AlfredManager;
|
|
import com.navi.alfred.utils.CommonUtilsKt;
|
|
|
|
|
|
import android.os.Bundle;
|
|
import android.view.MotionEvent;
|
|
|
|
|
|
public class MainActivity extends ReactActivity implements AlfredFirebaseHelper {
|
|
|
|
private static int appInForegroundCounter = 0;
|
|
private boolean cruiseApiCalled = false;
|
|
public static boolean hasAlfredRecordingStarted = false;
|
|
|
|
/**
|
|
* Returns the name of the main component registered from JavaScript. This is used to schedule
|
|
* rendering of the component.
|
|
*/
|
|
@Override
|
|
protected String getMainComponentName() {
|
|
return "AVAPP";
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(null);
|
|
FirebaseRemoteConfigHelper.setAlfredFirebaseHelper(this);
|
|
}
|
|
|
|
/**
|
|
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
|
|
* you can specify the renderer you wish to use - the new renderer (Fabric) or the old renderer
|
|
* (Paper).
|
|
*/
|
|
@Override
|
|
protected ReactActivityDelegate createReactActivityDelegate() {
|
|
return new MainActivityDelegate(this, getMainComponentName());
|
|
}
|
|
|
|
public static class MainActivityDelegate extends ReactActivityDelegate {
|
|
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
|
|
super(activity, mainComponentName);
|
|
}
|
|
|
|
@Override
|
|
protected ReactRootView createRootView() {
|
|
ReactRootView reactRootView = new ReactRootView(getContext());
|
|
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
|
|
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
|
|
return reactRootView;
|
|
}
|
|
|
|
@Override
|
|
protected boolean isConcurrentRootEnabled() {
|
|
// If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
|
|
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
|
|
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean dispatchTouchEvent(MotionEvent ev) {
|
|
if (isAlfredEnabledFromFirebase && AlfredManager.INSTANCE.isAlfredRecordingEnabled() && cruiseApiCalled) {
|
|
AlfredManager.INSTANCE.handleTouchEvent(ev, BuildConfig.APP_NAME, BuildConfig.APP_NAME);
|
|
}
|
|
return super.dispatchTouchEvent(ev);
|
|
}
|
|
|
|
@Override
|
|
protected void onStart() {
|
|
super.onStart();
|
|
appInForegroundCounter++;
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
if (isAlfredEnabledFromFirebase && !hasAlfredRecordingStarted) {
|
|
callCruiseAndStartAlfredRecording();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onStop() {
|
|
super.onStop();
|
|
appInForegroundCounter--;
|
|
if (isAlfredEnabledFromFirebase && AlfredManager.INSTANCE.isAlfredRecordingEnabled() && cruiseApiCalled) {
|
|
if (!isAppInForeground()) {
|
|
AlfredManager.INSTANCE.stopRecording();
|
|
hasAlfredRecordingStarted = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Boolean isAppInForeground() {
|
|
return appInForegroundCounter >= 1;
|
|
}
|
|
|
|
@Override
|
|
public void callCruiseAndStartAlfredRecording() {
|
|
if (cruiseApiCalled) {
|
|
startAlfredRecording();
|
|
} else {
|
|
AlfredManager.INSTANCE.getAlfredCruiseInfo(response -> {
|
|
cruiseApiCalled = true;
|
|
startAlfredRecording();
|
|
return null;
|
|
});
|
|
}
|
|
}
|
|
|
|
public void startAlfredRecording() {
|
|
if (AlfredManager.INSTANCE.isAlfredRecordingEnabled() && !hasAlfredRecordingStarted) {
|
|
try {
|
|
AlfredManager.INSTANCE.onActivityResumed(
|
|
BuildConfig.APP_NAME,
|
|
BuildConfig.APP_NAME,
|
|
this,
|
|
this.getApplicationContext()
|
|
);
|
|
hasAlfredRecordingStarted = true;
|
|
} catch (Exception e) {
|
|
CommonUtilsKt.log(e);
|
|
}
|
|
}
|
|
}
|
|
}
|