Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.facebook.widget;
18
 
19
import android.content.Intent;
20
import android.os.Bundle;
21
import android.support.v4.app.Fragment;
22
import com.facebook.Session;
23
import com.facebook.SessionLoginBehavior;
24
import com.facebook.SessionState;
25
import com.facebook.internal.SessionAuthorizationType;
26
import com.facebook.internal.SessionTracker;
27
 
28
import java.util.Date;
29
import java.util.List;
30
 
31
/**
32
 * <p>Basic implementation of a Fragment that uses a Session to perform 
33
 * Single Sign On (SSO). This class is package private, and is not intended
34
 * to be consumed by external applications.</p>
35
 * 
36
 * <p>The method {@link android.support.v4.app.Fragment#onActivityResult} is
37
 * used to manage the session information, so if you override it in a subclass, 
38
 * be sure to call {@code super.onActivityResult}.</p>
39
 * 
40
 * <p>The methods in this class are not thread-safe.</p>
41
 */
42
class FacebookFragment extends Fragment {
43
 
44
    private SessionTracker sessionTracker;
45
 
46
    @Override
47
    public void onActivityCreated(Bundle savedInstanceState) {
48
        super.onActivityCreated(savedInstanceState);
49
        sessionTracker = new SessionTracker(getActivity(), new DefaultSessionStatusCallback());
50
    }
51
 
52
    /**
53
     * Called when the activity that was launched exits. This method manages session
54
     * information when a session is opened. If this method is overridden in subclasses,
55
     * be sure to call {@code super.onActivityResult(...)} first.
56
     */
57
    @Override
58
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
59
        super.onActivityResult(requestCode, resultCode, data);
60
        sessionTracker.getSession().onActivityResult(this.getActivity(), requestCode, resultCode, data);
61
    }
62
 
63
    @Override
64
    public void onDestroy() {
65
        super.onDestroy();
66
        sessionTracker.stopTracking();
67
    }
68
 
69
    /**
70
     * Use the supplied Session object instead of the active Session.
71
     *
72
     * @param newSession the Session object to use
73
     */
74
    public void setSession(Session newSession) {
75
        if (sessionTracker != null) {
76
            sessionTracker.setSession(newSession);
77
        }
78
    }
79
 
80
    // METHOD TO BE OVERRIDDEN
81
 
82
    /**
83
     * Called when the session state changes. Override this method to take action
84
     * on session state changes.
85
     * 
86
     * @param state the new state
87
     * @param exception any exceptions that occurred during the state change
88
     */
89
    protected void onSessionStateChange(SessionState state, Exception exception) {
90
    }
91
 
92
    // ACCESSORS (CANNOT BE OVERRIDDEN)
93
 
94
    /**
95
     * Gets the current Session.
96
     * 
97
     * @return the current Session object.
98
     */
99
    protected final Session getSession() {
100
        if (sessionTracker != null) {
101
            return sessionTracker.getSession();
102
        }
103
        return null;
104
    }
105
 
106
    /**
107
     * Determines whether the current session is open.
108
     * 
109
     * @return true if the current session is open
110
     */
111
    protected final boolean isSessionOpen() {
112
        if (sessionTracker != null) {
113
            return sessionTracker.getOpenSession() != null;
114
        }
115
        return false;
116
    }
117
 
118
    /**
119
     * Gets the current state of the session or null if no session has been created.
120
     * 
121
     * @return the current state of the session
122
     */
123
    protected final SessionState getSessionState() {
124
        if (sessionTracker != null) {
125
            Session currentSession = sessionTracker.getSession();
126
            return (currentSession != null) ? currentSession.getState() : null;
127
        }
128
        return null;
129
    }
130
 
131
    /**
132
     * Gets the access token associated with the current session or null if no 
133
     * session has been created.
134
     * 
135
     * @return the access token
136
     */
137
    protected final String getAccessToken() {
138
        if (sessionTracker != null) {
139
            Session currentSession = sessionTracker.getOpenSession();
140
            return (currentSession != null) ? currentSession.getAccessToken() : null;
141
        }
142
        return null;
143
    }
144
 
145
    /**
146
     * Gets the date at which the current session will expire or null if no session 
147
     * has been created.
148
     * 
149
     * @return the date at which the current session will expire
150
     */
151
    protected final Date getExpirationDate() {
152
        if (sessionTracker != null) {
153
            Session currentSession = sessionTracker.getOpenSession();
154
            return (currentSession != null) ? currentSession.getExpirationDate() : null;
155
        }
156
        return null;
157
    }
158
 
159
    /**
160
     * Closes the current session.
161
     */
162
    protected final void closeSession() {
163
        if (sessionTracker != null) {
164
            Session currentSession = sessionTracker.getOpenSession();
165
            if (currentSession != null) {
166
                currentSession.close();
167
            }
168
        }
169
    }
170
 
171
    /**
172
     * Closes the current session as well as clearing the token cache.
173
     */
174
    protected final void closeSessionAndClearTokenInformation() {
175
        if (sessionTracker != null) {
176
            Session currentSession = sessionTracker.getOpenSession();
177
            if (currentSession != null) {
178
                currentSession.closeAndClearTokenInformation();
179
            }
180
        }
181
    }
182
 
183
    /**
184
     * Gets the permissions associated with the current session or null if no session 
185
     * has been created.
186
     * 
187
     * @return the permissions associated with the current session
188
     */
189
    protected final List<String> getSessionPermissions() {
190
        if (sessionTracker != null) {
191
            Session currentSession = sessionTracker.getSession();
192
            return (currentSession != null) ? currentSession.getPermissions() : null;
193
        }
194
        return null;
195
    }
196
 
197
    /**
198
     * Opens a new session. This method will use the application id from
199
     * the associated meta-data value and an empty list of permissions.
200
     */
201
    protected final void openSession() {
202
        openSessionForRead(null, null);
203
    }
204
 
205
    /**
206
     * Opens a new session with read permissions. If either applicationID or permissions
207
     * is null, this method will default to using the values from the associated
208
     * meta-data value and an empty list respectively.
209
     *
210
     * @param applicationId the applicationID, can be null
211
     * @param permissions the permissions list, can be null
212
     */
213
    protected final void openSessionForRead(String applicationId, List<String> permissions) {
214
        openSessionForRead(applicationId, permissions, SessionLoginBehavior.SSO_WITH_FALLBACK,
215
                Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);
216
    }
217
 
218
    /**
219
     * Opens a new session with read permissions. If either applicationID or permissions
220
     * is null, this method will default to using the values from the associated
221
     * meta-data value and an empty list respectively.
222
     *
223
     * @param applicationId the applicationID, can be null
224
     * @param permissions the permissions list, can be null
225
     * @param behavior the login behavior to use with the session
226
     * @param activityCode the activity code to use for the SSO activity
227
     */
228
    protected final void openSessionForRead(String applicationId, List<String> permissions,
229
            SessionLoginBehavior behavior, int activityCode) {
230
        openSession(applicationId, permissions, behavior, activityCode, SessionAuthorizationType.READ);
231
    }
232
 
233
    /**
234
     * Opens a new session with publish permissions. If either applicationID is null,
235
     * this method will default to using the value from the associated
236
     * meta-data value. The permissions list cannot be null.
237
     *
238
     * @param applicationId the applicationID, can be null
239
     * @param permissions the permissions list, cannot be null
240
     */
241
    protected final void openSessionForPublish(String applicationId, List<String> permissions) {
242
        openSessionForPublish(applicationId, permissions, SessionLoginBehavior.SSO_WITH_FALLBACK,
243
                Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);
244
    }
245
 
246
    /**
247
     * Opens a new session with publish permissions. If either applicationID is null,
248
     * this method will default to using the value from the associated
249
     * meta-data value. The permissions list cannot be null.
250
     *
251
     * @param applicationId the applicationID, can be null
252
     * @param permissions the permissions list, cannot be null
253
     * @param behavior the login behavior to use with the session
254
     * @param activityCode the activity code to use for the SSO activity
255
     */
256
    protected final void openSessionForPublish(String applicationId, List<String> permissions,
257
            SessionLoginBehavior behavior, int activityCode) {
258
        openSession(applicationId, permissions, behavior, activityCode, SessionAuthorizationType.PUBLISH);
259
    }
260
 
261
    private void openSession(String applicationId, List<String> permissions,
262
            SessionLoginBehavior behavior, int activityCode, SessionAuthorizationType authType) {
263
        if (sessionTracker != null) {
264
            Session currentSession = sessionTracker.getSession();
265
            if (currentSession == null || currentSession.getState().isClosed()) {
266
                Session session = new Session.Builder(getActivity()).setApplicationId(applicationId).build();
267
                Session.setActiveSession(session);
268
                currentSession = session;
269
            }
270
            if (!currentSession.isOpened()) {
271
                Session.OpenRequest openRequest = new Session.OpenRequest(this).
272
                        setPermissions(permissions).
273
                        setLoginBehavior(behavior).
274
                        setRequestCode(activityCode);
275
                if (SessionAuthorizationType.PUBLISH.equals(authType)) {
276
                    currentSession.openForPublish(openRequest);
277
                } else {
278
                    currentSession.openForRead(openRequest);
279
                }
280
            }
281
        }
282
    }
283
 
284
    /**
285
     * The default callback implementation for the session.
286
     */
287
    private class DefaultSessionStatusCallback implements Session.StatusCallback {
288
 
289
        @Override
290
        public void call(Session session, 
291
                         SessionState state,
292
                         Exception exception) {
293
            FacebookFragment.this.onSessionStateChange(state, exception);
294
        }
295
 
296
    }
297
}