summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/authenticator/FxADefaultLoginStateMachineDelegate.java
blob: ff31223224bdd02d68f95a9b703bdc6a10e13a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.fxa.authenticator;

import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.background.fxa.FxAccountClient;
import org.mozilla.gecko.background.fxa.FxAccountClient20;
import org.mozilla.gecko.browserid.BrowserIDKeyPair;
import org.mozilla.gecko.fxa.login.FxAccountLoginStateMachine.LoginStateMachineDelegate;
import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.Transition;
import org.mozilla.gecko.fxa.login.Married;
import org.mozilla.gecko.fxa.login.State;
import org.mozilla.gecko.fxa.login.State.StateLabel;
import org.mozilla.gecko.fxa.login.StateFactory;
import org.mozilla.gecko.fxa.sync.FxAccountNotificationManager;
import org.mozilla.gecko.fxa.sync.FxAccountSyncAdapter;

import android.content.Context;

public abstract class FxADefaultLoginStateMachineDelegate implements LoginStateMachineDelegate {
  protected final static String LOG_TAG = LoginStateMachineDelegate.class.getSimpleName();

  protected final Context context;
  protected final AndroidFxAccount fxAccount;
  protected final Executor executor;
  protected final FxAccountClient client;

  public FxADefaultLoginStateMachineDelegate(Context context, AndroidFxAccount fxAccount) {
    this.context = context;
    this.fxAccount = fxAccount;
    this.executor = Executors.newSingleThreadExecutor();
    this.client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor);
  }

  abstract public void handleNotMarried(State notMarried);
  abstract public void handleMarried(Married married);

  @Override
  public FxAccountClient getClient() {
    return client;
  }

  @Override
  public long getCertificateDurationInMilliseconds() {
    return 12 * 60 * 60 * 1000;
  }

  @Override
  public long getAssertionDurationInMilliseconds() {
    return 15 * 60 * 1000;
  }

  @Override
  public BrowserIDKeyPair generateKeyPair() throws NoSuchAlgorithmException {
    return StateFactory.generateKeyPair();
  }

  @Override
  public void handleTransition(Transition transition, State state) {
    Logger.info(LOG_TAG, "handleTransition: " + transition + " to " + state.getStateLabel());
  }

  @Override
  public void handleFinal(State state) {
    Logger.info(LOG_TAG, "handleFinal: in " + state.getStateLabel());
    fxAccount.setState(state);
    // Update any notifications displayed.
    final FxAccountNotificationManager notificationManager = new FxAccountNotificationManager(FxAccountSyncAdapter.NOTIFICATION_ID);
    notificationManager.update(context, fxAccount);

    if (state.getStateLabel() != StateLabel.Married) {
      handleNotMarried(state);
      return;
    } else {
      handleMarried((Married) state);
    }
  }
}