Skip to content

Commit

Permalink
feat(deviceinfo): add state for background / foreground
Browse files Browse the repository at this point in the history
fix #1029

Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Feb 11, 2019
1 parent afa3574 commit b06027c
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 13 deletions.
@@ -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";
Expand All @@ -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);
}
}
}
24 changes: 23 additions & 1 deletion 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{}

Expand All @@ -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
}
43 changes: 37 additions & 6 deletions client/react-native/ios/Berty/AppDelegate.swift
Expand Up @@ -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()
Expand All @@ -22,18 +23,15 @@ 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))!

let rootView: RCTRootView = RCTRootView.init(
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)

Expand All @@ -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)
Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion core/chunk/chunk_test.go
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions 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
}
67 changes: 67 additions & 0 deletions core/pkg/deviceinfo/application.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions 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;
}
8 changes: 6 additions & 2 deletions core/pkg/deviceinfo/storage.go
Expand Up @@ -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")
}

Expand Down

0 comments on commit b06027c

Please sign in to comment.