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

Flash first flow #63

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ android {
}

dependencies {
implementation 'androidx.lifecycle:lifecycle-process:2.7.0'
testImplementation 'junit:junit:4.12'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
Expand Down
64 changes: 61 additions & 3 deletions app/src/main/java/com/samsung/microbit/MBApp.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package com.samsung.microbit;

import static com.samsung.microbit.BuildConfig.DEBUG;

import android.app.Application;
import android.graphics.Typeface;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.samsung.microbit.MBAppState;

/**
* Represents a custom class of the app.
* Provides some resources that use along app modules,
* such as app context, font styles and etc.
*/
public class MBApp extends Application {
public class MBApp extends Application implements DefaultLifecycleObserver {
private static final String TAG = MBApp.class.getSimpleName();

public void logi(String message) {
if ( DEBUG) {
Log.i(TAG, "### " + Thread.currentThread().getId() + " # " + message);
}
}

private static MBApp app = null;

Expand All @@ -19,12 +35,15 @@ public class MBApp extends Application {

private boolean justPaired;

private MBAppState appState = new MBAppState();

@Override
public void onCreate() {
super.onCreate();
app = this;
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
initTypefaces();

appState.prefsLoad();
Log.d("MBApp", "App Created");
}

Expand Down Expand Up @@ -61,4 +80,43 @@ public static MBApp getApp() {
return app;
}

}
public static MBAppState getAppState() {
return app.appState;
}

@Override
public void onCreate(@NonNull LifecycleOwner owner) {
logi("onCreate");
MBApp.getAppState().eventPairForeground();
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
logi("onStart");
MBApp.getAppState().eventPairForeground();
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
logi("onResume");
MBApp.getAppState().eventPairForeground();
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
logi("onPause");
MBApp.getAppState().eventPairBackground();
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
logi("onStop");
MBApp.getAppState().eventPairBackground();
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
logi("onDestroy");
MBApp.getAppState().eventPairBackground();
}
}
166 changes: 166 additions & 0 deletions app/src/main/java/com/samsung/microbit/MBAppState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.samsung.microbit;

import static com.samsung.microbit.BuildConfig.DEBUG;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.samsung.microbit.MBApp;
import com.samsung.microbit.core.bluetooth.BluetoothUtils;
import com.samsung.microbit.data.constants.Constants;
import com.samsung.microbit.data.model.ConnectedDevice;

public class MBAppState {
private static final String TAG = MBAppState.class.getSimpleName();

public void logi(String message) {
if ( DEBUG) {
Log.i(TAG, "### " + Thread.currentThread().getId() + " # " + message);
}
}

public enum PairState {
PairStateNone,
PairStateError,
PairStateLaunch,
PairStateSession,
PairStateChecked
}

public boolean pairStateLaunch;
public boolean pairStateSession;
public boolean pairStateError;
public boolean pairStateMakeCode;
public boolean pairStateResetTriple;

public MBAppState() {
pairStateLaunch = true;
pairStateSession = true;
pairStateError = false;
pairStateMakeCode = false;
pairStateResetTriple = true;
}

public PairState pairState() {
PairState ps = PairState.PairStateChecked;

if ( !BluetoothUtils.getCurrentMicrobitIsValid( MBApp.getApp()))
ps = PairState.PairStateNone;
else if ( pairStateError)
ps = PairState.PairStateError;
else if ( pairStateLaunch)
ps = PairState.PairStateLaunch;
else if ( pairStateSession)
ps = PairState.PairStateSession;
else
ps = PairState.PairStateChecked;

logi( "pairState " + ps);
return ps;
}

public void eventPairSuccess() {
logi( "eventPairSuccess");
pairStateLaunch = false;
pairStateSession = false;
pairStateError = false;
prefsSave();
}

public void eventPairChecked() {
logi( "eventPairChecked");
pairStateLaunch = false;
pairStateSession = false;
}

public void eventPairDifferent() {
logi( "eventPairDifferent");
pairStateSession = true;
}

public void eventPairBackground() {
logi( "eventPairBackground");
if ( !pairStateMakeCode)
pairStateSession = true;
}

public void eventPairForeground() {
logi( "eventPairForeground");
if ( !pairStateMakeCode)
pairStateSession = true;
}

public void eventPairError() {
logi( "eventPairError");
pairStateError = true;
prefsSave();
}

public void eventPairMakeCodeBegin() {
logi( "eventPairMakeCodeBegin");
pairStateSession = true;
pairStateMakeCode = true;
}

public void eventPairMakeCodeEnd() {
logi( "eventPairMakeCodeEnd");
pairStateMakeCode = false;
}

public void eventPairSendError() {
logi( "eventPairSendError");
pairStateError = true;
prefsSave();
}

public void eventPairResetTriple( boolean yes)
{
logi( "eventPairResetTriple " + yes);
pairStateResetTriple = yes;
prefsSave();
}

public void eventPairResetMethodCheck( ConnectedDevice device)
{
logi( "eventPairResetMethodCheck");
if ( device != null && device.mPattern != null)
{
if ( device.mhardwareVersion == 1) // MICROBIT_V1
{
eventPairResetTriple( false);
return;
}
}
eventPairResetTriple( true);
}

public static final String PREFERENCES_pairStateError = "Preferences.pairStateError";
public static final String PREFERENCES_pairStateResetTriple = "Preferences.pairStateResetTriple";

public void prefsLoad()
{
Context ctx = MBApp.getApp();
SharedPreferences prefs = ctx.getSharedPreferences(Constants.PREFERENCES, Context.MODE_MULTI_PROCESS);
if ( prefs == null) {
logi( "prefsLoad failed");
return;
}
pairStateError = prefs.getBoolean( PREFERENCES_pairStateError, pairStateError);
pairStateResetTriple = prefs.getBoolean( PREFERENCES_pairStateResetTriple, pairStateResetTriple);
}

public void prefsSave()
{
Context ctx = MBApp.getApp();
SharedPreferences prefs = ctx.getSharedPreferences(Constants.PREFERENCES, Context.MODE_MULTI_PROCESS);
SharedPreferences.Editor prefsEdit = prefs == null ? null: prefs.edit();
if ( prefsEdit == null) {
logi( "prefsSave failed");
return;
}
prefsEdit.putBoolean( PREFERENCES_pairStateError, pairStateError);
prefsEdit.putBoolean( PREFERENCES_pairStateResetTriple, pairStateResetTriple);
prefsEdit.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,13 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
}

if(status == BluetoothGatt.GATT_SUCCESS) {
ConnectedDevice cD = BluetoothUtils.getPairedMicrobit(MBApp.getApp());
if(gatt.getService(UUID.fromString("0000fe59-0000-1000-8000-00805f9b34fb")) != null ) {
Log.v(TAG, "Hardware Type: V2");
cD.mhardwareVersion = 2;
BluetoothUtils.setCurrentMicrobitHardwareVersion(MBApp.getApp(), 2);
} else {
Log.v(TAG, "Hardware Type: V1");
cD.mhardwareVersion = 1;
BluetoothUtils.setCurrentMicrobitHardwareVersion(MBApp.getApp(), 1);
}
BluetoothUtils.setPairedMicroBit(MBApp.getApp(), cD);
bleState |= state;
} else {
bleState &= (~state);
Expand Down
Loading