summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java
blob: b33d01a105b99924714919ef9ff9d008cccd4ba9 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.mapbox.mapboxsdk;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
import com.mapbox.mapboxsdk.exceptions.MapboxAccountManagerNotStartedException;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;

public class MapboxAccountManager {

    private static MapboxAccountManager mapboxAccountManager = null;

    private final String accessToken;
    private final Context applicationContext;

    private Boolean connected = null;

    /**
     * MapboxAccountManager should NOT be instantiated directly.
     * Use @see MapboxAccountManager#getInstance() instead.
     *
     * @param applicationContext Context used to get ApplicationContext
     * @param accessToken        Mapbox Access Token
     */
    private MapboxAccountManager(Context applicationContext, String accessToken) {
        super();
        this.applicationContext = applicationContext.getApplicationContext();
        this.accessToken = accessToken;
    }

    /**
     * Primary entry point to Mapbox for implementing developers.
     * Must be configured in either Application.onCreate() or Launch Activity.onCreate()
     *
     * @param context     Context used to get Application Context
     * @param accessToken Mapbox Access Token.  You can get one on the Mapbox Web site.
     * @return MapboxAccountManager instance for app
     */
    public static MapboxAccountManager start(Context context, String accessToken) {
        if (mapboxAccountManager == null) {
            //Create a new account manager
            mapboxAccountManager = new MapboxAccountManager(context, accessToken);

            //Initialize the event manager
            MapboxEventManager.getMapboxEventManager().initialize(context, accessToken);

            //Register a receiver to listen for connectivity updates
            ConnectivityReceiver.instance(context);
        }

        return mapboxAccountManager;
    }

    /**
     * Internal Use Only
     * Get an instance of MapboxAccountManager configured with the app's Access Token
     *
     * @return MapboxAccountManager instance for app.  May be NULL if not configured yet.
     */
    public static MapboxAccountManager getInstance() {
        if (mapboxAccountManager == null) {
            throw new MapboxAccountManagerNotStartedException();
        }

        return mapboxAccountManager;
    }

    /**
     * Access Token for this application.
     *
     * @return Mapbox Access Token
     */
    public String getAccessToken() {
        return accessToken;
    }

    /**
     * Runtime validation of Access Token.
     *
     * @param accessToken Access Token to check
     * @throws InvalidAccessTokenException the exception thrown
     */
    public static void validateAccessToken(String accessToken) throws InvalidAccessTokenException {
        if (TextUtils.isEmpty(accessToken) || (!accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("pk.") && !accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("sk."))) {
            throw new InvalidAccessTokenException();
        }
    }

    /**
     * Manually sets the connectivity state of the app. This is useful for apps that control their
     * own connectivity state and want to bypass any checks to the ConnectivityManager.
     *
     * @param connected flag to determine the connectivity state, true for connected, false for
     *                  disconnected, null for ConnectivityManager to determine.
     */
    public void setConnected(Boolean connected) {
        // Connectivity state overridden by app
        this.connected = connected;
    }

    /**
     * Determines whether we have an Internet connection available. Please do not rely on this
     * method in your apps, this method is used internally by the SDK.
     *
     * @return true if there is an Internet connection, false otherwise
     */
    public Boolean isConnected() {
        if (connected != null) {
            // Connectivity state overridden by app
            return connected;
        }

        ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return (activeNetwork != null && activeNetwork.isConnected());
    }

    /**
     * Not public API
     * @return the Application Context
     */
    public Context getApplicationContext() {
        return applicationContext;
    }

}