Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional config available for UpgradeManager instance #566

Merged
merged 6 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions android/app/src/main/java/com/mapeo/AppInfoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,22 @@
public class AppInfoModule extends ReactContextBaseJavaModule {
private static ReactApplicationContext reactContext;

static final String MODULE_NAME = "AppInfo";

private static final String sourceDir = "sourceDir";

AppInfoModule(ReactApplicationContext context) {
super(context);
reactContext = context;
}

@Override
public String getName() {
return MODULE_NAME;
return "AppInfo";
}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
ApplicationInfo ai = getReactApplicationContext().getApplicationInfo();
constants.put(sourceDir, ai.sourceDir);
constants.put("sourceDir", ai.sourceDir);
constants.put("minSdkVersion", ai.minSdkVersion);
return constants;
}
}
35 changes: 18 additions & 17 deletions src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const releaseStage = prereleaseComponents
const log = debug("mapeo-core:index");
const PORT = 9081;
const status = new ServerStatus();
let storagePath;
let server;

// This is nastily circular: we need an instance of status for the constructor
Expand Down Expand Up @@ -68,26 +67,24 @@ status.startHeartbeat();
* card. It is a shared folder that the user can access. The data folder
* accessible through rnBridge.app.datadir() is not accessible by the user.
*
* Other config data such as the path to the APK file is only available in the
* React Native process, so we pass that to the backend here
*
* We need to wait for the React Native process to tell us where the folder is.
* This code supports re-starting the server with a different folder if
* necessary (we probably shouldn't do that)
*/
rnBridge.channel.on("config", config => {
log("storagePath", config.storagePath);
if (config.storagePath === storagePath) return;
const prevStoragePath = storagePath;
if (server)
stopServer(() => {
log(`closed server with:
storagePath: ${prevStoragePath}`);
});
storagePath = config.storagePath;
rnBridge.channel.once("config", config => {
log("config", config);
if (server) {
const error = new Error(
"Server already existed when config event was received"
);
status.setState(constants.ERROR, { error, context: "config" });
return;
}
try {
server = createServer({
privateStorage: rnBridge.app.datadir(),
sharedStorage: storagePath,
apkPath: config.apkPath,
apkVersion: config.apkVersion,
...config,
});
} catch (error) {
status.setState(constants.ERROR, { error, context: "createServer" });
Expand Down Expand Up @@ -123,7 +120,11 @@ rnBridge.app.on("resume", () => {
const noop = () => {};

function startServer() {
if (!server) return;
if (!server) {
const error = new Error("Tried to start server before config was received");
status.setState(constants.ERROR, { error, context: "createServer" });
return;
}
const state = status.getState();
if (state === constants.CLOSING) {
log("Server was closing when it tried to start");
Expand Down
7 changes: 5 additions & 2 deletions src/backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ function createServer({
privateStorage,
sharedStorage,
apkPath,
apkVersion,
flavor,
minSdkVersion,
version,
buildNumber,
bundleId,
}) {
const defaultConfigPath = path.join(sharedStorage, "presets/default");
log("Creating server");
Expand Down Expand Up @@ -178,6 +180,7 @@ function createServer({
const emitFn = rnBridge.channel.post.bind(rnBridge.channel);
const listenFn = rnBridge.channel.on.bind(rnBridge.channel);
const removeFn = rnBridge.channel.removeListener.bind(rnBridge.channel);
// TODO: Pass apkPath, version, buildNumber, minSdkVersion, bundleId
const manager = new UpgradeManager(
upgradePath,
port,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ServerStatus {
if (this.state === constants.ERROR) return;
if (nextState === constants.ERROR) {
error = error || new Error("Unknown server error");
log(error.message);
log(context, error.message);
main.bugsnag.notify(error, { context });
}
this.state = nextState;
Expand Down
8 changes: 5 additions & 3 deletions src/frontend/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ export function Api({
// As soon as we hear from the Node process, send the storagePath and
// other config that the server requires
nodejs.channel.post("config", {
storagePath: RNFS.ExternalDirectoryPath,
upgradeStoragePath: RNFS.DocumentDirectoryPath,
sharedStorage: RNFS.ExternalDirectoryPath,
apkPath: AppInfo.sourceDir,
apkVersion: APP_VERSION,
minSdkVersion: AppInfo.minSdkVersion,
version: DeviceInfo.getVersion(),
buildNumber: DeviceInfo.getBuildNumber(),
bundleId: DeviceInfo.getBundleId(),
});
// Resolve once the server reports status as "LISTENING"
return onReady();
Expand Down
10 changes: 9 additions & 1 deletion src/frontend/lib/AppInfo.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import { NativeModules } from "react-native";
export default NativeModules.AppInfo;

/**
* @type {object}
* @property {string} sourceDir Full path to the base APK for this application.
* @property {string} minSdkVersion The minimum SDK version this application can run on. It will not run on earlier versions.
*/
const constants = NativeModules.AppInfo.getConstants();

export default constants;