Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OsmOAuth.java « editor « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c84eaec12e63cd095c37eab1745d173422ceb2e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.mapswithme.maps.editor;

import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.support.annotation.WorkerThread;

import java.lang.ref.WeakReference;

import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.editor.data.UserStats;

public final class OsmOAuth
{
  private OsmOAuth() {}

  public enum AuthType
  {
    OSM("OSM"),
    FACEBOOK("Facebook"),
    GOOGLE("Google");

    public final String name;

    AuthType(String name)
    {
      this.name = name;
    }
  }

  // Result type corresponds to OsmOAuth::AuthResult.
  @IntDef({OK, FAIL_COOKIE, FAIL_LOGIN, NO_O_AUTH, FAIL_AUTH, NO_ACCESS, NETWORK_ERROR, SERVER_ERROR})
  public @interface AuthResult {}

  public static final int OK = 0;
  public static final int FAIL_COOKIE = 1;
  public static final int FAIL_LOGIN = 2;
  public static final int NO_O_AUTH = 3;
  public static final int FAIL_AUTH = 4;
  public static final int NO_ACCESS = 5;
  public static final int NETWORK_ERROR = 6;
  public static final int SERVER_ERROR = 7;

  private static final String PREF_OSM_TOKEN = "OsmToken";
  private static final String PREF_OSM_SECRET = "OsmSecret";
  private static final String PREF_OSM_USERNAME = "OsmUsername";

  public interface OnUserStatsChanged
  {
    void onStatsChange(UserStats stats);
  }

  private static WeakReference<OnUserStatsChanged> sListener;

  public static void setUserStatsListener(OnUserStatsChanged listener)
  {
    sListener = new WeakReference<>(listener);
  }

  // Called from native OsmOAuth.cpp.
  @SuppressWarnings("unused")
  public static void onUserStatsUpdated(UserStats stats)
  {
    if (sListener == null)
      return;

    OnUserStatsChanged listener = sListener.get();
    if (listener != null)
      listener.onStatsChange(stats);
  }

  public static final String URL_PARAM_VERIFIER = "oauth_verifier";

  public static boolean isAuthorized()
  {
    return MwmApplication.prefs().contains(PREF_OSM_TOKEN) &&
           MwmApplication.prefs().contains(PREF_OSM_SECRET);
  }

  public static String getAuthToken()
  {
    return MwmApplication.prefs().getString(PREF_OSM_TOKEN, "");
  }

  public static String getAuthSecret()
  {
    return MwmApplication.prefs().getString(PREF_OSM_SECRET, "");
  }

  public static String getUsername()
  {
    return MwmApplication.prefs().getString(PREF_OSM_USERNAME, "");
  }

  public static void setAuthorization(String token, String secret, String username)
  {
    MwmApplication.prefs().edit()
                  .putString(PREF_OSM_TOKEN, token)
                  .putString(PREF_OSM_SECRET, secret)
                  .putString(PREF_OSM_USERNAME, username)
                  .apply();
  }

  public static void clearAuthorization()
  {
    MwmApplication.prefs().edit()
                  .remove(PREF_OSM_TOKEN)
                  .remove(PREF_OSM_SECRET)
                  .remove(PREF_OSM_USERNAME)
                  .apply();
  }

  /**
   * Some redirect urls indicates that user wasn't registered before.
   * Initial auth url should be reloaded to get correct {@link #URL_PARAM_VERIFIER} then.
   */
  public static boolean shouldReloadWebviewUrl(String url)
  {
    return url.contains("/welcome") || url.endsWith("/");
  }

  /**
   * @return array containing auth token and secret
   */
  @WorkerThread
  @Size(2)
  @Nullable
  public static native String[] nativeAuthWithPassword(String login, String password);

  /**
   * @return array containing auth token and secret
   */
  @WorkerThread
  @Size(2)
  @Nullable
  public static native String[] nativeAuthWithWebviewToken(String key, String secret, String verifier);

  /**
   * @return url for web auth, and token with secret for finishing authorization later
   */
  @Size(3)
  @Nullable
  public static native String[] nativeGetFacebookAuthUrl();

  /**
   * @return url for web auth, and token with secret for finishing authorization later
   */
  @Size(3)
  @Nullable
  public static native String[] nativeGetGoogleAuthUrl();

  @WorkerThread
  @Nullable
  public static native String nativeGetOsmUsername(String token, String secret);

  public static native void nativeUpdateOsmUserStats(String username, boolean forceUpdate);
}