blob: 49131781ff4e1822418861072a8105ea68cd510c [file] [log] [blame]
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package io.baku.examples.distro;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import org.chromium.base.PathUtils;
import org.json.JSONObject;
import java.io.File;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterView;
import io.v.v23.verror.CanceledException;
import io.v.v23.verror.EndOfFileException;
public class HostActivity extends Activity {
public static final String SESSION_EXTRA = "Session";
private static final String TAG = HostActivity.class.getSimpleName();
private static final long POLL_INTERVAL = 750;
private FlutterView flutterView;
private DistroAndroidService.Session session;
private final FutureCallback<State> render = new FutureCallback<State>() {
@Override
public void onSuccess(final State state) {
Log.i(TAG, "Received state update: " + JSONObject.quote(state.toString()));
flutterView.sendToFlutter("updateCast", state.getValue());
Futures.addCallback(session.stream.recv(), render);
}
@Override
public void onFailure(final Throwable t) {
if (!(t instanceof EndOfFileException ||
t instanceof CanceledException)) {
Log.e(TAG, "Unexpected error while hosting cast", t);
} else {
Log.i(TAG, "Terminated cast", t);
}
finish();
}
};
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterMain.ensureInitializationComplete(getApplicationContext(), null);
setContentView(R.layout.flutter_layout);
flutterView = (FlutterView) findViewById(R.id.flutter_view);
File appBundle = new File(PathUtils.getDataDirectory(this),
FlutterMain.APP_BUNDLE);
flutterView.runFromBundle(appBundle.getPath(), null);
flutterView.sendToFlutter("runHost", "", r -> {
Futures.addCallback(session.stream.recv(), render);
});
session = DistroAndroidService.getSession(getIntent().getStringExtra(SESSION_EXTRA));
}
@Override
protected void onPause() {
super.onPause();
flutterView.onPause();
finish();
}
@Override
protected void onResume() {
super.onResume();
flutterView.onResume();
}
@Override
protected void onDestroy() {
session.completion.set(null);
session = null;
if (flutterView != null) {
flutterView.destroy();
}
Log.i(TAG, "Locally terminated cast");
super.onDestroy();
}
}