diff --git a/client/react-native/android/app/src/main/java/chat/berty/main/MainActivity.java b/client/react-native/android/app/src/main/java/chat/berty/main/MainActivity.java index 4da007a397..dafd59b4f1 100644 --- a/client/react-native/android/app/src/main/java/chat/berty/main/MainActivity.java +++ b/client/react-native/android/app/src/main/java/chat/berty/main/MainActivity.java @@ -1,18 +1,24 @@ package chat.berty.main; import android.content.Intent; +import android.os.Bundle; import android.util.Log; import com.facebook.react.ReactActivity; +import chat.berty.core.Level; +import chat.berty.core.Logger; +import core.Core; + public class MainActivity extends ReactActivity { protected String TAG = "MainActivity"; + private Logger logger = new Logger("chat.berty.io"); /** - * Returns the name of the main component registered from JavaScript. - * This is used to schedule rendering of the component. - */ + * Returns the name of the main component registered from JavaScript. + * This is used to schedule rendering of the component. + */ @Override protected String getMainComponentName() { return "root"; @@ -27,4 +33,45 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onNewIntent(Intent intent) { super.onNewIntent(intent); } + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + try { + Core.getDeviceInfo().setAppState(Core.getDeviceInfoAppStateBackground()); + } catch (Exception err) { + this.logger.format(Level.ERROR, TAG, "on create: %s", err); + } + } + + @Override + protected void onPause() { + super.onPause(); + try { + Core.getDeviceInfo().setAppState(Core.getDeviceInfoAppStateBackground()); + } catch (Exception err) { + this.logger.format(Level.ERROR, TAG, "on pause: %s", err); + } + } + + @Override + protected void onResume() { + super.onResume(); + try { + Core.getDeviceInfo().setAppState(Core.getDeviceInfoAppStateForeground()); + } catch (Exception err) { + this.logger.format(Level.ERROR, TAG, "on resume: %s", err); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + try { + Core.getDeviceInfo().setAppState(Core.getDeviceInfoAppStateKill()); + } catch (Exception err) { + this.logger.format(Level.ERROR, TAG, "on destroy: %s", err); + } + } } diff --git a/client/react-native/gomobile/core/deviceinfo.go b/client/react-native/gomobile/core/deviceinfo.go index 10aea83a9a..355712f182 100644 --- a/client/react-native/gomobile/core/deviceinfo.go +++ b/client/react-native/gomobile/core/deviceinfo.go @@ -1,6 +1,9 @@ package core -import "berty.tech/core/pkg/deviceinfo" +import ( + "berty.tech/core/pkg/deviceinfo" + "github.com/pkg/errors" +) type DeviceInfoPkg struct{} @@ -13,3 +16,22 @@ func (*DeviceInfoPkg) GetStoragePath() string { func (*DeviceInfoPkg) SetStoragePath(path string) error { return deviceinfo.SetStoragePath(path) } + +var ( + DeviceInfoAppStateKill string = deviceinfo.AppState_name[int32(deviceinfo.AppState_Kill)] + DeviceInfoAppStateBackground string = deviceinfo.AppState_name[int32(deviceinfo.AppState_Background)] + DeviceInfoAppStateForeground string = deviceinfo.AppState_name[int32(deviceinfo.AppState_Foreground)] +) + +func (*DeviceInfoPkg) GetAppState() string { + return deviceinfo.AppState_name[int32(deviceinfo.GetAppState())] +} + +func (*DeviceInfoPkg) SetAppState(state string) error { + val, ok := deviceinfo.AppState_value[state] + if !ok { + return errors.New("cannot specify " + state + " as state") + } + deviceinfo.SetAppState(deviceinfo.AppState(val)) + return nil +} diff --git a/client/react-native/ios/Berty/AppDelegate.swift b/client/react-native/ios/Berty/AppDelegate.swift index fc53d1f0fd..dd36531b79 100644 --- a/client/react-native/ios/Berty/AppDelegate.swift +++ b/client/react-native/ios/Berty/AppDelegate.swift @@ -5,6 +5,7 @@ import PushKit @UIApplicationMain class AppDelegate: AppDelegateObjC { var logger = Logger("chat.berty.io", "AppDelegate") + var launchOptions: [UIApplicationLaunchOptionsKey: Any]? override init() { super.init() @@ -22,10 +23,7 @@ class AppDelegate: AppDelegateObjC { return filesPath! } - override func application( - _ application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil - ) -> Bool { + func startReact() { let jsCodeLocation: URL = (RCTBundleURLProvider.sharedSettings()?.jsBundleURL(forBundleRoot: "index", fallbackResource: nil))! @@ -33,7 +31,7 @@ class AppDelegate: AppDelegateObjC { bundleURL: jsCodeLocation, moduleName: "root", initialProperties: nil, - launchOptions: launchOptions + launchOptions: self.launchOptions ) rootView.backgroundColor = UIColor.init(red: 1.0, green: 1.0, blue: 1.0, alpha: 1) @@ -42,6 +40,18 @@ class AppDelegate: AppDelegateObjC { rootViewController.view = rootView self.window.rootViewController = rootViewController self.window.makeKeyAndVisible() + } + + override func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil + ) -> Bool { + self.launchOptions = launchOptions + + // TODO: Move this line to applicationDidBecomeActive when envelope db with network independant start will be implem + self.startReact() + // TODO: remote comment when independant start will be implem + // Core.startNetwork() // permit to show local notification in background mode application.beginBackgroundTask(withName: "showNotification", expirationHandler: nil) @@ -66,9 +76,30 @@ class AppDelegate: AppDelegateObjC { do { try Core.deviceInfo().setStoragePath(try self.getFilesDir()) } catch let error as NSError { - logger.format("unable to set storage path", level: .error, error.userInfo.description) + logger.format("unable to set storage path: %@", level: .error, error.userInfo.description) return false } return true } + + override func applicationDidBecomeActive(_ application: UIApplication) { + // start react if app was killed + if Core.deviceInfo()?.getAppState() == Core.deviceInfoAppStateKill() { + // self.startReact() + } + + do { + try Core.deviceInfo().setAppState(Core.deviceInfoAppStateForeground()) + } catch let err as NSError { + logger.format("application did become active: %@", level: .error, err.userInfo.description) + } + } + + override func applicationDidEnterBackground(_ application: UIApplication) { + do { + try Core.deviceInfo().setAppState(Core.deviceInfoAppStateBackground()) + } catch let err as NSError { + logger.format("application did enter background: %@", level: .error, err.userInfo.description) + } + } } diff --git a/core/chunk/chunk_test.go b/core/chunk/chunk_test.go index 3c9a383de3..2e23e4a8ee 100644 --- a/core/chunk/chunk_test.go +++ b/core/chunk/chunk_test.go @@ -33,7 +33,7 @@ func Test(t *testing.T) { Convey("set storage path and remove last db", t, FailureHalts, func() { err = deviceinfo.SetStoragePath("/tmp/chunk_test") So(err, ShouldBeNil) - os.Remove("/tmp/chunk_test/berty.core.db") + os.Remove("/tmp/chunk_test/berty.core.chunk.db") }) Convey("split random data", t, FailureHalts, func() { diff --git a/core/pkg/deviceinfo/application.go b/core/pkg/deviceinfo/application.go new file mode 100644 index 0000000000..aa742fb5a9 --- /dev/null +++ b/core/pkg/deviceinfo/application.go @@ -0,0 +1,12 @@ +package deviceinfo + +var appState = AppState_Kill + +func GetAppState() AppState { + return appState +} + +func SetAppState(state AppState) { + logger().Info("application switch from " + AppState_name[int32(appState)] + " to " + AppState_name[int32(state)] + " mode") + appState = state +} diff --git a/core/pkg/deviceinfo/application.pb.go b/core/pkg/deviceinfo/application.pb.go new file mode 100644 index 0000000000..2acfca9df9 --- /dev/null +++ b/core/pkg/deviceinfo/application.pb.go @@ -0,0 +1,67 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: pkg/deviceinfo/application.proto + +package deviceinfo // import "berty.tech/core/pkg/deviceinfo" + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type AppState int32 + +const ( + AppState_Kill AppState = 0 + AppState_Background AppState = 1 + AppState_Foreground AppState = 2 +) + +var AppState_name = map[int32]string{ + 0: "Kill", + 1: "Background", + 2: "Foreground", +} +var AppState_value = map[string]int32{ + "Kill": 0, + "Background": 1, + "Foreground": 2, +} + +func (x AppState) String() string { + return proto.EnumName(AppState_name, int32(x)) +} +func (AppState) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_application_287cae36931c55be, []int{0} +} + +func init() { + proto.RegisterEnum("berty.pkg.deviceinfo.AppState", AppState_name, AppState_value) +} + +func init() { + proto.RegisterFile("pkg/deviceinfo/application.proto", fileDescriptor_application_287cae36931c55be) +} + +var fileDescriptor_application_287cae36931c55be = []byte{ + // 159 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x28, 0xc8, 0x4e, 0xd7, + 0x4f, 0x49, 0x2d, 0xcb, 0x4c, 0x4e, 0xcd, 0xcc, 0x4b, 0xcb, 0xd7, 0x4f, 0x2c, 0x28, 0xc8, 0xc9, + 0x4c, 0x4e, 0x2c, 0xc9, 0xcc, 0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x49, 0x4a, + 0x2d, 0x2a, 0xa9, 0xd4, 0x2b, 0xc8, 0x4e, 0xd7, 0x43, 0xa8, 0xd3, 0x32, 0xe1, 0xe2, 0x70, 0x2c, + 0x28, 0x08, 0x2e, 0x49, 0x2c, 0x49, 0x15, 0xe2, 0xe0, 0x62, 0xf1, 0xce, 0xcc, 0xc9, 0x11, 0x60, + 0x10, 0xe2, 0xe3, 0xe2, 0x72, 0x4a, 0x4c, 0xce, 0x4e, 0x2f, 0xca, 0x2f, 0xcd, 0x4b, 0x11, 0x60, + 0x04, 0xf1, 0xdd, 0xf2, 0x8b, 0x52, 0xa1, 0x7c, 0x26, 0x27, 0x83, 0x13, 0x8f, 0xe4, 0x18, 0x2f, + 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x39, 0x88, 0xe9, + 0x25, 0xa9, 0xc9, 0x19, 0xfa, 0xc9, 0xf9, 0x45, 0xa9, 0xfa, 0xa8, 0xee, 0x49, 0x62, 0x03, 0x3b, + 0xc2, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x81, 0x2e, 0xd6, 0xb5, 0xa8, 0x00, 0x00, 0x00, +} diff --git a/core/pkg/deviceinfo/application.proto b/core/pkg/deviceinfo/application.proto new file mode 100644 index 0000000000..3e7f4a0b73 --- /dev/null +++ b/core/pkg/deviceinfo/application.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package berty.pkg.deviceinfo; + +option go_package = "berty.tech/core/pkg/deviceinfo"; + +enum AppState { + Kill = 0; + Background = 1; + Foreground = 2; +} diff --git a/core/pkg/deviceinfo/storage.go b/core/pkg/deviceinfo/storage.go index 3ff44c4b44..2c80a957ef 100644 --- a/core/pkg/deviceinfo/storage.go +++ b/core/pkg/deviceinfo/storage.go @@ -11,12 +11,16 @@ func SetStoragePath(path string) error { stat, err := os.Stat(path) if os.IsNotExist(err) { - if err := os.MkdirAll(path, 0700); err != nil { + if err := os.MkdirAll(path, 0777); err != nil { return err } + err = nil + } + if err != nil { + return err } - if stat.IsDir() == false { + if stat != nil && stat.IsDir() == false { return errors.New("storage path is not a directory: not a dir") }