javaandroidsamsung-gear-fit

How to trigger alert


My source code is here https://github.com/jackygrahamez/MayDay

I have a HomeActivity.java with an onCreate method

...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_screen);

    Bundle bundle=getIntent().getExtras();
    boolean startedByCUP=false;
    if(bundle!=null) {
        Log.e(">>>>>>", "START_BY_CUP");
        startedByCUP = bundle.getBoolean("START_BY_CUP");
    }
...

I am trying to figure out how I can tie the condition where bundle is true to trigger the multiClickEvent so that after 5 clicks an alarm will trigger. The hardware trigger is built into this

HardwareTriggerReceiver.java

   ...
   @Override
   public void onReceive(Context context, Intent intent) {
   Log.e(">>>>>>>", "in onReceive of HWReceiver");
   String action = intent.getAction();

   if (!isCallActive(context) && isScreenLocked(context) 
      && (action.equals(ACTION_SCREEN_OFF) ||           
      action.equals(ACTION_SCREEN_ON))) {
    multiClickEvent.registerClick(System.currentTimeMillis());
    if (multiClickEvent.isActivated()) {
        onActivation(context);
        resetEvent();
    }
}
} 
...

MultiClickEvent.java

package com.mayday.md.trigger;

import android.util.Log;

public class MultiClickEvent {
public static final int TIME_INTERVAL = 10000;
private static final int TOTAL_CLICKS = 5;

private Long firstEventTime;
private int clickCount = 0;

public void reset() {
    firstEventTime = null;
    clickCount = 0;
}

public void registerClick(Long eventTime) {
    if (isFirstClick() || notWithinLimit(eventTime)) {
        firstEventTime = eventTime;
        clickCount = 1;
        return;
    }
    clickCount++;
    Log.e(">>>>>>", "MultiClickEvent clickCount = " + clickCount);
}

private boolean notWithinLimit(long current) {
    return (current - firstEventTime) > TIME_INTERVAL;
}

private boolean isFirstClick() {
    return firstEventTime == null;
}

public boolean isActivated() {
    return clickCount >= TOTAL_CLICKS;
}
}

I have tried creating an instance of MultiClickEvent into the HomeActivity but that did not track the clicks.


Solution

  • I ended up tracking the clicks on the Gear Fit app and then sending the alarm all within the onCreate

            if(bundle!=null) {
            if (!hardwareTriggerReceiver.isCallActive(getApplicationContext())) {
                int c = mPref.getInt("numRun", 0);
                int TIME_INTERVAL = 10000;
                int TOTAL_CLICKS = 5;
                long delta = 0;
                Long eventTime = System.currentTimeMillis();
                mPref.edit().putLong("eventTime", eventTime).commit();
                Long firstEventTime = mPref.getLong("firstEventTime", 0);
                if (firstEventTime == 0) {
                    firstEventTime = eventTime;
                    mPref.edit().putLong("firstEventTime", firstEventTime).commit();
                }
                delta = eventTime - firstEventTime;
                Log.e(">>>>>>", "START_BY_CUP delta "+delta);
                if (delta < TIME_INTERVAL) {
                    c++;
                    mPref.edit().putInt("numRun",c).commit();
                    Log.e(">>>>>>", "START_BY_CUP "+c);
                    if (c >=TOTAL_CLICKS) {
                        hardwareTriggerReceiver.onActivation(getApplicationContext());
                        mPref.edit().putInt("numRun", 0).apply();
                        mPref.edit().putLong("firstEventTime", 0).apply();
                    }
                } else {
                    mPref.edit().putInt("numRun", 0).apply();
                    mPref.edit().putLong("firstEventTime", 0).apply();
                }
            }
        }